3 * Base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: runner.php,v 1.31 2005/08/03 23:25:13 lastcraft Exp $
10 * Includes SimpleTest files and defined the root constant
11 * for dependent libraries.
13 require_once(dirname(__FILE__
) . '/errors.php');
14 require_once(dirname(__FILE__
) . '/compatibility.php');
15 require_once(dirname(__FILE__
) . '/scorer.php');
16 require_once(dirname(__FILE__
) . '/expectation.php');
17 require_once(dirname(__FILE__
) . '/dumper.php');
18 if (! defined('SIMPLE_TEST')) {
19 define('SIMPLE_TEST', dirname(__FILE__
) . '/');
24 * This is called by the class runner to run a
25 * single test method. Will also run the setUp()
26 * and tearDown() methods.
28 * @subpackage UnitTester
34 * Stashes the test case for later.
35 * @param SimpleTestCase $test_case Test case to run.
37 function SimpleInvoker(&$test_case) {
38 $this->_test_case
= &$test_case;
42 * Accessor for test case being run.
43 * @return SimpleTestCase Test case.
46 function &getTestCase() {
47 return $this->_test_case
;
51 * Invokes a test method and buffered with setUp()
52 * and tearDown() calls.
53 * @param string $method Test method to call.
56 function invoke($method) {
57 $this->_test_case
->before($method);
58 $this->_test_case
->setUp();
59 $this->_test_case
->$method();
60 $this->_test_case
->tearDown();
61 $this->_test_case
->after($method);
66 * Do nothing decorator. Just passes the invocation
69 * @subpackage UnitTester
71 class SimpleInvokerDecorator
{
75 * Stores the invoker to wrap.
76 * @param SimpleInvoker $invoker Test method runner.
78 function SimpleInvokerDecorator(&$invoker) {
79 $this->_invoker
= &$invoker;
83 * Accessor for test case being run.
84 * @return SimpleTestCase Test case.
87 function &getTestCase() {
88 return $this->_invoker
->getTestCase();
92 * Invokes a test method and buffered with setUp()
93 * and tearDown() calls.
94 * @param string $method Test method to call.
97 function invoke($method) {
98 $this->_invoker
->invoke($method);
103 * Extension that traps errors into an error queue.
104 * @package SimpleTest
105 * @subpackage UnitTester
107 class SimpleErrorTrappingInvoker
extends SimpleInvokerDecorator
{
111 * Stores the invoker to wrap.
112 * @param SimpleInvoker $invoker Test method runner.
114 function SimpleErrorTrappingInvoker(&$invoker) {
115 $this->SimpleInvokerDecorator($invoker);
119 * Invokes a test method and dispatches any
120 * untrapped errors. Called back from
121 * the visiting runner.
122 * @param string $method Test method to call.
125 function invoke($method) {
126 set_error_handler('simpleTestErrorHandler');
127 parent
::invoke($method);
128 $queue = &SimpleErrorQueue
::instance();
129 while (list($severity, $message, $file, $line, $globals) = $queue->extract()) {
130 $test_case = &$this->getTestCase();
131 $test_case->error($severity, $message, $file, $line, $globals);
133 restore_error_handler();
138 * The standard runner. Will run every method starting
140 * @package SimpleTest
141 * @subpackage UnitTester
148 * Takes in the test case and reporter to mediate between.
149 * @param SimpleTestCase $test_case Test case to run.
150 * @param SimpleScorer $scorer Reporter to receive events.
152 function SimpleRunner(&$test_case, &$scorer) {
153 $this->_test_case
= &$test_case;
154 $this->_scorer
= &$scorer;
158 * Accessor for test case being run.
159 * @return SimpleTestCase Test case.
162 function &getTestCase() {
163 return $this->_test_case
;
167 * Runs the test methods in the test case, or not if the
169 * @param SimpleTest $test_case Test case to run test on.
170 * @param string $method Name of test method.
174 $methods = get_class_methods(get_class($this->_test_case
));
175 $invoker = &$this->_test_case
->createInvoker();
176 foreach ($methods as $method) {
177 if (! $this->_isTest($method)) {
180 if ($this->_isConstructor($method)) {
183 if ($this->_scorer
->shouldInvoke($this->_test_case
->getLabel(), $method)) {
184 $invoker->invoke($method);
190 * Tests to see if the method is the constructor and
191 * so should be ignored.
192 * @param string $method Method name to try.
193 * @return boolean True if constructor.
196 function _isConstructor($method) {
197 return SimpleTestCompatibility
::isA(
199 strtolower($method));
203 * Tests to see if the method is a test that should
204 * be run. Currently any method that starts with 'test'
206 * @param string $method Method name to try.
207 * @return boolean True if test method.
210 function _isTest($method) {
211 return strtolower(substr($method, 0, 4)) == 'test';
215 * Paints the start of a test method.
216 * @param string $test_name Name of test or other label.
219 function paintMethodStart($test_name) {
220 $this->_scorer
->paintMethodStart($test_name);
224 * Paints the end of a test method.
225 * @param string $test_name Name of test or other label.
228 function paintMethodEnd($test_name) {
229 $this->_scorer
->paintMethodEnd($test_name);
233 * Chains to the wrapped reporter.
234 * @param string $message Message is ignored.
237 function paintPass($message) {
238 $this->_scorer
->paintPass($message);
242 * Chains to the wrapped reporter.
243 * @param string $message Message is ignored.
246 function paintFail($message) {
247 $this->_scorer
->paintFail($message);
251 * Chains to the wrapped reporter.
252 * @param string $message Text of error formatted by
256 function paintError($message) {
257 $this->_scorer
->paintError($message);
261 * Chains to the wrapped reporter.
262 * @param Exception $exception Object thrown.
265 function paintException($exception) {
266 $this->_scorer
->paintException($exception);
270 * Chains to the wrapped reporter.
271 * @param string $message Text to display.
274 function paintMessage($message) {
275 $this->_scorer
->paintMessage($message);
279 * Chains to the wrapped reporter.
280 * @param string $message Text to display.
283 function paintFormattedMessage($message) {
284 $this->_scorer
->paintFormattedMessage($message);
288 * Chains to the wrapped reporter.
289 * @param string $type Event type as text.
290 * @param mixed $payload Message or object.
291 * @return boolean Should return false if this
292 * type of signal should fail the
296 function paintSignal($type, &$payload) {
297 $this->_scorer
->paintSignal($type, $payload);