les commentaires dans l'historique sont en gras
[bazdig.git] / test / PHPUnit / TestSuite.php
blobe226bfc54cc810de5846c21b22ac9d777981ad1d
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 // +------------------------------------------------------------------------+
15 // $Id: TestSuite.php,v 1.12 2004/10/01 06:11:39 sebastian Exp $
18 require_once 'PHPUnit/TestCase.php';
20 /**
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.
25 * <code>
26 * <?php
27 * $suite = new PHPUnit_TestSuite();
28 * $suite->addTest(new MathTest('testPass'));
29 * ?>
30 * </code>
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
34 * constructor.
36 * <code>
37 * <?php
38 * $suite = new TestSuite('classname');
39 * ?>
40 * </code>
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 &copy; 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
47 * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
48 * @category Testing
49 * @package PHPUnit
51 class PHPUnit_TestSuite {
52 /**
53 * The name of the test suite.
55 * @var string
56 * @access private
58 var $_name = '';
60 /**
61 * The tests in the test suite.
63 * @var array
64 * @access private
66 var $_tests = array();
68 /**
69 * Constructs a TestSuite.
71 * @param mixed
72 * @access public
74 function PHPUnit_TestSuite($test = FALSE) {
75 if ($test !== FALSE) {
76 $this->setName($test);
77 $this->addTestSuite($test);
81 /**
82 * Adds a test to the suite.
84 * @param object
85 * @access public
87 function addTest(&$test) {
88 $this->_tests[] = $test;
91 /**
92 * Adds the tests from the given class to the suite.
94 * @param string
95 * @access public
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.
119 * @return integer
120 * @access public
122 function countTestCases() {
123 $count = 0;
125 foreach ($this->_tests as $test) {
126 $count += $test->countTestCases();
129 return $count;
133 * Returns the name of the suite.
135 * @return string
136 * @access public
138 function getName() {
139 return $this->_name;
143 * Runs the tests and collects their result in a TestResult.
145 * @param object
146 * @access public
148 function run(&$result) {
149 for ($i = 0; $i < sizeof($this->_tests) && !$result->shouldStop(); $i++) {
150 $this->_tests[$i]->run($result);
155 * Runs a test.
157 * @param object
158 * @param object
159 * @access public
161 function runTest(&$test, &$result) {
162 $test->run($result);
166 * Sets the name of the suite.
168 * @param string
169 * @access public
171 function setName($name) {
172 $this->_name = $name;
176 * Returns the test at the given index.
178 * @param integer
179 * @return object
180 * @access public
182 function &testAt($index) {
183 if (isset($this->_tests[$index])) {
184 return $this->_tests[$index];
185 } else {
186 return FALSE;
191 * Returns the number of tests in this suite.
193 * @return integer
194 * @access public
196 function testCount() {
197 return sizeof($this->_tests);
201 * Returns the tests as an enumeration.
203 * @return array
204 * @access public
206 function &tests() {
207 return $this->_tests;
211 * Returns a string representation of the test suite.
213 * @return string
214 * @access public
216 function toString() {
217 return '';