Handle test traits in DataProviderAnnotationToAttributeRector - #751
Merged
Conversation
Traits have no parent class, so isInTestClass() could never match them and every @dataProvider annotation in a test trait was left behind. Detect traits by a Test/Tests namespace part instead, and drop the now-redundant isClass() guard in the rule.
A trait method can only be a test method when it is public and non-static. Beyond that, either the trait sits in a Test/Tests namespace or the method carries the "test" prefix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@dataProviderannotations inside test traits were never converted, becauseTestsNodeAnalyzer::isInTestClass()only checks$classReflection->is(TestCase::class). A trait has no parent, so that check can never match — the whole file is skipped.Real-world case in Mautic:
Left untouched by the PHPUnit 10 set, so the upgrade silently misses it.
Change
Traits are now detected by a
Test/Testsnamespace part, since test traits live next to the test cases that use them:DataProviderAnnotationToAttributeRectoralso carried its own! $classReflection->isClass()guard, which blocked traits even once the analyzer accepted them. Dropped —isInTestClass()already rejects interfaces and enums, so the guard was redundant.Result:
trait SomeTestTrait { - /** - * @dataProvider someMethod() - */ + #[\PHPUnit\Framework\Attributes\DataProvider('someMethod')] public function test(): void { } }Note
isInTestClass()is used by ~74 rules, so this widens all of them to test traits — which is the intended behaviour, but worth a second look.