website/errors/phpunit.attributeRequiresPhpVersion.md
<?php declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
class MyTest extends TestCase
{
#[RequiresPhp('8.1')]
public function testFeature(): void
{
// ...
}
}
The #[RequiresPhp] attribute is also checked when placed on the test class itself, not just on individual test methods:
<?php declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
#[RequiresPhp('< 7.0')]
class MyTest extends TestCase
{
public function testFeature(): void
{
// ...
}
}
This rule is provided by the phpstan-phpunit extension.
The #[RequiresPhp] attribute controls whether a test (or every test in a class) runs on the analysed PHP version. PHPStan reports several problems with its version requirement:
'8.1' is given without a comparison operator. In newer PHPUnit versions the requirement must include an explicit operator (e.g. >= 8.1); a bare numeric version is ambiguous. On older PHPUnit versions where it is merely deprecated, the error is reported only when phpstan-deprecation-rules is installed.'< 7.0' while analysing PHP 8.x), so the test would always be skipped.major.minor.patch triple. PHPUnit may interpret it in surprising ways. This warning is reported on bleeding edge.Use a full major.minor.patch version together with an operator that can actually be satisfied by the analysed PHP version.
Add a comparison operator to the version requirement:
<?php declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
class MyTest extends TestCase
{
- #[RequiresPhp('8.1')]
+ #[RequiresPhp('>= 8.1')]
public function testFeature(): void
{
// ...
}
}