[ Index ]
 

Code source de PHP PEAR 1.4.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/PEAR/ -> RunTest.php (source)

   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.23 2006/03/28 05:33:42 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   */
  27  require_once  'PEAR.php';
  28  require_once  'PEAR/Config.php';
  29  
  30  define('DETAILED', 1);
  31  putenv("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.5.0
  48   * @link       http://pear.php.net/package/PEAR
  49   * @since      Class available since Release 1.3.3
  50   */
  51  class 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          if (file_exists($tmp_skipif)) {
 143              unlink($tmp_skipif);
 144          }
 145          if (file_exists($tmp_file)) {
 146              unlink($tmp_file);
 147          }
 148          if (file_exists($tmp_post)) {
 149              unlink($tmp_post);
 150          }
 151  
 152          // unlink old test results
 153          if (file_exists(ereg_replace('\.phpt$','.diff',$file))) {
 154              unlink(ereg_replace('\.phpt$','.diff',$file));
 155          }
 156          if (file_exists(ereg_replace('\.phpt$','.log',$file))) {
 157              unlink(ereg_replace('\.phpt$','.log',$file));
 158          }
 159          if (file_exists(ereg_replace('\.phpt$','.exp',$file))) {
 160              unlink(ereg_replace('\.phpt$','.exp',$file));
 161          }
 162          if (file_exists(ereg_replace('\.phpt$','.out',$file))) {
 163              unlink(ereg_replace('\.phpt$','.out',$file));
 164          }
 165  
 166          // Check if test should be skipped.
 167          $info = '';
 168          $warn = false;
 169          if (array_key_exists('SKIPIF', $section_text)) {
 170              if (trim($section_text['SKIPIF'])) {
 171                  $this->save_text($tmp_skipif, $section_text['SKIPIF']);
 172                  //$extra = substr(PHP_OS, 0, 3) !== "WIN" ?
 173                  //    "unset REQUEST_METHOD;": "";
 174  
 175                  //$output = `$extra $php $info_params -f $tmp_skipif`;
 176                  $output = `$php $info_params -f $tmp_skipif`;
 177                  unlink($tmp_skipif);
 178                  if (eregi("^skip", trim($output))) {
 179                      $skipreason = "SKIP $tested";
 180                      $reason = (eregi("^skip[[:space:]]*(.+)\$", trim($output))) ? eregi_replace("^skip[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
 181                      if ($reason) {
 182                          $skipreason .= " (reason: $reason)";
 183                      }
 184                      if (!isset($this->_options['quiet'])) {
 185                          $this->_logger->log(0, $skipreason);
 186                      }
 187                      if (isset($old_php)) {
 188                          $php = $old_php;
 189                      }
 190                      if (isset($this->_options['tapoutput'])) {
 191                          return array('ok', ' # skip ' . $reason);
 192                      }
 193                      return 'SKIPPED';
 194                  }
 195                  if (eregi("^info", trim($output))) {
 196                      $reason = (ereg("^info[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^info[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
 197                      if ($reason) {
 198                          $info = " (info: $reason)";
 199                      }
 200                  }
 201                  if (eregi("^warn", trim($output))) {
 202                      $reason = (ereg("^warn[[:space:]]*(.+)\$", trim($output))) ? ereg_replace("^warn[[:space:]]*(.+)\$", "\\1", trim($output)) : FALSE;
 203                      if ($reason) {
 204                          $warn = true; /* only if there is a reason */
 205                          $info = " (warn: $reason)";
 206                      }
 207                  }
 208              }
 209          }
 210  
 211          // We've satisfied the preconditions - run the test!
 212          $this->save_text($tmp_file,$section_text['FILE']);
 213  
 214          $args = $section_text['ARGS'] ? ' -- '.$section_text['ARGS'] : '';
 215  
 216          $cmd = "$php$ini_settings -f $tmp_file$args 2>&1";
 217          if (isset($this->_logger)) {
 218              $this->_logger->log(2, 'Running command "' . $cmd . '"');
 219          }
 220  
 221          $savedir = getcwd(); // in case the test moves us around
 222          if (isset($section_text['RETURNS'])) {
 223              ob_start();
 224              system($cmd, $return_value);
 225              $out = ob_get_contents();
 226              ob_end_clean();
 227              if (file_exists($tmp_post)) {
 228                  unlink($tmp_post);
 229              }
 230              $section_text['RETURNS'] = (int) trim($section_text['RETURNS']);
 231              $returnfail = ($return_value != $section_text['RETURNS']);
 232          } else {
 233              $out = `$cmd`;
 234              $returnfail = false;
 235          }
 236          chdir($savedir);
 237  
 238          if ($section_text['CLEAN']) {
 239              // perform test cleanup
 240              $this->save_text($clean = $tmp . uniqid('/phpt.'), $section_text['CLEAN']);
 241              `$php $clean`;
 242              if (file_exists($clean)) {
 243                  unlink($clean);
 244              }
 245          }
 246          // Does the output match what is expected?
 247          $output = trim($out);
 248          $output = preg_replace('/\r\n/', "\n", $output);
 249  
 250          if (isset($section_text['EXPECTF']) || isset($section_text['EXPECTREGEX'])) {
 251              if (isset($section_text['EXPECTF'])) {
 252                  $wanted = trim($section_text['EXPECTF']);
 253              } else {
 254                  $wanted = trim($section_text['EXPECTREGEX']);
 255              }
 256              $wanted_re = preg_replace('/\r\n/',"\n",$wanted);
 257              if (isset($section_text['EXPECTF'])) {
 258                  $wanted_re = preg_quote($wanted_re, '/');
 259                  // Stick to basics
 260                  $wanted_re = str_replace("%s", ".+?", $wanted_re); //not greedy
 261                  $wanted_re = str_replace("%i", "[+\-]?[0-9]+", $wanted_re);
 262                  $wanted_re = str_replace("%d", "[0-9]+", $wanted_re);
 263                  $wanted_re = str_replace("%x", "[0-9a-fA-F]+", $wanted_re);
 264                  $wanted_re = str_replace("%f", "[+\-]?\.?[0-9]+\.?[0-9]*(E-?[0-9]+)?", $wanted_re);
 265                  $wanted_re = str_replace("%c", ".", $wanted_re);
 266                  // %f allows two points "-.0.0" but that is the best *simple* expression
 267              }
 268      /* DEBUG YOUR REGEX HERE
 269              var_dump($wanted_re);
 270              print(str_repeat('=', 80) . "\n");
 271              var_dump($output);
 272      */
 273              if (!$returnfail && preg_match("/^$wanted_re\$/s", $output)) {
 274                  if (file_exists($tmp_file)) {
 275                      unlink($tmp_file);
 276                  }
 277                  if (!isset($this->_options['quiet'])) {
 278                      $this->_logger->log(0, "PASS $tested$info");
 279                  }
 280                  if (isset($old_php)) {
 281                      $php = $old_php;
 282                  }
 283                  if (isset($this->_options['tapoutput'])) {
 284                      return array('ok', ' - ' . $tested);
 285                  }
 286                  return 'PASSED';
 287              }
 288  
 289          } else {
 290              $wanted = trim($section_text['EXPECT']);
 291              $wanted = preg_replace('/\r\n/',"\n",$wanted);
 292          // compare and leave on success
 293              $ok = (0 == strcmp($output,$wanted));
 294              if (!$returnfail && $ok) {
 295                  if (file_exists($tmp_file)) {
 296                      unlink($tmp_file);
 297                  }
 298                  if (!isset($this->_options['quiet'])) {
 299                      $this->_logger->log(0, "PASS $tested$info");
 300                  }
 301                  if (isset($old_php)) {
 302                      $php = $old_php;
 303                  }
 304                  if (isset($this->_options['tapoutput'])) {
 305                      return array('ok', ' - ' . $tested);
 306                  }
 307                  return 'PASSED';
 308              }
 309          }
 310  
 311          // Test failed so we need to report details.
 312          if ($warn) {
 313              $this->_logger->log(0, "WARN $tested$info");
 314          } else {
 315              $this->_logger->log(0, "FAIL $tested$info");
 316          }
 317  
 318          if (isset($section_text['RETURNS'])) {
 319              $GLOBALS['__PHP_FAILED_TESTS__'][] = array(
 320                              'name' => $file,
 321                              'test_name' => $tested,
 322                              'output' => ereg_replace('\.phpt$','.log', $file),
 323                              'diff'   => ereg_replace('\.phpt$','.diff', $file),
 324                              'info'   => $info,
 325                              'return' => $return_value
 326                              );
 327          } else {
 328              $GLOBALS['__PHP_FAILED_TESTS__'][] = array(
 329                              'name' => $file,
 330                              'test_name' => $tested,
 331                              'output' => ereg_replace('\.phpt$','.log', $file),
 332                              'diff'   => ereg_replace('\.phpt$','.diff', $file),
 333                              'info'   => $info,
 334                              );
 335          }
 336  
 337          // write .exp
 338          if (strpos($log_format,'E') !== FALSE) {
 339              $logname = ereg_replace('\.phpt$','.exp',$file);
 340              if (!$log = fopen($logname,'w')) {
 341                  return PEAR::raiseError("Cannot create test log - $logname");
 342              }
 343              fwrite($log,$wanted);
 344              fclose($log);
 345          }
 346  
 347          // write .out
 348          if (strpos($log_format,'O') !== FALSE) {
 349              $logname = ereg_replace('\.phpt$','.out',$file);
 350              if (!$log = fopen($logname,'w')) {
 351                  return PEAR::raiseError("Cannot create test log - $logname");
 352              }
 353              fwrite($log,$output);
 354              fclose($log);
 355          }
 356  
 357          // write .diff
 358          if (strpos($log_format,'D') !== FALSE) {
 359              $logname = ereg_replace('\.phpt$','.diff',$file);
 360              if (!$log = fopen($logname,'w')) {
 361                  return PEAR::raiseError("Cannot create test log - $logname");
 362              }
 363              fwrite($log, $this->generate_diff($wanted, $output,
 364                  isset($section_text['RETURNS']) ? array(trim($section_text['RETURNS']),
 365                      $return_value) : null));
 366              fclose($log);
 367          }
 368  
 369          // write .log
 370          if (strpos($log_format,'L') !== FALSE) {
 371              $logname = ereg_replace('\.phpt$','.log',$file);
 372              if (!$log = fopen($logname,'w')) {
 373                  return PEAR::raiseError("Cannot create test log - $logname");
 374              }
 375              fwrite($log,"
 376  ---- EXPECTED OUTPUT
 377  $wanted
 378  ---- ACTUAL OUTPUT
 379  $output
 380  ---- FAILED
 381  ");
 382              if ($returnfail) {
 383                  fwrite($log,"
 384  ---- EXPECTED RETURN
 385  $section_text[RETURNS]
 386  ---- ACTUAL RETURN
 387  $return_value
 388  ");
 389              }
 390              fclose($log);
 391              //error_report($file,$logname,$tested);
 392          }
 393  
 394          if (isset($old_php)) {
 395              $php = $old_php;
 396          }
 397  
 398          if (isset($this->_options['tapoutput'])) {
 399              $wanted = explode("\n", $wanted);
 400              $wanted = "# Expected output:\n#\n#" . implode("\n#", $wanted);
 401              $output = explode("\n", $output);
 402              $output = "#\n#\n# Actual output:\n#\n#" . implode("\n#", $output);
 403              return array($wanted . $output . 'not ok', ' - ' . $tested);
 404          }
 405          return $warn ? 'WARNED' : 'FAILED';
 406      }
 407  
 408      function generate_diff($wanted, $output, $return_value)
 409      {
 410          $w = explode("\n", $wanted);
 411          $o = explode("\n", $output);
 412          $w1 = array_diff_assoc($w,$o);
 413          $o1 = array_diff_assoc($o,$w);
 414          $w2 = array();
 415          $o2 = array();
 416          foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1).$val;
 417          foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1).$val;
 418          $diff = array_merge($w2, $o2);
 419          ksort($diff);
 420          if ($return_value) {
 421              $extra = "##EXPECTED: $return_value[0]\r\n##RETURNED: $return_value[1]";
 422          } else {
 423              $extra = '';
 424          }
 425          return implode("\r\n", $diff) . $extra;
 426      }
 427  
 428      //
 429      //  Write the given text to a temporary file, and return the filename.
 430      //
 431  
 432      function save_text($filename, $text)
 433      {
 434          if (!$fp = fopen($filename, 'w')) {
 435              return PEAR::raiseError("Cannot open file '" . $filename . "' (save_text)");
 436          }
 437          fwrite($fp,$text);
 438          fclose($fp);
 439      if (1 < DETAILED) echo "
 440  FILE $filename {{{
 441  $text
 442  }}}
 443  ";
 444      }
 445  
 446  }
 447  ?>


Généré le : Sun Feb 25 14:08:00 2007 par Balluche grâce à PHPXref 0.7