3 // +------------------------------------------------------------------------+
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 // +------------------------------------------------------------------------+
15 // $Id: TestSuite.php,v 1.12 2004/10/01 06:11:39 sebastian Exp $
18 require_once 'PHPUnit/TestCase.php';
21 * A TestSuite is a Composite of Tests. It runs a collection of test cases.
23 * Here is an example using the dynamic test definition.
27 * $suite = new PHPUnit_TestSuite();
28 * $suite->addTest(new MathTest('testPass'));
32 * Alternatively, a TestSuite can extract the tests to be run automatically.
33 * To do so you pass the classname of your TestCase class to the TestSuite
38 * $suite = new TestSuite('classname');
42 * This constructor creates a suite with all the methods starting with
43 * "test" that take no arguments.
45 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
46 * @copyright Copyright © 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
47 * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
51 class PHPUnit_TestSuite
{
53 * The name of the test suite.
61 * The tests in the test suite.
66 var $_tests = array();
69 * Constructs a TestSuite.
74 function PHPUnit_TestSuite($test = FALSE) {
75 if ($test !== FALSE) {
76 $this->setName($test);
77 $this->addTestSuite($test);
82 * Adds a test to the suite.
87 function addTest(&$test) {
88 $this->_tests
[] = $test;
92 * Adds the tests from the given class to the suite.
97 function addTestSuite($testClass) {
98 if (class_exists($testClass)) {
99 $methods = get_class_methods($testClass);
100 $parentClasses = array(strtolower($testClass));
101 $parentClass = $testClass;
103 while(is_string($parentClass = get_parent_class($parentClass))) {
104 $parentClasses[] = $parentClass;
107 foreach ($methods as $method) {
108 if (substr($method, 0, 4) == 'test' &&
109 !in_array($method, $parentClasses)) {
110 $this->addTest(new $testClass($method));
117 * Counts the number of test cases that will be run by this test.
122 function countTestCases() {
125 foreach ($this->_tests
as $test) {
126 $count +
= $test->countTestCases();
133 * Returns the name of the suite.
143 * Runs the tests and collects their result in a TestResult.
148 function run(&$result) {
149 for ($i = 0; $i < sizeof($this->_tests
) && !$result->shouldStop(); $i++
) {
150 $this->_tests
[$i]->run($result);
161 function runTest(&$test, &$result) {
166 * Sets the name of the suite.
171 function setName($name) {
172 $this->_name
= $name;
176 * Returns the test at the given index.
182 function &testAt($index) {
183 if (isset($this->_tests
[$index])) {
184 return $this->_tests
[$index];
191 * Returns the number of tests in this suite.
196 function testCount() {
197 return sizeof($this->_tests
);
201 * Returns the tests as an enumeration.
207 return $this->_tests
;
211 * Returns a string representation of the test suite.
216 function toString() {