/
TestRunner.php
344 lines (282 loc) · 8.64 KB
/
TestRunner.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
//
// +------------------------------------------------------------------------+
// | PEAR :: PHPUnit2 |
// +------------------------------------------------------------------------+
// | Copyright (c) 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License, |
// | that is available at http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +------------------------------------------------------------------------+
//
// $Id: TestRunner.php 539 2006-02-13 16:08:42Z sb $
//
if (!defined('PHPUnit2_MAIN_METHOD')) {
define('PHPUnit2_MAIN_METHOD', 'PHPUnit2_TextUI_TestRunner::main');
}
require_once 'Console/Getopt.php';
require_once 'PHPUnit2/Framework/AssertionFailedError.php';
require_once 'PHPUnit2/Framework/Test.php';
require_once 'PHPUnit2/Framework/TestResult.php';
require_once 'PHPUnit2/Runner/BaseTestRunner.php';
require_once 'PHPUnit2/Runner/Version.php';
require_once 'PHPUnit2/TextUI/ResultPrinter.php';
@include_once 'Benchmark/Timer.php';
/**
* A TestRunner for the Command Line Interface (CLI)
* PHP SAPI Module.
*
* @author Sebastian Bergmann <sb@sebastian-bergmann.de>
* @copyright Copyright © 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
* @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
* @category PHP
* @package PHPUnit2
* @subpackage TextUI
*/
class PHPUnit2_TextUI_TestRunner extends PHPUnit2_Runner_BaseTestRunner {
// {{{ Constants
const SUCCESS_EXIT = 0;
const FAILURE_EXIT = 1;
const EXCEPTION_EXIT = 2;
// }}}
// {{{ Members
/**
* @var PHPUnit2_TextUI_ResultPrinter
* @access private
*/
private $printer = null;
// }}}
// {{{ public static function main()
/**
* @access public
* @static
*/
public static function main() {
$aTestRunner = new PHPUnit2_TextUI_TestRunner;
try {
$result = $aTestRunner->start($_SERVER['argv']);
if (!$result->wasSuccessful()) {
exit(self::FAILURE_EXIT);
}
exit(self::SUCCESS_EXIT);
}
catch (Exception $e) {
print $e->getMessage();
exit(self::EXCEPTION_EXIT);
}
}
// }}}
// {{{ protected function start($arguments)
/**
* @param array $arguments
* @access protected
*/
protected function start($arguments) {
$wait = false;
$possibleOptions = array(
'help',
'version',
'wait'
);
$options = Console_Getopt::getopt(
$arguments,
'',
$possibleOptions
);
foreach ($options[0] as $option) {
switch ($option[0]) {
case '--help': {
$this->showHelp();
exit(self::SUCCESS_EXIT);
}
break;
case '--version': {
print PHPUnit2_Runner_Version::getVersionString() . "\n";
exit(self::SUCCESS_EXIT);
}
break;
case '--wait': {
$wait = true;
}
break;
}
}
$test = isset($options[1][0]) ? $options[1][0] : false;
if ($test === false) {
$this->showHelp();
exit(self::SUCCESS_EXIT);
}
try {
return $this->doRun(
$this->getTest($test),
$wait
);
}
catch (Exception $e) {
throw new Exception(
'Could not create and run test suite: ' . $e->getMessage()
);
}
}
// }}}
// {{{ public static function run($test)
/**
* @param mixed $test
* @access public
* @static
*/
public static function run($test) {
if ($test instanceof ReflectionClass) {
self::run(new PHPUnit2_Framework_TestSuite($testClass));
}
else if ($test instanceof PHPUnit2_Framework_Test) {
$aTestRunner = new PHPUnit2_TextUI_TestRunner;
return $aTestRunner->doRun($test);
}
}
// }}}
// {{{ public static function runAndWait(PHPUnit2_Framework_Test $suite)
/**
* @param PHPUnit2_Framework_Test $suite
* @access public
* @static
*/
public static function runAndWait(PHPUnit2_Framework_Test $suite) {
$aTestRunner = new PHPUnit2_TextUI_TestRunner;
$aTestRunner->doRun($suite, true);
}
// }}}
// {{{ protected TestResult createTestResult()
/**
* @return PHPUnit2_Framework_TestResult
* @access protected
*/
protected function createTestResult() {
return new PHPUnit2_Framework_TestResult;
}
// }}}
// {{{ public function doRun(PHPUnit2_Framework_Test $suite, $wait = false)
/**
* @param PHPUnit2_Framework_Test $suite
* @param boolean $wait
* @return PHPUnit2_Framework_TestResult
* @access public
*/
public function doRun(PHPUnit2_Framework_Test $suite, $wait = false) {
$result = $this->createTestResult();
if ($this->printer === null) {
$this->printer = new PHPUnit2_TextUI_ResultPrinter;
}
$this->printer->write(
PHPUnit2_Runner_Version::getVersionString() . "\n\n"
);
$result->addListener($this->printer);
if (class_exists('Benchmark_Timer')) {
$timer = new Benchmark_Timer;
}
if (isset($timer)) {
$timer->start();
}
$suite->run($result);
if (isset($timer)) {
$timer->stop();
$timeElapsed = $timer->timeElapsed();
} else {
$timeElapsed = false;
}
$this->pause($wait);
$this->printer->printResult($result, $timeElapsed);
return $result;
}
// }}}
// {{{ public function showHelp()
/**
* @access public
*/
public function showHelp() {
print PHPUnit2_Runner_Version::getVersionString() . "\n\n" .
"Usage: phpunit [switches] UnitTest\n" .
" --wait Waits for a keystroke after each test.\n" .
" --help Prints this usage information.\n" .
" --version Prints the version and exits.\n";
}
// }}}
// {{{ protected function pause($wait)
/**
* @param boolean $wait
* @access protected
*/
protected function pause($wait) {
if (!$wait) {
return;
}
$this->printer->printWaitPrompt();
fgets(STDIN);
}
// }}}
// {{{ public function testEnded($testName)
/**
* A test ended.
*
* @param string $testName
* @access public
*/
public function testEnded($testName) {
}
// }}}
// {{{ public function testFailed($status, PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e)
/**
* A test failed.
*
* @param integer $status
* @param PHPUnit2_Framework_Test $test
* @param PHPUnit2_Framework_AssertionFailedError $e
* @access public
*/
public function testFailed($status, PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e) {
}
// }}}
// {{{ public function testStarted($testName)
/**
* A test started.
*
* @param string $testName
* @access public
*/
public function testStarted($testName) {
}
// }}}
// {{{ protected function runFailed($message)
/**
* Override to define how to handle a failed loading of
* a test suite.
*
* @param string $message
* @access protected
*/
protected function runFailed($message) {
print $message;
exit(self::FAILURE_EXIT);
}
// }}}
// {{{ public function setPrinter(PHPUnit2_TextUI_ResultPrinter $resultPrinter)
/**
* @param PHPUnit2_TextUI_ResultPrinter $resultPrinter
* @access public
*/
public function setPrinter(PHPUnit2_TextUI_ResultPrinter $resultPrinter) {
$this->printer = $resultPrinter;
}
// }}}
}
if (PHPUnit2_MAIN_METHOD == 'PHPUnit2_TextUI_TestRunner::main') {
PHPUnit2_TextUI_TestRunner::main();
}
/*
* vim600: et sw=2 ts=2 fdm=marker
* vim<600: et sw=2 ts=2
*/
?>