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 / Extensions / TestSetup.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  5.0 KB  |  155 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: TestSetup.php,v 1.13.2.6 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/TestSuite.php';
  50. require_once 'PHPUnit2/Extensions/TestDecorator.php';
  51.  
  52. /**
  53.  * A Decorator to set up and tear down additional fixture state.
  54.  * Subclass TestSetup and insert it into your tests when you want
  55.  * to set up additional state once before the tests are run.
  56.  *
  57.  * @category   Testing
  58.  * @package    PHPUnit2
  59.  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
  60.  * @copyright  2002-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>
  61.  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
  62.  * @version    Release: 2.3.6
  63.  * @link       http://pear.php.net/package/PHPUnit2
  64.  * @since      Class available since Release 2.0.0
  65.  */
  66. class PHPUnit2_Extensions_TestSetup extends PHPUnit2_Extensions_TestDecorator {
  67.     /**
  68.      * Runs the decorated test and collects the
  69.      * result in a TestResult.
  70.      *
  71.      * @param  PHPUnit2_Framework_TestResult $result
  72.      * @return PHPUnit2_Framework_TestResult
  73.      * @throws Exception
  74.      * @access public
  75.      */
  76.     public function run($result = NULL) {
  77.         if ($result === NULL) {
  78.             $result = $this->createResult();
  79.         }
  80.  
  81.         // XXX: Workaround for missing ability to declare type-hinted parameters as optional.
  82.         else if (!($result instanceof PHPUnit2_Framework_TestResult)) {
  83.             throw new Exception(
  84.               'Argument 1 must be an instance of PHPUnit2_Framework_TestResult.'
  85.             );
  86.         }
  87.  
  88.         $this->setUp();
  89.         $this->copyFixtureToTest();
  90.         $this->basicRun($result);
  91.         $this->tearDown();
  92.  
  93.         return $result;
  94.     }
  95.  
  96.     /**
  97.      * Copies the fixture set up by setUp() to the test.
  98.      *
  99.      * @access private
  100.      * @since  Method available since Release 2.3.0
  101.      */
  102.     private function copyFixtureToTest() {
  103.         $object = new ReflectionClass($this);
  104.  
  105.         foreach ($object->getProperties() as $property) {
  106.             $name = $property->getName();
  107.  
  108.             if ($name != 'test') {
  109.                 $this->doCopyFixtureToTest($this->test, $name, $this->$name);
  110.             }
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * @access private
  116.      * @since  Method available since Release 2.3.0
  117.      */
  118.     private function doCopyFixtureToTest($object, $name, &$value) {
  119.         if ($object instanceof PHPUnit2_Framework_TestSuite) {
  120.             foreach ($object->tests() as $test) {
  121.                 $this->doCopyFixtureToTest($test, $name, $value);
  122.             }
  123.         } else {
  124.             $object->$name =& $value;
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Sets up the fixture. Override to set up additional fixture
  130.      * state.
  131.      *
  132.      * @access protected
  133.      */
  134.     protected function setUp() {
  135.     }
  136.  
  137.     /**
  138.      * Tears down the fixture. Override to tear down the additional
  139.      * fixture state.
  140.      *
  141.      * @access protected
  142.      */
  143.     protected function tearDown() {
  144.     }
  145. }
  146.  
  147. /*
  148.  * Local variables:
  149.  * tab-width: 4
  150.  * c-basic-offset: 4
  151.  * c-hanging-comment-ender-p: nil
  152.  * End:
  153.  */
  154. ?>
  155.