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 / Framework / TestResult.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  11.7 KB  |  448 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: TestResult.php,v 1.32.2.7 2006/02/25 09:44:23 sebastian Exp $
  45.  * @link       http://pear.php.net/package/PHPUnit2
  46.  * @since      File available since Release 2.0.0
  47.  */
  48.  
  49. require_once 'PHPUnit2/Framework/AssertionFailedError.php';
  50. require_once 'PHPUnit2/Framework/IncompleteTest.php';
  51. require_once 'PHPUnit2/Framework/TestFailure.php';
  52. require_once 'PHPUnit2/Framework/TestListener.php';
  53. require_once 'PHPUnit2/Util/ErrorHandler.php';
  54. require_once 'PHPUnit2/Util/Filter.php';
  55.  
  56. /**
  57.  * A TestResult collects the results of executing a test case.
  58.  *
  59.  * @category   Testing
  60.  * @package    PHPUnit2
  61.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  62.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  63.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  64.  * @version    Release: 2.3.6
  65.  * @link       http://pear.php.net/package/PHPUnit2
  66.  * @since      Class available since Release 2.0.0
  67.  */
  68. class PHPUnit2_Framework_TestResult {
  69.     /**
  70.      * @var    array
  71.      * @access protected
  72.      */
  73.     protected $errors = array();
  74.  
  75.     /**
  76.      * @var    array
  77.      * @access protected
  78.      */
  79.     protected $failures = array();
  80.  
  81.     /**
  82.      * @var    array
  83.      * @access protected
  84.      */
  85.     protected $notImplemented = array();
  86.  
  87.     /**
  88.      * @var    array
  89.      * @access protected
  90.      */
  91.     protected $listeners = array();
  92.  
  93.     /**
  94.      * @var    integer
  95.      * @access protected
  96.      */
  97.     protected $runTests = 0;
  98.  
  99.     /**
  100.      * Code Coverage information provided by Xdebug.
  101.      *
  102.      * @var    array
  103.      * @access protected
  104.      */
  105.     protected $codeCoverageInformation = array();
  106.  
  107.     /**
  108.      * @var    boolean
  109.      * @access protected
  110.      */
  111.     protected $collectCodeCoverageInformation = FALSE;
  112.  
  113.     /**
  114.      * @var    boolean
  115.      * @access private
  116.      */
  117.     private $stop = FALSE;
  118.  
  119.     /**
  120.      * Registers a TestListener.
  121.      *
  122.      * @param  PHPUnit2_Framework_TestListener
  123.      * @access public
  124.      */
  125.     public function addListener(PHPUnit2_Framework_TestListener $listener) {
  126.         $this->listeners[] = $listener;
  127.     }
  128.  
  129.     /**
  130.      * Unregisters a TestListener.
  131.      *
  132.      * @param  PHPUnit2_Framework_TestListener $listener
  133.      * @access public
  134.      */
  135.     public function removeListener(PHPUnit2_Framework_TestListener $listener) {
  136.         for ($i = 0; $i < sizeof($this->listeners); $i++) {
  137.             if ($this->listeners[$i] === $listener) {
  138.                 unset($this->listeners[$i]);
  139.             }
  140.         }
  141.     }
  142.  
  143.     /**
  144.      * Adds an error to the list of errors.
  145.      * The passed in exception caused the error.
  146.      *
  147.      * @param  PHPUnit2_Framework_Test $test
  148.      * @param  Exception               $e
  149.      * @access public
  150.      */
  151.     public function addError(PHPUnit2_Framework_Test $test, Exception $e) {
  152.         if ($e instanceof PHPUnit2_Framework_IncompleteTest) {
  153.             $this->notImplemented[] = new PHPUnit2_Framework_TestFailure($test, $e);
  154.  
  155.             foreach ($this->listeners as $listener) {
  156.                 $listener->addIncompleteTest($test, $e);
  157.             }
  158.         } else {
  159.             $this->errors[] = new PHPUnit2_Framework_TestFailure($test, $e);
  160.  
  161.             foreach ($this->listeners as $listener) {
  162.                 $listener->addError($test, $e);
  163.             }
  164.         }
  165.     }
  166.  
  167.     /**
  168.      * Adds a failure to the list of failures.
  169.      * The passed in exception caused the failure.
  170.      *
  171.      * @param  PHPUnit2_Framework_Test                  $test
  172.      * @param  PHPUnit2_Framework_AssertionFailedError  $e
  173.      * @access public
  174.      */
  175.     public function addFailure(PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e) {
  176.         if ($e instanceof PHPUnit2_Framework_IncompleteTest) {
  177.             $this->notImplemented[] = new PHPUnit2_Framework_TestFailure($test, $e);
  178.  
  179.             foreach ($this->listeners as $listener) {
  180.                 $listener->addIncompleteTest($test, $e);
  181.             }
  182.         } else {
  183.             $this->failures[] = new PHPUnit2_Framework_TestFailure($test, $e);
  184.  
  185.             foreach ($this->listeners as $listener) {
  186.                 $listener->addFailure($test, $e);
  187.             }
  188.         }
  189.     }
  190.  
  191.     /**
  192.      * Informs the result that a testsuite will be started.
  193.      *
  194.      * @param  PHPUnit2_Framework_TestSuite $suite
  195.      * @access public
  196.      * @since  Method available since Release 2.2.0
  197.      */
  198.     public function startTestSuite(PHPUnit2_Framework_TestSuite $suite) {
  199.         foreach ($this->listeners as $listener) {
  200.             $listener->startTestSuite($suite);
  201.         }
  202.     }
  203.  
  204.     /**
  205.      * Informs the result that a testsuite was completed.
  206.      *
  207.      * @param  PHPUnit2_Framework_TestSuite $suite
  208.      * @access public
  209.      * @since  Method available since Release 2.2.0
  210.      */
  211.     public function endTestSuite(PHPUnit2_Framework_TestSuite $suite) {
  212.         foreach ($this->listeners as $listener) {
  213.             $listener->endTestSuite($suite);
  214.         }
  215.     }
  216.  
  217.     /**
  218.      * Informs the result that a test will be started.
  219.      *
  220.      * @param  PHPUnit2_Framework_Test $test
  221.      * @access public
  222.      */
  223.     public function startTest(PHPUnit2_Framework_Test $test) {
  224.         $this->runTests += $test->countTestCases();
  225.  
  226.         foreach ($this->listeners as $listener) {
  227.             $listener->startTest($test);
  228.         }
  229.     }
  230.  
  231.     /**
  232.      * Informs the result that a test was completed.
  233.      *
  234.      * @param  PHPUnit2_Framework_Test
  235.      * @access public
  236.      */
  237.     public function endTest(PHPUnit2_Framework_Test $test) {
  238.         foreach ($this->listeners as $listener) {
  239.             $listener->endTest($test);
  240.         }
  241.     }
  242.  
  243.     /**
  244.      * Returns TRUE if no incomplete test occured.
  245.      *
  246.      * @return boolean
  247.      * @access public
  248.      */
  249.     public function allCompletlyImplemented() {
  250.         return $this->notImplementedCount() == 0;
  251.     }
  252.  
  253.     /**
  254.      * Gets the number of incomplete tests.
  255.      *
  256.      * @return integer
  257.      * @access public
  258.      */
  259.     public function notImplementedCount() {
  260.         return sizeof($this->notImplemented);
  261.     }
  262.  
  263.     /**
  264.      * Returns an Enumeration for the incomplete tests.
  265.      *
  266.      * @return array
  267.      * @access public
  268.      */
  269.     public function notImplemented() {
  270.         return $this->notImplemented;
  271.     }
  272.  
  273.     /**
  274.      * Gets the number of detected errors.
  275.      *
  276.      * @return integer
  277.      * @access public
  278.      */
  279.     public function errorCount() {
  280.         return sizeof($this->errors);
  281.     }
  282.  
  283.     /**
  284.      * Returns an Enumeration for the errors.
  285.      *
  286.      * @return array
  287.      * @access public
  288.      */
  289.     public function errors() {
  290.         return $this->errors;
  291.     }
  292.  
  293.     /**
  294.      * Gets the number of detected failures.
  295.      *
  296.      * @return integer
  297.      * @access public
  298.      */
  299.     public function failureCount() {
  300.         return sizeof($this->failures);
  301.     }
  302.  
  303.     /**
  304.      * Returns an Enumeration for the failures.
  305.      *
  306.      * @return array
  307.      * @access public
  308.      */
  309.     public function failures() {
  310.         return $this->failures;
  311.     }
  312.  
  313.     /**
  314.      * Enables or disables the collection of Code Coverage information.
  315.      *
  316.      * @param  boolean $flag
  317.      * @throws Exception
  318.      * @access public
  319.      * @since  Method available since Release 2.3.0
  320.      */
  321.     public function collectCodeCoverageInformation($flag) {
  322.         if (is_bool($flag)) {
  323.             $this->collectCodeCoverageInformation = $flag;
  324.         } else {
  325.             throw new Exception;
  326.         }
  327.     }
  328.  
  329.     /**
  330.      * Returns Code Coverage data per test case.
  331.      *
  332.      * Format of the result array:
  333.      *
  334.      * <code>
  335.      * array(
  336.      *   "testCase" => array(
  337.      *     "/tested/code.php" => array(
  338.      *       linenumber => flag
  339.      *     )
  340.      *   )
  341.      * )
  342.      * </code>
  343.      *
  344.      * flag < 0: Line is executable but was not executed.
  345.      * flag > 0: Line was executed.
  346.      *
  347.      * @return array
  348.      * @access public
  349.      */
  350.     public function getCodeCoverageInformation() {
  351.         return $this->codeCoverageInformation;
  352.     }
  353.  
  354.     /**
  355.      * Runs a TestCase.
  356.      *
  357.      * @param  PHPUnit2_Framework_Test $test
  358.      * @access public
  359.      */
  360.     public function run(PHPUnit2_Framework_Test $test) {
  361.         $this->startTest($test);
  362.  
  363.         set_error_handler('PHPUnit2_Util_ErrorHandler', E_USER_ERROR);
  364.  
  365.         $useXdebug = (extension_loaded('xdebug') && $this->collectCodeCoverageInformation);
  366.  
  367.         if ($useXdebug) {
  368.             xdebug_start_code_coverage(XDEBUG_CC_UNUSED);
  369.         }
  370.  
  371.         $globalsBackup = $GLOBALS;
  372.  
  373.         try {
  374.             $test->runBare();
  375.         }
  376.  
  377.         catch (PHPUnit2_Framework_AssertionFailedError $e) {
  378.             $this->addFailure($test, $e);
  379.         }
  380.  
  381.         catch (Exception $e) {
  382.             $this->addError($test, $e);
  383.         }
  384.  
  385.         $GLOBALS = $globalsBackup;
  386.  
  387.         if ($useXdebug) {
  388.             $this->codeCoverageInformation[$test->getName()] = PHPUnit2_Util_Filter::getFilteredCodeCoverage(
  389.               xdebug_get_code_coverage()
  390.             );
  391.  
  392.             xdebug_stop_code_coverage();
  393.         }
  394.  
  395.         restore_error_handler();
  396.  
  397.         $this->endTest($test);
  398.     }
  399.  
  400.     /**
  401.      * Gets the number of run tests.
  402.      *
  403.      * @return integer
  404.      * @access public
  405.      */
  406.     public function runCount() {
  407.         return $this->runTests;
  408.     }
  409.  
  410.     /**
  411.      * Checks whether the test run should stop.
  412.      *
  413.      * @return boolean
  414.      * @access public
  415.      */
  416.     public function shouldStop() {
  417.         return $this->stop;
  418.     }
  419.  
  420.     /**
  421.      * Marks that the test run should stop.
  422.      *
  423.      * @access public
  424.      */
  425.     public function stop() {
  426.         $this->stop = TRUE;
  427.     }
  428.  
  429.     /**
  430.      * Returns whether the entire test was successful or not.
  431.      *
  432.      * @return boolean
  433.      * @access public
  434.      */
  435.     public function wasSuccessful() {
  436.         return empty($this->errors) && empty($this->failures);
  437.     }
  438. }
  439.  
  440. /*
  441.  * Local variables:
  442.  * tab-width: 4
  443.  * c-basic-offset: 4
  444.  * c-hanging-comment-ender-p: nil
  445.  * End:
  446.  */
  447. ?>
  448.