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 / Runner / BaseTestRunner.php next >
Encoding:
PHP Script  |  2008-07-02  |  8.2 KB  |  284 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: BaseTestRunner.php,v 1.18.2.3 2005/12/17 16:04:56 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/TestListener.php';
  51. require_once 'PHPUnit2/Runner/StandardTestSuiteLoader.php';
  52.  
  53. /**
  54.  * Base class for all test runners.
  55.  *
  56.  * @category   Testing
  57.  * @package    PHPUnit2
  58.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  59.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  60.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  61.  * @version    Release: 2.3.6
  62.  * @link       http://pear.php.net/package/PHPUnit2
  63.  * @since      Class available since Release 2.0.0
  64.  * @abstract
  65.  */
  66. abstract class PHPUnit2_Runner_BaseTestRunner implements PHPUnit2_Framework_TestListener {
  67.     const STATUS_ERROR      = 1;
  68.     const STATUS_FAILURE    = 2;
  69.     const STATUS_INCOMPLETE = 3;
  70.     const SUITE_METHODNAME  = 'suite';
  71.  
  72.     /**
  73.      * An error occurred.
  74.      *
  75.      * @param  PHPUnit2_Framework_Test $test
  76.      * @param  Exception               $e
  77.      * @access public
  78.      */
  79.     public function addError(PHPUnit2_Framework_Test $test, Exception $e) {
  80.         $this->testFailed(self::STATUS_ERROR, $test, $e);
  81.     }
  82.  
  83.     /**
  84.      * A failure occurred.
  85.      *
  86.      * @param  PHPUnit2_Framework_Test                 $test
  87.      * @param  PHPUnit2_Framework_AssertionFailedError $e
  88.      * @access public
  89.      */
  90.     public function addFailure(PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e) {
  91.         $this->testFailed(self::STATUS_FAILURE, $test, $e);
  92.     }
  93.  
  94.     /**
  95.      * Incomplete test.
  96.      *
  97.      * @param  PHPUnit2_Framework_Test $test
  98.      * @param  Exception               $e
  99.      * @access public
  100.      */
  101.     public function addIncompleteTest(PHPUnit2_Framework_Test $test, Exception $e) {
  102.         $this->testFailed(self::STATUS_INCOMPLETE, $test, $e);
  103.     }
  104.  
  105.     /**
  106.      * A testsuite started.
  107.      *
  108.      * @param  PHPUnit2_Framework_TestSuite $suite
  109.      * @access public
  110.      * @since  Method available since Release 2.2.0
  111.      */
  112.     public function startTestSuite(PHPUnit2_Framework_TestSuite $suite) {
  113.     }
  114.  
  115.     /**
  116.      * A testsuite ended.
  117.      *
  118.      * @param  PHPUnit2_Framework_TestSuite $suite
  119.      * @access public
  120.      * @since  Method available since Release 2.2.0
  121.      */
  122.     public function endTestSuite(PHPUnit2_Framework_TestSuite $suite) {
  123.     }
  124.  
  125.     /**
  126.      * A test started.
  127.      *
  128.      * @param  PHPUnit2_Framework_Test  $test
  129.      * @access public
  130.      */
  131.     public function startTest(PHPUnit2_Framework_Test $test) {
  132.         $this->testStarted($test->getName());
  133.     }
  134.  
  135.     /**
  136.      * A test ended.
  137.      *
  138.      * @param  PHPUnit2_Framework_Test  $test
  139.      * @access public
  140.      */
  141.     public function endTest(PHPUnit2_Framework_Test $test) {
  142.         $this->testEnded($test->getName());
  143.     }
  144.  
  145.     /**
  146.      * Returns the loader to be used.
  147.      *
  148.      * @return PHPUnit2_Runner_TestSuiteLoader
  149.      * @access public
  150.      */
  151.     public function getLoader() {
  152.         return new PHPUnit2_Runner_StandardTestSuiteLoader;
  153.     }
  154.  
  155.     /**
  156.      * Returns the Test corresponding to the given suite.
  157.      * This is a template method, subclasses override
  158.      * the runFailed() and clearStatus() methods.
  159.      *
  160.      * @param  string  $suiteClassName
  161.      * @param  string  $suiteClassFile
  162.      * @return PHPUnit2_Framework_Test
  163.      * @access public
  164.      */
  165.     public function getTest($suiteClassName, $suiteClassFile = '') {
  166.         if ($suiteClassFile == $suiteClassName . '.php') {
  167.             $suiteClassFile = '';
  168.         }
  169.  
  170.         try {
  171.             $testClass = $this->loadSuiteClass($suiteClassName, $suiteClassFile);
  172.         }
  173.  
  174.         catch (Exception $e) {
  175.             $this->runFailed($e->getMessage());
  176.             return NULL;
  177.         }
  178.  
  179.         try {
  180.             $suiteMethod = $testClass->getMethod(self::SUITE_METHODNAME);
  181.  
  182.             if (!$suiteMethod->isStatic()) {
  183.                 $this->runFailed(
  184.                   'suite() method must be static.'
  185.                 );
  186.  
  187.                 return NULL;
  188.             }
  189.  
  190.             try {
  191.                 $test = $suiteMethod->invoke(NULL);
  192.             }
  193.  
  194.             catch (ReflectionException $e) {
  195.                 $this->runFailed(
  196.                   sprintf(
  197.                     "Failed to invoke suite() method.\n%s",
  198.  
  199.                     $e->getMessage()
  200.                   )
  201.                 );
  202.  
  203.                 return NULL;
  204.             }
  205.         }
  206.  
  207.         catch (ReflectionException $e) {
  208.             $test = new PHPUnit2_Framework_TestSuite($testClass);
  209.         }
  210.  
  211.         $this->clearStatus();
  212.  
  213.         return $test;
  214.     }
  215.  
  216.     /**
  217.      * Override to define how to handle a failed loading of
  218.      * a test suite.
  219.      *
  220.      * @param  string  $message
  221.      * @access protected
  222.      * @abstract
  223.      */
  224.     protected abstract function runFailed($message);
  225.  
  226.     /**
  227.      * Returns the loaded ReflectionClass for a suite name.
  228.      *
  229.      * @param  string  $suiteClassName
  230.      * @param  string  $suiteClassFile
  231.      * @return ReflectionClass
  232.      * @access protected
  233.      */
  234.     protected function loadSuiteClass($suiteClassName, $suiteClassFile = '') {
  235.         return $this->getLoader()->load($suiteClassName, $suiteClassFile);
  236.     }
  237.  
  238.     /**
  239.      * Clears the status message.
  240.      *
  241.      * @access protected
  242.      */
  243.     protected function clearStatus() {
  244.     }
  245.  
  246.     /**
  247.      * A test started.
  248.      *
  249.      * @param  string  $testName
  250.      * @access public
  251.      * @abstract
  252.      */
  253.     public abstract function testStarted($testName);
  254.  
  255.     /**
  256.      * A test ended.
  257.      *
  258.      * @param  string  $testName
  259.      * @access public
  260.      * @abstract
  261.      */
  262.     public abstract function testEnded($testName);
  263.  
  264.     /**
  265.      * A test failed.
  266.      *
  267.      * @param  integer                                 $status
  268.      * @param  PHPUnit2_Framework_Test                 $test
  269.      * @param  PHPUnit2_Framework_AssertionFailedError $e
  270.      * @access public
  271.      * @abstract
  272.      */
  273.     public abstract function testFailed($status, PHPUnit2_Framework_Test $test, PHPUnit2_Framework_AssertionFailedError $e);
  274. }
  275.  
  276. /*
  277.  * Local variables:
  278.  * tab-width: 4
  279.  * c-basic-offset: 4
  280.  * c-hanging-comment-ender-p: nil
  281.  * End:
  282.  */
  283. ?>
  284.