Skip to content

Latest commit

 

History

History
85 lines (57 loc) · 3.16 KB

README.md

File metadata and controls

85 lines (57 loc) · 3.16 KB

PHPStan Clean Test rules

Continuous Integration Coverage Status Codacy Badge Latest Stable Version License

This extension provides highly opinionated and strict rules for test cases for the PHPStan static analysis tool.

Installation

Run

$ composer require --dev brainbits/phpstan-rules

Usage

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

Rules

This package provides the following rules for use with phpstan/phpstan:

CoversClassExistsRule

This rule checks that classes that are covered by @covers annotation or #[CoversClass] attribute exist.

CoversClassPresentRule

This rule forces you to specify either a @covers annotation or #[CoversClass], #[CoversFunction] or #[CoversNothing] attributes in unit tests (default: PHPUnit\Framework\TestCase).

Why:

  1. 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 {}

Defaults

  • 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 string Test).

Detecting unit tests namespace

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