PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file Test.php

Documentation is available at Test.php

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

Documentation generated on Wed, 06 Jul 2011 23:31:25 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.