PhpUnitDataProviderStaticFixerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\PhpUnit;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderStaticFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderStaticFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitDataProviderStaticFixer
  22. */
  23. final class PhpUnitDataProviderStaticFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @param _AutogeneratedInputConfiguration $configuration
  27. *
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  31. {
  32. $this->fixer->configure($configuration);
  33. $this->doTest($expected, $input);
  34. }
  35. /**
  36. * @return iterable<array{0: string, 1?: null|string, 2?: array<string, bool>}>
  37. */
  38. public static function provideFixCases(): iterable
  39. {
  40. yield 'do not fix when containing dynamic calls by default' => [
  41. '<?php
  42. class FooTest extends TestCase {
  43. /**
  44. * @dataProvider provideFoo1Cases
  45. */
  46. public function testFoo1() {}
  47. public function provideFoo1Cases() { $this->init(); }
  48. }',
  49. ];
  50. yield 'fix single' => [
  51. '<?php
  52. class FooTest extends TestCase {
  53. /**
  54. * @dataProvider provideFooCases
  55. */
  56. public function testFoo() {}
  57. public static function provideFooCases() { $x->getData(); }
  58. }',
  59. '<?php
  60. class FooTest extends TestCase {
  61. /**
  62. * @dataProvider provideFooCases
  63. */
  64. public function testFoo() {}
  65. public function provideFooCases() { $x->getData(); }
  66. }',
  67. ];
  68. yield 'fix multiple' => [
  69. '<?php
  70. class FooTest extends TestCase {
  71. /** @dataProvider provider1 */
  72. public function testFoo1() {}
  73. /** @dataProvider provider2 */
  74. public function testFoo2() {}
  75. /** @dataProvider provider3 */
  76. public function testFoo3() {}
  77. /** @dataProvider provider4 */
  78. public function testFoo4() {}
  79. public static function provider1() {}
  80. public function provider2() { $this->init(); }
  81. public static function provider3() {}
  82. public static function provider4() {}
  83. }',
  84. '<?php
  85. class FooTest extends TestCase {
  86. /** @dataProvider provider1 */
  87. public function testFoo1() {}
  88. /** @dataProvider provider2 */
  89. public function testFoo2() {}
  90. /** @dataProvider provider3 */
  91. public function testFoo3() {}
  92. /** @dataProvider provider4 */
  93. public function testFoo4() {}
  94. public function provider1() {}
  95. public function provider2() { $this->init(); }
  96. public function provider3() {}
  97. public static function provider4() {}
  98. }',
  99. ];
  100. yield 'fix with multilines' => [
  101. '<?php
  102. class FooTest extends TestCase {
  103. /**
  104. * @dataProvider provideFooCases
  105. */
  106. public function testFoo() {}
  107. public
  108. static function
  109. provideFooCases() { $x->getData(); }
  110. }',
  111. '<?php
  112. class FooTest extends TestCase {
  113. /**
  114. * @dataProvider provideFooCases
  115. */
  116. public function testFoo() {}
  117. public
  118. function
  119. provideFooCases() { $x->getData(); }
  120. }',
  121. ];
  122. yield 'fix when data provider is abstract' => [
  123. '<?php
  124. abstract class FooTest extends TestCase {
  125. /**
  126. * @dataProvider provideFooCases
  127. */
  128. public function testFoo() {}
  129. abstract public static function provideFooCases();
  130. }',
  131. '<?php
  132. abstract class FooTest extends TestCase {
  133. /**
  134. * @dataProvider provideFooCases
  135. */
  136. public function testFoo() {}
  137. abstract public function provideFooCases();
  138. }',
  139. ];
  140. yield 'fix when containing dynamic calls and with `force` disabled' => [
  141. '<?php
  142. class FooTest extends TestCase {
  143. /**
  144. * @dataProvider provideFooCases1
  145. * @dataProvider provideFooCases2
  146. */
  147. public function testFoo() {}
  148. public function provideFooCases1() { return $this->getFoo(); }
  149. public static function provideFooCases2() { /* no dynamic calls */ }
  150. }',
  151. '<?php
  152. class FooTest extends TestCase {
  153. /**
  154. * @dataProvider provideFooCases1
  155. * @dataProvider provideFooCases2
  156. */
  157. public function testFoo() {}
  158. public function provideFooCases1() { return $this->getFoo(); }
  159. public function provideFooCases2() { /* no dynamic calls */ }
  160. }',
  161. ['force' => false],
  162. ];
  163. yield 'fix when containing dynamic calls and with `force` enabled' => [
  164. '<?php
  165. class FooTest extends TestCase {
  166. /**
  167. * @dataProvider provideFooCases1
  168. * @dataProvider provideFooCases2
  169. */
  170. public function testFoo() {}
  171. public static function provideFooCases1() { return $this->getFoo(); }
  172. public static function provideFooCases2() { /* no dynamic calls */ }
  173. }',
  174. '<?php
  175. class FooTest extends TestCase {
  176. /**
  177. * @dataProvider provideFooCases1
  178. * @dataProvider provideFooCases2
  179. */
  180. public function testFoo() {}
  181. public function provideFooCases1() { return $this->getFoo(); }
  182. public function provideFooCases2() { /* no dynamic calls */ }
  183. }',
  184. ['force' => true],
  185. ];
  186. }
  187. }