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