3 * PEAR_Command_Test (run-tests)
9 * @author Stig Bakken <ssb@php.net>
10 * @author Martin Jansen <mj@php.net>
11 * @author Greg Beaver <cellog@php.net>
12 * @copyright 1997-2009 The Authors
13 * @license http://opensource.org/licenses/bsd-license.php New BSD License
14 * @version CVS: $Id: Test.php 313023 2011-07-06 19:17:11Z dufuz $
15 * @link http://pear.php.net/package/PEAR
16 * @since File available since Release 0.1
22 require_once 'PEAR/Command/Common.php';
25 * PEAR commands for login/logout
29 * @author Stig Bakken <ssb@php.net>
30 * @author Martin Jansen <mj@php.net>
31 * @author Greg Beaver <cellog@php.net>
32 * @copyright 1997-2009 The Authors
33 * @license http://opensource.org/licenses/bsd-license.php New BSD License
34 * @version Release: 1.9.4
35 * @link http://pear.php.net/package/PEAR
36 * @since Class available since Release 0.1
39 class PEAR_Command_Test extends PEAR_Command_Common
41 var $commands = array(
43 'summary' => 'Run Regression Tests',
44 'function' => 'doRunTests',
49 'doc' => 'Run tests in child directories, recursively. 4 dirs deep maximum',
53 'doc' => 'actual string of settings to pass to php in format " -d setting=blah"',
56 'realtimelog' => array(
58 'doc' => 'Log test runs/results as they are run',
62 'doc' => 'Only display detail for failed tests',
66 'doc' => 'Display simple output for all tests',
70 'doc' => 'Treat parameters as installed packages from which to run tests',
74 'doc' => 'Search parameters for AllTests.php, and use that to run phpunit-based tests
75 If none is found, all .phpt tests will be tried instead.',
79 'doc' => 'Output run-tests.log in TAP-compliant format',
83 'doc' => 'CGI php executable (needed for tests with POST/GET section)',
88 'doc' => 'Generate a code coverage report (requires Xdebug 2.0.0+)',
91 'doc' => '[testfile|dir ...]
92 Run regression tests with PHP\'s regression testing script (run-tests.php).',
99 * PEAR_Command_Test constructor.
103 function PEAR_Command_Test(&$ui, &$config)
105 parent::PEAR_Command_Common($ui, $config);
108 function doRunTests($command, $options, $params)
110 if (isset($options['phpunit']) && isset($options['tapoutput'])) {
111 return $this->raiseError('ERROR: cannot use both --phpunit and --tapoutput at the same time');
114 require_once 'PEAR/Common.php';
115 require_once 'System.php';
116 $log = new PEAR_Common;
117 $log->ui = &$this->ui; // slightly hacky, but it will work
119 $depth = isset($options['recur']) ? 14 : 1;
121 if (!count($params)) {
125 if (isset($options['package'])) {
126 $oldparams = $params;
128 $reg = &$this->config->getRegistry();
129 foreach ($oldparams as $param) {
130 $pname = $reg->parsePackageName($param, $this->config->get('default_channel'));
131 if (PEAR::isError($pname)) {
132 return $this->raiseError($pname);
135 $package = &$reg->getPackage($pname['package'], $pname['channel']);
137 return PEAR::raiseError('Unknown package "' .
138 $reg->parsedPackageNameToString($pname) . '"');
141 $filelist = $package->getFilelist();
142 foreach ($filelist as $name => $atts) {
143 if (isset($atts['role']) && $atts['role'] != 'test') {
147 if (isset($options['phpunit']) && preg_match('/AllTests\.php\\z/i', $name)) {
148 $params[] = $atts['installed_as'];
150 } elseif (!preg_match('/\.phpt\\z/', $name)) {
153 $params[] = $atts['installed_as'];
158 foreach ($params as $p) {
160 if (isset($options['phpunit'])) {
161 $dir = System::find(array($p, '-type', 'f',
163 '-name', 'AllTests.php'));
165 foreach ($dir as $p) {
167 if (!count($tests) ||
168 (count($tests) && strlen($p) < strlen($tests[0]))) {
169 // this is in a higher-level directory, use this one instead.
177 $args = array($p, '-type', 'f', '-name', '*.phpt');
179 if (isset($options['phpunit'])) {
180 if (preg_match('/AllTests\.php\\z/i', $p)) {
182 if (!count($tests) ||
183 (count($tests) && strlen($p) < strlen($tests[0]))) {
184 // this is in a higher-level directory, use this one instead.
191 if (file_exists($p) && preg_match('/\.phpt$/', $p)) {
196 if (!preg_match('/\.phpt\\z/', $p)) {
200 $args = array(dirname($p), '-type', 'f', '-name', $p);
203 if (!isset($options['recur'])) {
204 $args[] = '-maxdepth';
208 $dir = System::find($args);
209 $tests = array_merge($tests, $dir);
213 if (isset($options['ini'])) {
214 $ini_settings .= $options['ini'];
217 if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) {
218 $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}";
222 $this->ui->outputData('Using INI settings: "' . $ini_settings . '"');
225 $skipped = $passed = $failed = array();
226 $tests_count = count($tests);
227 $this->ui->outputData('Running ' . $tests_count . ' tests', $command);
229 if (isset($options['realtimelog']) && file_exists('run-tests.log')) {
230 unlink('run-tests.log');
233 if (isset($options['tapoutput'])) {
234 $tap = '1..' . $tests_count . "\n";
237 require_once 'PEAR/RunTest.php';
238 $run = new PEAR_RunTest($log, $options);
239 $run->tests_count = $tests_count;
241 if (isset($options['coverage']) && extension_loaded('xdebug')){
242 $run->xdebug_loaded = true;
244 $run->xdebug_loaded = false;
248 foreach ($tests as $t) {
249 if (isset($options['realtimelog'])) {
250 $fp = @fopen('run-tests.log', 'a');
252 fwrite($fp, "Running test [$i / $tests_count] $t...");
256 PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
257 if (isset($options['phpunit'])) {
258 $result = $run->runPHPUnit($t, $ini_settings);
260 $result = $run->run($t, $ini_settings, $j);
262 PEAR::staticPopErrorHandling();
263 if (PEAR::isError($result)) {
264 $this->ui->log($result->getMessage());
268 if (isset($options['tapoutput'])) {
269 $tap .= $result[0] . ' ' . $i . $result[1] . "\n";
273 if (isset($options['realtimelog'])) {
274 $fp = @fopen('run-tests.log', 'a');
276 fwrite($fp, "$result\n");
281 if ($result == 'FAILED') {
284 if ($result == 'PASSED') {
287 if ($result == 'SKIPPED') {
294 $total = date('i:s', time() - $start);
295 if (isset($options['tapoutput'])) {
296 $fp = @fopen('run-tests.log', 'w');
298 fwrite($fp, $tap, strlen($tap));
300 $this->ui->outputData('wrote TAP-format log to "' .realpath('run-tests.log') .
304 if (count($failed)) {
305 $output = "TOTAL TIME: $total\n";
306 $output .= count($passed) . " PASSED TESTS\n";
307 $output .= count($skipped) . " SKIPPED TESTS\n";
308 $output .= count($failed) . " FAILED TESTS:\n";
309 foreach ($failed as $failure) {
310 $output .= $failure . "\n";
313 $mode = isset($options['realtimelog']) ? 'a' : 'w';
314 $fp = @fopen('run-tests.log', $mode);
317 fwrite($fp, $output, strlen($output));
319 $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command);
321 } elseif (file_exists('run-tests.log') && !is_dir('run-tests.log')) {
322 @unlink('run-tests.log');
325 $this->ui->outputData('TOTAL TIME: ' . $total);
326 $this->ui->outputData(count($passed) . ' PASSED TESTS', $command);
327 $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command);
328 if (count($failed)) {
329 $this->ui->outputData(count($failed) . ' FAILED TESTS:', $command);
330 foreach ($failed as $failure) {
331 $this->ui->outputData($failure, $command);