[ Index ]
 

Code source de PHP PEAR 1.4.5

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

title

Body

[fermer]

/PEAR/Command/ -> Test.php (source)

   1  <?php
   2  /**
   3   * PEAR_Command_Test (run-tests)
   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     Stig Bakken <ssb@php.net>
  16   * @author     Martin Jansen <mj@php.net>
  17   * @author     Greg Beaver <cellog@php.net>
  18   * @copyright  1997-2006 The PHP Group
  19   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  20   * @version    CVS: $Id: Test.php,v 1.12 2006/03/28 05:33:42 cellog Exp $
  21   * @link       http://pear.php.net/package/PEAR
  22   * @since      File available since Release 0.1
  23   */
  24  
  25  /**
  26   * base class
  27   */
  28  require_once  'PEAR/Command/Common.php';
  29  
  30  /**
  31   * PEAR commands for login/logout
  32   *
  33   * @category   pear
  34   * @package    PEAR
  35   * @author     Stig Bakken <ssb@php.net>
  36   * @author     Martin Jansen <mj@php.net>
  37   * @author     Greg Beaver <cellog@php.net>
  38   * @copyright  1997-2006 The PHP Group
  39   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  40   * @version    Release: 1.5.0
  41   * @link       http://pear.php.net/package/PEAR
  42   * @since      Class available since Release 0.1
  43   */
  44  
  45  class PEAR_Command_Test extends PEAR_Command_Common
  46  {
  47      // {{{ properties
  48  
  49      var $commands = array(
  50          'run-tests' => array(
  51              'summary' => 'Run Regression Tests',
  52              'function' => 'doRunTests',
  53              'shortcut' => 'rt',
  54              'options' => array(
  55                  'recur' => array(
  56                      'shortopt' => 'r',
  57                      'doc' => 'Run tests in child directories, recursively.  4 dirs deep maximum',
  58                  ),
  59                  'ini' => array(
  60                      'shortopt' => 'i',
  61                      'doc' => 'actual string of settings to pass to php in format " -d setting=blah"',
  62                      'arg' => 'SETTINGS'
  63                  ),
  64                  'realtimelog' => array(
  65                      'shortopt' => 'l',
  66                      'doc' => 'Log test runs/results as they are run',
  67                  ),
  68                  'quiet' => array(
  69                      'shortopt' => 'q',
  70                      'doc' => 'Only display detail for failed tests',
  71                  ),
  72                  'simple' => array(
  73                      'shortopt' => 's',
  74                      'doc' => 'Display simple output for all tests',
  75                  ),
  76                  'package' => array(
  77                      'shortopt' => 'p',
  78                      'doc' => 'Treat parameters as installed packages from which to run tests',
  79                  ),
  80                  'phpunit' => array(
  81                      'shortopt' => 'u',
  82                      'doc' => 'Search parameters for AllTests.php, and use that to run phpunit-based tests',
  83                  ),
  84                  'tapoutput' => array(
  85                      'shortopt' => 't',
  86                      'doc' => 'Output run-tests.log in TAP-compliant format',
  87                  ),
  88              ),
  89              'doc' => '[testfile|dir ...]
  90  Run regression tests with PHP\'s regression testing script (run-tests.php).',
  91              ),
  92          );
  93  
  94      var $output;
  95  
  96      // }}}
  97      // {{{ constructor
  98  
  99      /**
 100       * PEAR_Command_Test constructor.
 101       *
 102       * @access public
 103       */
 104      function PEAR_Command_Test(&$ui, &$config)
 105      {
 106          parent::PEAR_Command_Common($ui, $config);
 107      }
 108  
 109      // }}}
 110      // {{{ doRunTests()
 111  
 112      function doRunTests($command, $options, $params)
 113      {
 114          if (isset($options['phpunit']) && isset($options['tapoutput'])) {
 115              return $this->raiseError('ERROR: cannot use both --phpunit and --tapoutput at the same time');
 116          }
 117          require_once  'PEAR/Common.php';
 118          require_once  'PEAR/RunTest.php';
 119          require_once  'System.php';
 120          $log = new PEAR_Common;
 121          $log->ui = &$this->ui; // slightly hacky, but it will work
 122          $run = new PEAR_RunTest($log, $options);
 123          $tests = array();
 124          if (isset($options['recur'])) {
 125              $depth = 4;
 126          } else {
 127              $depth = 1;
 128          }
 129          if (!count($params)) {
 130              $params[] = '.';
 131          }
 132          if (isset($options['package'])) {
 133              $oldparams = $params;
 134              $params = array();
 135              $reg = &$this->config->getRegistry();
 136              foreach ($oldparams as $param) {
 137                  $pname = $reg->parsePackageName($param, $this->config->get('default_channel'));
 138                  if (PEAR::isError($pname)) {
 139                      return $this->raiseError($pname);
 140                  }
 141                  $package = &$reg->getPackage($pname['package'], $pname['channel']);
 142                  if (!$package) {
 143                      return PEAR::raiseError('Unknown package "' .
 144                          $reg->parsedPackageNameToString($pname) . '"');
 145                  }
 146                  $filelist = $package->getFilelist();
 147                  foreach ($filelist as $name => $atts) {
 148                      if (isset($atts['role']) && $atts['role'] != 'test') {
 149                          continue;
 150                      }
 151                      if (isset($options['phpunit'])) {
 152                          if (!preg_match('/AllTests\.php$/i', $name)) {
 153                              continue;
 154                          }
 155                      } else {
 156                          if (!preg_match('/\.phpt$/', $name)) {
 157                              continue;
 158                          }
 159                      }
 160                      $params[] = $atts['installed_as'];
 161                  }
 162              }
 163          }
 164          foreach ($params as $p) {
 165              if (is_dir($p)) {
 166                  if (isset($options['phpunit'])) {
 167                      $dir = System::find(array($p, '-type', 'f',
 168                                                  '-maxdepth', $depth,
 169                                                  '-name', 'AllTests.php'));
 170                  } else {
 171                      $dir = System::find(array($p, '-type', 'f',
 172                                                  '-maxdepth', $depth,
 173                                                  '-name', '*.phpt'));
 174                  }
 175                  $tests = array_merge($tests, $dir);
 176              } else {
 177                  if (isset($options['phpunit'])) {
 178                      if (!preg_match('/AllTests\.php$/i', $p)) {
 179                          continue;
 180                      }
 181                      $tests[] = $p;
 182                  } else {
 183                      if (!file_exists($p)) {
 184                          if (!preg_match('/\.phpt$/', $p)) {
 185                              $p .= '.phpt';
 186                          }
 187                          $dir = System::find(array(dirname($p), '-type', 'f',
 188                                                      '-maxdepth', $depth,
 189                                                      '-name', $p));
 190                          $tests = array_merge($tests, $dir);
 191                      } else {
 192                          $tests[] = $p;
 193                      }
 194                  }
 195              }
 196          }
 197          $ini_settings = '';
 198          if (isset($options['ini'])) {
 199              $ini_settings .= $options['ini'];
 200          }
 201          if (isset($_ENV['TEST_PHP_INCLUDE_PATH'])) {
 202              $ini_settings .= " -d include_path={$_ENV['TEST_PHP_INCLUDE_PATH']}";
 203          }
 204          if ($ini_settings) {
 205              $this->ui->outputData('Using INI settings: "' . $ini_settings . '"');
 206          }
 207          $skipped = $passed = $failed = array();
 208          $this->ui->outputData('Running ' . count($tests) . ' tests', $command);
 209          $start = time();
 210          if (isset($options['realtimelog'])) {
 211              if (file_exists('run-tests.log')) {
 212                  unlink('run-tests.log');
 213              }
 214          }
 215          if (isset($options['tapoutput'])) {
 216              $tap = '1..' . count($tests) . "\n";
 217          }
 218          $i = 1;
 219          foreach ($tests as $t) {
 220              if (isset($options['realtimelog'])) {
 221                  $fp = @fopen('run-tests.log', 'a');
 222                  if ($fp) {
 223                      fwrite($fp, "Running test $t...");
 224                      fclose($fp);
 225                  }
 226              }
 227              PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
 228              $result = $run->run($t, $ini_settings);
 229              PEAR::staticPopErrorHandling();
 230              if (PEAR::isError($result)) {
 231                  $this->ui->log(0, $result->getMessage());
 232                  continue;
 233              }
 234              if (isset($options['tapoutput'])) {
 235                  $tap .= $result[0] . ' ' . $i . $result[1] . "\n";
 236                  $i++;
 237                  continue;
 238              }
 239              if (isset($options['realtimelog'])) {
 240                  $fp = @fopen('run-tests.log', 'a');
 241                  if ($fp) {
 242                      fwrite($fp, "$result\n");
 243                      fclose($fp);
 244                  }
 245              }
 246              if ($result == 'FAILED') {
 247                  $failed[] = $t;
 248              }
 249              if ($result == 'PASSED') {
 250                  $passed[] = $t;
 251              }
 252              if ($result == 'SKIPPED') {
 253                  $skipped[] = $t;
 254              }
 255          }
 256          $total = date('i:s', time() - $start);
 257          if (isset($options['tapoutput'])) {
 258              $fp = @fopen('run-tests.log', 'w');
 259              if ($fp) {
 260                  fwrite($fp, $tap, strlen($tap));
 261                  fclose($fp);
 262                  $this->ui->outputData('wrote TAP-format log to "' .realpath('run-tests.log') .
 263                      '"', $command);
 264              }
 265          } else {
 266              if (count($failed)) {
 267                  $output = "TOTAL TIME: $total\n";
 268                  $output .= count($passed) . " PASSED TESTS\n";
 269                  $output .= count($skipped) . " SKIPPED TESTS\n";
 270                  $output .= count($failed) . " FAILED TESTS:\n";
 271                  foreach ($failed as $failure) {
 272                      $output .= $failure . "\n";
 273                  }
 274                  if (isset($options['realtimelog'])) {
 275                      $fp = @fopen('run-tests.log', 'a');
 276                  } else {
 277                      $fp = @fopen('run-tests.log', 'w');
 278                  }
 279                  if ($fp) {
 280                      fwrite($fp, $output, strlen($output));
 281                      fclose($fp);
 282                      $this->ui->outputData('wrote log to "' . realpath('run-tests.log') . '"', $command);
 283                  }
 284              } elseif (file_exists('run-tests.log') && !is_dir('run-tests.log')) {
 285                  @unlink('run-tests.log');
 286              }
 287          }
 288          $this->ui->outputData('TOTAL TIME: ' . $total);
 289          $this->ui->outputData(count($passed) . ' PASSED TESTS', $command);
 290          $this->ui->outputData(count($skipped) . ' SKIPPED TESTS', $command);
 291          if (count($failed)) {
 292              $this->ui->outputData(count($failed) . ' FAILED TESTS:', $command);
 293              foreach ($failed as $failure) {
 294                  $this->ui->outputData($failure, $command);
 295              }
 296          }
 297  
 298          return true;
 299      }
 300      // }}}
 301  }
 302  
 303  ?>


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