function doRunTests($command, $options, $params) { if (isset($options['phpunit']) && isset($options['tapoutput'])) { return $this->raiseError('ERROR: cannot use both --phpunit and --tapoutput at the same time'); } require_once 'PEAR/Common.php'; require_once 'System.php'; $log = new PEAR_Common(); $log->ui =& $this->ui; // slightly hacky, but it will work $tests = array(); $depth = isset($options['recur']) ? 4 : 1; if (!count($params)) { $params[] = '.'; } if (isset($options['package'])) { $oldparams = $params; $params = array(); $reg =& $this->config->getRegistry(); foreach ($oldparams as $param) { $pname = $reg->parsePackageName($param, $this->config->get('default_channel')); if (PEAR::isError($pname)) { return $this->raiseError($pname); } $package =& $reg->getPackage($pname['package'], $pname['channel']); if (!$package) { return PEAR::raiseError('Unknown package "' . $reg->parsedPackageNameToString($pname) . '"'); } $filelist = $package->getFilelist(); foreach ($filelist as $name => $atts) { if (isset($atts['role']) && $atts['role'] != 'test') { continue; } if (isset($options['phpunit']) && preg_match('/AllTests\\.php\\z/i', $name)) { $params[] = $atts['installed_as']; continue; } elseif (!preg_match('/\\.phpt\\z/', $name)) { continue; } $params[] = $atts['installed_as']; } } } foreach ($params as $p) { if (is_dir($p)) { if (isset($options['phpunit'])) { $dir = System::find(array($p, '-type', 'f', '-maxdepth', $depth, '-name', 'AllTests.php')); if (count($dir)) { foreach ($dir as $p) { $p = realpath($p); if (!count($tests) || count($tests) && strlen($p) < strlen($tests[0])) { // this is in a higher-level directory, use this one instead. $tests = array($p); } } } continue; } $dir = System::find(array($p, '-type', 'f', '-maxdepth', $depth, '-name', '*.phpt')); $tests = array_merge($tests, $dir); } else { if (isset($options['phpunit'])) { if (preg_match('/AllTests\\.php\\z/i', $p)) { $p = realpath($p); if (!count($tests) || count($tests) && strlen($p) < strlen($tests[0])) { // this is in a higher-level directory, use this one instead. $tests = array($p); } } continue; } if (file_exists($p) && preg_match('/\\.phpt$/', $p)) { $tests[] = $p; continue; } if (!preg_match('/\\.phpt\\z/', $p)) { $p .= '.phpt'; } $dir = System::find(array(dirname($p), '-type', 'f', '-maxdepth', $depth, '-name', $p)); $tests = array_merge($tests, $dir); } } $ini_settings = ''; if (isset($options['ini'])) { $ini_settings .= $options['ini']; } if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) { $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}"; } if ($ini_settings) { $this->ui->outputData('Using INI settings: "' . $ini_settings . '"'); } $skipped = $passed = $failed = array(); $tests_count = count($tests); $this->ui->outputData('Running ' . $tests_count . ' tests', $command); $start = time(); if (isset($options['realtimelog']) && file_exists('run-tests.log')) { unlink('run-tests.log'); } if (isset($options['tapoutput'])) { $tap = '1..' . $tests_count . "\n"; } require_once 'PEAR/RunTest.php'; $run = new PEAR_RunTest($log, $options); $run->tests_count = $tests_count; if (isset($options['coverage']) && extension_loaded('xdebug')) { $run->xdebug_loaded = true; } else { $run->xdebug_loaded = false; } $j = $i = 1; foreach ($tests as $t) { if (isset($options['realtimelog'])) { $fp = @fopen('run-tests.log', 'a'); if ($fp) { fwrite($fp, "Running test [{$i} / {$tests_count}] {$t}..."); fclose($fp); } } PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); if (isset($options['phpunit'])) { $result = $run->runPHPUnit($t, $ini_settings); } else { $result = $run->run($t, $ini_settings, $j); } PEAR::staticPopErrorHandling(); if (PEAR::isError($result)) { $this->ui->log($result->getMessage()); continue; } if (isset($options['tapoutput'])) { $tap .= $result[0] . ' ' . $i . $result[1] . "\n"; continue; } if (isset($options['realtimelog'])) { $fp = @fopen('run-tests.log', 'a'); if ($fp) { fwrite($fp, "{$result}\n"); fclose($fp); } } if ($result == 'FAILED') { $failed[] = $t; } if ($result == 'PASSED') { $passed[] = $t; } if ($result == 'SKIPPED') { $skipped[] = $t; } $j++; } $total = date('i:s', time() - $start); if (isset($options['tapoutput'])) { $fp = @fopen('run-tests.log', 'w'); if ($fp) { fwrite($fp, $tap, strlen($tap)); fclose($fp); $this->ui->outputData('wrote TAP-format log to "' . realpath('run-tests.log') . '"', $command); } } else { if (count($failed)) { $output = "TOTAL TIME: {$total}\n"; $output .= count($passed) . " PASSED TESTS\n"; $output .= count($skipped) . " SKIPPED TESTS\n"; $output .= count($failed) . " FAILED TESTS:\n"; foreach ($failed as $failure) { $output .= $failure . "\n"; } $mode = isset($options['realtimelog']) ? 'a' : 'w'; $fp = @fopen('run-tests.log', $mode); if ($fp) { fwrite($fp, $output, strlen($output)); fclose($fp); $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command); } } elseif (file_exists('run-tests.log') && !is_dir('run-tests.log')) { @unlink('run-tests.log'); } } $this->ui->outputData('TOTAL TIME: ' . $total); $this->ui->outputData(count($passed) . ' PASSED TESTS', $command); $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command); if (count($failed)) { $this->ui->outputData(count($failed) . ' FAILED TESTS:', $command); foreach ($failed as $failure) { $this->ui->outputData($failure, $command); } } return true; }
/** * Runs a test and collects its result in a TestResult instance. * * @param PHPUnit_Framework_TestResult $result * @param array $options * @return PHPUnit_Framework_TestResult */ public function run(PHPUnit_Framework_TestResult $result = NULL, array $options = array()) { if (!class_exists('PEAR_RunTest', FALSE)) { throw new PHPUnit_Framework_Exception('Class PEAR_RunTest not found.'); } if (isset($GLOBALS['_PEAR_destructor_object_list']) && is_array($GLOBALS['_PEAR_destructor_object_list']) && !empty($GLOBALS['_PEAR_destructor_object_list'])) { $pearDestructorObjectListCount = count($GLOBALS['_PEAR_destructor_object_list']); } else { $pearDestructorObjectListCount = 0; } if ($result === NULL) { $result = new PHPUnit_Framework_TestResult(); } $coverage = $result->getCollectCodeCoverageInformation(); $options = array_merge($options, $this->options); if (!isset($options['include_path'])) { $options['include_path'] = get_include_path(); } if ($coverage) { $options['coverage'] = TRUE; } else { $options['coverage'] = FALSE; } $currentErrorReporting = error_reporting(E_ERROR | E_WARNING | E_PARSE); $runner = new PEAR_RunTest(new PHPUnit_Extensions_PhptTestCase_Logger(), $options); if ($coverage) { $runner->xdebug_loaded = TRUE; } else { $runner->xdebug_loaded = FALSE; } $result->startTest($this); PHP_Timer::start(); $buffer = $runner->run($this->filename, $options); $time = PHP_Timer::stop(); error_reporting($currentErrorReporting); $base = basename($this->filename); $path = dirname($this->filename); $coverageFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.xdebug', $base); $diffFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.diff', $base); $expFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.exp', $base); $logFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.log', $base); $outFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.out', $base); $phpFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.php', $base); if (is_object($buffer) && $buffer instanceof PEAR_Error) { $result->addError($this, new PHPUnit_Framework_Exception($buffer->getMessage()), $time); } else { if ($buffer == 'SKIPPED') { $result->addFailure($this, new PHPUnit_Framework_SkippedTestError(), 0); } else { if ($buffer != 'PASSED') { $expContent = file_get_contents($expFile); $outContent = file_get_contents($outFile); $result->addFailure($this, new PHPUnit_Framework_ComparisonFailure($expContent, $outContent, $expContent, $outContent), $time); } } } foreach (array($diffFile, $expFile, $logFile, $phpFile, $outFile) as $file) { if (file_exists($file)) { unlink($file); } } if ($coverage && file_exists($coverageFile)) { eval('$coverageData = ' . file_get_contents($coverageFile) . ';'); unset($coverageData[$phpFile]); $result->getCodeCoverage()->append($coverageData, $this); unlink($coverageFile); } $result->endTest($this, $time); // Do not invoke PEAR's destructor mechanism for PHP 4 // as it raises an E_STRICT. if ($pearDestructorObjectListCount == 0) { unset($GLOBALS['_PEAR_destructor_object_list']); } else { $count = count($GLOBALS['_PEAR_destructor_object_list']) - $pearDestructorObjectListCount; for ($i = 0; $i < $count; $i++) { array_pop($GLOBALS['_PEAR_destructor_object_list']); } } return $result; }
function doRunTests($command, $options, $params) { require_once 'PEAR/Common.php'; require_once 'PEAR/RunTest.php'; require_once 'System.php'; $log = new PEAR_Common(); $log->ui =& $this->ui; // slightly hacky, but it will work $run = new PEAR_RunTest($log, $options); $tests = array(); if (isset($options['recur'])) { $depth = 4; } else { $depth = 1; } if (!count($params)) { $params[] = '.'; } if (isset($options['package'])) { $oldparams = $params; $params = array(); $reg =& $this->config->getRegistry(); foreach ($oldparams as $param) { $pname = $reg->parsePackageName($param, $this->config->get('default_channel')); if (PEAR::isError($pname)) { return $this->raiseError($pname); } $package =& $reg->getPackage($pname['package'], $pname['channel']); if (!$package) { return PEAR::raiseError('Unknown package "' . $reg->parsedPackageNameToString($pname) . '"'); } $filelist = $package->getFilelist(); foreach ($filelist as $name => $atts) { if (isset($atts['role']) && $atts['role'] != 'test') { continue; } if (!preg_match('/\\.phpt$/', $name)) { continue; } $params[] = $atts['installed_as']; } } } foreach ($params as $p) { if (is_dir($p)) { $dir = System::find(array($p, '-type', 'f', '-maxdepth', $depth, '-name', '*.phpt')); $tests = array_merge($tests, $dir); } else { if (!@file_exists($p)) { if (!preg_match('/\\.phpt$/', $p)) { $p .= '.phpt'; } $dir = System::find(array(dirname($p), '-type', 'f', '-maxdepth', $depth, '-name', $p)); $tests = array_merge($tests, $dir); } else { $tests[] = $p; } } } $ini_settings = ''; if (isset($options['ini'])) { $ini_settings .= $options['ini']; } if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) { $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}"; } if ($ini_settings) { $this->ui->outputData('Using INI settings: "' . $ini_settings . '"'); } $skipped = $passed = $failed = array(); $this->ui->outputData('Running ' . count($tests) . ' tests', $command); $start = time(); if (isset($options['realtimelog'])) { @unlink('run-tests.log'); } foreach ($tests as $t) { if (isset($options['realtimelog'])) { $fp = @fopen('run-tests.log', 'a'); if ($fp) { fwrite($fp, "Running test {$t}..."); fclose($fp); } } PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN); $result = $run->run($t, $ini_settings); PEAR::staticPopErrorHandling(); if (PEAR::isError($result)) { $this->ui->log(0, $result->getMessage()); continue; } if (OS_WINDOWS) { for ($i = 0; $i < 2000; $i++) { $i = $i; // delay - race conditions on windows } } if (isset($options['realtimelog'])) { $fp = @fopen('run-tests.log', 'a'); if ($fp) { fwrite($fp, "{$result}\n"); fclose($fp); } } if ($result == 'FAILED') { $failed[] = $t; } if ($result == 'PASSED') { $passed[] = $t; } if ($result == 'SKIPPED') { $skipped[] = $t; } } $total = date('i:s', time() - $start); if (count($failed)) { $output = "TOTAL TIME: {$total}\n"; $output .= count($passed) . " PASSED TESTS\n"; $output .= count($skipped) . " SKIPPED TESTS\n"; $output .= count($failed) . " FAILED TESTS:\n"; foreach ($failed as $failure) { $output .= $failure . "\n"; } if (isset($options['realtimelog'])) { $fp = @fopen('run-tests.log', 'a'); } else { $fp = @fopen('run-tests.log', 'w'); } if ($fp) { fwrite($fp, $output, strlen($output)); fclose($fp); $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command); } } elseif (@file_exists('run-tests.log') && !@is_dir('run-tests.log')) { @unlink('run-tests.log'); } $this->ui->outputData('TOTAL TIME: ' . $total); $this->ui->outputData(count($passed) . ' PASSED TESTS', $command); $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command); if (count($failed)) { $this->ui->outputData(count($failed) . ' FAILED TESTS:', $command); foreach ($failed as $failure) { $this->ui->outputData($failure, $command); } } return true; }
/** * Runs a test and collects its result in a TestResult instance. * * @param PHPUnit_Framework_TestResult $result * @param array $options Array with ini settings for the php instance run, * key being the name if the setting, value the ini value. * @return PHPUnit_Framework_TestResult * @access public */ public function run(PHPUnit_Framework_TestResult $result = NULL, $options = array()) { if (!class_exists('PEAR_RunTest', FALSE)) { throw new RuntimeException('Class PEAR_RunTest not found.'); } if ($result === NULL) { $result = new PHPUnit_Framework_TestResult(); } if (!is_array($options)) { throw new InvalidArgumentException(); } $options = array_merge($options, $this->options); $coverage = $result->getCollectCodeCoverageInformation(); if ($coverage) { $options = array('coverage' => TRUE); } else { $options = array(); } $runner = new PEAR_RunTest(new PHPUnit_Extensions_PhptTestCase_Logger(), $options); if ($coverage) { $runner->xdebug_loaded = TRUE; } else { $runner->xdebug_loaded = FALSE; } $result->startTest($this); PHPUnit_Util_Timer::start(); $buffer = $runner->run($this->filename, $options); $time = PHPUnit_Util_Timer::stop(); $base = basename($this->filename); $path = dirname($this->filename); $coverageFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.xdebug', $base); $diffFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.diff', $base); $expFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.exp', $base); $logFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.log', $base); $outFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.out', $base); $phpFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.php', $base); if (is_object($buffer) && $buffer instanceof PEAR_Error) { $result->addError($this, new RuntimeException($buffer->getMessage()), $time); } else { if ($buffer == 'SKIPPED') { $result->addFailure($this, new PHPUnit_Framework_SkippedTestError(), 0); } else { if ($buffer != 'PASSED') { $result->addFailure($this, PHPUnit_Framework_ComparisonFailure::diffEqual(file_get_contents($expFile), file_get_contents($outFile), FALSE, $this->getName()), $time); } } } foreach (array($diffFile, $expFile, $logFile, $phpFile, $outFile) as $file) { if (file_exists($file)) { unlink($file); } } if ($coverage) { eval('$coverageData = ' . file_get_contents($coverageFile) . ';'); unset($coverageData[$phpFile]); $codeCoverageInformation = array('test' => $this, 'files' => $coverageData); $result->appendCodeCoverageInformation($this, $codeCoverageInformation); unlink($coverageFile); } $result->endTest($this, $time); return $result; }
function doRunTests($command, $options, $params) { include_once 'PEAR/RunTest.php'; $log = new PEAR_Common(); $log->ui =& $this->ui; // slightly hacky, but it will work $run = new PEAR_RunTest($log); $tests = array(); if (isset($options['recur'])) { $depth = 4; } else { $depth = 1; } foreach ($params as $p) { if (is_dir($p)) { $dir = System::find(array($p, '-type', 'f', '-maxdepth', $depth, '-name', '*.phpt')); $tests = array_merge($tests, $dir); } else { if (!@file_exists($p)) { if (!preg_match('/\\.phpt$/', $p)) { $p .= '.phpt'; } $dir = System::find(array(dirname($p), '-type', 'f', '-maxdepth', $depth, '-name', $p)); $tests = array_merge($tests, $dir); } else { $tests[] = $p; } } } $ini_settings = ''; if (isset($options['ini'])) { $ini_settings .= $options['ini']; } if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) { $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}"; } if ($ini_settings) { $this->ui->outputData('Using INI settings: "' . $ini_settings . '"'); } $skipped = $passed = $failed = array(); $this->ui->outputData('Running ' . count($tests) . ' tests'); foreach ($tests as $t) { $result = $run->run($t, $ini_settings); if ($result == 'FAILED') { $failed[] = $t; } if ($result == 'PASSED') { $passed[] = $t; } if ($result == 'SKIPPED') { $skipped[] = $t; } } $this->ui->outputData(count($passed) . ' PASSED TESTS'); $this->ui->outputData(count($skipped) . ' SKIPPED TESTS'); if (count($failed)) { $this->ui->outputData(count($failed) . ' FAILED TESTS:'); foreach ($failed as $failure) { $this->ui->outputData($failure); } } return true; /* $cwd = getcwd(); $php = $this->config->get('php_bin'); putenv("TEST_PHP_EXECUTABLE=$php"); // all core PEAR tests use this constant to determine whether they should be run or not putenv("PHP_PEAR_RUNTESTS=1"); $ip = ini_get("include_path"); $ps = OS_WINDOWS ? ';' : ':'; $run_tests = $rtsts = $this->config->get('php_dir') . DIRECTORY_SEPARATOR . 'run-tests.php'; if (!file_exists($run_tests)) { $run_tests = PEAR_INSTALL_DIR . DIRECTORY_SEPARATOR . 'run-tests.php'; if (!file_exists($run_tests)) { return $this->raiseError("No run-tests.php file found. Please copy this ". "file from the sources of your PHP distribution to $rtsts"); } } if (OS_WINDOWS) { // note, this requires a slightly modified version of run-tests.php // for some setups // unofficial download location is in the pear-dev archives $argv = $params; array_unshift($argv, $run_tests); $argc = count($argv); include $run_tests; } else { $plist = implode(' ', $params); $cmd = "$php -d include_path=$cwd$ps$ip -f $run_tests -- $plist"; system($cmd); } return true; */ }
function doRunTests($command, $options, $params) { include_once 'PEAR/RunTest.php'; $log = new PEAR_Common(); $log->ui =& $this->ui; // slightly hacky, but it will work $run = new PEAR_RunTest($log); $tests = array(); if (isset($options['recur'])) { $depth = 4; } else { $depth = 1; } if (!count($params)) { $params[] = '.'; } foreach ($params as $p) { if (is_dir($p)) { $dir = System::find(array($p, '-type', 'f', '-maxdepth', $depth, '-name', '*.phpt')); $tests = array_merge($tests, $dir); } else { if (!@file_exists($p)) { if (!preg_match('/\\.phpt$/', $p)) { $p .= '.phpt'; } $dir = System::find(array(dirname($p), '-type', 'f', '-maxdepth', $depth, '-name', $p)); $tests = array_merge($tests, $dir); } else { $tests[] = $p; } } } $ini_settings = ''; if (isset($options['ini'])) { $ini_settings .= $options['ini']; } if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) { $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}"; } if ($ini_settings) { $this->ui->outputData('Using INI settings: "' . $ini_settings . '"'); } $skipped = $passed = $failed = array(); $this->ui->outputData('Running ' . count($tests) . ' tests', $command); $start = time(); if (isset($options['realtimelog'])) { @unlink('run-tests.log'); } foreach ($tests as $t) { if (isset($options['realtimelog'])) { $fp = @fopen('run-tests.log', 'a'); if ($fp) { fwrite($fp, "Running test {$t}..."); fclose($fp); } } $result = $run->run($t, $ini_settings); if (OS_WINDOWS) { for ($i = 0; $i < 2000; $i++) { $i = $i; // delay - race conditions on windows } } if (isset($options['realtimelog'])) { $fp = @fopen('run-tests.log', 'a'); if ($fp) { fwrite($fp, "{$result}\n"); fclose($fp); } } if ($result == 'FAILED') { $failed[] = $t; } if ($result == 'PASSED') { $passed[] = $t; } if ($result == 'SKIPPED') { $skipped[] = $t; } } $total = date('i:s', time() - $start); if (count($failed)) { $output = "TOTAL TIME: {$total}\n"; $output .= count($passed) . " PASSED TESTS\n"; $output .= count($skipped) . " SKIPPED TESTS\n"; $output .= count($failed) . " FAILED TESTS:\n"; foreach ($failed as $failure) { $output .= $failure . "\n"; } if (isset($options['realtimelog'])) { $fp = @fopen('run-tests.log', 'a'); } else { $fp = @fopen('run-tests.log', 'w'); } if ($fp) { fwrite($fp, $output, strlen($output)); fclose($fp); $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command); } } elseif (@file_exists('run-tests.log') && !@is_dir('run-tests.log')) { @unlink('run-tests.log'); } $this->ui->outputData('TOTAL TIME: ' . $total); $this->ui->outputData(count($passed) . ' PASSED TESTS', $command); $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command); if (count($failed)) { $this->ui->outputData(count($failed) . ' FAILED TESTS:', $command); foreach ($failed as $failure) { $this->ui->outputData($failure, $command); } } return true; }