PhpUnitDataProviderStaticFixerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. final class PhpUnitDataProviderStaticFixerTest extends AbstractFixerTestCase
  20. {
  21. /**
  22. * @param array<string, bool> $configuration
  23. *
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  27. {
  28. $this->fixer->configure($configuration);
  29. $this->doTest($expected, $input);
  30. }
  31. /**
  32. * @return iterable<array{0: string, 1?: null|string, 2?: array<string, bool>}>
  33. */
  34. public static function provideFixCases(): iterable
  35. {
  36. yield 'do not fix when containing dynamic calls by default' => [
  37. '<?php
  38. class FooTest extends TestCase {
  39. /**
  40. * @dataProvider provideFoo1Cases
  41. */
  42. public function testFoo1() {}
  43. public function provideFoo1Cases() { $this->init(); }
  44. }',
  45. ];
  46. yield 'fix single' => [
  47. '<?php
  48. class FooTest extends TestCase {
  49. /**
  50. * @dataProvider provideFooCases
  51. */
  52. public function testFoo() {}
  53. public static function provideFooCases() { $x->getData(); }
  54. }',
  55. '<?php
  56. class FooTest extends TestCase {
  57. /**
  58. * @dataProvider provideFooCases
  59. */
  60. public function testFoo() {}
  61. public function provideFooCases() { $x->getData(); }
  62. }',
  63. ];
  64. yield 'fix multiple' => [
  65. '<?php
  66. class FooTest extends TestCase {
  67. /** @dataProvider provider1 */
  68. public function testFoo1() {}
  69. /** @dataProvider provider2 */
  70. public function testFoo2() {}
  71. /** @dataProvider provider3 */
  72. public function testFoo3() {}
  73. /** @dataProvider provider4 */
  74. public function testFoo4() {}
  75. public static function provider1() {}
  76. public function provider2() { $this->init(); }
  77. public static function provider3() {}
  78. public static function provider4() {}
  79. }',
  80. '<?php
  81. class FooTest extends TestCase {
  82. /** @dataProvider provider1 */
  83. public function testFoo1() {}
  84. /** @dataProvider provider2 */
  85. public function testFoo2() {}
  86. /** @dataProvider provider3 */
  87. public function testFoo3() {}
  88. /** @dataProvider provider4 */
  89. public function testFoo4() {}
  90. public function provider1() {}
  91. public function provider2() { $this->init(); }
  92. public function provider3() {}
  93. public static function provider4() {}
  94. }',
  95. ];
  96. yield 'fix with multilines' => [
  97. '<?php
  98. class FooTest extends TestCase {
  99. /**
  100. * @dataProvider provideFooCases
  101. */
  102. public function testFoo() {}
  103. public
  104. static function
  105. provideFooCases() { $x->getData(); }
  106. }',
  107. '<?php
  108. class FooTest extends TestCase {
  109. /**
  110. * @dataProvider provideFooCases
  111. */
  112. public function testFoo() {}
  113. public
  114. function
  115. provideFooCases() { $x->getData(); }
  116. }',
  117. ];
  118. yield 'fix when data provider is abstract' => [
  119. '<?php
  120. abstract class FooTest extends TestCase {
  121. /**
  122. * @dataProvider provideFooCases
  123. */
  124. public function testFoo() {}
  125. abstract public static function provideFooCases();
  126. }',
  127. '<?php
  128. abstract class FooTest extends TestCase {
  129. /**
  130. * @dataProvider provideFooCases
  131. */
  132. public function testFoo() {}
  133. abstract public function provideFooCases();
  134. }',
  135. ];
  136. yield 'fix when containing dynamic calls and with `force` disabled' => [
  137. '<?php
  138. class FooTest extends TestCase {
  139. /**
  140. * @dataProvider provideFooCases1
  141. * @dataProvider provideFooCases2
  142. */
  143. public function testFoo() {}
  144. public function provideFooCases1() { return $this->getFoo(); }
  145. public static function provideFooCases2() { /* no dynamic calls */ }
  146. }',
  147. '<?php
  148. class FooTest extends TestCase {
  149. /**
  150. * @dataProvider provideFooCases1
  151. * @dataProvider provideFooCases2
  152. */
  153. public function testFoo() {}
  154. public function provideFooCases1() { return $this->getFoo(); }
  155. public function provideFooCases2() { /* no dynamic calls */ }
  156. }',
  157. ['force' => false],
  158. ];
  159. yield 'fix when containing dynamic calls and with `force` enabled' => [
  160. '<?php
  161. class FooTest extends TestCase {
  162. /**
  163. * @dataProvider provideFooCases1
  164. * @dataProvider provideFooCases2
  165. */
  166. public function testFoo() {}
  167. public static function provideFooCases1() { return $this->getFoo(); }
  168. public static function provideFooCases2() { /* no dynamic calls */ }
  169. }',
  170. '<?php
  171. class FooTest extends TestCase {
  172. /**
  173. * @dataProvider provideFooCases1
  174. * @dataProvider provideFooCases2
  175. */
  176. public function testFoo() {}
  177. public function provideFooCases1() { return $this->getFoo(); }
  178. public function provideFooCases2() { /* no dynamic calls */ }
  179. }',
  180. ['force' => true],
  181. ];
  182. }
  183. }