adding some strings
[moodle-linuxchix.git] / lib / simpletestlib / scorer.php
blob279d714dbfbee4ea1683f2157fa7bb01d999bba1
1 <?php
2 /**
3 * base include file for SimpleTest
4 * @package SimpleTest
5 * @subpackage UnitTester
6 * @version $Id$
7 */
9 /**#@+*/
10 require_once(dirname(__FILE__) . '/invoker.php');
11 /**#@-*/
13 /**
14 * Can recieve test events and display them. Display
15 * is achieved by making display methods available
16 * and visiting the incoming event.
17 * @package SimpleTest
18 * @subpackage UnitTester
19 * @abstract
21 class SimpleScorer {
22 var $_passes;
23 var $_fails;
24 var $_exceptions;
25 var $_is_dry_run;
27 /**
28 * Starts the test run with no results.
29 * @access public
31 function SimpleScorer() {
32 $this->_passes = 0;
33 $this->_fails = 0;
34 $this->_exceptions = 0;
35 $this->_is_dry_run = false;
38 /**
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.
43 * @access public
45 function makeDry($is_dry = true) {
46 $this->_is_dry_run = $is_dry;
49 /**
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.
53 * @access public
55 function shouldInvoke($test_case_name, $method) {
56 return ! $this->_is_dry_run;
59 /**
60 * Can wrap the invoker in preperation for running
61 * a test.
62 * @param SimpleInvoker $invoker Individual test runner.
63 * @return SimpleInvoker Wrapped test runner.
64 * @access public
66 function &createInvoker(&$invoker) {
67 return $invoker;
70 /**
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.
75 * @access public
77 function getStatus() {
78 if ($this->_exceptions + $this->_fails > 0) {
79 return false;
81 return true;
84 /**
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.
88 * @access public
90 function paintGroupStart($test_name, $size) {
93 /**
94 * Paints the end of a group test.
95 * @param string $test_name Name of test or other label.
96 * @access public
98 function paintGroupEnd($test_name) {
102 * Paints the start of a test case.
103 * @param string $test_name Name of test or other label.
104 * @access public
106 function paintCaseStart($test_name) {
110 * Paints the end of a test case.
111 * @param string $test_name Name of test or other label.
112 * @access public
114 function paintCaseEnd($test_name) {
118 * Paints the start of a test method.
119 * @param string $test_name Name of test or other label.
120 * @access public
122 function paintMethodStart($test_name) {
126 * Paints the end of a test method.
127 * @param string $test_name Name of test or other label.
128 * @access public
130 function paintMethodEnd($test_name) {
134 * Increments the pass count.
135 * @param string $message Message is ignored.
136 * @access public
138 function paintPass($message) {
139 $this->_passes++;
143 * Increments the fail count.
144 * @param string $message Message is ignored.
145 * @access public
147 function paintFail($message) {
148 $this->_fails++;
152 * Deals with PHP 4 throwing an error.
153 * @param string $message Text of error formatted by
154 * the test case.
155 * @access public
157 function paintError($message) {
158 $this->_exceptions++;
162 * Deals with PHP 5 throwing an exception.
163 * @param Exception $exception The actual exception thrown.
164 * @access public
166 function paintException($exception) {
167 $this->_exceptions++;
171 * Prints the message for skipping tests.
172 * @param string $message Text of skip condition.
173 * @access public
175 function paintSkip($message) {
179 * Accessor for the number of passes so far.
180 * @return integer Number of passes.
181 * @access public
183 function getPassCount() {
184 return $this->_passes;
188 * Accessor for the number of fails so far.
189 * @return integer Number of fails.
190 * @access public
192 function getFailCount() {
193 return $this->_fails;
197 * Accessor for the number of untrapped errors
198 * so far.
199 * @return integer Number of exceptions.
200 * @access public
202 function getExceptionCount() {
203 return $this->_exceptions;
207 * Paints a simple supplementary message.
208 * @param string $message Text to display.
209 * @access public
211 function paintMessage($message) {
215 * Paints a formatted ASCII message such as a
216 * variable dump.
217 * @param string $message Text to display.
218 * @access public
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.
227 * @access public
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 {
242 var $_test_stack;
243 var $_size;
244 var $_progress;
247 * Starts the display with no results in.
248 * @access public
250 function SimpleReporter() {
251 $this->SimpleScorer();
252 $this->_test_stack = array();
253 $this->_size = null;
254 $this->_progress = 0;
258 * Gets the formatter for variables and other small
259 * generic data items.
260 * @return SimpleDumper Formatter.
261 * @access public
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
271 * start.
272 * @param string $test_name Name of test that is starting.
273 * @param integer $size Number of test cases starting.
274 * @access public
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.
291 * @access public
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
304 * start.
305 * @param string $test_name Name of test that is starting.
306 * @access public
308 function paintCaseStart($test_name) {
309 if (! isset($this->_size)) {
310 $this->_size = 1;
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.
322 * @access public
324 function paintCaseEnd($test_name) {
325 $this->_progress++;
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.
335 * @access public
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.
345 * @access public
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
354 * to start.
355 * @access public
356 * @abstract
358 function paintHeader($test_name) {
362 * Paints the test document footer.
363 * @param string $test_name The top level test.
364 * @access public
365 * @abstract
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.
375 * @access public
377 function getTestList() {
378 return $this->_test_stack;
382 * Accessor for total test size in number
383 * of test cases. Null until the first
384 * test is started.
385 * @return integer Total number of cases at start.
386 * @access public
388 function getTestCaseCount() {
389 return $this->_size;
393 * Accessor for the number of test cases
394 * completed so far.
395 * @return integer Number of ended cases.
396 * @access public
398 function getTestCaseProgress() {
399 return $this->_progress;
403 * Static check for running in the comand line.
404 * @return boolean True if CLI.
405 * @access public
406 * @static
408 function inCli() {
409 return php_sapi_name() == 'cli';
414 * For modifying the behaviour of the visual reporters.
415 * @package SimpleTest
416 * @subpackage UnitTester
418 class SimpleReporterDecorator {
419 var $_reporter;
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.
434 * @access public
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.
445 * @access public
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.
456 * @access public
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
464 * a test.
465 * @param SimpleInvoker $invoker Individual test runner.
466 * @return SimpleInvoker Wrapped test runner.
467 * @access public
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.
477 * @access public
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.
487 * @access public
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.
496 * @access public
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.
505 * @access public
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.
514 * @access public
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.
523 * @access public
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.
532 * @access public
534 function paintMethodEnd($test_name) {
535 $this->_reporter->paintMethodEnd($test_name);
539 * Chains to the wrapped reporter.
540 * @param string $message Message is ignored.
541 * @access public
543 function paintPass($message) {
544 $this->_reporter->paintPass($message);
548 * Chains to the wrapped reporter.
549 * @param string $message Message is ignored.
550 * @access public
552 function paintFail($message) {
553 $this->_reporter->paintFail($message);
557 * Chains to the wrapped reporter.
558 * @param string $message Text of error formatted by
559 * the test case.
560 * @access public
562 function paintError($message) {
563 $this->_reporter->paintError($message);
567 * Chains to the wrapped reporter.
568 * @param Exception $exception Exception to show.
569 * @access public
571 function paintException($exception) {
572 $this->_reporter->paintException($exception);
576 * Prints the message for skipping tests.
577 * @param string $message Text of skip condition.
578 * @access public
580 function paintSkip($message) {
581 $this->_reporter->paintSkip($message);
585 * Chains to the wrapped reporter.
586 * @param string $message Text to display.
587 * @access public
589 function paintMessage($message) {
590 $this->_reporter->paintMessage($message);
594 * Chains to the wrapped reporter.
595 * @param string $message Text to display.
596 * @access public
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
608 * test suite.
609 * @access public
611 function paintSignal($type, &$payload) {
612 $this->_reporter->paintSignal($type, $payload);
617 * For sending messages to multiple reporters at
618 * the same time.
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.
628 * @access public
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.
639 * @access public
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
651 * suite fails.
652 * @return boolean True if no failures.
653 * @access public
655 function getStatus() {
656 for ($i = 0; $i < count($this->_reporters); $i++) {
657 if (! $this->_reporters[$i]->getStatus()) {
658 return false;
661 return true;
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.
669 * @access public
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)) {
674 return false;
677 return true;
681 * Every reporter gets a chance to wrap the invoker.
682 * @param SimpleInvoker $invoker Individual test runner.
683 * @return SimpleInvoker Wrapped test runner.
684 * @access public
686 function &createInvoker(&$invoker) {
687 for ($i = 0; $i < count($this->_reporters); $i++) {
688 $invoker = &$this->_reporters[$i]->createInvoker($invoker);
690 return $invoker;
694 * Gets the formatter for variables and other small
695 * generic data items.
696 * @return SimpleDumper Formatter.
697 * @access public
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.
707 * @access public
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.
718 * @access public
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.
729 * @access public
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.
740 * @access public
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.
751 * @access public
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.
762 * @access public
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.
773 * @access public
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.
784 * @access public
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
795 * the test case.
796 * @access public
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.
807 * @access public
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.
818 * @access public
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.
829 * @access public
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.
840 * @access public
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
854 * test suite.
855 * @access public
857 function paintSignal($type, &$payload) {
858 for ($i = 0; $i < count($this->_reporters); $i++) {
859 $this->_reporters[$i]->paintSignal($type, $payload);