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
/
PHPUnitTestToPestTestFunctionsRector.php
140 lines (116 loc) · 3.53 KB
/
PHPUnitTestToPestTestFunctionsRector.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
<?php
declare(strict_types=1);
namespace Rector\Pest\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PHPUnit\Framework\TestCase;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Pest\Tests\Rector\Class_\PHPUnitTestToPestTestFunctionsRector\PHPUnitTestToPestTestFunctionsRectorTest
*/
final class PHPUnitTestToPestTestFunctionsRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Convert PHPUnit test to Pest test functions', [
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function test()
{
$result = 100 + 50;
$this->assertSame(150, $result);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
test('test', function () {
$result = 100 + 50;
expect($result)->toBe(150);
});
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
* @return Expression[]|null
*/
public function refactor(Node $node): ?array
{
// only direct classes for now
if (! $node->extends instanceof Name) {
return null;
}
if (! $this->isName($node->extends, TestCase::class)) {
return null;
}
$testFuncCalls = [];
foreach ($node->getMethods() as $classMethod) {
if (! $this->testsNodeAnalyzer->isTestClassMethod($classMethod)) {
continue;
}
$testNameString = new String_($classMethod->name->toString());
$testClosure = $this->createTestClosure($classMethod);
$testFuncCall = new FuncCall(new Name('test'), [new Arg($testNameString), new Arg($testClosure)]);
$testFuncCalls[] = new Expression($testFuncCall);
}
if ($testFuncCalls === []) {
return null;
}
return $testFuncCalls;
}
/**
* @param Stmt[] $stmts
* @return Stmt[]
*/
private function createTestStmts(array $stmts): array
{
// replace assertSame
$this->traverseNodesWithCallable($stmts, function (Node $node) {
if (! $node instanceof MethodCall) {
return null;
}
if (! $this->isName($node->name, 'assertSame')) {
return null;
}
$funcCall = new FuncCall(new Name('expect'), [$node->args[1]]);
$toBeMethodCall = new MethodCall($funcCall, new Identifier('toBe'), [$node->args[0]]);
return $toBeMethodCall;
});
return $stmts;
}
private function createTestClosure(ClassMethod $classMethod): Closure
{
return new Closure([
'stmts' => $this->createTestStmts((array) $classMethod->stmts),
]);
}
}