1
|
|
|
<?php |
2
|
|
|
namespace ParaTest\Runners\PHPUnit; |
3
|
|
|
|
4
|
|
|
use Habitat\Habitat; |
5
|
|
|
|
6
|
|
|
class Runner extends BaseRunner |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* A collection of available tokens based on the number |
10
|
|
|
* of processes specified in $options |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $tokens = array(); |
15
|
|
|
|
16
|
|
|
|
17
|
8 |
|
public function __construct($opts = array()) |
18
|
|
|
{ |
19
|
8 |
|
parent::__construct($opts); |
20
|
8 |
|
$this->initTokens(); |
21
|
8 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The money maker. Runs all ExecutableTest objects in separate processes. |
25
|
|
|
*/ |
26
|
2 |
|
public function run() |
27
|
|
|
{ |
28
|
2 |
|
parent::run(); |
29
|
|
|
|
30
|
2 |
|
while (count($this->running) || count($this->pending)) { |
31
|
|
|
/** @var ExecutableTest $test */ |
32
|
2 |
|
foreach ($this->running as $key => $test) { |
33
|
2 |
|
if (!$this->testIsStillRunning($test)) { |
34
|
2 |
|
if ($test->getExitCode() === 0) { |
35
|
|
|
$this->succeeded[] = $test; |
36
|
|
|
} |
37
|
2 |
|
unset($this->running[$key]); |
38
|
2 |
|
$this->releaseToken($key); |
39
|
2 |
|
} |
40
|
2 |
|
} |
41
|
2 |
|
$this->fillRunQueue(); |
42
|
2 |
|
usleep(10000); |
43
|
2 |
|
} |
44
|
2 |
|
$this->complete(); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Finalizes the run process. This method |
49
|
|
|
* prints all results, rewinds the log interpreter, |
50
|
|
|
* logs any results to JUnit, and cleans up temporary |
51
|
|
|
* files |
52
|
|
|
*/ |
53
|
2 |
|
public function complete() |
54
|
|
|
{ |
55
|
2 |
|
$this->printer->printResults(); |
56
|
2 |
|
$this->interpreter->rewind(); |
57
|
2 |
|
$this->log(); |
58
|
2 |
|
$this->logCoverage(); |
59
|
2 |
|
$readers = $this->interpreter->getReaders(); |
60
|
2 |
|
foreach ($readers as $reader) { |
61
|
2 |
|
$reader->removeLog(); |
62
|
2 |
|
} |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* This method removes ExecutableTest objects from the pending collection |
67
|
|
|
* and adds them to the running collection. It is also in charge of recycling and |
68
|
|
|
* acquiring available test tokens for use |
69
|
|
|
*/ |
70
|
2 |
|
private function fillRunQueue() |
71
|
|
|
{ |
72
|
2 |
|
$opts = $this->options; |
73
|
2 |
|
while (sizeof($this->pending) && sizeof($this->running) < $opts->processes) { |
|
|
|
|
74
|
2 |
|
$tokenData = $this->getNextAvailableToken(); |
75
|
2 |
|
if ($tokenData !== false) { |
76
|
2 |
|
$this->acquireToken($tokenData['token']); |
77
|
2 |
|
$env = array('TEST_TOKEN' => $tokenData['token'], 'UNIQUE_TEST_TOKEN' => $tokenData['unique']) + Habitat::getAll(); |
78
|
2 |
|
$this->running[$tokenData['token']] = array_shift($this->pending)->run($opts->phpunit, $opts->filtered, $env); |
|
|
|
|
79
|
2 |
|
} |
80
|
2 |
|
} |
81
|
2 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns whether or not a test has finished being |
85
|
|
|
* executed. If it has, this method also halts a test process - optionally |
86
|
|
|
* throwing an exception if a fatal error has occurred - |
87
|
|
|
* prints feedback, and updates the overall exit code |
88
|
|
|
* |
89
|
|
|
* @param ExecutableTest $test |
90
|
|
|
* @return bool |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
2 |
|
private function testIsStillRunning($test) |
94
|
|
|
{ |
95
|
2 |
|
if (!$test->isDoneRunning()) { |
96
|
2 |
|
return true; |
97
|
|
|
} |
98
|
2 |
|
$this->setExitCode($test); |
99
|
2 |
|
$test->stop(); |
100
|
2 |
|
if ($this->options->stopOnFailure && $test->getExitCode() > 0) { |
|
|
|
|
101
|
|
|
$this->pending = array(); |
102
|
|
|
} |
103
|
2 |
|
if (static::PHPUNIT_FATAL_ERROR === $test->getExitCode()) { |
104
|
|
|
$errorOutput = $test->getStderr(); |
105
|
|
|
if (!$errorOutput) { |
106
|
|
|
$errorOutput = $test->getStdout(); |
107
|
|
|
} |
108
|
|
|
throw new \Exception($errorOutput); |
109
|
|
|
} |
110
|
2 |
|
$this->printer->printFeedback($test); |
111
|
2 |
|
if ($this->hasCoverage()) { |
112
|
2 |
|
$this->addCoverage($test); |
113
|
2 |
|
} |
114
|
|
|
|
115
|
2 |
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* If the provided test object has an exit code |
120
|
|
|
* higher than the currently set exit code, that exit |
121
|
|
|
* code will be set as the overall exit code |
122
|
|
|
* |
123
|
|
|
* @param ExecutableTest $test |
124
|
|
|
*/ |
125
|
2 |
|
private function setExitCode(ExecutableTest $test) |
126
|
|
|
{ |
127
|
2 |
|
$exit = $test->getExitCode(); |
128
|
2 |
|
if ($exit > $this->exitcode) { |
129
|
2 |
|
$this->exitcode = $exit; |
130
|
2 |
|
} |
131
|
2 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Initialize the available test tokens based |
135
|
|
|
* on how many processes ParaTest will be run in |
136
|
|
|
*/ |
137
|
8 |
|
protected function initTokens() |
138
|
|
|
{ |
139
|
8 |
|
$this->tokens = array(); |
140
|
8 |
|
for ($i = 0; $i < $this->options->processes; $i++) { |
|
|
|
|
141
|
8 |
|
$this->tokens[$i] = array('token' => $i, 'unique' => uniqid($i), 'available' => true); |
142
|
8 |
|
} |
143
|
8 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets the next token that is available to be acquired |
147
|
|
|
* from a finished process |
148
|
|
|
* |
149
|
|
|
* @return bool|array |
150
|
|
|
*/ |
151
|
4 |
|
protected function getNextAvailableToken() |
152
|
|
|
{ |
153
|
4 |
|
foreach ($this->tokens as $data) { |
154
|
4 |
|
if ($data['available']) { |
155
|
3 |
|
return $data; |
156
|
|
|
} |
157
|
4 |
|
} |
158
|
1 |
|
return false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Flag a token as available for use |
163
|
|
|
* |
164
|
|
|
* @param string $tokenIdentifier |
165
|
|
|
*/ |
166
|
3 |
|
protected function releaseToken($tokenIdentifier) |
167
|
|
|
{ |
168
|
|
|
$filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) { |
169
|
3 |
|
return ($val['token'] === $tokenIdentifier); |
170
|
3 |
|
}); |
171
|
3 |
|
$keys = array_keys($filtered); |
172
|
3 |
|
$this->tokens[$keys[0]]['available'] = true; |
173
|
3 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Flag a token as acquired and not available for use |
177
|
|
|
* |
178
|
|
|
* @param string $tokenIdentifier |
179
|
|
|
*/ |
180
|
2 |
|
protected function acquireToken($tokenIdentifier) |
181
|
|
|
{ |
182
|
|
|
$filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) { |
183
|
2 |
|
return ($val['token'] === $tokenIdentifier); |
184
|
2 |
|
}); |
185
|
2 |
|
$keys = array_keys($filtered); |
186
|
2 |
|
$this->tokens[$keys[0]]['available'] = false; |
187
|
2 |
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param ExecutableTest $test |
191
|
|
|
*/ |
192
|
2 |
|
private function addCoverage(ExecutableTest $test) |
193
|
|
|
{ |
194
|
2 |
|
$coverageFile = $test->getCoverageFileName(); |
195
|
2 |
|
$this->getCoverage()->addCoverageFromFile($coverageFile); |
196
|
2 |
|
} |
197
|
|
|
} |
198
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.