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.33.2.5 2004/11/02 16:26:28 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 'Benchmark/Timer.php';
  24.  
  25. require_once 'PHPUnit2/Extensions/CodeCoverage/Renderer.php';
  26. require_once 'PHPUnit2/Extensions/Log/XML.php';
  27. require_once 'PHPUnit2/Extensions/TestDox/ResultPrinter.php';
  28. require_once 'PHPUnit2/Framework/AssertionFailedError.php';
  29. require_once 'PHPUnit2/Framework/Test.php';
  30. require_once 'PHPUnit2/Framework/TestResult.php';
  31. require_once 'PHPUnit2/Runner/BaseTestRunner.php';
  32. require_once 'PHPUnit2/Runner/Version.php';
  33. require_once 'PHPUnit2/TextUI/ResultPrinter.php';
  34. require_once 'PHPUnit2/Util/Skeleton.php';
  35.  
  36. /**
  37.  * A TestRunner for the Command Line Interface (CLI)
  38.  * PHP SAPI Module.
  39.  *
  40.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  41.  * @copyright   Copyright &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
  42.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  43.  * @category    Testing
  44.  * @package     PHPUnit2
  45.  * @subpackage  TextUI
  46.  */
  47.     // {{{ Constants
  48.  
  49.     const SUCCESS_EXIT   = 0;
  50.     const FAILURE_EXIT   = 1;
  51.     const EXCEPTION_EXIT = 2;
  52.  
  53.     // }}}
  54.     // {{{ Members
  55.  
  56.     /**
  57.     * @var    PHPUnit2_TextUI_ResultPrinter 
  58.     * @access private
  59.     */
  60.     private $printer = NULL;
  61.  
  62.     // }}}
  63.     // {{{ public static function main()
  64.  
  65.     /**
  66.     * @access public
  67.     * @static
  68.     */
  69.     public static function main({
  70.         $aTestRunner = new PHPUnit2_TextUI_TestRunner;
  71.  
  72.         try {
  73.             $result $aTestRunner->start($_SERVER['argv']);
  74.  
  75.             if (!$result->wasSuccessful()) {
  76.                 exit(self::FAILURE_EXIT);
  77.             }
  78.  
  79.             exit(self::SUCCESS_EXIT);
  80.         }
  81.  
  82.         catch (Exception $e{
  83.             print $e->getMessage();
  84.             exit(self::EXCEPTION_EXIT);
  85.         }
  86.     }
  87.  
  88.     // }}}
  89.     // {{{ protected function start($arguments)
  90.  
  91.     /**
  92.     * @param  array $arguments 
  93.     * @access protected
  94.     */
  95.     protected function start($arguments{
  96.         $coverageDataFile = FALSE;
  97.         $coverageHTMLFile = FALSE;
  98.         $coverageTextFile = FALSE;
  99.         $testdoxHTMLFile  = FALSE;
  100.         $testdoxTextFile  = FALSE;
  101.         $xmlLogfile       = FALSE;
  102.         $wait             = FALSE;
  103.  
  104.         $possibleOptions = array(
  105.           'help',
  106.           'log-xml=',
  107.           'skeleton',
  108.           'testdox-html=',
  109.           'testdox-text=',
  110.           'version',
  111.           'wait'
  112.         );
  113.  
  114.         if (extension_loaded('xdebug')) {
  115.             $possibleOptions['coverage-data=';
  116.             $possibleOptions['coverage-html=';
  117.             $possibleOptions['coverage-text=';
  118.         }
  119.  
  120.         $options = Console_Getopt::getopt(
  121.           $arguments,
  122.           '',
  123.           $possibleOptions
  124.         );
  125.  
  126.         if (PEAR::isError($options)) {
  127.             print PHPUnit2_Runner_Version::getVersionString("\n\n";
  128.             print $options->getMessage("\n";
  129.  
  130.             exit(self::FAILURE_EXIT);
  131.         }
  132.  
  133.         $test = isset($options[1][0]$options[1][0: FALSE;
  134.  
  135.         foreach ($options[0as $option{
  136.             switch ($option[0]{
  137.                 case '--coverage-data'{
  138.                     $coverageDataFile $option[1];
  139.                 }
  140.                 break;
  141.  
  142.                 case '--coverage-html'{
  143.                     $coverageHTMLFile $option[1];
  144.                 }
  145.                 break;
  146.  
  147.                 case '--coverage-text'{
  148.                     $coverageTextFile $option[1];
  149.                 }
  150.                 break;
  151.  
  152.                 case '--help'{
  153.                     $this->showHelp();
  154.                     exit(self::SUCCESS_EXIT);
  155.                 }
  156.                 break;
  157.  
  158.                 case '--testdox-html'{
  159.                     $testdoxHTMLFile $option[1];
  160.                 }
  161.                 break;
  162.  
  163.                 case '--testdox-text'{
  164.                     $testdoxTextFile $option[1];
  165.                 }
  166.                 break;
  167.  
  168.                 case '--log-xml'{
  169.                     $xmlLogfile $option[1];
  170.                 }
  171.                 break;
  172.  
  173.                 case '--skeleton'{
  174.                     if ($test !== FALSE{
  175.                         print PHPUnit2_Runner_Version::getVersionString("\n\n";
  176.  
  177.                         try {
  178.                             $skeleton = new PHPUnit2_Util_Skeleton($test);
  179.                             $skeleton->write();
  180.  
  181.                             printf(
  182.                               "Wrote test class skeleton for %s to %s.\n",
  183.                               $test,
  184.                               $test 'Test.php'
  185.                             );
  186.  
  187.                             exit(self::SUCCESS_EXIT);
  188.                         }
  189.  
  190.                         catch (Exception $e{
  191.                             printf(
  192.                               "Could not write test class skeleton for %s to %s.\n",
  193.                               $test,
  194.                               $test 'Test.php'
  195.                             );
  196.  
  197.                             exit(self::FAILURE_EXIT);
  198.                         }
  199.                     }
  200.                 }
  201.                 break;
  202.  
  203.                 case '--version'{
  204.                     print PHPUnit2_Runner_Version::getVersionString("\n";
  205.                     exit(self::SUCCESS_EXIT);
  206.                 }
  207.                 break;
  208.  
  209.                 case '--wait'{
  210.                     $wait = TRUE;
  211.                 }
  212.                 break;
  213.             }
  214.         }
  215.  
  216.         if ($test === FALSE{
  217.             $this->showHelp();
  218.  
  219.             exit(self::SUCCESS_EXIT);
  220.         }
  221.  
  222.         try {
  223.             return $this->doRun(
  224.               $this->getTest($test),
  225.               $coverageDataFile,
  226.               $coverageHTMLFile,
  227.               $coverageTextFile,
  228.               $testdoxHTMLFile,
  229.               $testdoxTextFile,
  230.               $xmlLogfile,
  231.               $wait
  232.             );
  233.         }
  234.  
  235.         catch (Exception $e{
  236.             throw new Exception(
  237.               'Could not create and run test suite: ' $e->getMessage()
  238.             );
  239.         }
  240.     }
  241.  
  242.     // }}}
  243.     // {{{ public static function run($test, $coverageDataFile = FALSE, $coverageHTMLFile = FALSE, $coverageTextFile = FALSE, $testdoxHTMLFile = FALSE, $testdoxTextFile = FALSE, $xmlLogfile = FALSE, $wait = FALSE)
  244.  
  245.     /**
  246.     * @param  mixed   $test 
  247.     * @param  mixed   $coverageDataFile 
  248.     * @param  mixed   $testdoxHTMLFile 
  249.     * @param  mixed   $testdoxTextFile 
  250.     * @param  mixed   $xmlLogfile 
  251.     * @param  boolean $wait 
  252.     * @access public
  253.     * @static
  254.     */
  255.     public static function run($test$coverageDataFile = FALSE$coverageHTMLFile = FALSE$coverageTextFile = FALSE$testdoxHTMLFile = FALSE$testdoxTextFile = FALSE$xmlLogfile = FALSE$wait = FALSE{
  256.         if ($test instanceof ReflectionClass{
  257.             $test = new PHPUnit2_Framework_TestSuite($test);
  258.         }
  259.  
  260.         if ($test instanceof PHPUnit2_Framework_Test{
  261.             $aTestRunner = new PHPUnit2_TextUI_TestRunner;
  262.  
  263.             return $aTestRunner->doRun(
  264.               $test,
  265.               $coverageDataFile,
  266.               $coverageHTMLFile,
  267.               $coverageTextFile,
  268.               $testdoxHTMLFile,
  269.               $testdoxTextFile,
  270.               $xmlLogfile,
  271.               $wait
  272.             );
  273.         }
  274.     }
  275.  
  276.     // }}}
  277.     // {{{ public static function runAndWait(PHPUnit2_Framework_Test $suite)
  278.  
  279.     /**
  280.     * Runs a single test and waits until the user types RETURN.
  281.     *
  282.     * @param  PHPUnit2_Framework_Test $suite 
  283.     * @access public
  284.     * @static
  285.     */
  286.     public static function runAndWait(PHPUnit2_Framework_Test $suite{
  287.         $aTestRunner = new PHPUnit2_TextUI_TestRunner;
  288.  
  289.         $aTestRunner->doRun(
  290.           $suite,
  291.           FALSE,
  292.           FALSE,
  293.           FALSE,
  294.           FALSE,
  295.           FALSE,
  296.           FALSE,
  297.           TRUE
  298.         );
  299.     }
  300.  
  301.     // }}}
  302.     // {{{ protected TestResult createTestResult()
  303.  
  304.     /**
  305.     * @return PHPUnit2_Framework_TestResult 
  306.     * @access protected
  307.     */
  308.     protected function createTestResult({
  309.         return new PHPUnit2_Framework_TestResult;
  310.     }
  311.  
  312.     // }}}
  313.     // {{{ public function doRun(PHPUnit2_Framework_Test $suite, $coverageDataFile = FALSE, $coverageHTMLFile = FALSE, $coverageTextFile = FALSE, $testdoxHTMLFile = FALSE, $testdoxTextFile = FALSE, $xmlLogfile = FALSE, $wait = FALSE)
  314.  
  315.     /**
  316.     * @param  PHPUnit2_Framework_Test $suite 
  317.     * @param  mixed                   $coverageDataFile 
  318.     * @param  mixed                   $coverageHTMLFile 
  319.     * @param  mixed                   $coverageTextFile 
  320.     * @param  mixed                   $testdoxHTMLFile 
  321.     * @param  mixed                   $testdoxTextFile 
  322.     * @param  mixed                   $xmlLogfile 
  323.     * @param  boolean                 $wait 
  324.     * @return PHPUnit2_Framework_TestResult 
  325.     * @access public
  326.     */
  327.     public function doRun(PHPUnit2_Framework_Test $suite$coverageDataFile = FALSE$coverageHTMLFile = FALSE$coverageTextFile = FALSE$testdoxHTMLFile = FALSE$testdoxTextFile = FALSE$xmlLogfile = FALSE$wait = FALSE{
  328.         $result $this->createTestResult();
  329.         $timer  = new Benchmark_Timer;
  330.  
  331.         if ($this->printer === NULL{
  332.             $this->printer = new PHPUnit2_TextUI_ResultPrinter;
  333.         }
  334.  
  335.         $this->printer->write(
  336.           PHPUnit2_Runner_Version::getVersionString("\n\n"
  337.         );
  338.  
  339.         $result->addListener($this->printer);
  340.  
  341.         if ($testdoxHTMLFile !== FALSE{
  342.             $result->addListener(
  343.                 'HTML',
  344.                 $testdoxHTMLFile
  345.               )
  346.             );
  347.         }
  348.  
  349.         if ($testdoxTextFile !== FALSE{
  350.             $result->addListener(
  351.                 'Text',
  352.                 $testdoxTextFile
  353.               )
  354.             );
  355.         }
  356.  
  357.         if ($xmlLogfile !== FALSE{
  358.             $result->addListener(
  359.               new PHPUnit2_Extensions_Log_XML($xmlLogfile)
  360.             );
  361.         }
  362.  
  363.         $timer->start();
  364.         $suite->run($result);
  365.         $timer->stop();
  366.         $timeElapsed $timer->timeElapsed();
  367.  
  368.         $this->pause($wait);
  369.  
  370.         $this->printer->printResult($result$timeElapsed);
  371.  
  372.         $this->handleCodeCoverageInformation(
  373.           $result,
  374.           $coverageDataFile,
  375.           $coverageHTMLFile,
  376.           $coverageTextFile
  377.         );
  378.  
  379.         return $result;
  380.     }
  381.  
  382.     // }}}
  383.     // {{{ protected function handleCodeCoverageInformation(PHPUnit2_Framework_TestResult $result, $coverageDataFile, $coverageHTMLFile, $coverageTextFile)
  384.  
  385.     /**
  386.     * @param  PHPUnit2_Framework_TestResult $result 
  387.     * @param  mixed                         $coverageDataFile 
  388.     * @param  mixed                         $coverageHTMLFile 
  389.     * @param  mixed                         $coverageTextFile 
  390.     * @access protected
  391.     * @since  2.1.0
  392.     */
  393.     protected function handleCodeCoverageInformation(PHPUnit2_Framework_TestResult $result$coverageDataFile$coverageHTMLFile$coverageTextFile{
  394.         if ($coverageDataFile !== FALSE &&
  395.             $fp fopen($coverageDataFile'w')) {
  396.             fputs($fpserialize($result->getCodeCoverageInformation()));
  397.             fclose($fp);
  398.         }
  399.  
  400.         if ($coverageHTMLFile !== FALSE{
  401.             $renderer PHPUnit2_Extensions_CodeCoverage_Renderer::factory(
  402.               'HTML',
  403.               $result->getCodeCoverageInformation()
  404.             );
  405.  
  406.             if ($fp fopen($coverageHTMLFile'w')) {
  407.                 fputs(
  408.                   $fp,
  409.                   $renderer->render()
  410.                 );
  411.  
  412.                 fclose($fp);
  413.             }
  414.         }
  415.  
  416.         if ($coverageTextFile !== FALSE{
  417.             $renderer PHPUnit2_Extensions_CodeCoverage_Renderer::factory(
  418.               'Text',
  419.               $result->getCodeCoverageInformation()
  420.             );
  421.  
  422.             if ($fp fopen($coverageTextFile'w')) {
  423.                 fputs(
  424.                   $fp,
  425.                   $renderer->render()
  426.                 );
  427.  
  428.                 fclose($fp);
  429.             }
  430.         }
  431.     }
  432.  
  433.     // }}}
  434.     // {{{ public function showHelp()
  435.  
  436.     /**
  437.     * @access public
  438.     */
  439.     public function showHelp({
  440.         print PHPUnit2_Runner_Version::getVersionString("\n\n" .
  441.               "Usage: phpunit [switches] UnitTest\n";
  442.  
  443.         if (extension_loaded('xdebug')) {
  444.             print "  --coverage-data <file> Write raw code coverage data to file.\n";
  445.             print "  --coverage-html <file> Write code coverage data in HTML format to file.\n";
  446.             print "  --coverage-text <file> Write code coverage data in text formar to file.\n\n";
  447.         }
  448.  
  449.         print "  --testdox-html <file>  Log test progress in TestDox/HTML format to file.\n" .
  450.               "  --testdox-text <file>  Log test progress in TestDox/Text format to file.\n" .
  451.               "  --log-xml <file>       Log test progress in XML format to file.\n\n" .
  452.               "  --skeleton             Generate skeleton UnitTest class for Unit in Unit.php.\n\n" .
  453.               "  --wait                 Waits for a keystroke after each test.\n\n" .
  454.               "  --help                 Prints this usage information.\n" .
  455.               "  --version              Prints the version and exits.\n";
  456.     }
  457.  
  458.     // }}}
  459.     // {{{ protected function pause($wait)
  460.  
  461.     /**
  462.     * @param  boolean $wait 
  463.     * @access protected
  464.     */
  465.     protected function pause($wait{
  466.         if (!$wait{
  467.             return;
  468.         }
  469.  
  470.         $this->printer->printWaitPrompt();
  471.  
  472.         fgets(STDIN);
  473.     }
  474.  
  475.     // }}}
  476.     // {{{ public function testEnded($testName)
  477.  
  478.     /**
  479.     * A test ended.
  480.     *
  481.     * @param  string  $testName 
  482.     * @access public
  483.     */
  484.     public function testEnded($testName{
  485.     }
  486.  
  487.     // }}}
  488.     // {{{ public function testFailed($status, PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e)
  489.  
  490.     /**
  491.     * A test failed.
  492.     *
  493.     * @param  integer                                 $status 
  494.     * @param  PHPUnit2_Framework_Test                 $test 
  495.     * @param  PHPUnit2_Framework_AssertionFailedError $e 
  496.     * @access public
  497.     */
  498.     public function testFailed($statusPHPUnit2_Framework_Test $testPHPUnit2_Framework_AssertionFailedError $e{
  499.     }
  500.  
  501.     // }}}
  502.     // {{{ public function testStarted($testName)
  503.  
  504.     /**
  505.     * A test started.
  506.     *
  507.     * @param  string  $testName 
  508.     * @access public
  509.     */
  510.     public function testStarted($testName{
  511.     }
  512.  
  513.     // }}}
  514.     // {{{ protected function runFailed($message)
  515.  
  516.     /**
  517.     * Override to define how to handle a failed loading of
  518.     * a test suite.
  519.     *
  520.     * @param  string  $message 
  521.     * @access protected
  522.     */
  523.     protected function runFailed($message{
  524.         print $message;
  525.         exit(self::FAILURE_EXIT);
  526.     }
  527.  
  528.     // }}}
  529.     // {{{ public function setPrinter(PHPUnit2_TextUI_ResultPrinter $resultPrinter)
  530.  
  531.     /**
  532.     * @param  PHPUnit2_TextUI_ResultPrinter $resultPrinter 
  533.     * @access public
  534.     */
  535.     public function setPrinter(PHPUnit2_TextUI_ResultPrinter $resultPrinter{
  536.         $this->printer $resultPrinter;
  537.     }
  538.  
  539.     // }}}
  540. }
  541.  
  542. if (PHPUnit2_MAIN_METHOD == 'PHPUnit2_TextUI_TestRunner::main'{
  543. }
  544.  
  545. /*
  546.  * vim600:  et sw=2 ts=2 fdm=marker
  547.  * vim<600: et sw=2 ts=2
  548.  */
  549. ?>

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