From 3ede29a80f826dc86b762ecde9679cd9042a2848 Mon Sep 17 00:00:00 2001 From: Levi Date: Wed, 7 May 2025 16:12:31 +0200 Subject: [PATCH 1/8] detect Test Attribute in TestsNodeAnalyzer::isTestClassMethod --- src/NodeAnalyzer/TestsNodeAnalyzer.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/NodeAnalyzer/TestsNodeAnalyzer.php b/src/NodeAnalyzer/TestsNodeAnalyzer.php index b3f726b2..f0668209 100644 --- a/src/NodeAnalyzer/TestsNodeAnalyzer.php +++ b/src/NodeAnalyzer/TestsNodeAnalyzer.php @@ -58,6 +58,16 @@ public function isTestClassMethod(ClassMethod $classMethod): bool return true; } + foreach ($classMethod->getAttrGroups() as $attrGroup) { + $hasTestAttribute = array_any( + $attrGroup->attrs, + static fn($attribute): bool => $attribute->name->toString() === 'PHPUnit\\Framework\\Attributes\\Test' + ); + if ($hasTestAttribute) { + return \true; + } + } + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod); return $phpDocInfo->hasByName('test'); } From 21199b3db1875a22afdf547b9d077535e9a84fc1 Mon Sep 17 00:00:00 2001 From: Levi Date: Wed, 7 May 2025 17:03:21 +0200 Subject: [PATCH 2/8] swap `array_any` to traditional foreach --- src/NodeAnalyzer/TestsNodeAnalyzer.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/NodeAnalyzer/TestsNodeAnalyzer.php b/src/NodeAnalyzer/TestsNodeAnalyzer.php index f0668209..c1155b4a 100644 --- a/src/NodeAnalyzer/TestsNodeAnalyzer.php +++ b/src/NodeAnalyzer/TestsNodeAnalyzer.php @@ -59,12 +59,10 @@ public function isTestClassMethod(ClassMethod $classMethod): bool } foreach ($classMethod->getAttrGroups() as $attrGroup) { - $hasTestAttribute = array_any( - $attrGroup->attrs, - static fn($attribute): bool => $attribute->name->toString() === 'PHPUnit\\Framework\\Attributes\\Test' - ); - if ($hasTestAttribute) { - return \true; + foreach ($attrGroup->attrs as $attribute) { + if($attribute->name->toString() === 'PHPUnit\\Framework\\Attributes\\Test'){ + return true; + } } } From 9a41b6427a4e2b42aaa18f48ccb1f1d08e924fad Mon Sep 17 00:00:00 2001 From: Levi Date: Wed, 7 May 2025 19:15:55 +0200 Subject: [PATCH 3/8] add tests for AddTestsVoidReturnTypeWhereNoReturnRector --- ...ddTestsVoidReturnTypeWhereNoReturnTest.php | 28 +++++++++++++++++++ .../Fixture/test_attribute.php.inc | 27 ++++++++++++++++++ .../Fixture/test_docblock.php.inc | 27 ++++++++++++++++++ .../Fixture/test_prefix_camelcase.php.inc | 25 +++++++++++++++++ .../Fixture/test_prefix_snakecase.php.inc | 25 +++++++++++++++++ .../Fixture/test_private_method.php.inc | 25 +++++++++++++++++ ...tests_void_return_type_where_no_return.php | 10 +++++++ 7 files changed, 167 insertions(+) create mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/AddTestsVoidReturnTypeWhereNoReturnTest.php create mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_attribute.php.inc create mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_docblock.php.inc create mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_camelcase.php.inc create mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_snakecase.php.inc create mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc create mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/config/add_tests_void_return_type_where_no_return.php diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/AddTestsVoidReturnTypeWhereNoReturnTest.php b/tests/AddTestsVoidReturnTypeWhereNoReturn/AddTestsVoidReturnTypeWhereNoReturnTest.php new file mode 100644 index 00000000..b3a73ce8 --- /dev/null +++ b/tests/AddTestsVoidReturnTypeWhereNoReturn/AddTestsVoidReturnTypeWhereNoReturnTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/add_tests_void_return_type_where_no_return.php'; + } +} diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_attribute.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_attribute.php.inc new file mode 100644 index 00000000..275b0ac9 --- /dev/null +++ b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_attribute.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_docblock.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_docblock.php.inc new file mode 100644 index 00000000..697d9b65 --- /dev/null +++ b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_docblock.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_camelcase.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_camelcase.php.inc new file mode 100644 index 00000000..1968af80 --- /dev/null +++ b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_camelcase.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_snakecase.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_snakecase.php.inc new file mode 100644 index 00000000..27f48522 --- /dev/null +++ b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_snakecase.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc new file mode 100644 index 00000000..9122d1d4 --- /dev/null +++ b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/config/add_tests_void_return_type_where_no_return.php b/tests/AddTestsVoidReturnTypeWhereNoReturn/config/add_tests_void_return_type_where_no_return.php new file mode 100644 index 00000000..c1f794f5 --- /dev/null +++ b/tests/AddTestsVoidReturnTypeWhereNoReturn/config/add_tests_void_return_type_where_no_return.php @@ -0,0 +1,10 @@ +rule(AddTestsVoidReturnTypeWhereNoReturnRector::class); +}; From a9513c1b51c47bcd556be3787daeb0640f3bcba7 Mon Sep 17 00:00:00 2001 From: Levi Date: Wed, 7 May 2025 19:21:36 +0200 Subject: [PATCH 4/8] fix style --- src/NodeAnalyzer/TestsNodeAnalyzer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NodeAnalyzer/TestsNodeAnalyzer.php b/src/NodeAnalyzer/TestsNodeAnalyzer.php index c1155b4a..9ba2f5eb 100644 --- a/src/NodeAnalyzer/TestsNodeAnalyzer.php +++ b/src/NodeAnalyzer/TestsNodeAnalyzer.php @@ -60,7 +60,7 @@ public function isTestClassMethod(ClassMethod $classMethod): bool foreach ($classMethod->getAttrGroups() as $attrGroup) { foreach ($attrGroup->attrs as $attribute) { - if($attribute->name->toString() === 'PHPUnit\\Framework\\Attributes\\Test'){ + if ($attribute->name->toString() === 'PHPUnit\\Framework\\Attributes\\Test') { return true; } } From abf99288ea731cec1e2f7eb871f9b1cd5bb4f4db Mon Sep 17 00:00:00 2001 From: Levi Date: Thu, 8 May 2025 08:04:15 +0200 Subject: [PATCH 5/8] run rector --- src/NodeAnalyzer/TestsNodeAnalyzer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NodeAnalyzer/TestsNodeAnalyzer.php b/src/NodeAnalyzer/TestsNodeAnalyzer.php index 9ba2f5eb..3c4bce34 100644 --- a/src/NodeAnalyzer/TestsNodeAnalyzer.php +++ b/src/NodeAnalyzer/TestsNodeAnalyzer.php @@ -58,8 +58,8 @@ public function isTestClassMethod(ClassMethod $classMethod): bool return true; } - foreach ($classMethod->getAttrGroups() as $attrGroup) { - foreach ($attrGroup->attrs as $attribute) { + foreach ($classMethod->getAttrGroups() as $attributeGroup) { + foreach ($attributeGroup->attrs as $attribute) { if ($attribute->name->toString() === 'PHPUnit\\Framework\\Attributes\\Test') { return true; } From b845d5ff4fc88515f7ce77bd453998e69715b14d Mon Sep 17 00:00:00 2001 From: Levi Date: Thu, 8 May 2025 08:07:48 +0200 Subject: [PATCH 6/8] also check protected method while we're at it --- .../Fixture/test_private_method.php.inc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc index 9122d1d4..ac72e859 100644 --- a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc +++ b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc @@ -7,6 +7,9 @@ class SomeClassTest extends TestCase private function testShouldDoStuff() { } + protected function testShouldDoStuffProtected() + { + } } ?> @@ -20,6 +23,9 @@ class SomeClassTest extends TestCase private function testShouldDoStuff() { } + protected function testShouldDoStuffProtected() + { + } } ?> From 034b6525d199e9eaa088d46281045a3264e56e52 Mon Sep 17 00:00:00 2001 From: Levi Date: Thu, 8 May 2025 10:12:37 +0200 Subject: [PATCH 7/8] add isTest tests with php attributes for existing rector rules --- .../some_fixture_with_test_attribute.php.inc | 37 +++++++++++++++++++ ..._in_annotation_with_test_attribute.php.inc | 32 ++++++++++++++++ ...ddTestsVoidReturnTypeWhereNoReturnTest.php | 28 -------------- .../Fixture/test_attribute.php.inc | 27 -------------- .../Fixture/test_docblock.php.inc | 27 -------------- .../Fixture/test_prefix_camelcase.php.inc | 25 ------------- .../Fixture/test_prefix_snakecase.php.inc | 25 ------------- .../Fixture/test_private_method.php.inc | 31 ---------------- ...tests_void_return_type_where_no_return.php | 10 ----- 9 files changed, 69 insertions(+), 173 deletions(-) create mode 100644 rules-tests/AnnotationsToAttributes/Rector/ClassMethod/TestWithAnnotationToAttributeRector/Fixture/some_fixture_with_test_attribute.php.inc create mode 100644 rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation_with_test_attribute.php.inc delete mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/AddTestsVoidReturnTypeWhereNoReturnTest.php delete mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_attribute.php.inc delete mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_docblock.php.inc delete mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_camelcase.php.inc delete mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_snakecase.php.inc delete mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc delete mode 100644 tests/AddTestsVoidReturnTypeWhereNoReturn/config/add_tests_void_return_type_where_no_return.php diff --git a/rules-tests/AnnotationsToAttributes/Rector/ClassMethod/TestWithAnnotationToAttributeRector/Fixture/some_fixture_with_test_attribute.php.inc b/rules-tests/AnnotationsToAttributes/Rector/ClassMethod/TestWithAnnotationToAttributeRector/Fixture/some_fixture_with_test_attribute.php.inc new file mode 100644 index 00000000..40134aba --- /dev/null +++ b/rules-tests/AnnotationsToAttributes/Rector/ClassMethod/TestWithAnnotationToAttributeRector/Fixture/some_fixture_with_test_attribute.php.inc @@ -0,0 +1,37 @@ + +----- + diff --git a/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation_with_test_attribute.php.inc b/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation_with_test_attribute.php.inc new file mode 100644 index 00000000..17018b88 --- /dev/null +++ b/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation_with_test_attribute.php.inc @@ -0,0 +1,32 @@ + +----- + diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/AddTestsVoidReturnTypeWhereNoReturnTest.php b/tests/AddTestsVoidReturnTypeWhereNoReturn/AddTestsVoidReturnTypeWhereNoReturnTest.php deleted file mode 100644 index b3a73ce8..00000000 --- a/tests/AddTestsVoidReturnTypeWhereNoReturn/AddTestsVoidReturnTypeWhereNoReturnTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/add_tests_void_return_type_where_no_return.php'; - } -} diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_attribute.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_attribute.php.inc deleted file mode 100644 index 275b0ac9..00000000 --- a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_attribute.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_docblock.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_docblock.php.inc deleted file mode 100644 index 697d9b65..00000000 --- a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_docblock.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_camelcase.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_camelcase.php.inc deleted file mode 100644 index 1968af80..00000000 --- a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_camelcase.php.inc +++ /dev/null @@ -1,25 +0,0 @@ - ------ - diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_snakecase.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_snakecase.php.inc deleted file mode 100644 index 27f48522..00000000 --- a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_prefix_snakecase.php.inc +++ /dev/null @@ -1,25 +0,0 @@ - ------ - diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc b/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc deleted file mode 100644 index ac72e859..00000000 --- a/tests/AddTestsVoidReturnTypeWhereNoReturn/Fixture/test_private_method.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/tests/AddTestsVoidReturnTypeWhereNoReturn/config/add_tests_void_return_type_where_no_return.php b/tests/AddTestsVoidReturnTypeWhereNoReturn/config/add_tests_void_return_type_where_no_return.php deleted file mode 100644 index c1f794f5..00000000 --- a/tests/AddTestsVoidReturnTypeWhereNoReturn/config/add_tests_void_return_type_where_no_return.php +++ /dev/null @@ -1,10 +0,0 @@ -rule(AddTestsVoidReturnTypeWhereNoReturnRector::class); -}; From f4e6c3c5d266aa8482d9797c96da8aaca8cec49d Mon Sep 17 00:00:00 2001 From: Levi Date: Thu, 8 May 2025 10:16:23 +0200 Subject: [PATCH 8/8] fix classname for test fixtures --- .../Fixture/test_in_annotation.php.inc | 4 ++-- .../Fixture/test_in_annotation_with_test_attribute.php.inc | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation.php.inc b/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation.php.inc index 1c1ea2e9..5659ec54 100644 --- a/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation.php.inc +++ b/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation.php.inc @@ -2,7 +2,7 @@ namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture; -class TestInAnnotatoin extends \PHPUnit\Framework\TestCase +class TestInAnnotation extends \PHPUnit\Framework\TestCase { /** * @test @@ -19,7 +19,7 @@ class TestInAnnotatoin extends \PHPUnit\Framework\TestCase namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture; -class TestInAnnotatoin extends \PHPUnit\Framework\TestCase +class TestInAnnotation extends \PHPUnit\Framework\TestCase { /** * @test diff --git a/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation_with_test_attribute.php.inc b/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation_with_test_attribute.php.inc index 17018b88..6ea6d07a 100644 --- a/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation_with_test_attribute.php.inc +++ b/rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation_with_test_attribute.php.inc @@ -2,7 +2,7 @@ namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture; -class TestInAnnotatoinWithTestAttribute extends \PHPUnit\Framework\TestCase +class TestInAnnotationWithTestAttribute extends \PHPUnit\Framework\TestCase { #[\PHPUnit\Framework\Attributes\Test] public function thisIsTest() @@ -17,7 +17,7 @@ class TestInAnnotatoinWithTestAttribute extends \PHPUnit\Framework\TestCase namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture; -class TestInAnnotatoinWithTestAttribute extends \PHPUnit\Framework\TestCase +class TestInAnnotationWithTestAttribute extends \PHPUnit\Framework\TestCase { /** * @doesNotPerformAssertions