[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/tests_runner/src/ -> lmbTestShellUI.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  require_once(dirname(__FILE__) . '/lmbTestGetopt.class.php');
  10  require_once(dirname(__FILE__) . '/lmbTestRunner.class.php');
  11  require_once(dirname(__FILE__) . '/lmbTestTreeGlobNode.class.php');
  12  
  13  /**

  14   * class lmbTestShellUI.

  15   *

  16   * @package tests_runner

  17   * @version $Id: lmbTestShellUI.class.php 6021 2007-06-28 13:18:44Z pachanga $

  18   */
  19  class lmbTestShellUI
  20  {
  21    protected $test_path;
  22    protected $argv;
  23    protected $posix_opts = true;
  24    protected $call_exit = true;
  25    protected $reporter;
  26  
  27    function __construct($argv = null)
  28    {
  29      try
  30      {
  31        $this->argv = is_array($argv) ? $argv : lmbTestGetopt::readPHPArgv();
  32      }
  33      catch(Exception $e)
  34      {
  35        $this->_error($e->getMessage() . "\n");
  36      }
  37    }
  38  
  39    function setReporter($reporter)
  40    {
  41      $this->reporter = $reporter;
  42    }
  43  
  44    function setPosixMode($flag = true)
  45    {
  46      $this->posix_opts = $flag;
  47    }
  48  
  49    function exitAfterRun($flag = true)
  50    {
  51      $this->call_exit = $flag;
  52    }
  53  
  54    function help($script = '')
  55    {
  56      $version = $this->_getVersion();
  57  
  58      $usage = <<<EOD
  59  
  60  $version
  61  
  62  Usage:
  63    limb_unit OPTIONS <file|dir> [<file|dir>, <file|dir>, ...]
  64    Advanced SimpleTest unit tests runner. Finds and executes unit tests within filesystem.
  65  Options:
  66    -c, --config=/file.php        PHP configuration file path
  67    -h, --help                    Displays this help and exit
  68    --cover=path1;path2           Sets paths delimitered with ';' which should be analyzed for coverage
  69    --cover-report=dir            Sets coverage report directory
  70    --cover-exclude=path1;path2   Sets paths delimitered with ';' which should be excluded from coverage analysis
  71  
  72  EOD;
  73      return $usage;
  74    }
  75  
  76    protected function _help($code = 0)
  77    {
  78      echo $this->help();
  79      exit($code);
  80    }
  81  
  82    protected function _error($message, $code = 1)
  83    {
  84      echo "ERROR: $message";
  85      echo $this->help();
  86      exit($code);
  87    }
  88  
  89    protected function _version()
  90    {
  91      echo $this->_getVersion();
  92      exit();
  93    }
  94  
  95    protected function _getVersion()
  96    {
  97      list(, $number, $status) = explode('-', trim(file_get_contents(dirname(__FILE__) . '/../VERSION')));
  98      return "limb_unit-$number-$status";
  99    }
 100  
 101    static function getShortOpts()
 102    {
 103      return 'hvt:b:c:';
 104    }
 105  
 106    static function getLongOpts()
 107    {
 108      return array('help', 'version', 'config=', 'cover=', 'cover-report=', 'cover-exclude=');
 109    }
 110  
 111    function run()
 112    {
 113      $res = $this->_doRun();
 114  
 115      if($this->call_exit)
 116        exit($res ? 0 : 1);
 117      else
 118        return $res;
 119    }
 120  
 121    function runEmbedded()
 122    {
 123      return $this->_doRun();
 124    }
 125  
 126    protected function _doRun()
 127    {
 128      $short_opts = self :: getShortOpts();
 129      $long_opts = self :: getLongOpts();
 130  
 131      try
 132      {
 133        if($this->posix_opts)
 134          $options = lmbTestGetopt :: getopt($this->argv, $short_opts, $long_opts);
 135        else
 136          $options = lmbTestGetopt :: getopt2($this->argv, $short_opts, $long_opts);
 137      }
 138      catch(Exception $e)
 139      {
 140        $this->_help(1);
 141      }
 142  
 143      lmbTestGetopt :: defineConstants($this->argv);
 144  
 145      $configured = false;
 146      $cover_include = '';
 147      $cover_exclude = '';
 148      $cover_report_dir = null;
 149  
 150      foreach($options[0] as $option)
 151      {
 152        switch($option[0])
 153        {
 154          case 'h':
 155          case '--help':
 156            $this->_help(0);
 157            break;
 158          case 'v':
 159          case '--version':
 160            $this->_version();
 161            break;
 162          case 'c':
 163          case '--config':
 164            if(!@include_once(realpath($option[1])))
 165              $this->_error("Could not include configuration file '{$option[1]}'\n");
 166            $configured = true;
 167            break;
 168          case '--cover':
 169            $cover_include = $option[1];
 170            break;
 171          case '--cover-report':
 172            $cover_report_dir = $option[1];
 173            break;
 174          case '--cover-exclude':
 175            $cover_exclude = $option[1];
 176            break;
 177        }
 178      }
 179  
 180      if(!$configured && $config = getenv('LIMB_TESTS_RUNNER_CONFIG'))
 181        include_once($config);
 182  
 183      if(!is_array($options[1]))
 184        $this->_help(1);
 185  
 186      if(!$cover_report_dir && defined('LIMB_TESTS_RUNNER_COVERAGE_REPORT_DIR'))
 187        $cover_report_dir = LIMB_TESTS_RUNNER_COVERAGE_REPORT_DIR;
 188  
 189      $runner = new lmbTestRunner();
 190  
 191      if($this->reporter)
 192        $runner->setReporter($this->reporter);
 193  
 194      if($cover_include)
 195        $runner->useCoverage($cover_include, $cover_exclude, $cover_report_dir);
 196  
 197      try
 198      {
 199        $node = new lmbTestTreeGlobNode($options[1]);
 200        $res = $runner->run($node);
 201      }
 202      catch(Exception $e)
 203      {
 204        $this->_error($e->getMessage());
 205      }
 206  
 207      echo $runner->getRuntime() . " sec.\n";
 208  
 209      return $res;
 210    }
 211  }
 212  
 213  ?>


Generated: Tue Dec 2 03:54:09 2008 Cross-referenced by PHPXref 0.7