3 * base include file for SimpleTest
5 * @subpackage UnitTester
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.
153 * @param string $message Text of error formatted by
157 function paintError($message) {
158 $this->_exceptions++
;
162 * Deals with PHP 5 throwing an exception.
163 * @param Exception $exception The actual exception thrown.
166 function paintException($exception) {
167 $this->_exceptions++
;
171 * Prints the message for skipping tests.
172 * @param string $message Text of skip condition.
175 function paintSkip($message) {
179 * Accessor for the number of passes so far.
180 * @return integer Number of passes.
183 function getPassCount() {
184 return $this->_passes
;
188 * Accessor for the number of fails so far.
189 * @return integer Number of fails.
192 function getFailCount() {
193 return $this->_fails
;
197 * Accessor for the number of untrapped errors
199 * @return integer Number of exceptions.
202 function getExceptionCount() {
203 return $this->_exceptions
;
207 * Paints a simple supplementary message.
208 * @param string $message Text to display.
211 function paintMessage($message) {
215 * Paints a formatted ASCII message such as a
217 * @param string $message Text to display.
220 function paintFormattedMessage($message) {
224 * By default just ignores user generated events.
225 * @param string $type Event type as text.
226 * @param mixed $payload Message or object.
229 function paintSignal($type, $payload) {
234 * Recipient of generated test messages that can display
235 * page footers and headers. Also keeps track of the
236 * test nesting. This is the main base class on which
237 * to build the finished test (page based) displays.
238 * @package SimpleTest
239 * @subpackage UnitTester
241 class SimpleReporter
extends SimpleScorer
{
247 * Starts the display with no results in.
250 function SimpleReporter() {
251 $this->SimpleScorer();
252 $this->_test_stack
= array();
254 $this->_progress
= 0;
258 * Gets the formatter for variables and other small
259 * generic data items.
260 * @return SimpleDumper Formatter.
263 function getDumper() {
264 return new SimpleDumper();
268 * Paints the start of a group test. Will also paint
269 * the page header and footer if this is the
270 * first test. Will stash the size if the first
272 * @param string $test_name Name of test that is starting.
273 * @param integer $size Number of test cases starting.
276 function paintGroupStart($test_name, $size) {
277 if (! isset($this->_size
)) {
278 $this->_size
= $size;
280 if (count($this->_test_stack
) == 0) {
281 $this->paintHeader($test_name);
283 $this->_test_stack
[] = $test_name;
287 * Paints the end of a group test. Will paint the page
288 * footer if the stack of tests has unwound.
289 * @param string $test_name Name of test that is ending.
290 * @param integer $progress Number of test cases ending.
293 function paintGroupEnd($test_name) {
294 array_pop($this->_test_stack
);
295 if (count($this->_test_stack
) == 0) {
296 $this->paintFooter($test_name);
301 * Paints the start of a test case. Will also paint
302 * the page header and footer if this is the
303 * first test. Will stash the size if the first
305 * @param string $test_name Name of test that is starting.
308 function paintCaseStart($test_name) {
309 if (! isset($this->_size
)) {
312 if (count($this->_test_stack
) == 0) {
313 $this->paintHeader($test_name);
315 $this->_test_stack
[] = $test_name;
319 * Paints the end of a test case. Will paint the page
320 * footer if the stack of tests has unwound.
321 * @param string $test_name Name of test that is ending.
324 function paintCaseEnd($test_name) {
326 array_pop($this->_test_stack
);
327 if (count($this->_test_stack
) == 0) {
328 $this->paintFooter($test_name);
333 * Paints the start of a test method.
334 * @param string $test_name Name of test that is starting.
337 function paintMethodStart($test_name) {
338 $this->_test_stack
[] = $test_name;
342 * Paints the end of a test method. Will paint the page
343 * footer if the stack of tests has unwound.
344 * @param string $test_name Name of test that is ending.
347 function paintMethodEnd($test_name) {
348 array_pop($this->_test_stack
);
352 * Paints the test document header.
353 * @param string $test_name First test top level
358 function paintHeader($test_name) {
362 * Paints the test document footer.
363 * @param string $test_name The top level test.
367 function paintFooter($test_name) {
371 * Accessor for internal test stack. For
372 * subclasses that need to see the whole test
373 * history for display purposes.
374 * @return array List of methods in nesting order.
377 function getTestList() {
378 return $this->_test_stack
;
382 * Accessor for total test size in number
383 * of test cases. Null until the first
385 * @return integer Total number of cases at start.
388 function getTestCaseCount() {
393 * Accessor for the number of test cases
395 * @return integer Number of ended cases.
398 function getTestCaseProgress() {
399 return $this->_progress
;
403 * Static check for running in the comand line.
404 * @return boolean True if CLI.
409 return php_sapi_name() == 'cli';
414 * For modifying the behaviour of the visual reporters.
415 * @package SimpleTest
416 * @subpackage UnitTester
418 class SimpleReporterDecorator
{
422 * Mediates between the reporter and the test case.
423 * @param SimpleScorer $reporter Reporter to receive events.
425 function SimpleReporterDecorator(&$reporter) {
426 $this->_reporter
= &$reporter;
430 * Signals that the next evaluation will be a dry
431 * run. That is, the structure events will be
432 * recorded, but no tests will be run.
433 * @param boolean $is_dry Dry run if true.
436 function makeDry($is_dry = true) {
437 $this->_reporter
->makeDry($is_dry);
441 * Accessor for current status. Will be false
442 * if there have been any failures or exceptions.
443 * Used for command line tools.
444 * @return boolean True if no failures.
447 function getStatus() {
448 return $this->_reporter
->getStatus();
452 * The reporter has a veto on what should be run.
453 * @param string $test_case_name name of test case.
454 * @param string $method Name of test method.
455 * @return boolean True if test should be run.
458 function shouldInvoke($test_case_name, $method) {
459 return $this->_reporter
->shouldInvoke($test_case_name, $method);
463 * Can wrap the invoker in preperation for running
465 * @param SimpleInvoker $invoker Individual test runner.
466 * @return SimpleInvoker Wrapped test runner.
469 function &createInvoker(&$invoker) {
470 return $this->_reporter
->createInvoker($invoker);
474 * Gets the formatter for variables and other small
475 * generic data items.
476 * @return SimpleDumper Formatter.
479 function getDumper() {
480 return $this->_reporter
->getDumper();
484 * Paints the start of a group test.
485 * @param string $test_name Name of test or other label.
486 * @param integer $size Number of test cases starting.
489 function paintGroupStart($test_name, $size) {
490 $this->_reporter
->paintGroupStart($test_name, $size);
494 * Paints the end of a group test.
495 * @param string $test_name Name of test or other label.
498 function paintGroupEnd($test_name) {
499 $this->_reporter
->paintGroupEnd($test_name);
503 * Paints the start of a test case.
504 * @param string $test_name Name of test or other label.
507 function paintCaseStart($test_name) {
508 $this->_reporter
->paintCaseStart($test_name);
512 * Paints the end of a test case.
513 * @param string $test_name Name of test or other label.
516 function paintCaseEnd($test_name) {
517 $this->_reporter
->paintCaseEnd($test_name);
521 * Paints the start of a test method.
522 * @param string $test_name Name of test or other label.
525 function paintMethodStart($test_name) {
526 $this->_reporter
->paintMethodStart($test_name);
530 * Paints the end of a test method.
531 * @param string $test_name Name of test or other label.
534 function paintMethodEnd($test_name) {
535 $this->_reporter
->paintMethodEnd($test_name);
539 * Chains to the wrapped reporter.
540 * @param string $message Message is ignored.
543 function paintPass($message) {
544 $this->_reporter
->paintPass($message);
548 * Chains to the wrapped reporter.
549 * @param string $message Message is ignored.
552 function paintFail($message) {
553 $this->_reporter
->paintFail($message);
557 * Chains to the wrapped reporter.
558 * @param string $message Text of error formatted by
562 function paintError($message) {
563 $this->_reporter
->paintError($message);
567 * Chains to the wrapped reporter.
568 * @param Exception $exception Exception to show.
571 function paintException($exception) {
572 $this->_reporter
->paintException($exception);
576 * Prints the message for skipping tests.
577 * @param string $message Text of skip condition.
580 function paintSkip($message) {
581 $this->_reporter
->paintSkip($message);
585 * Chains to the wrapped reporter.
586 * @param string $message Text to display.
589 function paintMessage($message) {
590 $this->_reporter
->paintMessage($message);
594 * Chains to the wrapped reporter.
595 * @param string $message Text to display.
598 function paintFormattedMessage($message) {
599 $this->_reporter
->paintFormattedMessage($message);
603 * Chains to the wrapped reporter.
604 * @param string $type Event type as text.
605 * @param mixed $payload Message or object.
606 * @return boolean Should return false if this
607 * type of signal should fail the
611 function paintSignal($type, &$payload) {
612 $this->_reporter
->paintSignal($type, $payload);
617 * For sending messages to multiple reporters at
619 * @package SimpleTest
620 * @subpackage UnitTester
622 class MultipleReporter
{
623 var $_reporters = array();
626 * Adds a reporter to the subscriber list.
627 * @param SimpleScorer $reporter Reporter to receive events.
630 function attachReporter(&$reporter) {
631 $this->_reporters
[] = &$reporter;
635 * Signals that the next evaluation will be a dry
636 * run. That is, the structure events will be
637 * recorded, but no tests will be run.
638 * @param boolean $is_dry Dry run if true.
641 function makeDry($is_dry = true) {
642 for ($i = 0; $i < count($this->_reporters
); $i++
) {
643 $this->_reporters
[$i]->makeDry($is_dry);
648 * Accessor for current status. Will be false
649 * if there have been any failures or exceptions.
650 * If any reporter reports a failure, the whole
652 * @return boolean True if no failures.
655 function getStatus() {
656 for ($i = 0; $i < count($this->_reporters
); $i++
) {
657 if (! $this->_reporters
[$i]->getStatus()) {
665 * The reporter has a veto on what should be run.
666 * It requires all reporters to want to run the method.
667 * @param string $test_case_name name of test case.
668 * @param string $method Name of test method.
671 function shouldInvoke($test_case_name, $method) {
672 for ($i = 0; $i < count($this->_reporters
); $i++
) {
673 if (! $this->_reporters
[$i]->shouldInvoke($test_case_name, $method)) {
681 * Every reporter gets a chance to wrap the invoker.
682 * @param SimpleInvoker $invoker Individual test runner.
683 * @return SimpleInvoker Wrapped test runner.
686 function &createInvoker(&$invoker) {
687 for ($i = 0; $i < count($this->_reporters
); $i++
) {
688 $invoker = &$this->_reporters
[$i]->createInvoker($invoker);
694 * Gets the formatter for variables and other small
695 * generic data items.
696 * @return SimpleDumper Formatter.
699 function getDumper() {
700 return new SimpleDumper();
704 * Paints the start of a group test.
705 * @param string $test_name Name of test or other label.
706 * @param integer $size Number of test cases starting.
709 function paintGroupStart($test_name, $size) {
710 for ($i = 0; $i < count($this->_reporters
); $i++
) {
711 $this->_reporters
[$i]->paintGroupStart($test_name, $size);
716 * Paints the end of a group test.
717 * @param string $test_name Name of test or other label.
720 function paintGroupEnd($test_name) {
721 for ($i = 0; $i < count($this->_reporters
); $i++
) {
722 $this->_reporters
[$i]->paintGroupEnd($test_name);
727 * Paints the start of a test case.
728 * @param string $test_name Name of test or other label.
731 function paintCaseStart($test_name) {
732 for ($i = 0; $i < count($this->_reporters
); $i++
) {
733 $this->_reporters
[$i]->paintCaseStart($test_name);
738 * Paints the end of a test case.
739 * @param string $test_name Name of test or other label.
742 function paintCaseEnd($test_name) {
743 for ($i = 0; $i < count($this->_reporters
); $i++
) {
744 $this->_reporters
[$i]->paintCaseEnd($test_name);
749 * Paints the start of a test method.
750 * @param string $test_name Name of test or other label.
753 function paintMethodStart($test_name) {
754 for ($i = 0; $i < count($this->_reporters
); $i++
) {
755 $this->_reporters
[$i]->paintMethodStart($test_name);
760 * Paints the end of a test method.
761 * @param string $test_name Name of test or other label.
764 function paintMethodEnd($test_name) {
765 for ($i = 0; $i < count($this->_reporters
); $i++
) {
766 $this->_reporters
[$i]->paintMethodEnd($test_name);
771 * Chains to the wrapped reporter.
772 * @param string $message Message is ignored.
775 function paintPass($message) {
776 for ($i = 0; $i < count($this->_reporters
); $i++
) {
777 $this->_reporters
[$i]->paintPass($message);
782 * Chains to the wrapped reporter.
783 * @param string $message Message is ignored.
786 function paintFail($message) {
787 for ($i = 0; $i < count($this->_reporters
); $i++
) {
788 $this->_reporters
[$i]->paintFail($message);
793 * Chains to the wrapped reporter.
794 * @param string $message Text of error formatted by
798 function paintError($message) {
799 for ($i = 0; $i < count($this->_reporters
); $i++
) {
800 $this->_reporters
[$i]->paintError($message);
805 * Chains to the wrapped reporter.
806 * @param Exception $exception Exception to display.
809 function paintException($exception) {
810 for ($i = 0; $i < count($this->_reporters
); $i++
) {
811 $this->_reporters
[$i]->paintException($exception);
816 * Prints the message for skipping tests.
817 * @param string $message Text of skip condition.
820 function paintSkip($message) {
821 for ($i = 0; $i < count($this->_reporters
); $i++
) {
822 $this->_reporters
[$i]->paintSkip($message);
827 * Chains to the wrapped reporter.
828 * @param string $message Text to display.
831 function paintMessage($message) {
832 for ($i = 0; $i < count($this->_reporters
); $i++
) {
833 $this->_reporters
[$i]->paintMessage($message);
838 * Chains to the wrapped reporter.
839 * @param string $message Text to display.
842 function paintFormattedMessage($message) {
843 for ($i = 0; $i < count($this->_reporters
); $i++
) {
844 $this->_reporters
[$i]->paintFormattedMessage($message);
849 * Chains to the wrapped reporter.
850 * @param string $type Event type as text.
851 * @param mixed $payload Message or object.
852 * @return boolean Should return false if this
853 * type of signal should fail the
857 function paintSignal($type, &$payload) {
858 for ($i = 0; $i < count($this->_reporters
); $i++
) {
859 $this->_reporters
[$i]->paintSignal($type, $payload);