DataProviderAnalyzerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\Tokenizer\Analyzer;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\Analyzer\Analysis\DataProviderAnalysis;
  15. use PhpCsFixer\Tokenizer\Analyzer\DataProviderAnalyzer;
  16. use PhpCsFixer\Tokenizer\Tokens;
  17. /**
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Tokenizer\Analyzer\DataProviderAnalyzer
  21. */
  22. final class DataProviderAnalyzerTest extends TestCase
  23. {
  24. /**
  25. * @param list<DataProviderAnalysis> $expected
  26. *
  27. * @dataProvider provideGettingDataProvidersCases
  28. */
  29. public function testGettingDataProviders(array $expected, string $code, int $startIndex = 0, ?int $endIndex = null): void
  30. {
  31. $tokens = Tokens::fromCode($code);
  32. if (null === $endIndex) {
  33. $endIndex = $tokens->count() - 1;
  34. }
  35. $analyzer = new DataProviderAnalyzer();
  36. self::assertSame(serialize($expected), serialize($analyzer->getDataProviders($tokens, $startIndex, $endIndex)));
  37. }
  38. /**
  39. * @return iterable<array{list<DataProviderAnalysis>, string}>
  40. */
  41. public static function provideGettingDataProvidersCases(): iterable
  42. {
  43. yield 'single data provider' => [
  44. [new DataProviderAnalysis('provider', 28, [[11, 23]])],
  45. '<?php class FooTest extends TestCase {
  46. /**
  47. * @dataProvider provider
  48. */
  49. public function testFoo() {}
  50. public function provider() {}
  51. }',
  52. ];
  53. yield 'single data provider with different casing' => [
  54. [new DataProviderAnalysis('dataProvider', 28, [[11, 23]])],
  55. '<?php class FooTest extends TestCase {
  56. /**
  57. * @dataProvider dataPROVIDER
  58. */
  59. public function testFoo() {}
  60. public function dataProvider() {}
  61. }',
  62. ];
  63. yield 'single static data provider' => [
  64. [new DataProviderAnalysis('provider', 30, [[11, 23]])],
  65. '<?php class FooTest extends TestCase {
  66. /**
  67. * @dataProvider provider
  68. */
  69. public function testFoo() {}
  70. public static function provider() {}
  71. }',
  72. ];
  73. yield 'multiple data provider' => [
  74. [
  75. new DataProviderAnalysis('provider1', 28, [[11, 23]]),
  76. new DataProviderAnalysis('provider2', 39, [[11, 66]]),
  77. new DataProviderAnalysis('provider3', 50, [[11, 109]]),
  78. ],
  79. '<?php class FooTest extends TestCase {
  80. /**
  81. * @dataProvider provider1
  82. * @dataProvider provider2
  83. * @dataProvider provider3
  84. */
  85. public function testFoo() {}
  86. public function provider1() {}
  87. public function provider2() {}
  88. public function provider3() {}
  89. }',
  90. ];
  91. yield 'single data provider with multiple usage' => [
  92. [
  93. new DataProviderAnalysis('provider', 28, [[11, 23], [35, 23]]),
  94. ],
  95. '<?php class FooTest extends TestCase {
  96. /**
  97. * @dataProvider provider
  98. */
  99. public function testFoo() {}
  100. public function provider() {}
  101. /**
  102. * @dataProvider provider
  103. */
  104. public function testFoo2() {}
  105. }',
  106. ];
  107. foreach (['abstract', 'final', 'private', 'protected', 'static', '/* private */'] as $modifier) {
  108. yield \sprintf('test function with %s modifier', $modifier) => [
  109. [
  110. new DataProviderAnalysis('provider1', 54, [[37, 4]]),
  111. new DataProviderAnalysis('provider2', 65, [[11, 4]]),
  112. new DataProviderAnalysis('provider3', 76, [[24, 4]]),
  113. ],
  114. \sprintf('<?php class FooTest extends TestCase {
  115. /** @dataProvider provider2 */
  116. public function testFoo1() {}
  117. /** @dataProvider provider3 */
  118. %s function testFoo2() {}
  119. /** @dataProvider provider1 */
  120. public function testFoo3() {}
  121. public function provider1() {}
  122. public function provider2() {}
  123. public function provider3() {}
  124. }', $modifier),
  125. ];
  126. }
  127. yield 'not existing data provider used' => [
  128. [],
  129. '<?php class FooTest extends TestCase {
  130. /**
  131. * @dataProvider provider
  132. */
  133. public function testFoo() {}
  134. }',
  135. ];
  136. yield 'data provider being constant' => [
  137. [],
  138. '<?php class FooTest extends TestCase {
  139. private const provider = [];
  140. /**
  141. * @dataProvider provider
  142. */
  143. public function testFoo() {}
  144. }',
  145. ];
  146. yield 'ignore anonymous function' => [
  147. [
  148. new DataProviderAnalysis('provider2', 93, [[65, 27]]),
  149. ],
  150. '<?php class FooTest extends TestCase {
  151. public function testFoo0() {}
  152. /**
  153. * @dataProvider provider0
  154. */
  155. public function testFoo1()
  156. {
  157. /**
  158. * @dataProvider provider1
  159. */
  160. $f = function ($x, $y) { return $x + $y; };
  161. }
  162. /**
  163. * @dataProvider provider2
  164. */
  165. public function testFoo2() {}
  166. public function provider1() {}
  167. public function provider2() {}
  168. }',
  169. ];
  170. }
  171. /**
  172. * @param list<DataProviderAnalysis> $expected
  173. *
  174. * @requires PHP ^8.0
  175. *
  176. * @dataProvider provideGettingDataProviders80Cases
  177. */
  178. public function testGettingDataProviders80(array $expected, string $code, int $startIndex = 0, ?int $endIndex = null): void
  179. {
  180. $this->testGettingDataProviders($expected, $code, $startIndex, $endIndex);
  181. }
  182. /**
  183. * @return iterable<array{list<DataProviderAnalysis>, string}>
  184. */
  185. public static function provideGettingDataProviders80Cases(): iterable
  186. {
  187. yield 'with an attribute between PHPDoc and test method' => [
  188. [new DataProviderAnalysis('provideFooCases', 35, [[11, 11]])],
  189. <<<'PHP'
  190. <?php
  191. class FooTest extends TestCase {
  192. /**
  193. * @dataProvider provideFooCases
  194. */
  195. #[CustomAttribute]
  196. public function testFoo(): void {}
  197. public function provideFooCases(): iterable {}
  198. }
  199. PHP,
  200. ];
  201. }
  202. }