1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Util\Phpunit; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
7
|
|
|
use PHPUnit\Framework\Test; |
8
|
|
|
use PHPUnit\Framework\TestListener; |
9
|
|
|
use PHPUnit\Framework\TestResult; |
10
|
|
|
use PHPUnit\Framework\TestSuite; |
11
|
|
|
use PHPUnit\Framework\Warning; |
12
|
|
|
|
13
|
|
|
class MasterListener implements TestListener, MasterListenerInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected $replay = []; |
17
|
|
|
protected $listeners = []; |
18
|
|
|
protected $result; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param $class |
22
|
|
|
* @return TestListener |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
public function getListener($class) |
26
|
|
|
{ |
27
|
|
|
foreach ($this->listeners as $listener) { |
28
|
|
|
if (get_class($listener) == $class) { |
29
|
|
|
return $listener; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function clear() |
35
|
|
|
{ |
36
|
|
|
$this->replay = []; |
37
|
|
|
$this->listeners = []; |
38
|
|
|
$this->result = null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function addListener($listener) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
// This pretty piece of code is to maintain compatibility between PHPUnit 5 and 6. |
44
|
|
|
if ($listener instanceof TestListener) { |
45
|
|
|
foreach ($this->listeners as $existingListener) { |
46
|
|
|
if (get_class($listener) == get_class($existingListener)) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$this->listeners[] = $listener; |
51
|
|
|
foreach ($this->replay as $replay) { |
52
|
|
|
$this->play($replay['method'], $replay['args'], $listener); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function play($method, $args, TestListener $instance = null) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
if ($instance instanceof TestListener) { |
60
|
|
|
call_user_func_array([$instance, $method], $args); |
61
|
|
|
} else { |
62
|
|
|
foreach ($this->listeners as $listener) { |
63
|
|
|
call_user_func_array([$listener, $method], $args); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function bindToResult(TestResult $result) |
69
|
|
|
{ |
70
|
|
|
if ($this->result !== $result) { |
71
|
|
|
$this->result = $result; |
72
|
|
|
$result->addListener($this); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function addError(Test $test, Exception $e, $time) |
77
|
|
|
{ |
78
|
|
|
$this->replay[] = [ |
79
|
|
|
'method' => __FUNCTION__, |
80
|
|
|
'args' => func_get_args() |
81
|
|
|
]; |
82
|
|
|
$this->play(__FUNCTION__, func_get_args()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function addWarning(Test $test, Warning $e, $time) |
86
|
|
|
{ |
87
|
|
|
$this->replay[] = [ |
88
|
|
|
'method' => __FUNCTION__, |
89
|
|
|
'args' => func_get_args() |
90
|
|
|
]; |
91
|
|
|
$this->play(__FUNCTION__, func_get_args()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function addFailure(Test $test, AssertionFailedError $e, $time) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$this->replay[] = [ |
97
|
|
|
'method' => __FUNCTION__, |
98
|
|
|
'args' => func_get_args() |
99
|
|
|
]; |
100
|
|
|
$this->play(__FUNCTION__, func_get_args()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function addIncompleteTest(Test $test, Exception $e, $time) |
104
|
|
|
{ |
105
|
|
|
$this->replay[] = [ |
106
|
|
|
'method' => __FUNCTION__, |
107
|
|
|
'args' => func_get_args() |
108
|
|
|
]; |
109
|
|
|
$this->play(__FUNCTION__, func_get_args()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function addRiskyTest(Test $test, Exception $e, $time) |
113
|
|
|
{ |
114
|
|
|
$this->replay[] = [ |
115
|
|
|
'method' => __FUNCTION__, |
116
|
|
|
'args' => func_get_args() |
117
|
|
|
]; |
118
|
|
|
$this->play(__FUNCTION__, func_get_args()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function addSkippedTest(Test $test, Exception $e, $time) |
122
|
|
|
{ |
123
|
|
|
$this->replay[] = [ |
124
|
|
|
'method' => __FUNCTION__, |
125
|
|
|
'args' => func_get_args() |
126
|
|
|
]; |
127
|
|
|
$this->play(__FUNCTION__, func_get_args()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
public function startTestSuite(TestSuite $suite) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$this->replay[] = [ |
133
|
|
|
'method' => __FUNCTION__, |
134
|
|
|
'args' => func_get_args() |
135
|
|
|
]; |
136
|
|
|
$this->play(__FUNCTION__, func_get_args()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function endTestSuite(TestSuite $suite) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$this->replay[] = [ |
142
|
|
|
'method' => __FUNCTION__, |
143
|
|
|
'args' => func_get_args() |
144
|
|
|
]; |
145
|
|
|
$this->play(__FUNCTION__, func_get_args()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
View Code Duplication |
public function startTest(Test $test) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$this->replay[] = [ |
151
|
|
|
'method' => __FUNCTION__, |
152
|
|
|
'args' => func_get_args() |
153
|
|
|
]; |
154
|
|
|
$this->play(__FUNCTION__, func_get_args()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
View Code Duplication |
public function endTest(Test $test, $time) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$this->replay[] = [ |
160
|
|
|
'method' => __FUNCTION__, |
161
|
|
|
'args' => func_get_args() |
162
|
|
|
]; |
163
|
|
|
$this->play(__FUNCTION__, func_get_args()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.