-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
TestListenerToHooksRector.php
174 lines (148 loc) · 4.92 KB
/
TestListenerToHooksRector.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
<?php declare(strict_types=1);
namespace Rector\PHPUnit\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://github.com/sebastianbergmann/phpunit/issues/3388
* @see https://github.com/sebastianbergmann/phpunit/commit/34a0abd8b56a4a9de83c9e56384f462541a0f939
*
* @see https://github.com/sebastianbergmann/phpunit/tree/master/src/Runner/Hook
*/
final class TestListenerToHooksRector extends AbstractRector
{
/**
* @var string[][]
*/
private $listenerMethodToHookInterfaces = [
'addIncompleteTest' => ['PHPUnit\Runner\AfterIncompleteTestHook', 'executeAfterIncompleteTest'],
'addRiskyTest' => ['PHPUnit\Runner\AfterRiskyTestHook', 'executeAfterRiskyTest'],
'addSkippedTest' => ['PHPUnit\Runner\AfterSkippedTestHook', 'executeAfterSkippedTest'],
'addError' => ['PHPUnit\Runner\AfterTestErrorHook', 'executeAfterTestError'],
'addFailure' => ['PHPUnit\Runner\AfterTestFailureHook', 'executeAfterTestFailure'],
'addWarning' => ['PHPUnit\Runner\AfterTestWarningHook', 'executeAfterTestWarning'],
# test
'startTest' => ['PHPUnit\Runner\BeforeTestHook', 'executeBeforeTest'],
'endTest' => ['PHPUnit\Runner\AfterTestHook', 'executeAfterTest'],
# suite
'startTestSuite' => ['PHPUnit\Runner\BeforeFirstTestHook', 'executeBeforeFirstTest'],
'endTestSuite' => ['PHPUnit\Runner\AfterLastTestHook', 'executeAfterLastTest'],
];
/**
* @var string
*/
private $testListenerClass;
public function __construct(string $testListenerClass = 'PHPUnit\Framework\TestListener')
{
$this->testListenerClass = $testListenerClass;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Refactor "*TestListener.php" to particular "*Hook.php" files', [
new CodeSample(
<<<'CODE_SAMPLE'
namespace App\Tests;
use PHPUnit\Framework\TestListener;
final class BeforeListHook implements TestListener
{
public function addError(Test $test, \Throwable $t, float $time): void
{
}
public function addWarning(Test $test, Warning $e, float $time): void
{
}
public function addFailure(Test $test, AssertionFailedError $e, float $time): void
{
}
public function addIncompleteTest(Test $test, \Throwable $t, float $time): void
{
}
public function addRiskyTest(Test $test, \Throwable $t, float $time): void
{
}
public function addSkippedTest(Test $test, \Throwable $t, float $time): void
{
}
public function startTestSuite(TestSuite $suite): void
{
}
public function endTestSuite(TestSuite $suite): void
{
}
public function startTest(Test $test): void
{
echo 'start test!';
}
public function endTest(Test $test, float $time): void
{
echo $time;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
namespace App\Tests;
final class BeforeListHook implements \PHPUnit\Runner\BeforeTestHook, \PHPUnit\Runner\AfterTestHook
{
public function executeBeforeTest(Test $test): void
{
echo 'start test!';
}
public function executeAfterTest(Test $test, float $time): void
{
echo $time;
}
}
CODE_SAMPLE
),
]);
}
/**
* List of nodes this class checks, classes that implement @see \PhpParser\Node
* @return string[]
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* Process Node of matched type
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isType($node, $this->testListenerClass)) {
return null;
}
foreach ($node->implements as $implement) {
if ($this->isName($implement, $this->testListenerClass)) {
$this->removeNode($implement);
}
}
foreach ($node->getMethods() as $classMethod) {
$this->processClassMethod($node, $classMethod);
}
return $node;
}
private function processClassMethod(Class_ $class, ClassMethod $classMethod): void
{
foreach ($this->listenerMethodToHookInterfaces as $methodName => $hookClassAndMethod) {
/** @var string $methodName */
if (! $this->isName($classMethod, $methodName)) {
continue;
}
// remove empty methods
if (empty($classMethod->stmts)) {
$this->removeNode($classMethod);
} else {
$class->implements[] = new FullyQualified($hookClassAndMethod[0]);
$classMethod->name = new Identifier($hookClassAndMethod[1]);
}
}
}
}