Browse Source

PhpUnitTestCaseIndicator - Check if PHPUnit-test class extends another class

SpacePossum 3 years ago
parent
commit
7d4b0b02db

+ 6 - 0
src/Indicator/PhpUnitTestCaseIndicator.php

@@ -34,6 +34,12 @@ final class PhpUnitTestCaseIndicator
             return false;
         }
 
+        $extendsIndex = $tokens->getNextTokenOfKind($index, ['{', [T_EXTENDS]]);
+
+        if (!$tokens[$extendsIndex]->isGivenKind(T_EXTENDS)) {
+            return false;
+        }
+
         if (0 !== Preg::match('/(?:Test|TestCase)$/', $tokens[$index]->getContent())) {
             return true;
         }

+ 9 - 9
tests/Fixer/PhpUnit/PhpUnitInternalClassFixerTest.php

@@ -276,7 +276,7 @@ class Test extends TestCase
             'By default it will not mark an abstract class as internal' => [
                 '<?php
 
-abstract class Test
+abstract class Test extends TestCase
 {
 }
 ',
@@ -287,13 +287,13 @@ abstract class Test
 /**
  * @internal
  */
-abstract class Test
+abstract class Test extends TestCase
 {
 }
 ',
                 '<?php
 
-abstract class Test
+abstract class Test extends TestCase
 {
 }
 ',
@@ -304,7 +304,7 @@ abstract class Test
             'If final is not added as an option, final classes will not be marked internal' => [
                 '<?php
 
-final class Test
+final class Test extends TestCase
 {
 }
 ',
@@ -316,7 +316,7 @@ final class Test
             'If normal is not added as an option, normal classes will not be marked internal' => [
                 '<?php
 
-class Test
+class Test extends TestCase
 {
 }
 ',
@@ -331,11 +331,11 @@ class Test
 /**
  * @internal
  */
-class Test
+class Test extends TestCase
 {
 }
 
-abstract class Test
+abstract class Test2 extends TestCase
 {
 }
 
@@ -352,11 +352,11 @@ class Test extends TestCase
 ',
                 '<?php
 
-class Test
+class Test extends TestCase
 {
 }
 
-abstract class Test
+abstract class Test2 extends TestCase
 {
 }
 

+ 6 - 6
tests/Fixer/PhpUnit/PhpUnitSizeClassFixerTest.php

@@ -273,11 +273,11 @@ abstract class Test
 /**
  * @small
  */
-class Test
+class Test extends TestCase
 {
 }
 
-abstract class Test
+abstract class Test2 extends TestCase
 {
 }
 
@@ -288,17 +288,17 @@ class FooBar
 /**
  * @small
  */
-class Test extends TestCase
+class Test3 extends TestCase
 {
 }
 ',
                 '<?php
 
-class Test
+class Test extends TestCase
 {
 }
 
-abstract class Test
+abstract class Test2 extends TestCase
 {
 }
 
@@ -306,7 +306,7 @@ class FooBar
 {
 }
 
-class Test extends TestCase
+class Test3 extends TestCase
 {
 }
 ',

+ 2 - 2
tests/Fixtures/Integration/priority/php_unit_test_annotation,no_empty_phpdoc.test

@@ -4,7 +4,7 @@ Integration of fixers: php_unit_test_annotation,no_empty_phpdoc.
 {"php_unit_test_annotation": true, "no_empty_phpdoc" : true}
 --EXPECT--
 <?php
-class Test
+class Test extends TestCase
 {
     
     public function testFooBar() {}
@@ -12,7 +12,7 @@ class Test
 
 --INPUT--
 <?php
-class Test
+class Test extends TestCase
 {
     /**
      * @test

+ 2 - 2
tests/Fixtures/Integration/priority/php_unit_test_annotation,php_unit_method_casing.test

@@ -4,7 +4,7 @@ Integration of fixers: php_unit_test_annotation,php_unit_method_casing.
 {"php_unit_test_annotation": true, "php_unit_method_casing" :  {"case": "snake_case"}}
 --EXPECT--
 <?php
-class Test
+class Test extends TestCase
 {
     /**
      * It does thing for foo and bar
@@ -15,7 +15,7 @@ class Test
 
 --INPUT--
 <?php
-class Test
+class Test extends TestCase
 {
     /**
      * It does thing for foo and bar

+ 2 - 2
tests/Fixtures/Integration/priority/php_unit_test_annotation,phpdoc_trim.test

@@ -4,7 +4,7 @@ Integration of fixers: php_unit_test_annotation,phpdoc_trim.
 {"php_unit_test_annotation": true, "phpdoc_trim" : true}
 --EXPECT--
 <?php
-class Test
+class Test extends TestCase
 {
     /**
      * It does thing for foo and bar
@@ -14,7 +14,7 @@ class Test
 
 --INPUT--
 <?php
-class Test
+class Test extends TestCase
 {
     /**
      * It does thing for foo and bar

+ 131 - 111
tests/Indicator/PhpUnitTestCaseIndicatorTest.php

@@ -24,10 +24,7 @@ use PhpCsFixer\Tokenizer\Tokens;
  */
 final class PhpUnitTestCaseIndicatorTest extends TestCase
 {
-    /**
-     * @var null|PhpUnitTestCaseIndicator
-     */
-    private $indicator;
+    private ?PhpUnitTestCaseIndicator $indicator;
 
     protected function setUp(): void
     {
@@ -51,75 +48,94 @@ final class PhpUnitTestCaseIndicatorTest extends TestCase
         static::assertSame($expected, $this->indicator->isPhpUnitClass($tokens, $index));
     }
 
-    public function provideIsPhpUnitClassCases(): array
+    public function provideIsPhpUnitClassCases(): iterable
     {
-        return [
-            'Test class' => [
-                true,
-                Tokens::fromCode('<?php final class MyTest {}'),
-                3,
-            ],
-            'TestCase class' => [
-                true,
-                Tokens::fromCode('<?php final class SomeTestCase {}'),
-                3,
-            ],
-            'Extends Test' => [
-                true,
-                Tokens::fromCode('<?php final class foo extends Test {}'),
-                3,
-            ],
-            'Extends TestCase' => [
-                true,
-                Tokens::fromCode('<?php final class bar extends TestCase {}'),
-                3,
-            ],
-            'Implements AbstractFixerTest' => [
-                true,
-                Tokens::fromCode('<?php
+        yield 'Test class' => [
+            true,
+            Tokens::fromCode('<?php final class MyTest extends A {}'),
+            3,
+        ];
+
+        yield 'TestCase class' => [
+            true,
+            Tokens::fromCode('<?php final class SomeTestCase extends A {}'),
+            3,
+        ];
+
+        yield 'Extends Test' => [
+            true,
+            Tokens::fromCode('<?php final class foo extends Test {}'),
+            3,
+        ];
+
+        yield 'Extends TestCase' => [
+            true,
+            Tokens::fromCode('<?php final class bar extends TestCase {}'),
+            3,
+        ];
+
+        yield 'Implements AbstractFixerTest' => [
+            true,
+            Tokens::fromCode('<?php
 class A extends Foo implements PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest
 {
 }
 '),
-                1,
-            ],
-            'Extends TestCase implements Foo' => [
-                true,
-                Tokens::fromCode('<?php
+            1,
+        ];
+
+        yield 'Extends TestCase implements Foo' => [
+            true,
+            Tokens::fromCode('<?php
 class A extends TestCase implements Foo
 {
 }
 '),
-                1,
-            ],
-            'Implements TestInterface' => [
-                true,
-                Tokens::fromCode('<?php
-class Foo implements SomeTestInterface
+            1,
+        ];
+
+        yield 'Implements TestInterface' => [
+            true,
+            Tokens::fromCode('<?php
+class Foo extends A implements SomeTestInterface
 {
 }
 '),
-                1,
-            ],
-            'Implements TestInterface, SomethingElse' => [
-                true,
-                Tokens::fromCode('<?php
-class Foo implements TestInterface, SomethingElse
+            1,
+        ];
+
+        yield 'Implements TestInterface, SomethingElse' => [
+            true,
+            Tokens::fromCode('<?php
+class Foo extends A implements TestInterface, SomethingElse
 {
 }
 '),
-                1,
-            ],
-            [
-                false,
-                Tokens::fromCode('<?php final class MyClass {}'),
-                3,
-            ],
-            'Anonymous class' => [
-                false,
-                Tokens::fromCode('<?php $a = new class {};'),
-                7,
-            ],
+            1,
+        ];
+
+        yield [
+            false,
+            Tokens::fromCode('<?php final class MyClass {}'),
+            3,
+        ];
+
+        yield 'Anonymous class' => [
+            false,
+            Tokens::fromCode('<?php $a = new class {};'),
+            7,
+        ];
+
+        yield 'Test class that does not extends' => [
+            false,
+            Tokens::fromCode('<?php final class MyTest {}'),
+            3,
+        ];
+
+        yield 'TestCase class that does not extends' => [
+            false,
+            Tokens::fromCode('<?php final class SomeTestCase implements PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest {}'),
+            3,
         ];
     }
 
@@ -148,64 +164,68 @@ class Foo implements TestInterface, SomethingElse
         static::assertSame($expectedIndexes, $classes);
     }
 
-    public function provideFindPhpUnitClassesCases(): array
+    public function provideFindPhpUnitClassesCases(): iterable
     {
-        return [
-            'empty' => [
-                [],
-                '',
-            ],
-            'empty script' => [
-                [],
-                "<?php\n",
-            ],
-            'no test class' => [
-                [],
-                '<?php class Foo{}',
-            ],
-            'single test class' => [
-                [
-                    [6, 7],
-                ],
-                '<?php
-                    class MyTest {}
-                ',
-            ],
-            'two PHPUnit classes' => [
-                [
-                    [13, 26],
-                    [6, 7],
-                ],
-                '<?php
-                    class My1Test {}
-                    class My2Test { public function A() {} }
-                ',
+        yield 'empty' => [
+            [],
+            '',
+        ];
+
+        yield 'empty script' => [
+            [],
+            "<?php\n",
+        ];
+
+        yield 'no test class' => [
+            [],
+            '<?php class Foo{}',
+        ];
+
+        yield 'single test class' => [
+            [
+                [10, 11],
             ],
-            'mixed classes' => [
-                [
-                    [63, 76],
-                    [25, 38],
-                ],
-                '<?php
-                    class Foo1 { public function A() {} }
-                    class My1Test { public function A() {} }
-                    class Foo2 { public function A() {} }
-                    class My2Test { public function A() {} }
-                    class Foo3 { public function A() { return function (){}; } }
-                ',
+            '<?php
+                class MyTest extends Foo {}
+            ',
+        ];
+
+        yield 'two PHPUnit classes' => [
+            [
+                [21, 34],
+                [10, 11],
             ],
-            'class with anonymous class inside' => [
-                [],
-                '<?php
-                    class Foo
+            '<?php
+                class My1Test extends Foo1 {}
+                class My2Test extends Foo2 { public function A8() {} }
+            ',
+        ];
+
+        yield 'mixed classes' => [
+            [
+                [71, 84],
+                [29, 42],
+            ],
+            '<?php
+                class Foo1 { public function A1() {} }
+                class My1Test extends Foo1 { public function A2() {} }
+                class Foo2 { public function A3() {} }
+                class My2Test extends Foo2 { public function A4() {} }
+                class Foo3 { public function A5() { return function (){}; } }
+            ',
+        ];
+
+        yield 'class with anonymous class inside' => [
+            [],
+            '<?php
+                class Foo
+                {
+                    public function getClass()
                     {
-                        public function getClass()
-                        {
-                            return new class {};
-                        }
+                        return new class {};
                     }
-                ',
-            ],
+                }
+            ',
         ];
     }
 }