forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhpSpecPromisesToPHPUnitAssertRector.php
308 lines (256 loc) · 9.72 KB
/
PhpSpecPromisesToPHPUnitAssertRector.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
declare(strict_types=1);
namespace Rector\PhpSpecToPHPUnit\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Clone_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpSpecToPHPUnit\MatchersManipulator;
use Rector\PhpSpecToPHPUnit\Naming\PhpSpecRenaming;
use Rector\PhpSpecToPHPUnit\NodeFactory\AssertMethodCallFactory;
use Rector\PhpSpecToPHPUnit\NodeFactory\BeConstructedWithAssignFactory;
use Rector\PhpSpecToPHPUnit\NodeFactory\DuringMethodCallFactory;
use Rector\PhpSpecToPHPUnit\Rector\AbstractPhpSpecToPHPUnitRector;
/**
* @see \Rector\Tests\PhpSpecToPHPUnit\Rector\Variable\PhpSpecToPHPUnitRector\PhpSpecToPHPUnitRectorTest
*/
final class PhpSpecPromisesToPHPUnitAssertRector extends AbstractPhpSpecToPHPUnitRector
{
/**
* @see https://github.com/phpspec/phpspec/blob/master/src/PhpSpec/Wrapper/Subject.php
* ↓
* @see https://phpunit.readthedocs.io/en/8.0/assertions.html
* @var array<string, string[]>
*/
private const NEW_METHOD_TO_OLD_METHODS = [
'assertInstanceOf' => ['shouldBeAnInstanceOf', 'shouldHaveType', 'shouldReturnAnInstanceOf'],
'assertSame' => ['shouldBe', 'shouldReturn'],
'assertNotSame' => ['shouldNotBe', 'shouldNotReturn'],
'assertCount' => ['shouldHaveCount'],
'assertEquals' => ['shouldBeEqualTo', 'shouldEqual'],
'assertNotEquals' => ['shouldNotBeEqualTo'],
'assertContains' => ['shouldContain'],
'assertNotContains' => ['shouldNotContain'],
// types
'assertIsIterable' => ['shouldBeArray'],
'assertIsNotIterable' => ['shouldNotBeArray'],
'assertIsString' => ['shouldBeString'],
'assertIsNotString' => ['shouldNotBeString'],
'assertIsBool' => ['shouldBeBool', 'shouldBeBoolean'],
'assertIsNotBool' => ['shouldNotBeBool', 'shouldNotBeBoolean'],
'assertIsCallable' => ['shouldBeCallable'],
'assertIsNotCallable' => ['shouldNotBeCallable'],
'assertIsFloat' => ['shouldBeDouble', 'shouldBeFloat'],
'assertIsNotFloat' => ['shouldNotBeDouble', 'shouldNotBeFloat'],
'assertIsInt' => ['shouldBeInt', 'shouldBeInteger'],
'assertIsNotInt' => ['shouldNotBeInt', 'shouldNotBeInteger'],
'assertIsNull' => ['shouldBeNull'],
'assertIsNotNull' => ['shouldNotBeNull'],
'assertIsNumeric' => ['shouldBeNumeric'],
'assertIsNotNumeric' => ['shouldNotBeNumeric'],
'assertIsObject' => ['shouldBeObject'],
'assertIsNotObject' => ['shouldNotBeObject'],
'assertIsResource' => ['shouldBeResource'],
'assertIsNotResource' => ['shouldNotBeResource'],
'assertIsScalar' => ['shouldBeScalar'],
'assertIsNotScalar' => ['shouldNotBeScalar'],
'assertNan' => ['shouldBeNan'],
'assertFinite' => ['shouldBeFinite', 'shouldNotBeFinite'],
'assertInfinite' => ['shouldBeInfinite', 'shouldNotBeInfinite'],
];
/**
* @var string
*/
private const THIS = 'this';
/**
* @var string
*/
private $testedClass;
/**
* @var bool
*/
private $isPrepared = false;
/**
* @var string[]
*/
private $matchersKeys = [];
/**
* @var PropertyFetch
*/
private $testedObjectPropertyFetch;
/**
* @var PhpSpecRenaming
*/
private $phpSpecRenaming;
/**
* @var MatchersManipulator
*/
private $matchersManipulator;
/**
* @var AssertMethodCallFactory
*/
private $assertMethodCallFactory;
/**
* @var BeConstructedWithAssignFactory
*/
private $beConstructedWithAssignFactory;
/**
* @var DuringMethodCallFactory
*/
private $duringMethodCallFactory;
public function __construct(
MatchersManipulator $matchersManipulator,
PhpSpecRenaming $phpSpecRenaming,
AssertMethodCallFactory $assertMethodCallFactory,
BeConstructedWithAssignFactory $beConstructedWithAssignFactory,
DuringMethodCallFactory $duringMethodCallFactory
) {
$this->phpSpecRenaming = $phpSpecRenaming;
$this->matchersManipulator = $matchersManipulator;
$this->assertMethodCallFactory = $assertMethodCallFactory;
$this->beConstructedWithAssignFactory = $beConstructedWithAssignFactory;
$this->duringMethodCallFactory = $duringMethodCallFactory;
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
$this->isPrepared = false;
$this->matchersKeys = [];
if (! $this->isInPhpSpecBehavior($node)) {
return null;
}
if ($this->isName($node->name, 'getWrappedObject')) {
return $node->var;
}
if ($this->isName($node->name, 'during')) {
return $this->duringMethodCallFactory->create($node, $this->testedObjectPropertyFetch);
}
if ($this->isName($node->name, 'duringInstantiation')) {
return $this->processDuringInstantiation($node);
}
if ($this->isName($node->name, 'getMatchers')) {
return null;
}
$this->prepareMethodCall($node);
if ($this->isName($node->name, 'beConstructed*')) {
return $this->beConstructedWithAssignFactory->create(
$node,
$this->testedClass,
$this->testedObjectPropertyFetch
);
}
$this->processMatchersKeys($node);
foreach (self::NEW_METHOD_TO_OLD_METHODS as $newMethod => $oldMethods) {
if (! $this->isNames($node->name, $oldMethods)) {
continue;
}
return $this->assertMethodCallFactory->createAssertMethod(
$newMethod,
$node->var,
$node->args[0]->value ?? null,
$this->testedObjectPropertyFetch
);
}
if ($this->shouldSkip($node)) {
return null;
}
if ($this->isName($node->name, 'clone')) {
return new Clone_($this->testedObjectPropertyFetch);
}
$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}
/** @var Class_ $classLike */
$classLike = $node->getAttribute(AttributeKey::CLASS_NODE);
$classMethod = $classLike->getMethod($methodName);
// it's a method call, skip
if ($classMethod !== null) {
return null;
}
$node->var = $this->testedObjectPropertyFetch;
return $node;
}
private function processDuringInstantiation(MethodCall $methodCall): MethodCall
{
/** @var MethodCall $parentMethodCall */
$parentMethodCall = $methodCall->var;
$parentMethodCall->name = new Identifier('expectException');
return $parentMethodCall;
}
private function prepareMethodCall(MethodCall $methodCall): void
{
if ($this->isPrepared) {
return;
}
/** @var Class_ $classLike */
$classLike = $methodCall->getAttribute(AttributeKey::CLASS_NODE);
$this->matchersKeys = $this->matchersManipulator->resolveMatcherNamesFromClass($classLike);
$this->testedClass = $this->phpSpecRenaming->resolveTestedClass($methodCall);
$this->testedObjectPropertyFetch = $this->createTestedObjectPropertyFetch($classLike);
$this->isPrepared = true;
}
/**
* @see https://johannespichler.com/writing-custom-phpspec-matchers/
*/
private function processMatchersKeys(MethodCall $methodCall): void
{
foreach ($this->matchersKeys as $matcherKey) {
if (! $this->isName($methodCall->name, 'should' . ucfirst($matcherKey))) {
continue;
}
if (! $methodCall->var instanceof MethodCall) {
continue;
}
// 1. assign callable to variable
$thisGetMatchers = $this->nodeFactory->createMethodCall(self::THIS, 'getMatchers');
$arrayDimFetch = new ArrayDimFetch($thisGetMatchers, new String_($matcherKey));
$matcherCallableVariable = new Variable('matcherCallable');
$assign = new Assign($matcherCallableVariable, $arrayDimFetch);
// 2. call it on result
$funcCall = new FuncCall($matcherCallableVariable);
$funcCall->args = $methodCall->args;
$methodCall->name = $methodCall->var->name;
$methodCall->var = $this->testedObjectPropertyFetch;
$methodCall->args = [];
$funcCall->args[] = new Arg($methodCall);
$this->addNodesAfterNode([$assign, $funcCall], $methodCall);
$this->removeNode($methodCall);
return;
}
}
private function shouldSkip(MethodCall $methodCall): bool
{
if (! $methodCall->var instanceof Variable) {
return true;
}
if (! $this->nodeNameResolver->isName($methodCall->var, self::THIS)) {
return true;
}
// skip "createMock" method
return $this->isName($methodCall->name, 'createMock');
}
private function createTestedObjectPropertyFetch(Class_ $class): PropertyFetch
{
$propertyName = $this->phpSpecRenaming->resolveObjectPropertyName($class);
return new PropertyFetch(new Variable(self::THIS), $propertyName);
}
}