PhpUnitTestCaseIndicatorTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\Indicator;
  13. use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
  14. use PhpCsFixer\Tests\TestCase;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Indicator\PhpUnitTestCaseIndicator
  20. */
  21. final class PhpUnitTestCaseIndicatorTest extends TestCase
  22. {
  23. private ?PhpUnitTestCaseIndicator $indicator;
  24. protected function setUp(): void
  25. {
  26. $this->indicator = new PhpUnitTestCaseIndicator();
  27. parent::setUp();
  28. }
  29. protected function tearDown(): void
  30. {
  31. $this->indicator = null;
  32. parent::tearDown();
  33. }
  34. /**
  35. * @dataProvider provideIsPhpUnitClassCases
  36. */
  37. public function testIsPhpUnitClass(bool $expected, Tokens $tokens, int $index): void
  38. {
  39. self::assertSame($expected, $this->indicator->isPhpUnitClass($tokens, $index));
  40. }
  41. /**
  42. * @return iterable<int|string, array{bool, Tokens, int}>
  43. */
  44. public static function provideIsPhpUnitClassCases(): iterable
  45. {
  46. yield 'Test class' => [
  47. true,
  48. Tokens::fromCode('<?php final class MyTest extends A {}'),
  49. 3,
  50. ];
  51. yield 'TestCase class' => [
  52. true,
  53. Tokens::fromCode('<?php final class SomeTestCase extends A {}'),
  54. 3,
  55. ];
  56. yield 'Extends Test' => [
  57. true,
  58. Tokens::fromCode('<?php final class foo extends Test {}'),
  59. 3,
  60. ];
  61. yield 'Extends TestCase' => [
  62. true,
  63. Tokens::fromCode('<?php final class bar extends TestCase {}'),
  64. 3,
  65. ];
  66. yield 'Implements AbstractFixerTest' => [
  67. true,
  68. Tokens::fromCode('<?php
  69. class A extends Foo implements PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest
  70. {
  71. }
  72. '),
  73. 1,
  74. ];
  75. yield 'Extends TestCase implements Foo' => [
  76. true,
  77. Tokens::fromCode('<?php
  78. class A extends TestCase implements Foo
  79. {
  80. }
  81. '),
  82. 1,
  83. ];
  84. yield 'Implements TestInterface' => [
  85. true,
  86. Tokens::fromCode('<?php
  87. class Foo extends A implements SomeTestInterface
  88. {
  89. }
  90. '),
  91. 1,
  92. ];
  93. yield 'Implements TestInterface, SomethingElse' => [
  94. true,
  95. Tokens::fromCode('<?php
  96. class Foo extends A implements TestInterface, SomethingElse
  97. {
  98. }
  99. '),
  100. 1,
  101. ];
  102. yield [
  103. false,
  104. Tokens::fromCode('<?php final class MyClass {}'),
  105. 3,
  106. ];
  107. yield 'Anonymous class' => [
  108. false,
  109. Tokens::fromCode('<?php $a = new class {};'),
  110. 7,
  111. ];
  112. yield 'Test class that does not extends' => [
  113. false,
  114. Tokens::fromCode('<?php final class MyTest {}'),
  115. 3,
  116. ];
  117. yield 'TestCase class that does not extends' => [
  118. false,
  119. Tokens::fromCode('<?php final class SomeTestCase implements PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest {}'),
  120. 3,
  121. ];
  122. }
  123. public function testThrowsExceptionIfNotClass(): void
  124. {
  125. $tokens = Tokens::fromCode('<?php echo 1;');
  126. $this->expectException(\LogicException::class);
  127. $this->expectExceptionMessageMatches('/^No "T_CLASS" at given index 1, got "T_ECHO"\.$/');
  128. $this->indicator->isPhpUnitClass($tokens, 1);
  129. }
  130. /**
  131. * @param list<array{0: int, 1: int}> $expectedIndexes
  132. *
  133. * @dataProvider provideFindPhpUnitClassesCases
  134. */
  135. public function testFindPhpUnitClasses(array $expectedIndexes, string $code): void
  136. {
  137. $tokens = Tokens::fromCode($code);
  138. $classes = $this->indicator->findPhpUnitClasses($tokens);
  139. $classes = iterator_to_array($classes);
  140. self::assertSame($expectedIndexes, $classes);
  141. }
  142. public static function provideFindPhpUnitClassesCases(): iterable
  143. {
  144. yield 'empty' => [
  145. [],
  146. '',
  147. ];
  148. yield 'empty script' => [
  149. [],
  150. "<?php\n",
  151. ];
  152. yield 'no test class' => [
  153. [],
  154. '<?php class Foo{}',
  155. ];
  156. yield 'single test class' => [
  157. [
  158. [10, 11],
  159. ],
  160. '<?php
  161. class MyTest extends Foo {}
  162. ',
  163. ];
  164. yield 'two PHPUnit classes' => [
  165. [
  166. [21, 34],
  167. [10, 11],
  168. ],
  169. '<?php
  170. class My1Test extends Foo1 {}
  171. class My2Test extends Foo2 { public function A8() {} }
  172. ',
  173. ];
  174. yield 'mixed classes' => [
  175. [
  176. [71, 84],
  177. [29, 42],
  178. ],
  179. '<?php
  180. class Foo1 { public function A1() {} }
  181. class My1Test extends Foo1 { public function A2() {} }
  182. class Foo2 { public function A3() {} }
  183. class My2Test extends Foo2 { public function A4() {} }
  184. class Foo3 { public function A5() { return function (){}; } }
  185. ',
  186. ];
  187. yield 'class with anonymous class inside' => [
  188. [],
  189. '<?php
  190. class Foo
  191. {
  192. public function getClass()
  193. {
  194. return new class {};
  195. }
  196. }
  197. ',
  198. ];
  199. }
  200. }