1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ParaTest\Runners\PHPUnit; |
6
|
|
|
|
7
|
|
|
class WrapperRunner extends BaseRunner |
8
|
|
|
{ |
9
|
|
|
const PHPUNIT_FAILURES = 1; |
10
|
|
|
const PHPUNIT_ERRORS = 2; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $streams; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Worker[] |
19
|
|
|
*/ |
20
|
|
|
protected $workers; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $modified; |
26
|
|
|
|
27
|
|
|
public function run() |
28
|
|
|
{ |
29
|
|
|
parent::run(); |
30
|
|
|
|
31
|
|
|
$this->startWorkers(); |
32
|
|
|
$this->assignAllPendingTests(); |
33
|
|
|
$this->sendStopMessages(); |
34
|
|
|
$this->waitForAllToFinish(); |
35
|
|
|
$this->complete(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function load() |
39
|
|
|
{ |
40
|
|
|
if ($this->options->functional) { |
|
|
|
|
41
|
|
|
throw new \RuntimeException('The `functional` option is not supported yet in the WrapperRunner. Only full classes can be run due to the current PHPUnit commands causing classloading issues.'); |
42
|
|
|
} |
43
|
|
|
parent::load(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function startWorkers() |
47
|
|
|
{ |
48
|
|
|
$wrapper = realpath(__DIR__ . '/../../../bin/phpunit-wrapper'); |
49
|
|
|
for ($i = 1; $i <= $this->options->processes; ++$i) { |
|
|
|
|
50
|
|
|
$worker = new Worker(); |
51
|
|
|
if ($this->options->noTestTokens) { |
|
|
|
|
52
|
|
|
$token = null; |
53
|
|
|
$uniqueToken = null; |
54
|
|
|
} else { |
55
|
|
|
$token = $i; |
56
|
|
|
$uniqueToken = uniqid(); |
57
|
|
|
} |
58
|
|
|
$worker->start($wrapper, $token, $uniqueToken); |
59
|
|
|
$this->streams[] = $worker->stdout(); |
60
|
|
|
$this->workers[] = $worker; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function assignAllPendingTests() |
65
|
|
|
{ |
66
|
|
|
$phpunit = $this->options->phpunit; |
|
|
|
|
67
|
|
|
$phpunitOptions = $this->options->filtered; |
|
|
|
|
68
|
|
|
// $phpunitOptions['no-globals-backup'] = null; // removed in phpunit 6.0 |
|
|
|
|
69
|
|
|
while (count($this->pending)) { |
70
|
|
|
$this->waitForStreamsToChange($this->streams); |
71
|
|
|
foreach ($this->progressedWorkers() as $worker) { |
72
|
|
|
if ($worker->isFree()) { |
73
|
|
|
$this->flushWorker($worker); |
74
|
|
|
$pending = array_shift($this->pending); |
75
|
|
|
if ($pending) { |
76
|
|
|
$worker->assign($pending, $phpunit, $phpunitOptions); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function sendStopMessages() |
84
|
|
|
{ |
85
|
|
|
foreach ($this->workers as $worker) { |
86
|
|
|
$worker->stop(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function waitForAllToFinish() |
91
|
|
|
{ |
92
|
|
|
$toStop = $this->workers; |
93
|
|
|
while (count($toStop) > 0) { |
94
|
|
|
$toCheck = $this->streamsOf($toStop); |
95
|
|
|
$new = $this->waitForStreamsToChange($toCheck); |
|
|
|
|
96
|
|
|
foreach ($this->progressedWorkers() as $index => $worker) { |
97
|
|
|
if (!$worker->isRunning()) { |
98
|
|
|
$this->flushWorker($worker); |
99
|
|
|
unset($toStop[$index]); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// put on WorkersPool |
106
|
|
|
private function waitForStreamsToChange(array $modified) |
107
|
|
|
{ |
108
|
|
|
$write = []; |
109
|
|
|
$except = []; |
110
|
|
|
$result = stream_select($modified, $write, $except, 1); |
111
|
|
|
if ($result === false) { |
112
|
|
|
throw new \RuntimeException('stream_select() returned an error while waiting for all workers to finish.'); |
113
|
|
|
} |
114
|
|
|
$this->modified = $modified; |
115
|
|
|
|
116
|
|
|
return $result; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* put on WorkersPool. |
121
|
|
|
* |
122
|
|
|
* @return Worker[] |
123
|
|
|
*/ |
124
|
|
|
private function progressedWorkers(): array |
125
|
|
|
{ |
126
|
|
|
$result = []; |
127
|
|
|
foreach ($this->modified as $modifiedStream) { |
128
|
|
|
$found = null; |
129
|
|
|
foreach ($this->streams as $index => $stream) { |
130
|
|
|
if ($modifiedStream === $stream) { |
131
|
|
|
$found = $index; |
132
|
|
|
break; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
$result[$found] = $this->workers[$found]; |
136
|
|
|
} |
137
|
|
|
$this->modified = []; |
138
|
|
|
|
139
|
|
|
return $result; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the output streams of a subset of workers. |
144
|
|
|
* |
145
|
|
|
* @param array keys are positions in $this->workers |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
private function streamsOf(array $workers): array |
150
|
|
|
{ |
151
|
|
|
$streams = []; |
152
|
|
|
foreach (array_keys($workers) as $index) { |
153
|
|
|
$streams[$index] = $this->streams[$index]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $streams; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private function complete() |
160
|
|
|
{ |
161
|
|
|
$this->setExitCode(); |
162
|
|
|
$this->printer->printResults(); |
163
|
|
|
$this->interpreter->rewind(); |
164
|
|
|
$this->log(); |
165
|
|
|
$this->logCoverage(); |
166
|
|
|
$readers = $this->interpreter->getReaders(); |
167
|
|
|
foreach ($readers as $reader) { |
168
|
|
|
$reader->removeLog(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
private function setExitCode() |
173
|
|
|
{ |
174
|
|
|
if ($this->interpreter->getTotalErrors()) { |
|
|
|
|
175
|
|
|
$this->exitcode = self::PHPUNIT_ERRORS; |
176
|
|
|
} elseif ($this->interpreter->getTotalFailures()) { |
|
|
|
|
177
|
|
|
$this->exitcode = self::PHPUNIT_FAILURES; |
178
|
|
|
} else { |
179
|
|
|
$this->exitcode = 0; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function flushWorker(Worker $worker) |
184
|
|
|
{ |
185
|
|
|
if ($this->hasCoverage()) { |
186
|
|
|
$this->getCoverage()->addCoverageFromFile($worker->getCoverageFileName()); |
187
|
|
|
} |
188
|
|
|
$worker->printFeedback($this->printer); |
189
|
|
|
$worker->reset(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/* |
|
|
|
|
193
|
|
|
private function testIsStillRunning($test) |
194
|
|
|
{ |
195
|
|
|
if(!$test->isDoneRunning()) return true; |
196
|
|
|
$this->setExitCode($test); |
197
|
|
|
$test->stop(); |
198
|
|
|
if (static::PHPUNIT_FATAL_ERROR === $test->getExitCode()) |
199
|
|
|
throw new \Exception($test->getStderr(), $test->getExitCode()); |
200
|
|
|
return false; |
201
|
|
|
} |
202
|
|
|
*/ |
203
|
|
|
} |
204
|
|
|
|
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.