1<?php 2/** 3 * PEAR_RunTest 4 * 5 * PHP versions 4 and 5 6 * 7 * LICENSE: This source file is subject to version 3.0 of the PHP license 8 * that is available through the world-wide-web at the following URI: 9 * http://www.php.net/license/3_0.txt. If you did not receive a copy of 10 * the PHP License and are unable to obtain it through the web, please 11 * send a note to license@php.net so we can mail you a copy immediately. 12 * 13 * @category pear 14 * @package PEAR 15 * @author Tomas V.V.Cox <cox@idecnet.com> 16 * @author Greg Beaver <cellog@php.net> 17 * @copyright 1997-2006 The PHP Group 18 * @license http://www.php.net/license/3_0.txt PHP License 3.0 19 * @version CVS: $Id: RunTest.php,v 1.20 2006/02/03 02:08:11 cellog Exp $ 20 * @link http://pear.php.net/package/PEAR 21 * @since File available since Release 1.3.3 22 */ 23 24/** 25 * for error handling 26 */ 27require_once 'PEAR.php'; 28require_once 'PEAR/Config.php'; 29 30define('DETAILED', 1); 31putenv("PHP_PEAR_RUNTESTS=1"); 32 33/** 34 * Simplified version of PHP's test suite 35 * 36 * Try it with: 37 * 38 * $ php -r 'include "../PEAR/RunTest.php"; $t=new PEAR_RunTest; $o=$t->run("./pear_system.phpt");print_r($o);' 39 * 40 * 41 * @category pear 42 * @package PEAR 43 * @author Tomas V.V.Cox <cox@idecnet.com> 44 * @author Greg Beaver <cellog@php.net> 45 * @copyright 1997-2006 The PHP Group 46 * @license http://www.php.net/license/3_0.txt PHP License 3.0 47 * @version Release: 1.4.11 48 * @link http://pear.php.net/package/PEAR 49 * @since Class available since Release 1.3.3 50 */ 51class PEAR_RunTest 52{ 53 var $_logger; 54 var $_options; 55 56 /** 57 * An object that supports the PEAR_Common->log() signature, or null 58 * @param PEAR_Common|null 59 */ 60 function PEAR_RunTest($logger = null, $options = array()) 61 { 62 if (is_null($logger)) { 63 require_once 'PEAR/Common.php'; 64 $logger = new PEAR_Common; 65 } 66 $this->_logger = $logger; 67 $this->_options = $options; 68 } 69 70 // 71 // Run an individual test case. 72 // 73 74 function run($file, $ini_settings = '') 75 { 76 $cwd = getcwd(); 77 $conf = &PEAR_Config::singleton(); 78 $php = $conf->get('php_bin'); 79 if (isset($this->_options['phpunit'])) { 80 $cmd = "$php$ini_settings -f $file"; 81 if (isset($this->_logger)) { 82 $this->_logger->log(2, 'Running command "' . $cmd . '"'); 83 } 84 85 $savedir = getcwd(); // in case the test moves us around 86 chdir(dirname($file)); 87 echo `$cmd`; 88 chdir($savedir); 89 return 'PASSED'; // we have no way of knowing this information so assume passing 90 } 91 //var_dump($php);exit; 92 global $log_format, $info_params, $ini_overwrites; 93 94 $info_params = ''; 95 $log_format = 'LEOD'; 96 97 // Load the sections of the test file. 98 $section_text = array( 99 'TEST' => '(unnamed test)', 100 'SKIPIF' => '', 101 'GET' => '', 102 'ARGS' => '', 103 'CLEAN' => '', 104 ); 105 106 $file = realpath($file); 107 if (!is_file($file) || !$fp = fopen($file, "r")) { 108 return PEAR::raiseError("Cannot open test file: $file"); 109 } 110 111 $section = ''; 112 while (!feof($fp)) { 113 $line = fgets($fp); 114 115 // Match the beginning of a section. 116 if (ereg('^--([A-Z]+)--',$line,$r)) { 117 $section = $r[1]; 118 $section_text[$section] = ''; 119 continue; 120 } elseif (empty($section)) { 121 fclose($fp); 122 return PEAR::raiseError("Invalid sections formats in test file: $file"); 123 } 124 125 // Add to the section text. 126 $section_text[$section] .= $line; 127 } 128 fclose($fp); 129 130 $shortname = str_replace($cwd . DIRECTORY_SEPARATOR, '', $file); 131 if (!isset($this->_options['simple'])) { 132 $tested = trim($section_text['TEST']) . "[$shortname]"; 133 } else { 134 $tested = trim($section_text['TEST']) . ' '; 135 } 136 137 $tmp = realpath(dirname($file)); 138 $tmp_skipif = $tmp . uniqid('/phpt.'); 139 $tmp_file = ereg_replace('\.phpt$','.php',$file); 140 $tmp_post = $tmp . uniqid('/phpt.'); 141 142 @unlink($tmp_skipif); 143 @unlink($tmp_file); 144 @unlink($tmp_post); 145 146 // unlink old test results 147 @unlink(ereg_replace('\.phpt$','.diff',$file)); 148 @unlink(ereg_replace('\.phpt$','.log',$file)); 149 @unlink(ereg_replace('\.phpt$','.exp',$file)); 150 @unlink(ereg_replace('\.phpt$','.out',$file)); 151 152 // Check if test should be skipped. 153 $info = ''; 154 $warn = false; 155 if (array_key_exists('SKIPIF', $section_text)) { 156 if (trim($section_text['SKIPIF'])) { 157 $this->save_text($tmp_skipif, $section_text['SKIPIF']); 158 //$extra = substr(PHP_OS, 0, 3) !== "WIN" ? 159 // "unset REQUEST_METHOD;": ""; 160 161 //$output = `$extra $php $info_params -f $tmp_skipif`; 162 $output = `$php $info_params -f $tmp_skipif`; 163 unlink($tmp_skipif); 164 if (eregi("^skip", trim($output))) { 165 $skipreason = "SKIP $tested"; 166 $reason = (eregi("^skip[[:space:]]*(.+)\$", trim($output))) ? eregi_replace("^skip[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE; 167 if ($reason) { 168 $skipreason .= " (reason: $reason)"; 169 } 170 if (!isset($this->_options['quiet'])) { 171 $this->_logger->log(0, $skipreason); 172 } 173 if (isset($old_php)) { 174 $php = $old_php; 175 } 176 return 'SKIPPED'; 177 } 178 if (eregi("^info", trim($output))) { 179 $reason = (ereg("^info[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^info[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE; 180 if ($reason) { 181 $info = " (info: $reason)"; 182 } 183 } 184 if (eregi("^warn", trim($output))) { 185 $reason = (ereg("^warn[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^warn[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE; 186 if ($reason) { 187 $warn = true; /* only if there is a reason */ 188 $info = " (warn: $reason)"; 189 } 190 } 191 } 192 } 193 194 // We've satisfied the preconditions - run the test! 195 $this->save_text($tmp_file,$section_text['FILE']); 196 197 $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : ''; 198 199 $cmd = "$php$ini_settings -f $tmp_file$args 2>&1"; 200 if (isset($this->_logger)) { 201 $this->_logger->log(2, 'Running command "' . $cmd . '"'); 202 } 203 204 $savedir = getcwd(); // in case the test moves us around 205 if (isset($section_text['RETURNS'])) { 206 ob_start(); 207 system($cmd, $return_value); 208 $out = ob_get_contents(); 209 ob_end_clean(); 210 @unlink($tmp_post); 211 $section_text['RETURNS'] = (int) trim($section_text['RETURNS']); 212 $returnfail = ($return_value != $section_text['RETURNS']); 213 } else { 214 $out = `$cmd`; 215 $returnfail = false; 216 } 217 chdir($savedir); 218 219 if ($section_text['CLEAN']) { 220 // perform test cleanup 221 $this->save_text($clean = $tmp . uniqid('/phpt.'), $section_text['CLEAN']); 222 `$php $clean`; 223 @unlink($clean); 224 } 225 // Does the output match what is expected? 226 $output = trim($out); 227 $output = preg_replace('/\r\n/', "\n", $output); 228 229 if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) { 230 if (isset($section_text['EXPECTF'])) { 231 $wanted = trim($section_text['EXPECTF']); 232 } else { 233 $wanted = trim($section_text['EXPECTREGEX']); 234 } 235 $wanted_re = preg_replace('/\r\n/',"\n",$wanted); 236 if (isset($section_text['EXPECTF'])) { 237 $wanted_re = preg_quote($wanted_re, '/'); 238 // Stick to basics 239 $wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy 240 $wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re); 241 $wanted_re = str_replace("%d", "[0-9]+", $wanted_re); 242 $wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re); 243 $wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?", $wanted_re); 244 $wanted_re = str_replace("%c", ".", $wanted_re); 245 // %f allows two points "-.0.0" but that is the best *simple* expression 246 } 247 /* DEBUG YOUR REGEX HERE 248 var_dump($wanted_re); 249 print(str_repeat('=', 80) . "\n"); 250 var_dump($output); 251 */ 252 if (!$returnfail && preg_match("/^$wanted_re\$/s", $output)) { 253 @unlink($tmp_file); 254 if (!isset($this->_options['quiet'])) { 255 $this->_logger->log(0, "PASS $tested$info"); 256 } 257 if (isset($old_php)) { 258 $php = $old_php; 259 } 260 return 'PASSED'; 261 } 262 263 } else { 264 $wanted = trim($section_text['EXPECT']); 265 $wanted = preg_replace('/\r\n/',"\n",$wanted); 266 // compare and leave on success 267 $ok = (0 == strcmp($output,$wanted)); 268 if (!$returnfail && $ok) { 269 @unlink($tmp_file); 270 if (!isset($this->_options['quiet'])) { 271 $this->_logger->log(0, "PASS $tested$info"); 272 } 273 if (isset($old_php)) { 274 $php = $old_php; 275 } 276 return 'PASSED'; 277 } 278 } 279 280 // Test failed so we need to report details. 281 if ($warn) { 282 $this->_logger->log(0, "WARN $tested$info"); 283 } else { 284 $this->_logger->log(0, "FAIL $tested$info"); 285 } 286 287 if (isset($section_text['RETURNS'])) { 288 $GLOBALS['__PHP_FAILED_TESTS__'][] = array( 289 'name' => $file, 290 'test_name' => $tested, 291 'output' => ereg_replace('\.phpt$','.log', $file), 292 'diff' => ereg_replace('\.phpt$','.diff', $file), 293 'info' => $info, 294 'return' => $return_value 295 ); 296 } else { 297 $GLOBALS['__PHP_FAILED_TESTS__'][] = array( 298 'name' => $file, 299 'test_name' => $tested, 300 'output' => ereg_replace('\.phpt$','.log', $file), 301 'diff' => ereg_replace('\.phpt$','.diff', $file), 302 'info' => $info, 303 ); 304 } 305 306 // write .exp 307 if (strpos($log_format,'E') !== FALSE) { 308 $logname = ereg_replace('\.phpt$','.exp',$file); 309 if (!$log = fopen($logname,'w')) { 310 return PEAR::raiseError("Cannot create test log - $logname"); 311 } 312 fwrite($log,$wanted); 313 fclose($log); 314 } 315 316 // write .out 317 if (strpos($log_format,'O') !== FALSE) { 318 $logname = ereg_replace('\.phpt$','.out',$file); 319 if (!$log = fopen($logname,'w')) { 320 return PEAR::raiseError("Cannot create test log - $logname"); 321 } 322 fwrite($log,$output); 323 fclose($log); 324 } 325 326 // write .diff 327 if (strpos($log_format,'D') !== FALSE) { 328 $logname = ereg_replace('\.phpt$','.diff',$file); 329 if (!$log = fopen($logname,'w')) { 330 return PEAR::raiseError("Cannot create test log - $logname"); 331 } 332 fwrite($log, $this->generate_diff($wanted, $output, 333 isset($section_text['RETURNS']) ? array(trim($section_text['RETURNS']), 334 $return_value) : null)); 335 fclose($log); 336 } 337 338 // write .log 339 if (strpos($log_format,'L') !== FALSE) { 340 $logname = ereg_replace('\.phpt$','.log',$file); 341 if (!$log = fopen($logname,'w')) { 342 return PEAR::raiseError("Cannot create test log - $logname"); 343 } 344 fwrite($log," 345---- EXPECTED OUTPUT 346$wanted 347---- ACTUAL OUTPUT 348$output 349---- FAILED 350"); 351 if ($returnfail) { 352 fwrite($log," 353---- EXPECTED RETURN 354$section_text[RETURNS] 355---- ACTUAL RETURN 356$return_value 357"); 358 } 359 fclose($log); 360 //error_report($file,$logname,$tested); 361 } 362 363 if (isset($old_php)) { 364 $php = $old_php; 365 } 366 367 return $warn ? 'WARNED' : 'FAILED'; 368 } 369 370 function generate_diff($wanted, $output, $return_value) 371 { 372 $w = explode("\n", $wanted); 373 $o = explode("\n", $output); 374 $w1 = array_diff_assoc($w,$o); 375 $o1 = array_diff_assoc($o,$w); 376 $w2 = array(); 377 $o2 = array(); 378 foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1).$val; 379 foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1).$val; 380 $diff = array_merge($w2, $o2); 381 ksort($diff); 382 if ($return_value) { 383 $extra = "##EXPECTED: $return_value[0]\r\n##RETURNED: $return_value[1]"; 384 } else { 385 $extra = ''; 386 } 387 return implode("\r\n", $diff) . $extra; 388 } 389 390 // 391 // Write the given text to a temporary file, and return the filename. 392 // 393 394 function save_text($filename, $text) 395 { 396 if (!$fp = fopen($filename, 'w')) { 397 return PEAR::raiseError("Cannot open file '" . $filename . "' (save_text)"); 398 } 399 fwrite($fp,$text); 400 fclose($fp); 401 if (1 < DETAILED) echo " 402FILE $filename {{{ 403$text 404}}} 405"; 406 } 407 408} 409?> 410