1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * PHP Version 4
6 *
7 * Copyright (c) 2002-2005, 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    PHPUnit
41 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
42 * @copyright  2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
43 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
44 * @version    CVS: $Id: TestSuite.php,v 1.17 2005/11/10 09:47:15 sebastian Exp $
45 * @link       http://pear.php.net/package/PHPUnit
46 * @since      File available since Release 1.0.0
47 */
48
49require_once 'PHPUnit/TestCase.php';
50
51/**
52 * A TestSuite is a Composite of Tests. It runs a collection of test cases.
53 *
54 * Here is an example using the dynamic test definition.
55 *
56 * <code>
57 * <?php
58 * $suite = new PHPUnit_TestSuite();
59 * $suite->addTest(new MathTest('testPass'));
60 * ?>
61 * </code>
62 *
63 * Alternatively, a TestSuite can extract the tests to be run automatically.
64 * To do so you pass the classname of your TestCase class to the TestSuite
65 * constructor.
66 *
67 * <code>
68 * <?php
69 * $suite = new TestSuite('classname');
70 * ?>
71 * </code>
72 *
73 * This constructor creates a suite with all the methods starting with
74 * "test" that take no arguments.
75 *
76 * @category   Testing
77 * @package    PHPUnit
78 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
79 * @copyright  2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
80 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
81 * @version    Release: 1.3.2
82 * @link       http://pear.php.net/package/PHPUnit
83 * @since      Class available since Release 1.0.0
84 */
85class PHPUnit_TestSuite {
86    /**
87     * The name of the test suite.
88     *
89     * @var    string
90     * @access private
91     */
92    var $_name = '';
93
94    /**
95     * The tests in the test suite.
96     *
97     * @var    array
98     * @access private
99     */
100    var $_tests = array();
101
102    /**
103     * Constructs a TestSuite.
104     *
105     * @param  mixed
106     * @access public
107     */
108    function PHPUnit_TestSuite($test = FALSE) {
109        if ($test !== FALSE) {
110            $this->setName($test);
111            $this->addTestSuite($test);
112        }
113    }
114
115    /**
116     * Adds a test to the suite.
117     *
118     * @param  object
119     * @access public
120     */
121    function addTest(&$test) {
122        $this->_tests[] = &$test;
123    }
124
125    /**
126     * Adds the tests from the given class to the suite.
127     *
128     * @param  string
129     * @access public
130     */
131    function addTestSuite($testClass) {
132        if (class_exists($testClass)) {
133            $methods       = get_class_methods($testClass);
134            $parentClasses = array(strtolower($testClass));
135            $parentClass   = $testClass;
136
137            while(is_string($parentClass = get_parent_class($parentClass))) {
138                $parentClasses[] = $parentClass;
139            }
140
141            foreach ($methods as $method) {
142                if (substr($method, 0, 4) == 'test' &&
143                    !in_array($method, $parentClasses)) {
144                    $this->addTest(new $testClass($method));
145                }
146            }
147        }
148    }
149
150    /**
151     * Counts the number of test cases that will be run by this test.
152     *
153     * @return integer
154     * @access public
155     */
156    function countTestCases() {
157        $count = 0;
158
159        foreach ($this->_tests as $test) {
160            $count += $test->countTestCases();
161        }
162
163        return $count;
164    }
165
166    /**
167     * Returns the name of the suite.
168     *
169     * @return string
170     * @access public
171     */
172    function getName() {
173        return $this->_name;
174    }
175
176    /**
177     * Runs the tests and collects their result in a TestResult.
178     *
179     * @param  object
180     * @access public
181     */
182    function run(&$result) {
183        for ($i = 0; $i < sizeof($this->_tests) && !$result->shouldStop(); $i++) {
184            $this->_tests[$i]->run($result);
185        }
186    }
187
188    /**
189     * Runs a test.
190     *
191     * @param  object
192     * @param  object
193     * @access public
194     */
195    function runTest(&$test, &$result) {
196        $test->run($result);
197    }
198
199    /**
200     * Sets the name of the suite.
201     *
202     * @param  string
203     * @access public
204     */
205    function setName($name) {
206        $this->_name = $name;
207    }
208
209    /**
210     * Returns the test at the given index.
211     *
212     * @param  integer
213     * @return object
214     * @access public
215     */
216    function &testAt($index) {
217        if (isset($this->_tests[$index])) {
218            return $this->_tests[$index];
219        } else {
220            return FALSE;
221        }
222    }
223
224    /**
225     * Returns the number of tests in this suite.
226     *
227     * @return integer
228     * @access public
229     */
230    function testCount() {
231        return sizeof($this->_tests);
232    }
233
234    /**
235     * Returns the tests as an enumeration.
236     *
237     * @return array
238     * @access public
239     */
240    function &tests() {
241        return $this->_tests;
242    }
243
244    /**
245     * Returns a string representation of the test suite.
246     *
247     * @return string
248     * @access public
249     */
250    function toString() {
251        return '';
252    }
253}
254
255/*
256 * Local variables:
257 * tab-width: 4
258 * c-basic-offset: 4
259 * c-hanging-comment-ender-p: nil
260 * End:
261 */
262?>
263