9  * @author     Tomas V.V.Cox <cox@idecnet.com>
 
  10  * @author     Greg Beaver <cellog@php.net>
 
  11  * @copyright  1997-2009 The Authors
 
  12  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 
  13  * @link       http://pear.php.net/package/PEAR
 
  14  * @since      File available since Release 1.3.3
 
  20 require_once 'PEAR.php';
 
  21 require_once 'PEAR/Config.php';
 
  23 define('DETAILED', 1);
 
  24 putenv("PHP_PEAR_RUNTESTS=1");
 
  27  * Simplified version of PHP's test suite
 
  31  * $ php -r 'include "../PEAR/RunTest.php"; $t=new PEAR_RunTest; $o=$t->run("./pear_system.phpt");print_r($o);'
 
  36  * @author     Tomas V.V.Cox <cox@idecnet.com>
 
  37  * @author     Greg Beaver <cellog@php.net>
 
  38  * @copyright  1997-2009 The Authors
 
  39  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 
  40  * @version    Release: 1.10.1
 
  41  * @link       http://pear.php.net/package/PEAR
 
  42  * @since      Class available since Release 1.3.3
 
  46     var $_headers = array();
 
  53      * Saved value of php executable, used to reset $_php when we
 
  54      * have a test that uses cgi
 
  59     var $ini_overwrites = array(
 
  63         'output_buffering=Off',
 
  69         'report_zend_debug=0',
 
  72         'error_prepend_string=',
 
  73         'error_append_string=',
 
  76         'xdebug.default_enable=0',
 
  81      * An object that supports the PEAR_Common->log() signature, or null
 
  82      * @param PEAR_Common|null
 
  84     function __construct($logger = null, $options = array())
 
  86         if (!defined('E_DEPRECATED')) {
 
  87             define('E_DEPRECATED', 0);
 
  89         if (!defined('E_STRICT')) {
 
  90             define('E_STRICT', 0);
 
  92         $this->ini_overwrites[] = 'error_reporting=' . (E_ALL & ~(E_DEPRECATED | E_STRICT));
 
  93         if (is_null($logger)) {
 
  94             require_once 'PEAR/Common.php';
 
  95             $logger = new PEAR_Common;
 
  97         $this->_logger  = $logger;
 
  98         $this->_options = $options;
 
 100         $conf = &PEAR_Config::singleton();
 
 101         $this->_php = $conf->get('php_bin');
 
 105      * Taken from php-src/run-tests.php
 
 107      * @param string $commandline command name
 
 109      * @param string $stdin standard input to pass to the command
 
 112     function system_with_timeout($commandline, $env = null, $stdin = null)
 
 115         $proc = proc_open($commandline, array(
 
 116             0 => array('pipe', 'r'),
 
 117             1 => array('pipe', 'w'),
 
 118             2 => array('pipe', 'w')
 
 119         ), $pipes, null, $env, array('suppress_errors' => true));
 
 125         if (is_string($stdin)) {
 
 126             fwrite($pipes[0], $stdin);
 
 131             /* hide errors from interrupted syscalls */
 
 134             $n = @stream_select($r, $w, $e, 60);
 
 138                 $data .= "\n ** ERROR: process timed out **\n";
 
 139                 proc_terminate($proc);
 
 140                 return array(1234567890, $data);
 
 142                 $line = fread($pipes[1], 8192);
 
 143                 if (strlen($line) == 0) {
 
 150         if (function_exists('proc_get_status')) {
 
 151             $stat = proc_get_status($proc);
 
 152             if ($stat['signaled']) {
 
 153                 $data .= "\nTermsig=".$stat['stopsig'];
 
 156         $code = proc_close($proc);
 
 157         if (function_exists('proc_get_status')) {
 
 158             $code = $stat['exitcode'];
 
 160         return array($code, $data);
 
 164      * Turns a PHP INI string into an array
 
 166      * Turns -d "include_path=/foo/bar" into this:
 
 168      *   'include_path' => array(
 
 169      *          'operator' => '-d',
 
 170      *          'value'    => '/foo/bar',
 
 173      * Works both with quotes and without
 
 175      * @param string an PHP INI string, -d "include_path=/foo/bar"
 
 178     function iniString2array($ini_string)
 
 183         $split = preg_split('/[\s]|=/', $ini_string, -1, PREG_SPLIT_NO_EMPTY);
 
 184         $key   = $split[1][0] == '"'                     ? substr($split[1], 1)     : $split[1];
 
 185         $value = $split[2][strlen($split[2]) - 1] == '"' ? substr($split[2], 0, -1) : $split[2];
 
 186         // FIXME review if this is really the struct to go with
 
 187         $array = array($key => array('operator' => $split[0], 'value' => $value));
 
 191     function settings2array($settings, $ini_settings)
 
 193         foreach ($settings as $setting) {
 
 194             if (strpos($setting, '=') !== false) {
 
 195                 $setting = explode('=', $setting, 2);
 
 196                 $name  = trim(strtolower($setting[0]));
 
 197                 $value = trim($setting[1]);
 
 198                 $ini_settings[$name] = $value;
 
 201         return $ini_settings;
 
 204     function settings2params($ini_settings)
 
 207         foreach ($ini_settings as $name => $value) {
 
 208             if (is_array($value)) {
 
 209                 $operator = $value['operator'];
 
 210                 $value    = $value['value'];
 
 214             $value = addslashes($value);
 
 215             $settings .= " $operator \"$name=$value\"";
 
 220     function _preparePhpBin($php, $file, $ini_settings)
 
 222         $file = escapeshellarg($file);
 
 223         $cmd = $php . $ini_settings . ' -f ' . $file;
 
 228     function runPHPUnit($file, $ini_settings = '')
 
 230         if (!file_exists($file) && file_exists(getcwd() . DIRECTORY_SEPARATOR . $file)) {
 
 231             $file = realpath(getcwd() . DIRECTORY_SEPARATOR . $file);
 
 232         } elseif (file_exists($file)) {
 
 233             $file = realpath($file);
 
 236         $cmd = $this->_preparePhpBin($this->_php, $file, $ini_settings);
 
 237         if (isset($this->_logger)) {
 
 238             $this->_logger->log(2, 'Running command "' . $cmd . '"');
 
 241         $savedir = getcwd(); // in case the test moves us around
 
 242         chdir(dirname($file));
 
 245         return 'PASSED'; // we have no way of knowing this information so assume passing
 
 249      * Runs an individual test case.
 
 251      * @param string       The filename of the test
 
 252      * @param array|string INI settings to be applied to the test run
 
 253      * @param integer      Number what the current running test is of the
 
 254      *                     whole test suite being runned.
 
 256      * @return string|object Returns PASSED, WARNED, FAILED depending on how the
 
 258      *                       PEAR Error when the tester it self fails
 
 260     function run($file, $ini_settings = array(), $test_number = 1)
 
 262         $this->_restorePHPBinary();
 
 264         if (empty($this->_options['cgi'])) {
 
 265             // try to see if php-cgi is in the path
 
 266             $res = $this->system_with_timeout('php-cgi -v');
 
 267             if (false !== $res && !(is_array($res) && in_array($res[0], array(-1, 127)))) {
 
 268                 $this->_options['cgi'] = 'php-cgi';
 
 271         if (1 < $len = strlen($this->tests_count)) {
 
 272             $test_number = str_pad($test_number, $len, ' ', STR_PAD_LEFT);
 
 273             $test_nr = "[$test_number/$this->tests_count] ";
 
 278         $file = realpath($file);
 
 279         $section_text = $this->_readFile($file);
 
 280         if (PEAR::isError($section_text)) {
 
 281             return $section_text;
 
 284         if (isset($section_text['POST_RAW']) && isset($section_text['UPLOAD'])) {
 
 285             return PEAR::raiseError("Cannot contain both POST_RAW and UPLOAD in test file: $file");
 
 291         if (!empty($this->_options['ini'])) {
 
 292             $pass_options = $this->_options['ini'];
 
 295         if (is_string($ini_settings)) {
 
 296             $ini_settings = $this->iniString2array($ini_settings);
 
 299         $ini_settings = $this->settings2array($this->ini_overwrites, $ini_settings);
 
 300         if ($section_text['INI']) {
 
 301             if (strpos($section_text['INI'], '{PWD}') !== false) {
 
 302                 $section_text['INI'] = str_replace('{PWD}', dirname($file), $section_text['INI']);
 
 304             $ini = preg_split( "/[\n\r]+/", $section_text['INI']);
 
 305             $ini_settings = $this->settings2array($ini, $ini_settings);
 
 307         $ini_settings = $this->settings2params($ini_settings);
 
 308         $shortname = str_replace($cwd . DIRECTORY_SEPARATOR, '', $file);
 
 310         $tested = trim($section_text['TEST']);
 
 311         $tested.= !isset($this->_options['simple']) ? "[$shortname]" : ' ';
 
 313         if (!empty($section_text['POST']) || !empty($section_text['POST_RAW']) ||
 
 314               !empty($section_text['UPLOAD']) || !empty($section_text['GET']) ||
 
 315               !empty($section_text['COOKIE']) || !empty($section_text['EXPECTHEADERS'])) {
 
 316             if (empty($this->_options['cgi'])) {
 
 317                 if (!isset($this->_options['quiet'])) {
 
 318                     $this->_logger->log(0, "SKIP $test_nr$tested (reason: --cgi option needed for this test, type 'pear help run-tests')");
 
 320                 if (isset($this->_options['tapoutput'])) {
 
 321                     return array('ok', ' # skip --cgi option needed for this test, "pear help run-tests" for info');
 
 325             $this->_savePHPBinary();
 
 326             $this->_php = $this->_options['cgi'];
 
 329         $temp_dir = realpath(dirname($file));
 
 330         $main_file_name = basename($file, 'phpt');
 
 331         $diff_filename     = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'diff';
 
 332         $log_filename      = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'log';
 
 333         $exp_filename      = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'exp';
 
 334         $output_filename   = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'out';
 
 335         $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'mem';
 
 336         $temp_file         = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'php';
 
 337         $temp_skipif       = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'skip.php';
 
 338         $temp_clean        = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name.'clean.php';
 
 339         $tmp_post          = $temp_dir . DIRECTORY_SEPARATOR . uniqid('phpt.');
 
 341         // unlink old test results
 
 342         $this->_cleanupOldFiles($file);
 
 344         // Check if test should be skipped.
 
 345         $res  = $this->_runSkipIf($section_text, $temp_skipif, $tested, $ini_settings);
 
 346         if (count($res) != 2) {
 
 349         $info = $res['info'];
 
 350         $warn = $res['warn'];
 
 352         // We've satisfied the preconditions - run the test!
 
 353         if (isset($this->_options['coverage']) && $this->xdebug_loaded) {
 
 354             $xdebug_file = $temp_dir . DIRECTORY_SEPARATOR . $main_file_name . 'xdebug';
 
 355             $text = "\n" . 'function coverage_shutdown() {' .
 
 356                     "\n" . '    $xdebug = var_export(xdebug_get_code_coverage(), true);';
 
 357             if (!function_exists('file_put_contents')) {
 
 358                 $text .= "\n" . '    $fh = fopen(\'' . $xdebug_file . '\', "wb");' .
 
 359                         "\n" . '    if ($fh !== false) {' .
 
 360                         "\n" . '        fwrite($fh, $xdebug);' .
 
 361                         "\n" . '        fclose($fh);' .
 
 364                 $text .= "\n" . '    file_put_contents(\'' . $xdebug_file . '\', $xdebug);';
 
 367             // Workaround for http://pear.php.net/bugs/bug.php?id=17292
 
 368             $lines             = explode("\n", $section_text['FILE']);
 
 369             $numLines          = count($lines);
 
 371             $coverage_shutdown = 'coverage_shutdown';
 
 374                 substr($lines[0], 0, 2) == '<?' ||
 
 375                 substr($lines[0], 0, 5) == '<?php'
 
 381             for ($i = 0; $i < $numLines; $i++) {
 
 382                 if (isset($lines[$i]) && substr($lines[$i], 0, 9) == 'namespace') {
 
 383                     $namespace         = substr($lines[$i], 10, -1);
 
 384                     $coverage_shutdown = $namespace . '\\coverage_shutdown';
 
 385                     $namespace         = "namespace " . $namespace . ";\n";
 
 392             $text .= "\n    xdebug_stop_code_coverage();" .
 
 393                 "\n" . '} // end coverage_shutdown()' .
 
 394                 "\n\n" . 'register_shutdown_function("' . $coverage_shutdown . '");';
 
 395             $text .= "\n" . 'xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);' . "\n";
 
 397             $this->save_text($temp_file, "<?php\n" . $namespace . $text  . "\n" . implode("\n", $lines));
 
 399             $this->save_text($temp_file, $section_text['FILE']);
 
 402         $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : '';
 
 403         $cmd = $this->_preparePhpBin($this->_php, $temp_file, $ini_settings);
 
 405         if (isset($this->_logger)) {
 
 406             $this->_logger->log(2, 'Running command "' . $cmd . '"');
 
 409         // Reset environment from any previous test.
 
 410         $env = $this->_resetEnv($section_text, $temp_file);
 
 412         $section_text = $this->_processUpload($section_text, $file);
 
 413         if (PEAR::isError($section_text)) {
 
 414             return $section_text;
 
 417         if (array_key_exists('POST_RAW', $section_text) && !empty($section_text['POST_RAW'])) {
 
 418             $post = trim($section_text['POST_RAW']);
 
 419             $raw_lines = explode("\n", $post);
 
 423             foreach ($raw_lines as $i => $line) {
 
 424                 if (empty($env['CONTENT_TYPE']) &&
 
 425                     preg_match('/^Content-Type:(.*)/i', $line, $res)) {
 
 426                     $env['CONTENT_TYPE'] = trim(str_replace("\r", '', $res[1]));
 
 436             $env['CONTENT_LENGTH'] = strlen($request);
 
 437             $env['REQUEST_METHOD'] = 'POST';
 
 439             $this->save_text($tmp_post, $request);
 
 440             $cmd = "$this->_php$pass_options$ini_settings \"$temp_file\" 2>&1 < $tmp_post";
 
 441         } elseif (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) {
 
 442             $post = trim($section_text['POST']);
 
 443             $this->save_text($tmp_post, $post);
 
 444             $content_length = strlen($post);
 
 446             $env['REQUEST_METHOD'] = 'POST';
 
 447             $env['CONTENT_TYPE']   = 'application/x-www-form-urlencoded';
 
 448             $env['CONTENT_LENGTH'] = $content_length;
 
 450             $cmd = "$this->_php$pass_options$ini_settings \"$temp_file\" 2>&1 < $tmp_post";
 
 452             $env['REQUEST_METHOD'] = 'GET';
 
 453             $env['CONTENT_TYPE']   = '';
 
 454             $env['CONTENT_LENGTH'] = '';
 
 457         if (OS_WINDOWS && isset($section_text['RETURNS'])) {
 
 459             system($cmd, $return_value);
 
 460             $out = ob_get_contents();
 
 462             $section_text['RETURNS'] = (int) trim($section_text['RETURNS']);
 
 463             $returnfail = ($return_value != $section_text['RETURNS']);
 
 466             $stdin = isset($section_text['STDIN']) ? $section_text['STDIN'] : null;
 
 467             $out = $this->system_with_timeout($cmd, $env, $stdin);
 
 468             $return_value = $out[0];
 
 472         $output = preg_replace('/\r\n/', "\n", trim($out));
 
 474         if (isset($tmp_post) && realpath($tmp_post) && file_exists($tmp_post)) {
 
 475             @unlink(realpath($tmp_post));
 
 477         chdir($cwd); // in case the test moves us around
 
 479         /* when using CGI, strip the headers from the output */
 
 480         $output = $this->_stripHeadersCGI($output);
 
 482         if (isset($section_text['EXPECTHEADERS'])) {
 
 483             $testheaders = $this->_processHeaders($section_text['EXPECTHEADERS']);
 
 484             $missing = array_diff_assoc($testheaders, $this->_headers);
 
 486             foreach ($missing as $header => $value) {
 
 487                 if (isset($this->_headers[$header])) {
 
 488                     $changed .= "-$header: $value\n+$header: ";
 
 489                     $changed .= $this->_headers[$header];
 
 491                     $changed .= "-$header: $value\n";
 
 495                 // tack on failed headers to output:
 
 496                 $output .= "\n====EXPECTHEADERS FAILURE====:\n$changed";
 
 500         $this->_testCleanup($section_text, $temp_clean);
 
 502         // Does the output match what is expected?
 
 504             if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) {
 
 505                 if (isset($section_text['EXPECTF'])) {
 
 506                     $wanted = trim($section_text['EXPECTF']);
 
 508                     $wanted = trim($section_text['EXPECTREGEX']);
 
 510                 $wanted_re = preg_replace('/\r\n/', "\n", $wanted);
 
 511                 if (isset($section_text['EXPECTF'])) {
 
 512                     $wanted_re = preg_quote($wanted_re, '/');
 
 514                     $wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy
 
 515                     $wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re);
 
 516                     $wanted_re = str_replace("%d", "[0-9]+", $wanted_re);
 
 517                     $wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re);
 
 518                     $wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?", $wanted_re);
 
 519                     $wanted_re = str_replace("%c", ".", $wanted_re);
 
 520                     // %f allows two points "-.0.0" but that is the best *simple* expression
 
 523     /* DEBUG YOUR REGEX HERE
 
 524             var_dump($wanted_re);
 
 525             print(str_repeat('=', 80) . "\n");
 
 528                 if (!$returnfail && preg_match("/^$wanted_re\$/s", $output)) {
 
 529                     if (file_exists($temp_file)) {
 
 532                     if (array_key_exists('FAIL', $section_text)) {
 
 535                     if (!isset($this->_options['quiet'])) {
 
 536                         $this->_logger->log(0, "PASS $test_nr$tested$info");
 
 538                     if (isset($this->_options['tapoutput'])) {
 
 539                         return array('ok', ' - ' . $tested);
 
 544                 if (isset($section_text['EXPECTFILE'])) {
 
 545                     $f = $temp_dir . '/' . trim($section_text['EXPECTFILE']);
 
 546                     if (!($fp = @fopen($f, 'rb'))) {
 
 547                         return PEAR::raiseError('--EXPECTFILE-- section file ' .
 
 551                     $section_text['EXPECT'] = file_get_contents($f);
 
 554                 if (isset($section_text['EXPECT'])) {
 
 555                     $wanted = preg_replace('/\r\n/', "\n", trim($section_text['EXPECT']));
 
 560                 // compare and leave on success
 
 561                 if (!$returnfail && 0 == strcmp($output, $wanted)) {
 
 562                     if (file_exists($temp_file)) {
 
 565                     if (array_key_exists('FAIL', $section_text)) {
 
 568                     if (!isset($this->_options['quiet'])) {
 
 569                         $this->_logger->log(0, "PASS $test_nr$tested$info");
 
 571                     if (isset($this->_options['tapoutput'])) {
 
 572                         return array('ok', ' - ' . $tested);
 
 579         if (array_key_exists('FAIL', $section_text)) {
 
 580             // we expect a particular failure
 
 581             // this is only used for testing PEAR_RunTest
 
 582             $expectf  = isset($section_text['EXPECTF']) ? $wanted_re : null;
 
 583             $faildiff = $this->generate_diff($wanted, $output, null, $expectf);
 
 584             $faildiff = preg_replace('/\r/', '', $faildiff);
 
 585             $wanted   = preg_replace('/\r/', '', trim($section_text['FAIL']));
 
 586             if ($faildiff == $wanted) {
 
 587                 if (!isset($this->_options['quiet'])) {
 
 588                     $this->_logger->log(0, "PASS $test_nr$tested$info");
 
 590                 if (isset($this->_options['tapoutput'])) {
 
 591                     return array('ok', ' - ' . $tested);
 
 595             unset($section_text['EXPECTF']);
 
 597             if (isset($section_text['RETURNS'])) {
 
 598                 return PEAR::raiseError('Cannot have both RETURNS and FAIL in the same test: ' .
 
 603         // Test failed so we need to report details.
 
 604         $txt = $warn ? 'WARN ' : 'FAIL ';
 
 605         $this->_logger->log(0, $txt . $test_nr . $tested . $info);
 
 608         $res = $this->_writeLog($exp_filename, $wanted);
 
 609         if (PEAR::isError($res)) {
 
 614         $res = $this->_writeLog($output_filename, $output);
 
 615         if (PEAR::isError($res)) {
 
 620         $returns = isset($section_text['RETURNS']) ?
 
 621                         array(trim($section_text['RETURNS']), $return_value) : null;
 
 622         $expectf = isset($section_text['EXPECTF']) ? $wanted_re : null;
 
 623         $data = $this->generate_diff($wanted, $output, $returns, $expectf);
 
 624         $res  = $this->_writeLog($diff_filename, $data);
 
 625         if (isset($this->_options['showdiff'])) {
 
 626             $this->_logger->log(0, "========DIFF========");
 
 627             $this->_logger->log(0, $data);
 
 628             $this->_logger->log(0, "========DONE========");
 
 630         if (PEAR::isError($res)) {
 
 646 $section_text[RETURNS]
 
 652         $res = $this->_writeLog($log_filename, $data);
 
 653         if (PEAR::isError($res)) {
 
 657         if (isset($this->_options['tapoutput'])) {
 
 658             $wanted = explode("\n", $wanted);
 
 659             $wanted = "# Expected output:\n#\n#" . implode("\n#", $wanted);
 
 660             $output = explode("\n", $output);
 
 661             $output = "#\n#\n# Actual output:\n#\n#" . implode("\n#", $output);
 
 662             return array($wanted . $output . 'not ok', ' - ' . $tested);
 
 664         return $warn ? 'WARNED' : 'FAILED';
 
 667     function generate_diff($wanted, $output, $rvalue, $wanted_re)
 
 669         $w  = explode("\n", $wanted);
 
 670         $o  = explode("\n", $output);
 
 671         $wr = explode("\n", $wanted_re);
 
 672         $w1 = array_diff_assoc($w, $o);
 
 673         $o1 = array_diff_assoc($o, $w);
 
 675         foreach ($w1 as $idx => $val) {
 
 676             if (!$wanted_re || !isset($wr[$idx]) || !isset($o1[$idx]) ||
 
 677                   !preg_match('/^' . $wr[$idx] . '\\z/', $o1[$idx])) {
 
 678                 $w2[sprintf("%03d<", $idx)] = sprintf("%03d- ", $idx + 1) . $val;
 
 681         foreach ($o1 as $idx => $val) {
 
 682             if (!$wanted_re || !isset($wr[$idx]) ||
 
 683                   !preg_match('/^' . $wr[$idx] . '\\z/', $val)) {
 
 684                 $o2[sprintf("%03d>", $idx)] = sprintf("%03d+ ", $idx + 1) . $val;
 
 687         $diff = array_merge($w2, $o2);
 
 689         $extra = $rvalue ? "##EXPECTED: $rvalue[0]\r\n##RETURNED: $rvalue[1]" : '';
 
 690         return implode("\r\n", $diff) . $extra;
 
 693     //  Write the given text to a temporary file, and return the filename.
 
 694     function save_text($filename, $text)
 
 696         if (!$fp = fopen($filename, 'w')) {
 
 697             return PEAR::raiseError("Cannot open file '" . $filename . "' (save_text)");
 
 701     if (1 < DETAILED) echo "
 
 708     function _cleanupOldFiles($file)
 
 710         $temp_dir = realpath(dirname($file));
 
 711         $mainFileName = basename($file, 'phpt');
 
 712         $diff_filename     = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'diff';
 
 713         $log_filename      = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'log';
 
 714         $exp_filename      = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'exp';
 
 715         $output_filename   = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'out';
 
 716         $memcheck_filename = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'mem';
 
 717         $temp_file         = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'php';
 
 718         $temp_skipif       = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'skip.php';
 
 719         $temp_clean        = $temp_dir . DIRECTORY_SEPARATOR . $mainFileName.'clean.php';
 
 720         $tmp_post          = $temp_dir . DIRECTORY_SEPARATOR . uniqid('phpt.');
 
 722         // unlink old test results
 
 723         @unlink($diff_filename);
 
 724         @unlink($log_filename);
 
 725         @unlink($exp_filename);
 
 726         @unlink($output_filename);
 
 727         @unlink($memcheck_filename);
 
 729         @unlink($temp_skipif);
 
 731         @unlink($temp_clean);
 
 734     function _runSkipIf($section_text, $temp_skipif, $tested, $ini_settings)
 
 738         if (array_key_exists('SKIPIF', $section_text) && trim($section_text['SKIPIF'])) {
 
 739             $this->save_text($temp_skipif, $section_text['SKIPIF']);
 
 740             $output = $this->system_with_timeout("$this->_php$ini_settings -f \"$temp_skipif\"");
 
 741             $output = $output[1];
 
 742             $loutput = ltrim($output);
 
 743             unlink($temp_skipif);
 
 744             if (!strncasecmp('skip', $loutput, 4)) {
 
 745                 $skipreason = "SKIP $tested";
 
 746                 if (preg_match('/^\s*skip\s*(.+)\s*/i', $output, $m)) {
 
 747                     $skipreason .= '(reason: ' . $m[1] . ')';
 
 749                 if (!isset($this->_options['quiet'])) {
 
 750                     $this->_logger->log(0, $skipreason);
 
 752                 if (isset($this->_options['tapoutput'])) {
 
 753                     return array('ok', ' # skip ' . $reason);
 
 758             if (!strncasecmp('info', $loutput, 4)
 
 759                 && preg_match('/^\s*info\s*(.+)\s*/i', $output, $m)) {
 
 760                 $info = " (info: $m[1])";
 
 763             if (!strncasecmp('warn', $loutput, 4)
 
 764                 && preg_match('/^\s*warn\s*(.+)\s*/i', $output, $m)) {
 
 765                 $warn = true; /* only if there is a reason */
 
 766                 $info = " (warn: $m[1])";
 
 770         return array('warn' => $warn, 'info' => $info);
 
 773     function _stripHeadersCGI($output)
 
 775         $this->headers = array();
 
 776         if (!empty($this->_options['cgi']) &&
 
 777               $this->_php == $this->_options['cgi'] &&
 
 778               preg_match("/^(.*?)(?:\n\n(.*)|\\z)/s", $output, $match)) {
 
 779             $output = isset($match[2]) ? trim($match[2]) : '';
 
 780             $this->_headers = $this->_processHeaders($match[1]);
 
 787      * Return an array that can be used with array_diff() to compare headers
 
 789      * @param string $text
 
 791     function _processHeaders($text)
 
 794         $rh = preg_split("/[\n\r]+/", $text);
 
 795         foreach ($rh as $line) {
 
 796             if (strpos($line, ':')!== false) {
 
 797                 $line = explode(':', $line, 2);
 
 798                 $headers[trim($line[0])] = trim($line[1]);
 
 804     function _readFile($file)
 
 806         // Load the sections of the test file.
 
 807         $section_text = array(
 
 808             'TEST'   => '(unnamed test)',
 
 818         if (!is_file($file) || !$fp = fopen($file, "r")) {
 
 819             return PEAR::raiseError("Cannot open test file: $file");
 
 826             // Match the beginning of a section.
 
 827             if (preg_match('/^--([_A-Z]+)--/', $line, $r)) {
 
 829                 $section_text[$section] = '';
 
 831             } elseif (empty($section)) {
 
 833                 return PEAR::raiseError("Invalid sections formats in test file: $file");
 
 836             // Add to the section text.
 
 837             $section_text[$section] .= $line;
 
 841         return $section_text;
 
 844     function _writeLog($logname, $data)
 
 846         if (!$log = fopen($logname, 'w')) {
 
 847             return PEAR::raiseError("Cannot create test log - $logname");
 
 853     function _resetEnv($section_text, $temp_file)
 
 856         $env['REDIRECT_STATUS'] = '';
 
 857         $env['QUERY_STRING']    = '';
 
 858         $env['PATH_TRANSLATED'] = '';
 
 859         $env['SCRIPT_FILENAME'] = '';
 
 860         $env['REQUEST_METHOD']  = '';
 
 861         $env['CONTENT_TYPE']    = '';
 
 862         $env['CONTENT_LENGTH']  = '';
 
 863         if (!empty($section_text['ENV'])) {
 
 864             if (strpos($section_text['ENV'], '{PWD}') !== false) {
 
 865                 $section_text['ENV'] = str_replace('{PWD}', dirname($temp_file), $section_text['ENV']);
 
 867             foreach (explode("\n", trim($section_text['ENV'])) as $e) {
 
 868                 $e = explode('=', trim($e), 2);
 
 869                 if (!empty($e[0]) && isset($e[1])) {
 
 874         if (array_key_exists('GET', $section_text)) {
 
 875             $env['QUERY_STRING'] = trim($section_text['GET']);
 
 877             $env['QUERY_STRING'] = '';
 
 879         if (array_key_exists('COOKIE', $section_text)) {
 
 880             $env['HTTP_COOKIE'] = trim($section_text['COOKIE']);
 
 882             $env['HTTP_COOKIE'] = '';
 
 884         $env['REDIRECT_STATUS'] = '1';
 
 885         $env['PATH_TRANSLATED'] = $temp_file;
 
 886         $env['SCRIPT_FILENAME'] = $temp_file;
 
 891     function _processUpload($section_text, $file)
 
 893         if (array_key_exists('UPLOAD', $section_text) && !empty($section_text['UPLOAD'])) {
 
 894             $upload_files = trim($section_text['UPLOAD']);
 
 895             $upload_files = explode("\n", $upload_files);
 
 897             $request = "Content-Type: multipart/form-data; boundary=---------------------------20896060251896012921717172737\n" .
 
 898                        "-----------------------------20896060251896012921717172737\n";
 
 899             foreach ($upload_files as $fileinfo) {
 
 900                 $fileinfo = explode('=', $fileinfo);
 
 901                 if (count($fileinfo) != 2) {
 
 902                     return PEAR::raiseError("Invalid UPLOAD section in test file: $file");
 
 904                 if (!realpath(dirname($file) . '/' . $fileinfo[1])) {
 
 905                     return PEAR::raiseError("File for upload does not exist: $fileinfo[1] " .
 
 906                         "in test file: $file");
 
 908                 $file_contents = file_get_contents(dirname($file) . '/' . $fileinfo[1]);
 
 909                 $fileinfo[1] = basename($fileinfo[1]);
 
 910                 $request .= "Content-Disposition: form-data; name=\"$fileinfo[0]\"; filename=\"$fileinfo[1]\"\n";
 
 911                 $request .= "Content-Type: text/plain\n\n";
 
 912                 $request .= $file_contents . "\n" .
 
 913                     "-----------------------------20896060251896012921717172737\n";
 
 916             if (array_key_exists('POST', $section_text) && !empty($section_text['POST'])) {
 
 918                 $post = trim($section_text['POST']);
 
 919                 $post = explode('&', $post);
 
 920                 foreach ($post as $i => $post_info) {
 
 921                     $post_info = explode('=', $post_info);
 
 922                     if (count($post_info) != 2) {
 
 923                         return PEAR::raiseError("Invalid POST data in test file: $file");
 
 925                     $post_info[0] = rawurldecode($post_info[0]);
 
 926                     $post_info[1] = rawurldecode($post_info[1]);
 
 927                     $post[$i] = $post_info;
 
 929                 foreach ($post as $post_info) {
 
 930                     $request .= "Content-Disposition: form-data; name=\"$post_info[0]\"\n\n";
 
 931                     $request .= $post_info[1] . "\n" .
 
 932                         "-----------------------------20896060251896012921717172737\n";
 
 934                 unset($section_text['POST']);
 
 936             $section_text['POST_RAW'] = $request;
 
 939         return $section_text;
 
 942     function _testCleanup($section_text, $temp_clean)
 
 944         if ($section_text['CLEAN']) {
 
 945             $this->_restorePHPBinary();
 
 947             // perform test cleanup
 
 948             $this->save_text($temp_clean, $section_text['CLEAN']);
 
 949             $output = $this->system_with_timeout("$this->_php $temp_clean  2>&1");
 
 950             if (strlen($output[1])) {
 
 951                 echo "BORKED --CLEAN-- section! output:\n", $output[1];
 
 953             if (file_exists($temp_clean)) {
 
 959     function _savePHPBinary()
 
 961         $this->_savephp = $this->_php;
 
 964     function _restorePHPBinary()
 
 966         if (isset($this->_savephp))
 
 968             $this->_php = $this->_savephp;
 
 969             unset($this->_savephp);