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

Source for file TestRunner.php

Documentation is available at TestRunner.php

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit2                                                       |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: TestRunner.php,v 1.9.2.5 2004/07/19 13:18:41 sebastian Exp $
  16. //
  17.  
  18. if (!defined('PHPUnit2_MAIN_METHOD')) {
  19.     define('PHPUnit2_MAIN_METHOD''PHPUnit2_TextUI_TestRunner::main');
  20. }
  21.  
  22. require_once 'Console/Getopt.php';
  23. require_once 'PHPUnit2/Framework/AssertionFailedError.php';
  24. require_once 'PHPUnit2/Framework/Test.php';
  25. require_once 'PHPUnit2/Framework/TestResult.php';
  26. require_once 'PHPUnit2/Runner/BaseTestRunner.php';
  27. require_once 'PHPUnit2/Runner/Version.php';
  28. require_once 'PHPUnit2/TextUI/ResultPrinter.php';
  29. @include_once 'Benchmark/Timer.php';
  30.  
  31. /**
  32.  * A TestRunner for the Command Line Interface (CLI)
  33.  * PHP SAPI Module.
  34.  *
  35.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  36.  * @copyright   Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
  37.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  38.  * @category    PHP
  39.  * @package     PHPUnit2
  40.  * @subpackage  TextUI
  41.  */
  42.     // {{{ Constants
  43.  
  44.     const SUCCESS_EXIT   = 0;
  45.     const FAILURE_EXIT   = 1;
  46.     const EXCEPTION_EXIT = 2;
  47.  
  48.     // }}}
  49.     // {{{ Members
  50.  
  51.     /**
  52.     * @var    PHPUnit2_TextUI_ResultPrinter 
  53.     * @access private
  54.     */
  55.     private $printer = null;
  56.  
  57.     // }}}
  58.     // {{{ public static function main()
  59.  
  60.     /**
  61.     * @access public
  62.     * @static
  63.     */
  64.     public static function main({
  65.         $aTestRunner = new PHPUnit2_TextUI_TestRunner;
  66.  
  67.         try {
  68.             $result $aTestRunner->start($_SERVER['argv']);
  69.  
  70.             if (!$result->wasSuccessful()) {
  71.                 exit(self::FAILURE_EXIT);
  72.             }
  73.  
  74.             exit(self::SUCCESS_EXIT);
  75.         }
  76.  
  77.         catch (Exception $e{
  78.             print $e->getMessage();
  79.             exit(self::EXCEPTION_EXIT);
  80.         }
  81.     }
  82.  
  83.     // }}}
  84.     // {{{ protected function start($arguments)
  85.  
  86.     /**
  87.     * @param  array $arguments 
  88.     * @access protected
  89.     */
  90.     protected function start($arguments{
  91.         $wait = false;
  92.  
  93.         $possibleOptions = array(
  94.           'help',
  95.           'version',
  96.           'wait'
  97.         );
  98.  
  99.         $options = Console_Getopt::getopt(
  100.           $arguments,
  101.           '',
  102.           $possibleOptions
  103.         );
  104.  
  105.         foreach ($options[0as $option{
  106.             switch ($option[0]{
  107.                 case '--help'{
  108.                     $this->showHelp();
  109.                     exit(self::SUCCESS_EXIT);
  110.                 }
  111.                 break;
  112.  
  113.                 case '--version'{
  114.                     print PHPUnit2_Runner_Version::getVersionString("\n";
  115.                     exit(self::SUCCESS_EXIT);
  116.                 }
  117.                 break;
  118.  
  119.                 case '--wait'{
  120.                     $wait = true;
  121.                 }
  122.                 break;
  123.             }
  124.         }
  125.  
  126.         $test = isset($options[1][0]$options[1][0: false;
  127.  
  128.         if ($test === false{
  129.             $this->showHelp();
  130.  
  131.             exit(self::SUCCESS_EXIT);
  132.         }
  133.  
  134.         try {
  135.                   return $this->doRun(
  136.                     $this->getTest($test),
  137.                     $wait
  138.                   );
  139.         }
  140.  
  141.         catch (Exception $e{
  142.             throw new Exception(
  143.               'Could not create and run test suite: ' $e->getMessage()
  144.             );
  145.         }
  146.     }
  147.  
  148.     // }}}
  149.     // {{{ public static function run($test)
  150.  
  151.     /**
  152.     * @param  mixed $test 
  153.     * @access public
  154.     * @static
  155.     */
  156.       public static function run($test{
  157.         if ($test instanceof ReflectionClass{
  158.                 self::run(new PHPUnit2_Framework_TestSuite($testClass));
  159.             }
  160.  
  161.         else if ($test instanceof PHPUnit2_Framework_Test{
  162.             $aTestRunner = new PHPUnit2_TextUI_TestRunner;
  163.  
  164.             return $aTestRunner->doRun($test);
  165.         }
  166.       }
  167.  
  168.     // }}}
  169.     // {{{ public static function runAndWait(PHPUnit2_Framework_Test $suite)
  170.  
  171.     /**
  172.     * @param  PHPUnit2_Framework_Test $suite 
  173.     * @access public
  174.     * @static
  175.     */
  176.       public static function runAndWait(PHPUnit2_Framework_Test $suite{
  177.             $aTestRunner = new PHPUnit2_TextUI_TestRunner;
  178.             $aTestRunner->doRun($suitetrue);
  179.       }
  180.  
  181.     // }}}
  182.     // {{{ protected TestResult createTestResult()
  183.  
  184.     /**
  185.     * @return PHPUnit2_Framework_TestResult 
  186.     * @access protected
  187.     */
  188.       protected function createTestResult({
  189.             return new PHPUnit2_Framework_TestResult;
  190.       }
  191.     
  192.     // }}}
  193.     // {{{ public function doRun(PHPUnit2_Framework_Test $suite, $wait = false)
  194.  
  195.     /**
  196.     * @param  PHPUnit2_Framework_Test $suite 
  197.     * @param  boolean                 $wait 
  198.     * @return PHPUnit2_Framework_TestResult 
  199.     * @access public
  200.     */
  201.       public function doRun(PHPUnit2_Framework_Test $suite$wait = false{
  202.             $result $this->createTestResult();
  203.  
  204.         if ($this->printer === null{
  205.             $this->printer = new PHPUnit2_TextUI_ResultPrinter;
  206.         }
  207.  
  208.         $this->printer->write(
  209.           PHPUnit2_Runner_Version::getVersionString("\n\n"
  210.         );
  211.  
  212.             $result->addListener($this->printer);
  213.  
  214.         if (class_exists('Benchmark_Timer')) {
  215.             $timer = new Benchmark_Timer;
  216.         }
  217.  
  218.         if (isset($timer)) {
  219.             $timer->start();
  220.         }
  221.  
  222.         $suite->run($result);
  223.  
  224.         if (isset($timer)) {
  225.             $timer->stop();
  226.             $timeElapsed $timer->timeElapsed();
  227.         else {
  228.             $timeElapsed = false;
  229.         }
  230.  
  231.         $this->pause($wait);
  232.         $this->printer->printResult($result$timeElapsed);
  233.  
  234.             return $result;
  235.       }
  236.  
  237.     // }}}
  238.     // {{{ public function showHelp()
  239.  
  240.     /**
  241.     * @access public
  242.     */
  243.     public function showHelp({
  244.         print PHPUnit2_Runner_Version::getVersionString("\n\n" .
  245.               "Usage: phpunit [switches] UnitTest\n" .
  246.               "  --wait             Waits for a keystroke after each test.\n" .
  247.               "  --help             Prints this usage information.\n" .
  248.               "  --version          Prints the version and exits.\n";
  249.     }
  250.  
  251.     // }}}
  252.     // {{{ protected function pause($wait)
  253.  
  254.     /**
  255.     * @param  boolean $wait 
  256.     * @access protected
  257.     */
  258.     protected function pause($wait{
  259.         if (!$wait{
  260.             return;
  261.         }
  262.  
  263.         $this->printer->printWaitPrompt();
  264.  
  265.         fgets(STDIN);
  266.     }
  267.  
  268.     // }}}
  269.     // {{{ public function testEnded($testName)
  270.  
  271.     /**
  272.     * A test ended.
  273.     *
  274.     * @param  string  $testName 
  275.     * @access public
  276.     */
  277.     public function testEnded($testName{
  278.     }
  279.  
  280.     // }}}
  281.     // {{{ public function testFailed($status, PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e)
  282.  
  283.     /**
  284.     * A test failed.
  285.     *
  286.     * @param  integer                                 $status 
  287.     * @param  PHPUnit2_Framework_Test                 $test 
  288.     * @param  PHPUnit2_Framework_AssertionFailedError $e 
  289.     * @access public
  290.     */
  291.     public function testFailed($statusPHPUnit2_Framework_Test $testPHPUnit2_Framework_AssertionFailedError $e{
  292.     }
  293.  
  294.     // }}}
  295.     // {{{ public function testStarted($testName)
  296.  
  297.     /**
  298.     * A test started.
  299.     *
  300.     * @param  string  $testName 
  301.     * @access public
  302.     */
  303.     public function testStarted($testName{
  304.     }
  305.  
  306.     // }}}
  307.     // {{{ protected function runFailed($message)
  308.  
  309.     /**
  310.     * Override to define how to handle a failed loading of
  311.     * a test suite.
  312.     *
  313.     * @param  string  $message 
  314.     * @access protected
  315.     */
  316.     protected function runFailed($message{
  317.         print $message;
  318.         exit(self::FAILURE_EXIT);
  319.     }
  320.  
  321.     // }}}
  322.     // {{{ public function setPrinter(PHPUnit2_TextUI_ResultPrinter $resultPrinter)
  323.  
  324.     /**
  325.     * @param  PHPUnit2_TextUI_ResultPrinter $resultPrinter 
  326.     * @access public
  327.     */
  328.     public function setPrinter(PHPUnit2_TextUI_ResultPrinter $resultPrinter{
  329.         $this->printer $resultPrinter;
  330.     }
  331.  
  332.     // }}}
  333. }
  334.  
  335. if (PHPUnit2_MAIN_METHOD == 'PHPUnit2_TextUI_TestRunner::main'{
  336. }
  337.  
  338. /*
  339.  * vim600:  et sw=2 ts=2 fdm=marker
  340.  * vim<600: et sw=2 ts=2
  341.  */
  342. ?>

Documentation generated on Mon, 11 Mar 2019 13:55:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.