forked from rectorphp/rector-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelegateExceptionArgumentsRector.php
More file actions
151 lines (125 loc) · 4.16 KB
/
Copy pathDelegateExceptionArgumentsRector.php
File metadata and controls
151 lines (125 loc) · 4.16 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
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\PHPUnit60\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFactory\AssertCallFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\PHPUnit60\Rector\MethodCall\DelegateExceptionArgumentsRector\DelegateExceptionArgumentsRectorTest
*/
final class DelegateExceptionArgumentsRector extends AbstractRector
{
/**
* @var array<string, string>
*/
private const OLD_TO_NEW_METHOD = [
'setExpectedException' => 'expectExceptionMessage',
'setExpectedExceptionRegExp' => 'expectExceptionMessageRegExp',
];
public function __construct(
private readonly AssertCallFactory $assertCallFactory,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Takes `setExpectedException()` 2nd and next arguments to own methods in PHPUnit.',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
class SomeTest extends TestCase
{
public function test()
{
$this->setExpectedException(SomeException::class, "Message", "CODE");
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
class SomeTest extends TestCase
{
public function test()
{
$this->setExpectedException(SomeException::class);
$this->expectExceptionMessage('Message');
$this->expectExceptionCode('CODE');
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StmtsAwareInterface::class];
}
/**
* @param StmtsAwareInterface $node
*/
public function refactor(Node $node): ?Node
{
if ($node->stmts === null || $node->stmts === []) {
return null;
}
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
$hasChanged = false;
$oldMethodNames = array_keys(self::OLD_TO_NEW_METHOD);
foreach ($node->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
}
if (! $stmt->expr instanceof StaticCall && ! $stmt->expr instanceof MethodCall) {
continue;
}
$call = $stmt->expr;
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($call, $oldMethodNames)) {
continue;
}
if (isset($call->args[1])) {
$extraCall = $this->createFirstArgExtraMethodCall($call);
$node->stmts[] = new Expression($extraCall);
unset($call->args[1]);
}
// add exception code method call
if (isset($call->args[2])) {
$extraCall = $this->assertCallFactory->createCallWithName($call, 'expectExceptionCode');
$extraCall->args[] = $call->args[2];
$node->stmts[] = new Expression($extraCall);
unset($call->args[2]);
}
$hasChanged = true;
$call->name = new Identifier('expectException');
}
if ($hasChanged) {
return $node;
}
return null;
}
private function createFirstArgExtraMethodCall(StaticCall|MethodCall $call): MethodCall|StaticCall
{
/** @var Identifier $identifierNode */
$identifierNode = $call->name;
$oldMethodName = $identifierNode->name;
$extraCall = $this->assertCallFactory->createCallWithName($call, self::OLD_TO_NEW_METHOD[$oldMethodName]);
$extraCall->args[] = $call->args[1];
return $extraCall;
}
}