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 / TestCase.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  7.8 KB  |  293 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: TestCase.php,v 1.32.2.5 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/Assert.php';
  50. require_once 'PHPUnit2/Framework/Error.php';
  51. require_once 'PHPUnit2/Framework/Test.php';
  52. require_once 'PHPUnit2/Framework/TestResult.php';
  53.  
  54. /**
  55.  * A TestCase defines the fixture to run multiple tests.
  56.  *
  57.  * To define a TestCase
  58.  *
  59.  *   1) Implement a subclass of PHPUnit2_Framework_TestCase.
  60.  *   2) Define instance variables that store the state of the fixture.
  61.  *   3) Initialize the fixture state by overriding setUp().
  62.  *   4) Clean-up after a test by overriding tearDown().
  63.  *
  64.  * Each test runs in its own fixture so there can be no side effects
  65.  * among test runs.
  66.  *
  67.  * Here is an example:
  68.  *
  69.  * <code>
  70.  * <?php
  71.  * require_once 'PHPUnit2/Framework/TestCase.php';
  72.  *
  73.  * class MathTest extends PHPUnit2_Framework_TestCase {
  74.  *     public $value1;
  75.  *     public $value2;
  76.  *
  77.  *     public function __construct($name) {
  78.  *         parent::__construct($name);
  79.  *     }
  80.  *
  81.  *     public function setUp() {
  82.  *         $this->value1 = 2;
  83.  *         $this->value2 = 3;
  84.  *     }
  85.  * }
  86.  * ?>
  87.  * </code>
  88.  *
  89.  * For each test implement a method which interacts with the fixture.
  90.  * Verify the expected results with assertions specified by calling
  91.  * assert with a boolean.
  92.  *
  93.  * <code>
  94.  * <?php
  95.  * public function testPass() {
  96.  *     $this->assertTrue($this->value1 + $this->value2 == 5);
  97.  * }
  98.  * ?>
  99.  * </code>
  100.  *
  101.  * @category   Testing
  102.  * @package    PHPUnit2
  103.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  104.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  105.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  106.  * @version    Release: 2.3.6
  107.  * @link       http://pear.php.net/package/PHPUnit2
  108.  * @since      Class available since Release 2.0.0
  109.  * @abstract
  110.  */
  111. abstract class PHPUnit2_Framework_TestCase extends PHPUnit2_Framework_Assert implements PHPUnit2_Framework_Test {
  112.     /**
  113.      * The name of the test case.
  114.      *
  115.      * @var    string
  116.      * @access private
  117.      */
  118.     private $name = NULL;
  119.  
  120.     /**
  121.      * Constructs a test case with the given name.
  122.      *
  123.      * @param  string
  124.      * @access public
  125.      */
  126.     public function __construct($name = NULL) {
  127.         if ($name !== NULL) {
  128.             $this->setName($name);
  129.         }
  130.     }
  131.  
  132.     /**
  133.      * Returns a string representation of the test case.
  134.      *
  135.      * @return string
  136.      * @access public
  137.      */
  138.     public function toString() {
  139.         $class = new ReflectionClass($this);
  140.  
  141.         return sprintf(
  142.           '%s(%s)',
  143.  
  144.           $this->getName(),
  145.           $class->name
  146.         );
  147.     }
  148.  
  149.     /**
  150.      * Counts the number of test cases executed by run(TestResult result).
  151.      *
  152.      * @return integer
  153.      * @access public
  154.      */
  155.     public function countTestCases() {
  156.         return 1;
  157.     }
  158.  
  159.     /**
  160.      * Gets the name of a TestCase.
  161.      *
  162.      * @return string
  163.      * @access public
  164.      */
  165.     public function getName() {
  166.         return $this->name;
  167.     }
  168.  
  169.     /**
  170.      * Runs the test case and collects the results in a TestResult object.
  171.      * If no TestResult object is passed a new one will be created.
  172.      *
  173.      * @param  PHPUnit2_Framework_TestResult $result
  174.      * @return PHPUnit2_Framework_TestResult
  175.      * @throws Exception
  176.      * @access public
  177.      */
  178.     public function run($result = NULL) {
  179.         if ($result === NULL) {
  180.             $result = $this->createResult();
  181.         }
  182.  
  183.         // XXX: Workaround for missing ability to declare type-hinted parameters as optional.
  184.         else if (!($result instanceof PHPUnit2_Framework_TestResult)) {
  185.             throw new Exception(
  186.               'Argument 1 must be an instance of PHPUnit2_Framework_TestResult.'
  187.             );
  188.         }
  189.  
  190.         $result->run($this);
  191.  
  192.         return $result;
  193.     }
  194.  
  195.     /**
  196.      * Runs the bare test sequence.
  197.      *
  198.      * @access public
  199.      */
  200.     public function runBare() {
  201.         $catchedException = NULL;
  202.  
  203.         $this->setUp();
  204.  
  205.         try {
  206.             $this->runTest();
  207.         }
  208.  
  209.         catch (Exception $e) {
  210.             $catchedException = $e;
  211.         }
  212.  
  213.         $this->tearDown();
  214.  
  215.         // Workaround for missing "finally".
  216.         if ($catchedException !== NULL) {
  217.             throw $catchedException;
  218.         }
  219.     }
  220.  
  221.     /**
  222.      * Override to run the test and assert its state.
  223.      *
  224.      * @throws PHPUnit2_Framework_Error
  225.      * @access protected
  226.      */
  227.     protected function runTest() {
  228.         if ($this->name === NULL) {
  229.             throw new PHPUnit2_Framework_Error(
  230.               'PHPUnit2_Framework_TestCase::$name must not be NULL.'
  231.             );
  232.         }
  233.  
  234.         try {
  235.             $class  = new ReflectionClass($this);
  236.             $method = $class->getMethod($this->name);
  237.         }
  238.  
  239.         catch (ReflectionException $e) {
  240.             $this->fail($e->getMessage());
  241.         }
  242.  
  243.         $method->invoke($this);
  244.     }
  245.  
  246.     /**
  247.      * Sets the name of a TestCase.
  248.      *
  249.      * @param  string
  250.      * @access public
  251.      */
  252.     public function setName($name) {
  253.         $this->name = $name;
  254.     }
  255.  
  256.     /**
  257.      * Creates a default TestResult object.
  258.      *
  259.      * @return PHPUnit2_Framework_TestResult
  260.      * @access protected
  261.      */
  262.     protected function createResult() {
  263.         return new PHPUnit2_Framework_TestResult;
  264.     }
  265.  
  266.     /**
  267.      * Sets up the fixture, for example, open a network connection.
  268.      * This method is called before a test is executed.
  269.      *
  270.      * @access protected
  271.      */
  272.     protected function setUp() {
  273.     }
  274.  
  275.     /**
  276.      * Tears down the fixture, for example, close a network connection.
  277.      * This method is called after a test is executed.
  278.      *
  279.      * @access protected
  280.      */
  281.     protected function tearDown() {
  282.     }
  283. }
  284.  
  285. /*
  286.  * Local variables:
  287.  * tab-width: 4
  288.  * c-basic-offset: 4
  289.  * c-hanging-comment-ender-p: nil
  290.  * End:
  291.  */
  292. ?>
  293.