PhpUnitTestCaseIndicatorTest.php 5.6 KB

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