Skip to content

Check CoversClass, CoversTrait, CoversMethod and CoversFunction attributes - #358

Open
theluckystrike wants to merge 1 commit into
phpstan:2.1.xfrom
theluckystrike:covers-attributes
Open

theluckystrike wants to merge 1 commit into
phpstan:2.1.xfrom
theluckystrike:covers-attributes

Conversation

@theluckystrike

Copy link
Copy Markdown

Closes #289.

ClassCoversExistsRule now also checks the PHPUnit coverage attributes on test classes:

  • #[CoversClass(X::class)]: X must exist and must not be an interface (phpunit.coversClass, phpunit.coversInterface)
  • #[CoversTrait(T::class)]: T must exist and be a trait (phpunit.coversTrait)
  • #[CoversMethod(X::class, 'm')]: X must exist and have method m (phpunit.coversMethod)
  • #[CoversFunction('f')]: f must exist (phpunit.coversFunction)

The attributes are read with getAttributesByName() from the BetterReflection class, like ClassAttributeRequiresPhpVersionRule does, so arguments come in already evaluated. Named arguments (className: etc.) work too. Errors are reported on the class line, the same place as the @covers errors.

I did not change ClassMethodCoversExistsRule. In PHPUnit these attributes only target classes (Attribute::TARGET_CLASS), so there is nothing to read on methods. If you want the method rule to report a Covers attribute placed on a method, I can add that.

Two points for review: phpunit.coversTrait and phpunit.coversFunction are new identifiers, and I kept the messages close to the @covers ones. Happy to rename them.

The new testAttributes case in ClassCoversExistsRuleTest uses data/class-coverage-attributes.php with valid and invalid uses and a non-TestCase class. It fails on 2.1.x and passes with the change. phpunit, phpstan, parallel-lint and phpcs with build-cs pass locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ClassCoversExistsRule and ClassMethodCoversExistsRule do not support attributes

1 participant