Skip to content

Commit 8ec191f

Browse files
authored
Check #[RequiresPhpunit] in the same way we check #[RequiresPhp] (#308)
1 parent d138088 commit 8ec191f

6 files changed

Lines changed: 82 additions & 5 deletions

src/Rules/PHPUnit/AttributeRequiresPhpVersionRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Node\InClassMethodNode;
88
use PHPStan\Rules\Rule;
99
use PHPUnit\Framework\TestCase;
10+
use function array_merge;
1011

1112
/**
1213
* @implements Rule<InClassMethodNode>
@@ -44,8 +45,11 @@ public function processNode(Node $node, Scope $scope): array
4445
return [];
4546
}
4647

47-
return $this->attributeVersionRequirementHelper->checkRequiresPhpVersion(
48-
$reflectionMethod->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp'),
48+
return $this->attributeVersionRequirementHelper->checkVersionRequirement(
49+
array_merge(
50+
$reflectionMethod->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp'),
51+
$reflectionMethod->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhpunit'),
52+
),
4953
$scope,
5054
);
5155
}

src/Rules/PHPUnit/AttributeVersionRequirementHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct(
6161
*
6262
* @return list<IdentifierRuleError>
6363
*/
64-
public function checkRequiresPhpVersion(array $attributes, Scope $scope): array
64+
public function checkVersionRequirement(array $attributes, Scope $scope): array
6565
{
6666
$phpstanPharIoVersions = $this->getAnalyzedPhpVersions($scope);
6767
if ($phpstanPharIoVersions === []) {

src/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Node\InClassNode;
88
use PHPStan\Rules\Rule;
99
use PHPUnit\Framework\TestCase;
10+
use function array_merge;
1011

1112
/**
1213
* @implements Rule<InClassNode>
@@ -35,8 +36,11 @@ public function processNode(Node $node, Scope $scope): array
3536
return [];
3637
}
3738

38-
return $this->attributeVersionRequirementHelper->checkRequiresPhpVersion(
39-
$classReflection->getNativeReflection()->getBetterReflection()->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp'),
39+
return $this->attributeVersionRequirementHelper->checkVersionRequirement(
40+
array_merge(
41+
$classReflection->getNativeReflection()->getBetterReflection()->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhp'),
42+
$classReflection->getNativeReflection()->getBetterReflection()->getAttributesByName('PHPUnit\Framework\Attributes\RequiresPhpunit'),
43+
),
4044
$scope,
4145
);
4246
}

tests/Rules/PHPUnit/AttributeRequiresPhpVersionRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ public function testWarnAboutIncompleteVersion(): void
170170
]);
171171
}
172172

173+
public function testWarnAboutIncompletePhpunitVersion(): void
174+
{
175+
$this->phpunitMajorVersion = 12;
176+
$this->phpunitMinorVersion = 5;
177+
$this->deprecationRulesInstalled = false;
178+
$this->warnAboutIncompleteVersion = true;
179+
180+
$this->analyse([__DIR__ . '/data/requires-phpunit-version.php'], [
181+
[
182+
'Version requirement is incomplete.',
183+
12,
184+
],
185+
]);
186+
}
187+
173188
protected function getRule(): Rule
174189
{
175190
$phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion);

tests/Rules/PHPUnit/ClassAttributeRequiresPhpVersionRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ public function testWarnAboutIncompleteVersion(): void
3838
]);
3939
}
4040

41+
public function testWarnAboutIncompletePhpunitVersion(): void
42+
{
43+
$this->phpunitMajorVersion = 12;
44+
$this->phpunitMinorVersion = 5;
45+
$this->warnAboutIncompleteVersion = true;
46+
47+
$this->analyse([__DIR__ . '/data/requires-phpunit-version.php'], [
48+
[
49+
'Version requirement is incomplete.',
50+
18,
51+
],
52+
]);
53+
}
54+
4155
protected function getRule(): Rule
4256
{
4357
$phpunitVersion = new PHPUnitVersion($this->phpunitMajorVersion, $this->phpunitMinorVersion);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace RequiresPhpunitVersion;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use PHPUnit\Framework\TestCase;
8+
use PHPUnit\Framework\Attributes\RequiresPhpunit;
9+
10+
class TwoDigitVersionA extends TestCase
11+
{
12+
#[RequiresPhpunit('8.0')]
13+
public function testFoo(): void {
14+
15+
}
16+
}
17+
18+
#[RequiresPhpunit('>=8.0')]
19+
class TwoDigitVersionB extends TestCase
20+
{
21+
public function testBar(): void {
22+
23+
}
24+
}
25+
26+
class CorrectRequirement extends TestCase
27+
{
28+
#[RequiresPhpunit('>=8.0.0')]
29+
public function testBar(): void {
30+
31+
}
32+
}
33+
34+
#[RequiresPhpunit('>=8.0.0')]
35+
class CorrectClassRequirement extends TestCase
36+
{
37+
public function testBar(): void {
38+
39+
}
40+
}

0 commit comments

Comments
 (0)