-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathTestWithToDataProviderRector.php
More file actions
233 lines (195 loc) · 6.58 KB
/
TestWithToDataProviderRector.php
File metadata and controls
233 lines (195 loc) · 6.58 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
use Nette\Utils\Json;
use Nette\Utils\Strings;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\NodeManipulator\ClassInsertManipulator;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TestWithToDataProviderRector\TestWithToDataProviderRectorTest
*/
final class TestWithToDataProviderRector extends AbstractRector
{
private bool $hasChanged = false;
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly PhpDocTagRemover $phpDocTagRemover,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly ClassInsertManipulator $classInsertManipulator,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace testWith annotation to data provider.',
[
new CodeSample(
<<<'CODE_SAMPLE'
/**
* @testWith [0, 0, 0]
* @testWith [0, 1, 1]
* @testWith [1, 0, 1]
* @testWith [1, 1, 3]
*/
public function testSum(int $a, int $b, int $expected)
{
$this->assertSame($expected, $a + $b);
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
public function dataProviderSum()
{
return [
[0, 0, 0],
[0, 1, 1],
[1, 0, 1],
[1, 1, 3]
];
}
/**
* @dataProvider dataProviderSum
*/
public function test(int $a, int $b, int $expected)
{
$this->assertSame($expected, $a + $b);
}
CODE_SAMPLE
),
]
);
}
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
$this->hasChanged = false;
foreach ($node->stmts as $classMethod) {
if (! $classMethod instanceof ClassMethod) {
continue;
}
$this->refactorClassMethod($node, $classMethod);
}
if (! $this->hasChanged) {
return null;
}
return $node;
}
private function refactorClassMethod(Class_ $class, ClassMethod $classMethod): void
{
$arrayItemsSingleLine = [];
$arrayMultiLine = null;
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}
$testWithPhpDocTagNodes = [
...$phpDocInfo->getTagsByName('testWith'),
...$phpDocInfo->getTagsByName('testwith'),
];
if ($testWithPhpDocTagNodes === []) {
return;
}
foreach ($testWithPhpDocTagNodes as $testWithPhpDocTagNode) {
if (! $testWithPhpDocTagNode->value instanceof GenericTagValueNode) {
continue;
}
$values = $this->extractTestWithData($testWithPhpDocTagNode->value);
if (count($values) > 1) {
$arrayMultiLine = $this->createArrayItem($values);
}
if (count($values) === 1) {
$arrayItemsSingleLine[] = new ArrayItem($this->createArrayItem($values[0]));
}
//cleanup
if ($this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $testWithPhpDocTagNode)) {
$this->hasChanged = true;
}
}
if (! $this->hasChanged) {
return;
}
$dataProviderName = $this->generateDataProviderName($classMethod);
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@dataProvider', new GenericTagValueNode($dataProviderName)));
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
$returnValue = $arrayMultiLine;
if ($arrayItemsSingleLine !== []) {
$returnValue = new Array_($arrayItemsSingleLine);
}
$providerMethod = new ClassMethod($dataProviderName);
$providerMethod->flags = Modifiers::PUBLIC;
$providerMethod->stmts[] = new Return_($returnValue);
$this->classInsertManipulator->addAsFirstMethod($class, $providerMethod);
}
/**
* @return array<array<int, mixed>>
*/
private function extractTestWithData(GenericTagValueNode $genericTagValueNode): array
{
$testWithItems = explode("\n", trim($genericTagValueNode->value));
$jsonArray = [];
foreach ($testWithItems as $testWithItem) {
$jsonArray[] = Json::decode(trim($testWithItem), forceArrays: true);
}
return $jsonArray;
}
/**
* @param array<int|string, mixed> $data
*/
private function createArrayItem(array $data): Array_
{
$values = [];
foreach ($data as $index => $item) {
$key = null;
if (is_string($index)) {
$key = new String_($index);
}
$values[] = new ArrayItem($this->parseArrayItemValue($item), $key);
}
return new Array_($values);
}
private function parseArrayItemValue(mixed $value): Expr
{
if (is_array($value)) {
return $this->createArrayItem($value);
}
$name = new Name(Json::encode($value));
return new ConstFetch($name);
}
private function generateDataProviderName(ClassMethod $classMethod): string
{
$methodName = Strings::replace($classMethod->name->name, '/^test/', '');
$upperCaseFirstLatter = ucfirst($methodName);
return sprintf('%s%s', 'dataProvider', $upperCaseFirstLatter);
}
}