Repository navigation
Check CoversClass, CoversTrait, CoversMethod and CoversFunction attributes - #358
theluckystrike wants to merge 1 commit into
Conversation
…ction] attributes
|
//cc @SanderMuller |
There was a problem hiding this comment.
- please make the new errors only show up when bleeding-edge is enabled
- each error should point to the line of the attribute
#[CoversTrait]is never tested with a class that doesn't exist, so removing the hasClass() part of the condition goes unnoticed#[CoversClass]with a trait isn't reported. PHPUnit 11.x deprecates this in favour of#[CoversTrait], and 12 rejects it.
|
I checked the four points against 8d42830 and ran the targets through PHPUnit 11.5.58 and 12.5.45, with coverage on.
PHPUnit 12 warns on more targets that the PR accepts:
Reading the arguments from the attribute nodes of Performance: 2,000 generated test classes with four Covers attributes each, three interleaved rounds. User CPU was 4.70 s on 2.1.x and 4.72 s on the PR (medians), with runs between 4.54 and 4.84 s. That is no measurable difference. The PR's gate passes locally: 99 tests, |
Closes #289.
ClassCoversExistsRulenow 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, likeClassAttributeRequiresPhpVersionRuledoes, so arguments come in already evaluated. Named arguments (className:etc.) work too. Errors are reported on the class line, the same place as the@coverserrors.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.coversTraitandphpunit.coversFunctionare new identifiers, and I kept the messages close to the@coversones. Happy to rename them.The new
testAttributescase inClassCoversExistsRuleTestusesdata/class-coverage-attributes.phpwith 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.