-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathTestsNodeAnalyzer.php
More file actions
179 lines (148 loc) · 5.49 KB
/
Copy pathTestsNodeAnalyzer.php
File metadata and controls
179 lines (148 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PHPUnit\Enum\PHPUnitAttribute;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\Reflection\ReflectionResolver;
final readonly class TestsNodeAnalyzer
{
public function __construct(
private NodeTypeResolver $nodeTypeResolver,
private NodeNameResolver $nodeNameResolver,
private PhpDocInfoFactory $phpDocInfoFactory,
private ReflectionResolver $reflectionResolver
) {
}
public function isInTestClass(Node $node): bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if (! $classReflection instanceof ClassReflection) {
return false;
}
// traits have no parent, so the test case check below can never match them
if ($classReflection->isTrait()) {
return $this->isInTestTrait($classReflection, $node);
}
return array_any(
PHPUnitClassName::TEST_CLASSES,
fn (string $testCaseObjectClass): bool => $classReflection->is($testCaseObjectClass)
);
}
public function isTestClassMethod(ClassMethod $classMethod): bool
{
if (! $classMethod->isPublic()) {
return false;
}
if (str_starts_with($classMethod->name->toString(), 'test')) {
return true;
}
foreach ($classMethod->getAttrGroups() as $attributeGroup) {
foreach ($attributeGroup->attrs as $attribute) {
if ($this->nodeNameResolver->isName($attribute->name, PHPUnitAttribute::TEST)) {
return true;
}
}
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
return $phpDocInfo->hasByName('test');
}
public function isAssertMethodCallName(Node $node, string $name): bool
{
if ($node instanceof StaticCall) {
$callerType = $this->nodeTypeResolver->getType($node->class);
} elseif ($node instanceof MethodCall) {
$callerType = $this->nodeTypeResolver->getType($node->var);
} else {
return false;
}
$assertObjectType = new ObjectType(PHPUnitClassName::ASSERT);
if (! $assertObjectType->isSuperTypeOf($callerType)
->yes()) {
return false;
}
/** @var StaticCall|MethodCall $node */
return $this->nodeNameResolver->isName($node->name, $name);
}
/**
* Test traits live next to the test cases that use them, so the namespace is the main hint.
* Only public non-static methods can be test methods, and a "test" prefixed one is a test
* method even outside a tests namespace.
*/
private function isInTestTrait(ClassReflection $classReflection, Node $node): bool
{
if (! $node instanceof ClassMethod) {
return $this->isInTestsNamespace($classReflection);
}
if (! $node->isPublic()) {
return false;
}
if ($node->isStatic()) {
return false;
}
if ($this->isInTestsNamespace($classReflection)) {
return true;
}
return str_starts_with($node->name->toString(), 'test');
}
private function isInTestsNamespace(ClassReflection $classReflection): bool
{
$nameParts = explode('\\', $classReflection->getName());
// drop the short trait name, only the namespace matters here
array_pop($nameParts);
return array_any(
$nameParts,
static fn (string $namePart): bool => in_array($namePart, ['Test', 'Tests'], true)
);
}
/**
* @param string[] $names
*/
public function isPHPUnitMethodCallNames(Node $node, array $names): bool
{
if (! $this->isPHPUnitTestCaseCall($node)) {
return false;
}
/** @var MethodCall|StaticCall $node */
return $this->nodeNameResolver->isNames($node->name, $names);
}
public function isPHPUnitTestCaseCall(Node $node): bool
{
if ($node instanceof MethodCall) {
$callerType = $this->nodeTypeResolver->getType($node->var);
if ($callerType instanceof StaticType) {
$callerType = $callerType->getStaticObjectType();
}
if ($callerType instanceof ObjectType) {
if ($callerType->isInstanceOf(PHPUnitClassName::TEST_CASE)->yes()) {
return true;
}
if ($callerType->isInstanceOf(PHPUnitClassName::ASSERT)->yes()) {
return true;
}
}
return false;
}
if ($node instanceof StaticCall) {
$classType = $this->nodeTypeResolver->getType($node->class);
if ($classType instanceof ObjectType) {
if ($classType->isInstanceOf(PHPUnitClassName::TEST_CASE)->yes()) {
return true;
}
if ($classType->isInstanceOf(PHPUnitClassName::ASSERT)->yes()) {
return true;
}
}
}
return false;
}
}