This extension provides highly opinionated and strict rules for test cases for the PHPStan static analysis tool.
Run
$ composer require --dev brainbits/phpstan-rules
All of the rules provided by this library are included in rules.neon
.
When you are using phpstan/extension-installer
, rules.neon
will be automatically included.
Otherwise you need to include rules.neon
in your phpstan.neon
:
# phpstan.neon
includes:
- vendor/brainbits/phpstan-rules/rules.neon
This package provides the following rules for use with phpstan/phpstan
:
This rule checks that classes that are covered by @covers
annotation or #[CoversClass]
attribute exist.
This rule forces you to specify either a @covers
annotation or #[CoversClass]
, #[CoversFunction]
or #[CoversNothing]
attributes in unit tests (default: PHPUnit\Framework\TestCase
).
Why:
- It prevents code coverage sums to show higher values than expected.
❌
// tests/ExampleTestCase/Unit/MyInvalidClassTest.php
namespace ExampleTestCase\Unit;
final class MyInvalidClassTest extends \PHPUnit\Framework\TestCase {}
✅
// tests/ExampleTestCase/Unit/MyClassTest.php
namespace ExampleTestCase\Unit;
#[\PHPUnit\Framework\Attributes\CoversClass(MyClass::class)
final class MyClassTest extends \PHPUnit\Framework\TestCase {}
- By default, this rule detects unit tests by checking the namespace (it must contain the string
Unit
) and the class name ending (it must end with the stringTest
).
If you want to change the namespace string check described above, you can set your own string to be checked in the unitTestNamespaceContainsString
parameter.
# phpstan.neon
parameters:
brainbits:
unitTestNamespaceContainsString: CustomTestPath