home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / TestSuite.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  5.0 KB  |  211 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: TestSuite.php,v 1.6 2003/04/24 05:41:46 sebastian Exp $
  16. //
  17.  
  18. require_once 'PHPUnit/TestCase.php';
  19.  
  20. /**
  21.  * A TestSuite is a Composite of Tests. It runs a collection of test cases.
  22.  *
  23.  * Here is an example using the dynamic test definition. 
  24.  *
  25.  *   $suite = new PHPUnit_TestSuite();
  26.  *   $suite->addTest(new MathTest('testPass'));
  27.  *
  28.  * Alternatively, a TestSuite can extract the tests to be run automatically.
  29.  * To do so you pass the classname of your TestCase class to the TestSuite
  30.  * constructor. 
  31.  *
  32.  *   $suite = new TestSuite('classname');
  33.  *
  34.  * This constructor creates a suite with all the methods starting with
  35.  * "test" that take no arguments.
  36.  *
  37.  * @package PHPUnit
  38.  * @author  Sebastian Bergmann <sb@sebastian-bergmann.de>
  39.  *          Based upon JUnit, see http://www.junit.org/ for details.
  40.  */
  41. class PHPUnit_TestSuite {
  42.     /**
  43.     * The name of the test suite.
  44.     *
  45.     * @var    string
  46.     * @access private
  47.     */
  48.     var $_name = '';
  49.  
  50.     /**
  51.     * The tests in the test suite.
  52.     *
  53.     * @var    array
  54.     * @access private
  55.     */
  56.     var $_tests = array();
  57.  
  58.     /**
  59.     * Constructs a TestSuite.
  60.     *
  61.     * @param  mixed
  62.     * @access public
  63.     */
  64.     function PHPUnit_TestSuite($test = false) {
  65.         if ($test != false) {
  66.             $this->setName($test);
  67.             $this->addTestSuite($test);
  68.         }
  69.     }
  70.  
  71.     /**
  72.     * Adds a test to the suite.
  73.     *
  74.     * @param  object
  75.     * @access public
  76.     */
  77.     function addTest(&$test) {
  78.         $this->_tests[] = $test;
  79.     }
  80.  
  81.     /**
  82.     * Adds the tests from the given class to the suite.
  83.     *
  84.     * @param  string
  85.     * @access public
  86.     */
  87.     function addTestSuite($testClass) {
  88.         if (class_exists($testClass)) {
  89.             $methods       = get_class_methods($testClass);
  90.             $parentClasses = array(strtolower($testClass));
  91.             $parentClass   = $testClass;
  92.  
  93.             while(is_string($parentClass = get_parent_class($parentClass))) {
  94.                 $parentClasses[] = $parentClass;
  95.             }
  96.  
  97.             foreach ($methods as $method) {
  98.                 if (substr($method, 0, 4) == 'test' &&
  99.                     !in_array($method, $parentClasses)) {
  100.                     $this->addTest(new $testClass($method));
  101.                 }
  102.             }
  103.         }
  104.     }
  105.  
  106.     /**
  107.     * Counts the number of test cases that will be run by this test.
  108.     *
  109.     * @return integer
  110.     * @access public
  111.     */
  112.     function countTestCases() {
  113.         $count = 0;
  114.  
  115.         foreach ($this->_tests as $test) {
  116.             $count += $test->countTestCases();
  117.         }
  118.  
  119.         return $count;
  120.     }
  121.  
  122.     /**
  123.     * Returns the name of the suite.
  124.     *
  125.     * @return string
  126.     * @access public
  127.     */
  128.     function getName() {
  129.         return $this->_name;
  130.     }
  131.  
  132.     /**
  133.     * Runs the tests and collects their result in a TestResult.
  134.     *
  135.     * @param  object
  136.     * @access public
  137.     */
  138.     function run(&$result) {
  139.         for ($i = 0; $i < sizeof($this->_tests) && !$result->shouldStop(); $i++) {
  140.             $this->_tests[$i]->run($result);
  141.         }
  142.     }
  143.  
  144.     /**
  145.     * Runs a test.
  146.     *
  147.     * @param  object
  148.     * @param  object
  149.     * @access public
  150.     */
  151.     function runTest(&$test, &$result) {
  152.         $test->run($result);
  153.     }
  154.  
  155.     /**
  156.     * Sets the name of the suite.
  157.     *
  158.     * @param  string
  159.     * @access public
  160.     */
  161.     function setName($name) {
  162.         $this->_name = $name;
  163.     }
  164.  
  165.     /**
  166.     * Returns the test at the given index.
  167.     *
  168.     * @param  integer
  169.     * @return object
  170.     * @access public
  171.     */
  172.     function &testAt($index) {
  173.         if (isset($this->_tests[$index])) {
  174.             return $this->_tests[$index];
  175.         } else {
  176.             return false;
  177.         }
  178.     }
  179.  
  180.     /**
  181.     * Returns the number of tests in this suite.
  182.     *
  183.     * @return integer
  184.     * @access public
  185.     */
  186.     function testCount() {
  187.         return sizeof($this->_tests);
  188.     }
  189.  
  190.     /**
  191.     * Returns the tests as an enumeration.
  192.     *
  193.     * @return array
  194.     * @access public
  195.     */
  196.     function &tests() {
  197.         return $this->_tests;
  198.     }
  199.  
  200.     /**
  201.     * Returns a string representation of the test suite.
  202.     *
  203.     * @return string
  204.     * @access public
  205.     */
  206.     function toString() {
  207.         return '';
  208.     }
  209. }
  210. ?>
  211.