1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* lime_harness class. |
5
|
|
|
* |
6
|
|
|
* @extends lime_registration |
7
|
|
|
* @package lime |
8
|
|
|
*/ |
9
|
|
|
class lime_harness extends lime_registration |
10
|
|
|
{ |
11
|
|
|
public $options = array(); |
12
|
|
|
public $php_cli = null; |
13
|
|
|
public $stats = array(); |
14
|
|
|
public $output = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param array $options |
18
|
|
|
* @throws Exception |
19
|
|
|
*/ |
20
|
|
|
public function __construct($options = array()) |
21
|
|
|
{ |
22
|
|
|
// for BC |
23
|
|
|
if (!is_array($options)) { |
24
|
|
|
$options = array('output' => $options); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->options = array_merge(array( |
28
|
|
|
'php_cli' => null, |
29
|
|
|
'force_colors' => false, |
30
|
|
|
'output' => null, |
31
|
|
|
'verbose' => false, |
32
|
|
|
), $options); |
33
|
|
|
|
34
|
|
|
$this->php_cli = $this->find_php_cli($this->options['php_cli']); |
35
|
|
|
$this->output = $this->options['output'] ? $this->options['output'] : new lime_output($this->options['force_colors']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param null $php_cli |
40
|
|
|
* @return null|string |
41
|
|
|
* @throws Exception |
42
|
|
|
*/ |
43
|
|
|
protected function find_php_cli($php_cli = null) |
44
|
|
|
{ |
45
|
|
|
if (is_null($php_cli)) { |
46
|
|
|
if (getenv('PHP_PATH')) { |
47
|
|
|
$php_cli = getenv('PHP_PATH'); |
48
|
|
|
|
49
|
|
|
if (!is_executable($php_cli)) { |
50
|
|
|
throw new Exception('The defined PHP_PATH environment variable is not a valid PHP executable.'); |
51
|
|
|
} |
52
|
|
|
} else { |
53
|
|
|
$php_cli = PHP_BINDIR . DIRECTORY_SEPARATOR . 'php'; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (is_executable($php_cli)) { |
58
|
|
|
return $php_cli; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$path = getenv('PATH') ? getenv('PATH') : getenv('Path'); |
62
|
|
|
$exe_suffixes = DIRECTORY_SEPARATOR == '\\' ? (getenv('PATHEXT') ? explode(PATH_SEPARATOR, |
63
|
|
|
getenv('PATHEXT')) : array('.exe', '.bat', '.cmd', '.com')) : array(''); |
64
|
|
|
foreach (array('php5', 'php') as $php_cli) { |
65
|
|
|
foreach ($exe_suffixes as $suffix) { |
66
|
|
|
foreach (explode(PATH_SEPARATOR, $path) as $dir) { |
67
|
|
|
$file = $dir . DIRECTORY_SEPARATOR . $php_cli . $suffix; |
68
|
|
|
if (is_executable($file)) { |
69
|
|
|
return $file; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
throw new Exception("Unable to find PHP executable."); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function to_array() |
82
|
|
|
{ |
83
|
|
|
$results = array(); |
84
|
|
|
foreach ($this->stats['files'] as $stat) { |
85
|
|
|
$results = array_merge($results, $stat['output']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $results; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function to_xml() |
95
|
|
|
{ |
96
|
|
|
return lime_test::to_xml($this->to_array()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return bool |
101
|
|
|
* @throws Exception |
102
|
|
|
*/ |
103
|
|
|
public function run() |
104
|
|
|
{ |
105
|
|
|
if (!count($this->files)) { |
106
|
|
|
throw new Exception('You must register some test files before running them!'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// sort the files to be able to predict the order |
110
|
|
|
sort($this->files); |
111
|
|
|
|
112
|
|
|
$this->stats = array( |
113
|
|
|
'files' => array(), |
114
|
|
|
'failed_files' => array(), |
115
|
|
|
'failed_tests' => 0, |
116
|
|
|
'total' => 0, |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
foreach ($this->files as $file) { |
120
|
|
|
$this->stats['files'][$file] = array(); |
121
|
|
|
$stats = &$this->stats['files'][$file]; |
122
|
|
|
|
123
|
|
|
$relative_file = $this->get_relative_file($file); |
124
|
|
|
|
125
|
|
|
$test_file = tempnam(sys_get_temp_dir(), 'lime'); |
126
|
|
|
$result_file = tempnam(sys_get_temp_dir(), 'lime'); |
127
|
|
|
file_put_contents($test_file, <<<EOF |
128
|
|
|
<?php |
129
|
|
|
function lime_shutdown() |
130
|
|
|
{ |
131
|
|
|
file_put_contents('$result_file', serialize(lime_test::to_array())); |
132
|
|
|
} |
133
|
|
|
register_shutdown_function('lime_shutdown'); |
134
|
|
|
include('$file'); |
135
|
|
|
EOF |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
ob_start(); |
139
|
|
|
// see http://trac.symfony-project.org/ticket/5437 for the explanation on the weird "cd" thing |
140
|
|
|
passthru(sprintf('cd & %s %s 2>&1', escapeshellarg($this->php_cli), escapeshellarg($test_file)), $return); |
141
|
|
|
ob_end_clean(); |
142
|
|
|
unlink($test_file); |
143
|
|
|
|
144
|
|
|
$output = file_get_contents($result_file); |
145
|
|
|
$stats['output'] = $output ? unserialize($output) : ''; |
146
|
|
|
if (!$stats['output']) { |
147
|
|
|
$stats['output'] = array( |
148
|
|
|
array( |
149
|
|
|
'file' => $file, |
150
|
|
|
'tests' => array(), |
151
|
|
|
'stats' => array( |
152
|
|
|
'plan' => 1, |
153
|
|
|
'total' => 1, |
154
|
|
|
'failed' => array(0), |
155
|
|
|
'passed' => array(), |
156
|
|
|
'skipped' => array(), |
157
|
|
|
'errors' => array() |
158
|
|
|
) |
159
|
|
|
) |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
unlink($result_file); |
163
|
|
|
|
164
|
|
|
$file_stats = &$stats['output'][0]['stats']; |
165
|
|
|
|
166
|
|
|
$delta = 0; |
167
|
|
|
if ($return > 0) { |
168
|
|
|
$stats['status'] = $file_stats['errors'] ? 'errors' : 'dubious'; |
169
|
|
|
$stats['status_code'] = $return; |
170
|
|
|
} else { |
171
|
|
|
$this->stats['total'] += $file_stats['total']; |
172
|
|
|
|
173
|
|
|
if (!$file_stats['plan']) { |
174
|
|
|
$file_stats['plan'] = $file_stats['total']; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$delta = $file_stats['plan'] - $file_stats['total']; |
178
|
|
|
if (0 != $delta) { |
179
|
|
|
$stats['status'] = $file_stats['errors'] ? 'errors' : 'dubious'; |
180
|
|
|
$stats['status_code'] = 255; |
181
|
|
|
} else { |
182
|
|
|
$stats['status'] = $file_stats['failed'] ? 'not ok' : ($file_stats['errors'] ? 'errors' : 'ok'); |
183
|
|
|
$stats['status_code'] = 0; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$this->output->echoln(sprintf('%s%s%s', substr($relative_file, -min(67, strlen($relative_file))), |
188
|
|
|
str_repeat('.', 70 - min(67, strlen($relative_file))), $stats['status'])); |
189
|
|
|
|
190
|
|
|
if ('dubious' == $stats['status']) { |
191
|
|
|
$this->output->echoln(sprintf(' Test returned status %s', $stats['status_code'])); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ('ok' != $stats['status']) { |
195
|
|
|
$this->stats['failed_files'][] = $file; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if ($delta > 0) { |
199
|
|
|
$this->output->echoln(sprintf(' Looks like you planned %d tests but only ran %d.', |
200
|
|
|
$file_stats['plan'], $file_stats['total'])); |
201
|
|
|
|
202
|
|
|
$this->stats['failed_tests'] += $delta; |
203
|
|
|
$this->stats['total'] += $delta; |
204
|
|
|
} else { |
205
|
|
|
if ($delta < 0) { |
206
|
|
|
$this->output->echoln(sprintf(' Looks like you planned %s test but ran %s extra.', |
207
|
|
|
$file_stats['plan'], $file_stats['total'] - $file_stats['plan'])); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (false !== $file_stats && $file_stats['failed']) { |
212
|
|
|
$this->stats['failed_tests'] += count($file_stats['failed']); |
213
|
|
|
|
214
|
|
|
$this->output->echoln(sprintf(" Failed tests: %s", implode(', ', $file_stats['failed']))); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if (false !== $file_stats && $file_stats['errors']) { |
218
|
|
|
$this->output->echoln(' Errors:'); |
219
|
|
|
|
220
|
|
|
$error_count = count($file_stats['errors']); |
221
|
|
|
for ($i = 0; $i < 3 && $i < $error_count; ++$i) { |
222
|
|
|
$this->output->echoln(' - ' . $file_stats['errors'][$i]['message'], null, false); |
223
|
|
|
} |
224
|
|
|
if ($error_count > 3) { |
225
|
|
|
$this->output->echoln(sprintf(' ... and %s more', $error_count - 3)); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
if (count($this->stats['failed_files'])) { |
231
|
|
|
$format = "%-30s %4s %5s %5s %5s %s"; |
232
|
|
|
$this->output->echoln(sprintf($format, 'Failed Test', 'Stat', 'Total', 'Fail', 'Errors', 'List of Failed')); |
233
|
|
|
$this->output->echoln("--------------------------------------------------------------------------"); |
234
|
|
|
foreach ($this->stats['files'] as $file => $stat) { |
|
|
|
|
235
|
|
|
if (!in_array($file, $this->stats['failed_files'])) { |
236
|
|
|
continue; |
237
|
|
|
} |
238
|
|
|
$relative_file = $this->get_relative_file($file); |
239
|
|
|
|
240
|
|
|
if (isset($stat['output'][0])) { |
241
|
|
|
$this->output->echoln(sprintf($format, substr($relative_file, -min(30, strlen($relative_file))), |
242
|
|
|
$stat['status_code'], |
243
|
|
|
count($stat['output'][0]['stats']['failed']) + count($stat['output'][0]['stats']['passed']), |
244
|
|
|
count($stat['output'][0]['stats']['failed']), count($stat['output'][0]['stats']['errors']), |
245
|
|
|
implode(' ', $stat['output'][0]['stats']['failed']))); |
246
|
|
|
} else { |
247
|
|
|
$this->output->echoln(sprintf($format, substr($relative_file, -min(30, strlen($relative_file))), |
248
|
|
|
$stat['status_code'], '', '', '')); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$this->output->red_bar(sprintf('Failed %d/%d test scripts, %.2f%% okay. %d/%d subtests failed, %.2f%% okay.', |
253
|
|
|
$nb_failed_files = count($this->stats['failed_files']), |
254
|
|
|
$nb_files = count($this->files), |
255
|
|
|
($nb_files - $nb_failed_files) * 100 / $nb_files, |
256
|
|
|
$nb_failed_tests = $this->stats['failed_tests'], |
257
|
|
|
$nb_tests = $this->stats['total'], |
258
|
|
|
$nb_tests > 0 ? ($nb_tests - $nb_failed_tests) * 100 / $nb_tests : 0 |
259
|
|
|
)); |
260
|
|
|
|
261
|
|
|
if ($this->options['verbose']) { |
262
|
|
|
foreach ($this->to_array() as $testsuite) { |
263
|
|
|
$first = true; |
264
|
|
|
foreach ($testsuite['stats']['failed'] as $testcase) { |
265
|
|
|
if (!isset($testsuite['tests'][$testcase]['file'])) { |
266
|
|
|
continue; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
if ($first) { |
270
|
|
|
$this->output->echoln(''); |
271
|
|
|
$this->output->error($this->get_relative_file($testsuite['file']) . $this->extension); |
272
|
|
|
$first = false; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$this->output->comment(sprintf(' at %s line %s', |
276
|
|
|
$this->get_relative_file($testsuite['tests'][$testcase]['file']) . $this->extension, |
277
|
|
|
$testsuite['tests'][$testcase]['line'])); |
278
|
|
|
$this->output->info(' ' . $testsuite['tests'][$testcase]['message']); |
279
|
|
|
$this->output->echoln($testsuite['tests'][$testcase]['error'], null, false); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} else { |
284
|
|
|
$this->output->green_bar(' All tests successful.'); |
285
|
|
|
$this->output->green_bar(sprintf(' Files=%d, Tests=%d', count($this->files), $this->stats['total'])); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return $this->stats['failed_files'] ? false : true; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @return array |
293
|
|
|
*/ |
294
|
|
|
public function get_failed_files() |
295
|
|
|
{ |
296
|
|
|
return isset($this->stats['failed_files']) ? $this->stats['failed_files'] : array(); |
297
|
|
|
} |
298
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.