3 * base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: scorer.php,v 1.12 2006/02/06 06:05:18 lastcraft Exp $
10 require_once(dirname(__FILE__
) . '/invoker.php');
14 * Can recieve test events and display them. Display
15 * is achieved by making display methods available
16 * and visiting the incoming event.
18 * @subpackage UnitTester
28 * Starts the test run with no results.
31 function SimpleScorer() {
34 $this->_exceptions
= 0;
35 $this->_is_dry_run
= false;
39 * Signals that the next evaluation will be a dry
40 * run. That is, the structure events will be
41 * recorded, but no tests will be run.
42 * @param boolean $is_dry Dry run if true.
45 function makeDry($is_dry = true) {
46 $this->_is_dry_run
= $is_dry;
50 * The reporter has a veto on what should be run.
51 * @param string $test_case_name name of test case.
52 * @param string $method Name of test method.
55 function shouldInvoke($test_case_name, $method) {
56 return ! $this->_is_dry_run
;
60 * Can wrap the invoker in preperation for running
62 * @param SimpleInvoker $invoker Individual test runner.
63 * @return SimpleInvoker Wrapped test runner.
66 function &createInvoker(&$invoker) {
71 * Accessor for current status. Will be false
72 * if there have been any failures or exceptions.
73 * Used for command line tools.
74 * @return boolean True if no failures.
77 function getStatus() {
78 if ($this->_exceptions +
$this->_fails
> 0) {
85 * Paints the start of a group test.
86 * @param string $test_name Name of test or other label.
87 * @param integer $size Number of test cases starting.
90 function paintGroupStart($test_name, $size) {
94 * Paints the end of a group test.
95 * @param string $test_name Name of test or other label.
98 function paintGroupEnd($test_name) {
102 * Paints the start of a test case.
103 * @param string $test_name Name of test or other label.
106 function paintCaseStart($test_name) {
110 * Paints the end of a test case.
111 * @param string $test_name Name of test or other label.
114 function paintCaseEnd($test_name) {
118 * Paints the start of a test method.
119 * @param string $test_name Name of test or other label.
122 function paintMethodStart($test_name) {
126 * Paints the end of a test method.
127 * @param string $test_name Name of test or other label.
130 function paintMethodEnd($test_name) {
134 * Increments the pass count.
135 * @param string $message Message is ignored.
138 function paintPass($message) {
143 * Increments the fail count.
144 * @param string $message Message is ignored.
147 function paintFail($message) {
152 * Deals with PHP 4 throwing an error or PHP 5
153 * throwing an exception.
154 * @param string $message Text of error formatted by
158 function paintError($message) {
159 $this->_exceptions++
;
163 * Accessor for the number of passes so far.
164 * @return integer Number of passes.
167 function getPassCount() {
168 return $this->_passes
;
172 * Accessor for the number of fails so far.
173 * @return integer Number of fails.
176 function getFailCount() {
177 return $this->_fails
;
181 * Accessor for the number of untrapped errors
183 * @return integer Number of exceptions.
186 function getExceptionCount() {
187 return $this->_exceptions
;
191 * Paints a simple supplementary message.
192 * @param string $message Text to display.
195 function paintMessage($message) {
199 * Paints a formatted ASCII message such as a
201 * @param string $message Text to display.
204 function paintFormattedMessage($message) {
208 * By default just ignores user generated events.
209 * @param string $type Event type as text.
210 * @param mixed $payload Message or object.
213 function paintSignal($type, $payload) {
218 * Recipient of generated test messages that can display
219 * page footers and headers. Also keeps track of the
220 * test nesting. This is the main base class on which
221 * to build the finished test (page based) displays.
222 * @package SimpleTest
223 * @subpackage UnitTester
225 class SimpleReporter
extends SimpleScorer
{
231 * Starts the display with no results in.
234 function SimpleReporter() {
235 $this->SimpleScorer();
236 $this->_test_stack
= array();
238 $this->_progress
= 0;
242 * Paints the start of a group test. Will also paint
243 * the page header and footer if this is the
244 * first test. Will stash the size if the first
246 * @param string $test_name Name of test that is starting.
247 * @param integer $size Number of test cases starting.
250 function paintGroupStart($test_name, $size) {
251 if (! isset($this->_size
)) {
252 $this->_size
= $size;
254 if (count($this->_test_stack
) == 0) {
255 $this->paintHeader($test_name);
257 $this->_test_stack
[] = $test_name;
261 * Paints the end of a group test. Will paint the page
262 * footer if the stack of tests has unwound.
263 * @param string $test_name Name of test that is ending.
264 * @param integer $progress Number of test cases ending.
267 function paintGroupEnd($test_name) {
268 array_pop($this->_test_stack
);
269 if (count($this->_test_stack
) == 0) {
270 $this->paintFooter($test_name);
275 * Paints the start of a test case. Will also paint
276 * the page header and footer if this is the
277 * first test. Will stash the size if the first
279 * @param string $test_name Name of test that is starting.
282 function paintCaseStart($test_name) {
283 if (! isset($this->_size
)) {
286 if (count($this->_test_stack
) == 0) {
287 $this->paintHeader($test_name);
289 $this->_test_stack
[] = $test_name;
293 * Paints the end of a test case. Will paint the page
294 * footer if the stack of tests has unwound.
295 * @param string $test_name Name of test that is ending.
298 function paintCaseEnd($test_name) {
300 array_pop($this->_test_stack
);
301 if (count($this->_test_stack
) == 0) {
302 $this->paintFooter($test_name);
307 * Paints the start of a test method.
308 * @param string $test_name Name of test that is starting.
311 function paintMethodStart($test_name) {
312 $this->_test_stack
[] = $test_name;
316 * Paints the end of a test method. Will paint the page
317 * footer if the stack of tests has unwound.
318 * @param string $test_name Name of test that is ending.
321 function paintMethodEnd($test_name) {
322 array_pop($this->_test_stack
);
326 * Paints the test document header.
327 * @param string $test_name First test top level
332 function paintHeader($test_name) {
336 * Paints the test document footer.
337 * @param string $test_name The top level test.
341 function paintFooter($test_name) {
345 * Accessor for internal test stack. For
346 * subclasses that need to see the whole test
347 * history for display purposes.
348 * @return array List of methods in nesting order.
351 function getTestList() {
352 return $this->_test_stack
;
356 * Accessor for total test size in number
357 * of test cases. Null until the first
359 * @return integer Total number of cases at start.
362 function getTestCaseCount() {
367 * Accessor for the number of test cases
369 * @return integer Number of ended cases.
372 function getTestCaseProgress() {
373 return $this->_progress
;
377 * Static check for running in the comand line.
378 * @return boolean True if CLI.
383 return php_sapi_name() == 'cli';
388 * For modifying the behaviour of the visual reporters.
389 * @package SimpleTest
390 * @subpackage UnitTester
392 class SimpleReporterDecorator
{
396 * Mediates between teh reporter and the test case.
397 * @param SimpleScorer $reporter Reporter to receive events.
399 function SimpleReporterDecorator(&$reporter) {
400 $this->_reporter
= &$reporter;
404 * Signals that the next evaluation will be a dry
405 * run. That is, the structure events will be
406 * recorded, but no tests will be run.
407 * @param boolean $is_dry Dry run if true.
410 function makeDry($is_dry = true) {
411 $this->_reporter
->makeDry($is_dry);
415 * Accessor for current status. Will be false
416 * if there have been any failures or exceptions.
417 * Used for command line tools.
418 * @return boolean True if no failures.
421 function getStatus() {
422 return $this->_reporter
->getStatus();
426 * The reporter has a veto on what should be run.
427 * @param string $test_case_name name of test case.
428 * @param string $method Name of test method.
429 * @return boolean True if test should be run.
432 function shouldInvoke($test_case_name, $method) {
433 return $this->_reporter
->shouldInvoke($test_case_name, $method);
437 * Can wrap the invoker in preperation for running
439 * @param SimpleInvoker $invoker Individual test runner.
440 * @return SimpleInvoker Wrapped test runner.
443 function &createInvoker(&$invoker) {
444 return $this->_reporter
->createInvoker($invoker);
448 * Paints the start of a group test.
449 * @param string $test_name Name of test or other label.
450 * @param integer $size Number of test cases starting.
453 function paintGroupStart($test_name, $size) {
454 $this->_reporter
->paintGroupStart($test_name, $size);
458 * Paints the end of a group test.
459 * @param string $test_name Name of test or other label.
462 function paintGroupEnd($test_name) {
463 $this->_reporter
->paintGroupEnd($test_name);
467 * Paints the start of a test case.
468 * @param string $test_name Name of test or other label.
471 function paintCaseStart($test_name) {
472 $this->_reporter
->paintCaseStart($test_name);
476 * Paints the end of a test case.
477 * @param string $test_name Name of test or other label.
480 function paintCaseEnd($test_name) {
481 $this->_reporter
->paintCaseEnd($test_name);
485 * Paints the start of a test method.
486 * @param string $test_name Name of test or other label.
489 function paintMethodStart($test_name) {
490 $this->_reporter
->paintMethodStart($test_name);
494 * Paints the end of a test method.
495 * @param string $test_name Name of test or other label.
498 function paintMethodEnd($test_name) {
499 $this->_reporter
->paintMethodEnd($test_name);
503 * Chains to the wrapped reporter.
504 * @param string $message Message is ignored.
507 function paintPass($message) {
508 $this->_reporter
->paintPass($message);
512 * Chains to the wrapped reporter.
513 * @param string $message Message is ignored.
516 function paintFail($message) {
517 $this->_reporter
->paintFail($message);
521 * Chains to the wrapped reporter.
522 * @param string $message Text of error formatted by
526 function paintError($message) {
527 $this->_reporter
->paintError($message);
531 * Chains to the wrapped reporter.
532 * @param string $message Text to display.
535 function paintMessage($message) {
536 $this->_reporter
->paintMessage($message);
540 * Chains to the wrapped reporter.
541 * @param string $message Text to display.
544 function paintFormattedMessage($message) {
545 $this->_reporter
->paintFormattedMessage($message);
549 * Chains to the wrapped reporter.
550 * @param string $type Event type as text.
551 * @param mixed $payload Message or object.
552 * @return boolean Should return false if this
553 * type of signal should fail the
557 function paintSignal($type, &$payload) {
558 $this->_reporter
->paintSignal($type, $payload);
563 * For sending messages to multiple reporters at
565 * @package SimpleTest
566 * @subpackage UnitTester
568 class MultipleReporter
{
569 var $_reporters = array();
572 * Adds a reporter to the subscriber list.
573 * @param SimpleScorer $reporter Reporter to receive events.
576 function attachReporter(&$reporter) {
577 $this->_reporters
[] = &$reporter;
581 * Signals that the next evaluation will be a dry
582 * run. That is, the structure events will be
583 * recorded, but no tests will be run.
584 * @param boolean $is_dry Dry run if true.
587 function makeDry($is_dry = true) {
588 for ($i = 0; $i < count($this->_reporters
); $i++
) {
589 $this->_reporters
[$i]->makeDry($is_dry);
594 * Accessor for current status. Will be false
595 * if there have been any failures or exceptions.
596 * If any reporter reports a failure, the whole
598 * @return boolean True if no failures.
601 function getStatus() {
602 for ($i = 0; $i < count($this->_reporters
); $i++
) {
603 if (! $this->_reporters
[$i]->getStatus()) {
611 * The reporter has a veto on what should be run.
612 * It requires all reporters to want to run the method.
613 * @param string $test_case_name name of test case.
614 * @param string $method Name of test method.
617 function shouldInvoke($test_case_name, $method) {
618 for ($i = 0; $i < count($this->_reporters
); $i++
) {
619 if (! $this->_reporters
[$i]->shouldInvoke($test_case_name, $method)) {
627 * Every reporter gets a chance to wrap the invoker.
628 * @param SimpleInvoker $invoker Individual test runner.
629 * @return SimpleInvoker Wrapped test runner.
632 function &createInvoker(&$invoker) {
633 for ($i = 0; $i < count($this->_reporters
); $i++
) {
634 $invoker = &$this->_reporters
[$i]->createInvoker($invoker);
640 * Paints the start of a group test.
641 * @param string $test_name Name of test or other label.
642 * @param integer $size Number of test cases starting.
645 function paintGroupStart($test_name, $size) {
646 for ($i = 0; $i < count($this->_reporters
); $i++
) {
647 $this->_reporters
[$i]->paintGroupStart($test_name, $size);
652 * Paints the end of a group test.
653 * @param string $test_name Name of test or other label.
656 function paintGroupEnd($test_name) {
657 for ($i = 0; $i < count($this->_reporters
); $i++
) {
658 $this->_reporters
[$i]->paintGroupEnd($test_name);
663 * Paints the start of a test case.
664 * @param string $test_name Name of test or other label.
667 function paintCaseStart($test_name) {
668 for ($i = 0; $i < count($this->_reporters
); $i++
) {
669 $this->_reporters
[$i]->paintCaseStart($test_name);
674 * Paints the end of a test case.
675 * @param string $test_name Name of test or other label.
678 function paintCaseEnd($test_name) {
679 for ($i = 0; $i < count($this->_reporters
); $i++
) {
680 $this->_reporters
[$i]->paintCaseEnd($test_name);
685 * Paints the start of a test method.
686 * @param string $test_name Name of test or other label.
689 function paintMethodStart($test_name) {
690 for ($i = 0; $i < count($this->_reporters
); $i++
) {
691 $this->_reporters
[$i]->paintMethodStart($test_name);
696 * Paints the end of a test method.
697 * @param string $test_name Name of test or other label.
700 function paintMethodEnd($test_name) {
701 for ($i = 0; $i < count($this->_reporters
); $i++
) {
702 $this->_reporters
[$i]->paintMethodEnd($test_name);
707 * Chains to the wrapped reporter.
708 * @param string $message Message is ignored.
711 function paintPass($message) {
712 for ($i = 0; $i < count($this->_reporters
); $i++
) {
713 $this->_reporters
[$i]->paintPass($message);
718 * Chains to the wrapped reporter.
719 * @param string $message Message is ignored.
722 function paintFail($message) {
723 for ($i = 0; $i < count($this->_reporters
); $i++
) {
724 $this->_reporters
[$i]->paintFail($message);
729 * Chains to the wrapped reporter.
730 * @param string $message Text of error formatted by
734 function paintError($message) {
735 for ($i = 0; $i < count($this->_reporters
); $i++
) {
736 $this->_reporters
[$i]->paintError($message);
741 * Chains to the wrapped reporter.
742 * @param string $message Text to display.
745 function paintMessage($message) {
746 for ($i = 0; $i < count($this->_reporters
); $i++
) {
747 $this->_reporters
[$i]->paintMessage($message);
752 * Chains to the wrapped reporter.
753 * @param string $message Text to display.
756 function paintFormattedMessage($message) {
757 for ($i = 0; $i < count($this->_reporters
); $i++
) {
758 $this->_reporters
[$i]->paintFormattedMessage($message);
763 * Chains to the wrapped reporter.
764 * @param string $type Event type as text.
765 * @param mixed $payload Message or object.
766 * @return boolean Should return false if this
767 * type of signal should fail the
771 function paintSignal($type, &$payload) {
772 for ($i = 0; $i < count($this->_reporters
); $i++
) {
773 $this->_reporters
[$i]->paintSignal($type, $payload);