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

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