home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / PHPUnit2 / TextUI / TestRunner.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  18.5 KB  |  623 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * PHP Version 5
  6.  *
  7.  * Copyright (c) 2002-2006, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  *
  14.  *   * Redistributions of source code must retain the above copyright
  15.  *     notice, this list of conditions and the following disclaimer.
  16.  * 
  17.  *   * Redistributions in binary form must reproduce the above copyright
  18.  *     notice, this list of conditions and the following disclaimer in
  19.  *     the documentation and/or other materials provided with the
  20.  *     distribution.
  21.  *
  22.  *   * Neither the name of Sebastian Bergmann nor the names of his
  23.  *     contributors may be used to endorse or promote products derived
  24.  *     from this software without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29.  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30.  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  35.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37.  * POSSIBILITY OF SUCH DAMAGE.
  38.  *
  39.  * @category   Testing
  40.  * @package    PHPUnit2
  41.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  42.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  43.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  44.  * @version    CVS: $Id: TestRunner.php,v 1.64.2.5 2005/12/17 16:04:58 sebastian Exp $
  45.  * @link       http://pear.php.net/package/PHPUnit2
  46.  * @since      File available since Release 2.0.0
  47.  */
  48.  
  49. if (!defined('PHPUnit2_MAIN_METHOD')) {
  50.     define('PHPUnit2_MAIN_METHOD', 'PHPUnit2_TextUI_TestRunner::main');
  51. }
  52.  
  53. require_once 'PHPUnit2/Framework/TestSuite.php';
  54. require_once 'PHPUnit2/Runner/Version.php';
  55. require_once 'PHPUnit2/Runner/BaseTestRunner.php';
  56. require_once 'PHPUnit2/TextUI/ResultPrinter.php';
  57. require_once 'PHPUnit2/Util/Fileloader.php';
  58.  
  59. require_once 'Console/Getopt.php';
  60. require_once 'Benchmark/Timer.php';
  61.  
  62. /**
  63.  * A TestRunner for the Command Line Interface (CLI)
  64.  * PHP SAPI Module.
  65.  *
  66.  * @category   Testing
  67.  * @package    PHPUnit2
  68.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  69.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  70.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  71.  * @version    Release: 2.3.6
  72.  * @link       http://pear.php.net/package/PHPUnit2
  73.  * @since      Class available since Release 2.0.0
  74.  */
  75. class PHPUnit2_TextUI_TestRunner extends PHPUnit2_Runner_BaseTestRunner {
  76.     const SUCCESS_EXIT   = 0;
  77.     const FAILURE_EXIT   = 1;
  78.     const EXCEPTION_EXIT = 2;
  79.  
  80.     /**
  81.      * @var    PHPUnit2_Runner_TestSuiteLoader
  82.      * @access private
  83.      */
  84.     private $loader = NULL;
  85.  
  86.     /**
  87.      * @var    PHPUnit2_TextUI_ResultPrinter
  88.      * @access private
  89.      */
  90.     private $printer = NULL;
  91.  
  92.     /**
  93.      * @var    boolean
  94.      * @access private
  95.      * @static
  96.      */
  97.     private static $versionStringPrinted = FALSE;
  98.  
  99.     /**
  100.      * @access public
  101.      * @static
  102.      */
  103.     public static function main() {
  104.         $aTestRunner = new PHPUnit2_TextUI_TestRunner;
  105.  
  106.         try {
  107.             $result = $aTestRunner->start($_SERVER['argv']);
  108.  
  109.             if (!$result->wasSuccessful()) {
  110.                 exit(self::FAILURE_EXIT);
  111.             }
  112.  
  113.             exit(self::SUCCESS_EXIT);
  114.         }
  115.  
  116.         catch (Exception $e) {
  117.             self::printVersionString();
  118.             print $e->getMessage();
  119.             exit(self::EXCEPTION_EXIT);
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * @param  array $arguments
  125.      * @throws Exception
  126.      * @access protected
  127.      */
  128.     protected function start($arguments) {
  129.         $coverageDataFile = FALSE;
  130.         $coverageHTMLFile = FALSE;
  131.         $coverageTextFile = FALSE;
  132.         $testdoxHTMLFile  = FALSE;
  133.         $testdoxTextFile  = FALSE;
  134.         $xmlLogfile       = FALSE;
  135.         $wait             = FALSE;
  136.  
  137.         $possibleOptions = array(
  138.           'help',
  139.           'loader=',
  140.           'log-xml=',
  141.           'skeleton',
  142.           'testdox-html=',
  143.           'testdox-text=',
  144.           'version',
  145.           'wait'
  146.         );
  147.  
  148.         if (extension_loaded('xdebug')) {
  149.             $possibleOptions[] = 'coverage-data=';
  150.             $possibleOptions[] = 'coverage-html=';
  151.             $possibleOptions[] = 'coverage-text=';
  152.         }
  153.  
  154.         $options = Console_Getopt::getopt(
  155.           $arguments,
  156.           '',
  157.           $possibleOptions
  158.         );
  159.  
  160.         if (PEAR::isError($options)) {
  161.             $this->showError($options->getMessage());
  162.         }
  163.  
  164.         $test     = isset($options[1][0]) ? $options[1][0] : FALSE;
  165.         $testFile = isset($options[1][1]) ? $options[1][1] : $test . '.php';
  166.  
  167.         foreach ($options[0] as $option) {
  168.             switch ($option[0]) {
  169.                 case '--coverage-data': {
  170.                     $coverageDataFile = $option[1];
  171.                 }
  172.                 break;
  173.  
  174.                 case '--coverage-html': {
  175.                     $coverageHTMLFile = $option[1];
  176.                 }
  177.                 break;
  178.  
  179.                 case '--coverage-text': {
  180.                     $coverageTextFile = $option[1];
  181.                 }
  182.                 break;
  183.  
  184.                 case '--help': {
  185.                     $this->showHelp();
  186.                     exit(self::SUCCESS_EXIT);
  187.                 }
  188.                 break;
  189.  
  190.                 case '--testdox-html': {
  191.                     $testdoxHTMLFile = $option[1];
  192.                 }
  193.                 break;
  194.  
  195.                 case '--testdox-text': {
  196.                     $testdoxTextFile = $option[1];
  197.                 }
  198.                 break;
  199.  
  200.                 case '--loader': {
  201.                     if (!class_exists($option[1])) {
  202.                         PHPUnit2_Util_Fileloader::checkAndLoad(
  203.                           str_replace('_', '/', $option[1]) . '.php'
  204.                         );
  205.                     }
  206.  
  207.                     if (class_exists($option[1])) {
  208.                         $class = new ReflectionClass($option[1]);
  209.  
  210.                         if ($class->implementsInterface('PHPUnit2_Runner_TestSuiteLoader') &&
  211.                             $class->isInstantiable()) {
  212.                             $this->loader = $class->newInstance();
  213.                         }
  214.                     }
  215.  
  216.                     if ($this->loader === NULL) {
  217.                         $this->showError(
  218.                           sprintf(
  219.                             'Could not use "%s" as loader.',
  220.  
  221.                             $option[1]
  222.                           )
  223.                         );
  224.                     }
  225.                 }
  226.                 break;
  227.  
  228.                 case '--log-xml': {
  229.                     $xmlLogfile = $option[1];
  230.                 }
  231.                 break;
  232.  
  233.                 case '--skeleton': {
  234.                     if ($test !== FALSE) {
  235.                         self::printVersionString();
  236.  
  237.                         try {
  238.                             require_once 'PHPUnit2/Util/Skeleton.php';
  239.  
  240.                             $skeleton = new PHPUnit2_Util_Skeleton($test, $testFile);
  241.                             $skeleton->write();
  242.                         }
  243.  
  244.                         catch (Exception $e) {
  245.                             print $e->getMessage() . "\n";
  246.  
  247.                             printf(
  248.                               "Could not write test class skeleton for %s to %s.\n",
  249.                               $test,
  250.                               $test . 'Test.php'
  251.                             );
  252.  
  253.                             exit(self::FAILURE_EXIT);
  254.                         }
  255.  
  256.                         printf(
  257.                           "Wrote test class skeleton for %s to %s.\n",
  258.                           $test,
  259.                           $test . 'Test.php'
  260.                         );
  261.  
  262.                         exit(self::SUCCESS_EXIT);
  263.                     }
  264.                 }
  265.                 break;
  266.  
  267.                 case '--version': {
  268.                     self::printVersionString();
  269.                     exit(self::SUCCESS_EXIT);
  270.                 }
  271.                 break;
  272.  
  273.                 case '--wait': {
  274.                     $wait = TRUE;
  275.                 }
  276.                 break;
  277.             }
  278.         }
  279.  
  280.         if ($test === FALSE) {
  281.             $this->showHelp();
  282.  
  283.             exit(self::SUCCESS_EXIT);
  284.         }
  285.  
  286.         try {
  287.             return $this->doRun(
  288.               $this->getTest($test, $testFile),
  289.               $coverageDataFile,
  290.               $coverageHTMLFile,
  291.               $coverageTextFile,
  292.               $testdoxHTMLFile,
  293.               $testdoxTextFile,
  294.               $xmlLogfile,
  295.               $wait
  296.             );
  297.         }
  298.  
  299.         catch (Exception $e) {
  300.             throw new Exception(
  301.               'Could not create and run test suite: ' . $e->getMessage()
  302.             );
  303.         }
  304.     }
  305.  
  306.     /**
  307.      * @param  mixed   $test
  308.      * @param  mixed   $coverageDataFile
  309.      * @param  mixed   $testdoxHTMLFile
  310.      * @param  mixed   $testdoxTextFile
  311.      * @param  mixed   $xmlLogfile
  312.      * @param  boolean $wait
  313.      * @access public
  314.      * @static
  315.      */
  316.     public static function run($test, $coverageDataFile = FALSE, $coverageHTMLFile = FALSE, $coverageTextFile = FALSE, $testdoxHTMLFile = FALSE, $testdoxTextFile = FALSE, $xmlLogfile = FALSE, $wait = FALSE) {
  317.         if ($test instanceof ReflectionClass) {
  318.             $test = new PHPUnit2_Framework_TestSuite($test);
  319.         }
  320.  
  321.         if ($test instanceof PHPUnit2_Framework_Test) {
  322.             $aTestRunner = new PHPUnit2_TextUI_TestRunner;
  323.  
  324.             return $aTestRunner->doRun(
  325.               $test,
  326.               $coverageDataFile,
  327.               $coverageHTMLFile,
  328.               $coverageTextFile,
  329.               $testdoxHTMLFile,
  330.               $testdoxTextFile,
  331.               $xmlLogfile,
  332.               $wait
  333.             );
  334.         }
  335.     }
  336.  
  337.     /**
  338.      * Runs a single test and waits until the user types RETURN.
  339.      *
  340.      * @param  PHPUnit2_Framework_Test $suite
  341.      * @access public
  342.      * @static
  343.      */
  344.     public static function runAndWait(PHPUnit2_Framework_Test $suite) {
  345.         $aTestRunner = new PHPUnit2_TextUI_TestRunner;
  346.  
  347.         $aTestRunner->doRun(
  348.           $suite,
  349.           FALSE,
  350.           FALSE,
  351.           FALSE,
  352.           FALSE,
  353.           FALSE,
  354.           FALSE,
  355.           TRUE
  356.         );
  357.     }
  358.  
  359.     /**
  360.      * @return PHPUnit2_Framework_TestResult
  361.      * @access protected
  362.      */
  363.     protected function createTestResult() {
  364.         return new PHPUnit2_Framework_TestResult;
  365.     }
  366.  
  367.     /**
  368.      * @param  PHPUnit2_Framework_Test $suite
  369.      * @param  mixed                   $coverageDataFile
  370.      * @param  mixed                   $coverageHTMLFile
  371.      * @param  mixed                   $coverageTextFile
  372.      * @param  mixed                   $testdoxHTMLFile
  373.      * @param  mixed                   $testdoxTextFile
  374.      * @param  mixed                   $xmlLogfile
  375.      * @param  boolean                 $wait
  376.      * @return PHPUnit2_Framework_TestResult
  377.      * @access public
  378.      */
  379.     public function doRun(PHPUnit2_Framework_Test $suite, $coverageDataFile = FALSE, $coverageHTMLFile = FALSE, $coverageTextFile = FALSE, $testdoxHTMLFile = FALSE, $testdoxTextFile = FALSE, $xmlLogfile = FALSE, $wait = FALSE) {
  380.         $result = $this->createTestResult();
  381.         $timer  = new Benchmark_Timer;
  382.  
  383.         if ($this->printer === NULL) {
  384.             $this->printer = new PHPUnit2_TextUI_ResultPrinter;
  385.         }
  386.  
  387.         $this->printer->write(
  388.           PHPUnit2_Runner_Version::getVersionString() . "\n\n"
  389.         );
  390.  
  391.         $result->addListener($this->printer);
  392.  
  393.         if ($testdoxHTMLFile !== FALSE || $testdoxTextFile !== FALSE) {
  394.             require_once 'PHPUnit2/Util/TestDox/ResultPrinter.php';
  395.  
  396.             if ($testdoxHTMLFile !== FALSE) {
  397.                 $result->addListener(
  398.                   PHPUnit2_Util_TestDox_ResultPrinter::factory(
  399.                     'HTML',
  400.                     $testdoxHTMLFile
  401.                   )
  402.                 );
  403.             }
  404.  
  405.             if ($testdoxTextFile !== FALSE) {
  406.                 $result->addListener(
  407.                   PHPUnit2_Util_TestDox_ResultPrinter::factory(
  408.                     'Text',
  409.                     $testdoxTextFile
  410.                   )
  411.                 );
  412.             }
  413.         }
  414.  
  415.         if ($xmlLogfile !== FALSE) {
  416.             require_once 'PHPUnit2/Util/Log/XML.php';
  417.  
  418.             $result->addListener(
  419.               new PHPUnit2_Util_Log_XML($xmlLogfile)
  420.             );
  421.         }
  422.  
  423.         if ($coverageDataFile !== FALSE ||
  424.             $coverageHTMLFile !== FALSE ||
  425.             $coverageTextFile !== FALSE) {
  426.             $result->collectCodeCoverageInformation(TRUE);
  427.         }
  428.  
  429.         $timer->start();
  430.         $suite->run($result);
  431.         $timer->stop();
  432.         $timeElapsed = $timer->timeElapsed();
  433.  
  434.         $this->pause($wait);
  435.  
  436.         $this->printer->printResult($result, $timeElapsed);
  437.  
  438.         $this->handleCodeCoverageInformation(
  439.           $result,
  440.           $coverageDataFile,
  441.           $coverageHTMLFile,
  442.           $coverageTextFile
  443.         );
  444.  
  445.         return $result;
  446.     }
  447.  
  448.     /**
  449.      * Returns the loader to be used.
  450.      *
  451.      * @return PHPUnit2_Runner_TestSuiteLoader
  452.      * @access public
  453.      * @since  Method available since Release 2.2.0
  454.      */
  455.     public function getLoader() {
  456.         if ($this->loader === NULL) {
  457.             $this->loader = new PHPUnit2_Runner_StandardTestSuiteLoader;
  458.         }
  459.  
  460.         return $this->loader;
  461.     }
  462.  
  463.     /**
  464.      * @param  PHPUnit2_Framework_TestResult $result
  465.      * @param  mixed                         $coverageDataFile
  466.      * @param  mixed                         $coverageHTMLFile
  467.      * @param  mixed                         $coverageTextFile
  468.      * @access protected
  469.      * @since  Method available since Release 2.1.0
  470.      */
  471.     protected function handleCodeCoverageInformation(PHPUnit2_Framework_TestResult $result, $coverageDataFile, $coverageHTMLFile, $coverageTextFile) {
  472.         if ($coverageDataFile !== FALSE &&
  473.             $fp = fopen($coverageDataFile, 'w')) {
  474.             fputs($fp, serialize($result->getCodeCoverageInformation()));
  475.             fclose($fp);
  476.         }
  477.  
  478.         if ($coverageHTMLFile !== FALSE || $coverageTextFile !== FALSE) {
  479.             require_once 'PHPUnit2/Util/CodeCoverage/Renderer.php';
  480.  
  481.             if ($coverageHTMLFile !== FALSE) {
  482.                 $renderer = PHPUnit2_Util_CodeCoverage_Renderer::factory(
  483.                   'HTML',
  484.                   $result->getCodeCoverageInformation()
  485.                 );
  486.  
  487.                 $renderer->renderToFile($coverageHTMLFile);
  488.             }
  489.  
  490.             if ($coverageTextFile !== FALSE) {
  491.                 $renderer = PHPUnit2_Util_CodeCoverage_Renderer::factory(
  492.                   'Text',
  493.                   $result->getCodeCoverageInformation()
  494.                 );
  495.  
  496.                 $renderer->renderToFile($coverageTextFile);
  497.             }
  498.         }
  499.     }
  500.  
  501.     /**
  502.      * @access public
  503.      */
  504.     public function showError($message) {
  505.         self::printVersionString();
  506.         print $message . "\n";
  507.  
  508.         exit(self::FAILURE_EXIT);
  509.     }
  510.  
  511.     /**
  512.      * @access public
  513.      */
  514.     public function showHelp() {
  515.         self::printVersionString();
  516.         print "Usage: phpunit [switches] UnitTest [UnitTest.php]\n";
  517.  
  518.         if (extension_loaded('xdebug')) {
  519.             print "  --coverage-data <file> Write Code Coverage data in raw format to file.\n" .
  520.                   "  --coverage-html <file> Write Code Coverage data in HTML format to file.\n" .
  521.                   "  --coverage-text <file> Write Code Coverage data in text format to file.\n\n";
  522.         }
  523.  
  524.         print "  --testdox-html <file>  Write agile documentation in HTML format to file.\n" .
  525.               "  --testdox-text <file>  Write agile documentation in Text format to file.\n" .
  526.               "  --log-xml <file>       Log test progress in XML format to file.\n\n";
  527.  
  528.         print "  --loader <loader>      TestSuiteLoader implementation to use.\n\n" .
  529.               "  --skeleton             Generate skeleton UnitTest class for Unit in Unit.php.\n\n" .
  530.               "  --wait                 Waits for a keystroke after each test.\n\n" .
  531.               "  --help                 Prints this usage information.\n" .
  532.               "  --version              Prints the version and exits.\n";
  533.     }
  534.  
  535.     /**
  536.      * @param  boolean $wait
  537.      * @access protected
  538.      */
  539.     protected function pause($wait) {
  540.         if (!$wait) {
  541.             return;
  542.         }
  543.  
  544.         $this->printer->printWaitPrompt();
  545.  
  546.         fgets(STDIN);
  547.     }
  548.  
  549.     /**
  550.      * @param  PHPUnit2_TextUI_ResultPrinter $resultPrinter
  551.      * @access public
  552.      */
  553.     public function setPrinter(PHPUnit2_TextUI_ResultPrinter $resultPrinter) {
  554.         $this->printer = $resultPrinter;
  555.     }
  556.  
  557.     /**
  558.      * A test started.
  559.      *
  560.      * @param  string  $testName
  561.      * @access public
  562.      */
  563.     public function testStarted($testName) {
  564.     }
  565.  
  566.     /**
  567.      * A test ended.
  568.      *
  569.      * @param  string  $testName
  570.      * @access public
  571.      */
  572.     public function testEnded($testName) {
  573.     }
  574.  
  575.     /**
  576.      * A test failed.
  577.      *
  578.      * @param  integer                                 $status
  579.      * @param  PHPUnit2_Framework_Test                 $test
  580.      * @param  PHPUnit2_Framework_AssertionFailedError $e
  581.      * @access public
  582.      */
  583.     public function testFailed($status, PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e) {
  584.     }
  585.  
  586.     /**
  587.      * Override to define how to handle a failed loading of
  588.      * a test suite.
  589.      *
  590.      * @param  string  $message
  591.      * @access protected
  592.      */
  593.     protected function runFailed($message) {
  594.         self::printVersionString();
  595.         print $message;
  596.         exit(self::FAILURE_EXIT);
  597.     }
  598.  
  599.     /**
  600.      * @access private
  601.      * @since  Method available since Release 2.2.0
  602.      */
  603.     private static function printVersionString() {
  604.         if (!self::$versionStringPrinted) {
  605.             print PHPUnit2_Runner_Version::getVersionString() . "\n\n";
  606.             self::$versionStringPrinted = TRUE;
  607.         }
  608.     }
  609. }
  610.  
  611. if (PHPUnit2_MAIN_METHOD == 'PHPUnit2_TextUI_TestRunner::main') {
  612.     PHPUnit2_TextUI_TestRunner::main();
  613. }
  614.  
  615. /*
  616.  * Local variables:
  617.  * tab-width: 4
  618.  * c-basic-offset: 4
  619.  * c-hanging-comment-ender-p: nil
  620.  * End:
  621.  */
  622. ?>
  623.