Skip to content

Commit 2b7e7a0

Browse files
committed
Support test annotation and attribute
1 parent e25e5e9 commit 2b7e7a0

File tree

4 files changed

+70
-20
lines changed

4 files changed

+70
-20
lines changed

src/Rules/PHPUnit/DataProviderDataRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function processNode(Node $node, Scope $scope): array
8181
}
8282

8383
$testsWithProvider = [];
84-
$testMethods = $this->testMethodsHelper->getTestMethods($classReflection);
84+
$testMethods = $this->testMethodsHelper->getTestMethods($classReflection, $scope);
8585
foreach ($testMethods as $testMethod) {
8686
foreach ($this->dataProviderHelper->getDataProviderMethods($scope, $testMethod, $classReflection) as [, $providerMethodName]) {
8787
if ($providerMethodName === $method->getName()) {

src/Rules/PHPUnit/TestMethodsHelper.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace PHPStan\Rules\PHPUnit;
44

5+
use PHPStan\Analyser\Scope;
56
use PHPStan\Parser\Parser;
7+
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
68
use PHPStan\Reflection\ClassReflection;
79
use PHPStan\Reflection\ReflectionProvider;
810
use PHPStan\Type\FileTypeMapper;
@@ -33,18 +35,43 @@ public function __construct(
3335
/**
3436
* @return array<ReflectionMethod>
3537
*/
36-
public function getTestMethods(ClassReflection $class): array
38+
public function getTestMethods(ClassReflection $classReflection, Scope $scope): array
3739
{
3840
$testMethods = [];
39-
foreach ($class->getNativeReflection()->getMethods() as $reflectionMethod) {
41+
foreach ($classReflection->getNativeReflection()->getMethods() as $reflectionMethod) {
42+
if (!$reflectionMethod->isPublic()) {
43+
continue;
44+
}
45+
4046
if (str_starts_with(strtolower($reflectionMethod->getName()), 'test')) {
4147
$testMethods[] = $reflectionMethod;
4248
continue;
4349
}
4450

51+
$docComment = $reflectionMethod->getDocComment();
52+
if ($docComment !== false) {
53+
$methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
54+
$scope->getFile(),
55+
$classReflection->getName(),
56+
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
57+
$reflectionMethod->getName(),
58+
$docComment,
59+
);
60+
61+
if ($this->hasTestAnnotation($methodPhpDoc)) {
62+
$testMethods[] = $reflectionMethod;
63+
continue;
64+
}
65+
}
66+
4567
// todo: detect tests with @test annotation
4668

47-
$testAttributes = $reflectionMethod->getAttributes('PHPUnit\Framework\Attribute\Test');
69+
// XXX
70+
//if (!$this->phpunit10OrNewer) {
71+
// return;
72+
//}
73+
74+
$testAttributes = $reflectionMethod->getAttributes('PHPUnit\Framework\Attributes\Test');
4875
if ($testAttributes === []) {
4976
continue;
5077
}
@@ -55,4 +82,22 @@ public function getTestMethods(ClassReflection $class): array
5582
return $testMethods;
5683
}
5784

85+
private function hasTestAnnotation(?ResolvedPhpDocBlock $phpDoc): bool
86+
{
87+
if ($phpDoc === null) {
88+
return false;
89+
}
90+
91+
$phpDocNodes = $phpDoc->getPhpDocNodes();
92+
93+
foreach ($phpDocNodes as $docNode) {
94+
$tags = $docNode->getTagsByName('@test');
95+
if ($tags !== []) {
96+
return true;
97+
}
98+
}
99+
100+
return false;
101+
}
102+
58103
}

tests/Rules/PHPUnit/DataProviderDataRuleTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,35 @@ public function testRule(): void
4242
$this->analyse([__DIR__ . '/data/data-provider-data.php'], [
4343
[
4444
'Parameter #2 $input of method DataProviderDataTest\FooTest::testWithAttribute() expects string, int given.',
45-
23,
45+
24,
4646
],
4747
[
4848
'Parameter #2 $input of method DataProviderDataTest\FooTest::testWithAttribute() expects string, false given.',
49-
27,
49+
28,
5050
],
5151
[
5252
'Parameter #2 $input of method DataProviderDataTest\BarTest::testWithAnnotation() expects string, int given.',
53-
50,
53+
51,
5454
],
5555
[
5656
'Parameter #2 $input of method DataProviderDataTest\BarTest::testWithAnnotation() expects string, false given.',
57-
54,
57+
55,
5858
],
5959
[
60-
'Parameter #2 $input of method DataProviderDataTest\YieldTest::testYield() expects string, int given.',
61-
79,
60+
'Parameter #2 $input of method DataProviderDataTest\YieldTest::myTestMethod() expects string, int given.',
61+
81,
6262
],
6363
[
64-
'Parameter #2 $input of method DataProviderDataTest\YieldTest::testYield() expects string, false given.',
65-
85,
64+
'Parameter #2 $input of method DataProviderDataTest\YieldTest::myTestMethod() expects string, false given.',
65+
87,
6666
],
6767
[
68-
'Parameter #2 $input of method DataProviderDataTest\YieldFromTest::testYieldFrom() expects string, int given.',
69-
107,
68+
'Parameter #2 $input of method DataProviderDataTest\YieldFromTest::myTestMethod() expects string, int given.',
69+
112,
7070
],
7171
[
72-
'Parameter #2 $input of method DataProviderDataTest\YieldFromTest::testYieldFrom() expects string, false given.',
73-
111,
72+
'Parameter #2 $input of method DataProviderDataTest\YieldFromTest::myTestMethod() expects string, false given.',
73+
116,
7474
],
7575
]);
7676
}

tests/Rules/PHPUnit/data/data-provider-data.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace DataProviderDataTest;
44

55
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\Attributes\Test;
67
use PHPUnit\Framework\TestCase;
78

89
class FooTest extends TestCase
@@ -62,8 +63,9 @@ public function aProvider(): array
6263
class YieldTest extends TestCase
6364
{
6465

65-
/** @dataProvider yieldProvider */
66-
public function testYield(string $expectedResult, string $input): void
66+
#[DataProvider('yieldProvider')]
67+
#[Test]
68+
public function myTestMethod(string $expectedResult, string $input): void
6769
{
6870
}
6971

@@ -92,8 +94,11 @@ public function yieldProvider(): iterable
9294
class YieldFromTest extends TestCase
9395
{
9496

95-
/** @dataProvider yieldProvider */
96-
public function testYieldFrom(string $expectedResult, string $input): void
97+
/**
98+
* @dataProvider yieldProvider
99+
* @test
100+
*/
101+
public function myTestMethod(string $expectedResult, string $input): void
97102
{
98103
}
99104

0 commit comments

Comments
 (0)