This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestClassMethodToPestTestFuncCallRector.php
158 lines (129 loc) · 4.82 KB
/
TestClassMethodToPestTestFuncCallRector.php
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
<?php
declare(strict_types=1);
namespace Rector\Pest\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\Pest\NodeFactory\PestFuncCallFactory;
use Rector\Pest\PhpDocResolver;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFinder\DataProviderClassMethodFinder;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Pest\Tests\Rector\ClassMethod\TestClassMethodToPestTestFuncCallRector\TestClassMethodToPestTestFuncCallRectorTest
*/
final class TestClassMethodToPestTestFuncCallRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly PestFuncCallFactory $pestFuncCallFactory,
private readonly DataProviderClassMethodFinder $dataProviderClassMethodFinder,
private readonly PhpDocResolver $phpDocResolver,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change PHPUnit test method to Pest test function', [
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
class ExampleTest extends TestCase
{
public function testSimple()
{
$this->assertTrue(true);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
test('testSimple', function () {
$this->assertTrue(true);
});
CODE_SAMPLE
)]);
}
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
* @return Node\Stmt[]|null
*/
public function refactor(Node $node): ?array
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
$testCallExpressions = [];
foreach ($node->getMethods() as $classMethod) {
if (! $this->testsNodeAnalyzer->isTestClassMethod($classMethod)) {
continue;
}
$testCall = $this->createPestTestCall($classMethod);
$testCallExpressions[] = new Expression($testCall);
}
if ($testCallExpressions === []) {
return null;
}
return $testCallExpressions;
}
private function createPestTestCall(ClassMethod $classMethod): FuncCall|MethodCall
{
$pestTestCall = $this->pestFuncCallFactory->create($classMethod);
// doc block related
$pestTestCall = $this->refactorPhpDocAnnotationToMethodCall($classMethod, $pestTestCall, 'group');
$pestTestCall = $this->refactorPhpDocAnnotationToMethodCall($classMethod, $pestTestCall, 'depends');
foreach ((array) $classMethod->stmts as $classMethodStmt) {
if (! $classMethodStmt instanceof Expression) {
continue;
}
if (! $classMethodStmt->expr instanceof MethodCall) {
continue;
}
$methodCall = $classMethodStmt->expr;
// this is important!
if ($this->isName($methodCall->name, 'expectException')) {
$pestTestCall = new MethodCall($pestTestCall, 'throws', $methodCall->getArgs());
$this->removeNode($classMethodStmt);
}
if ($this->isName($methodCall->name, 'markTestSkipped')) {
$pestTestCall = new MethodCall($pestTestCall, 'skip', $methodCall->getArgs());
$this->removeNode($classMethodStmt);
}
}
return $this->migrateDataProvider($classMethod, $pestTestCall);
}
private function migrateDataProvider(
ClassMethod $classMethod,
FuncCall|MethodCall $pestTestNode
): FuncCall|MethodCall {
$dataProviderMethodNames = $this->dataProviderClassMethodFinder->findDataProviderNamesForClassMethod(
$classMethod
);
$dataProviderMethodName = $dataProviderMethodNames[0] ?? null;
if ($dataProviderMethodName !== null) {
$args = $this->nodeFactory->createArgs([$dataProviderMethodName]);
return new MethodCall($pestTestNode, 'with', $args);
}
return $pestTestNode;
}
private function refactorPhpDocAnnotationToMethodCall(
ClassMethod $classMethod,
FuncCall|MethodCall $pestTestNode,
string $annotationName
): FuncCall|MethodCall {
$groups = $this->phpDocResolver->resolvePhpDocValuesByName($classMethod, $annotationName);
if ($groups !== []) {
$args = $this->nodeFactory->createArgs($groups);
return new MethodCall($pestTestNode, $annotationName, $args);
}
return $pestTestNode;
}
}