forked from paratestphp/paratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.php
138 lines (121 loc) · 4 KB
/
Runner.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
<?php namespace ParaTest\Runners\PHPUnit;
use ParaTest\Logging\LogInterpreter,
ParaTest\Logging\JUnit\Writer;
class Runner
{
const PHPUNIT_FATAL_ERROR = 255;
protected $pending = array();
protected $running = array();
protected $options;
protected $interpreter;
protected $printer;
protected $exitcode = -1;
protected $tokens = array();
public function __construct($opts = array())
{
$this->options = new Options($opts);
$this->interpreter = new LogInterpreter();
$this->printer = new ResultPrinter($this->interpreter);
$this->initTokens();
}
public function run()
{
$this->verifyConfiguration();
$this->load();
$this->printer->start($this->options);
while(count($this->running) || count($this->pending)) {
foreach($this->running as $key => $test)
if(!$this->testIsStillRunning($test)) {
unset($this->running[$key]);
$this->releaseToken($key);
}
$this->fillRunQueue();
}
$this->complete();
}
public function getExitCode()
{
return $this->exitcode;
}
private function verifyConfiguration()
{
if (isset($this->options->filtered['configuration']) && !file_exists($this->options->filtered['configuration']->getPath())) {
$this->printer->println(sprintf('Could not read "%s".', $this->options->filtered['configuration']));
exit(1);
}
}
private function complete()
{
$this->printer->printResults();
$this->interpreter->rewind();
$this->log();
$readers = $this->interpreter->getReaders();
foreach($readers as $reader)
$reader->removeLog();
}
private function load()
{
$loader = new SuiteLoader($this->options);
$loader->load($this->options->path);
$executables = ($this->options->functional) ? $loader->getTestMethods() : $loader->getSuites();
$this->pending = array_merge($this->pending, $executables);
foreach($this->pending as $pending)
$this->printer->addTest($pending);
}
private function log()
{
if(!isset($this->options->filtered['log-junit'])) return;
$output = $this->options->filtered['log-junit'];
$writer = new Writer($this->interpreter, $this->options->path);
$writer->write($output);
}
private function fillRunQueue()
{
$opts = $this->options;
while(sizeof($this->pending) && sizeof($this->running) < $opts->processes) {
$token = $this->getNextAvailableToken();
if ($token !== false) {
$this->acquireToken($token);
$this->running[$token] = array_shift($this->pending)->run($opts->phpunit, $opts->filtered, array('TEST_TOKEN' => $token));
}
}
}
private function testIsStillRunning($test)
{
if(!$test->isDoneRunning()) return true;
$this->setExitCode($test);
$test->stop();
if (static::PHPUNIT_FATAL_ERROR === $test->getExitCode())
throw new \Exception($test->getStderr());
$this->printer->printFeedback($test);
return false;
}
private function setExitCode(ExecutableTest $test)
{
$exit = $test->getExitCode();
if($exit > $this->exitcode)
$this->exitcode = $exit;
}
protected function initTokens()
{
$this->tokens = array();
for ($i=0; $i< $this->options->processes; $i++) {
$this->tokens[$i] = true;
}
}
protected function getNextAvailableToken()
{
for ($i=0; $i< count($this->tokens); $i++) {
if ($this->tokens[$i]) return $i;
}
return false;
}
protected function releaseToken($tokenIdentifier)
{
$this->tokens[$tokenIdentifier] = true;
}
protected function acquireToken($tokenIdentifier)
{
$this->tokens[$tokenIdentifier] = false;
}
}