Merge branch 'MDL-39444_23' of git://github.com/timhunt/moodle into MOODLE_23_STABLE
[moodle.git] / lib / simpletestlib / reporter.php
blobbd4f3fa41dd2e4c569e07a88a66734851981dfc2
1 <?php
2 /**
3 * base include file for SimpleTest
4 * @package SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: reporter.php 2005 2010-11-02 14:09:34Z lastcraft $
7 */
9 /**#@+
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__) . '/scorer.php');
13 //require_once(dirname(__FILE__) . '/arguments.php');
14 /**#@-*/
16 /**
17 * Sample minimal test displayer. Generates only
18 * failure messages and a pass count.
19 * @package SimpleTest
20 * @subpackage UnitTester
22 class HtmlReporter extends SimpleReporter {
23 private $character_set;
25 /**
26 * Does nothing yet. The first output will
27 * be sent on the first test start. For use
28 * by a web browser.
29 * @access public
31 function __construct($character_set = 'ISO-8859-1') {
32 parent::__construct();
33 $this->character_set = $character_set;
36 /**
37 * Paints the top of the web page setting the
38 * title to the name of the starting test.
39 * @param string $test_name Name class of test.
40 * @access public
42 function paintHeader($test_name) {
43 $this->sendNoCacheHeaders();
44 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
45 print "<html>\n<head>\n<title>$test_name</title>\n";
46 print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" .
47 $this->character_set . "\">\n";
48 print "<style type=\"text/css\">\n";
49 print $this->getCss() . "\n";
50 print "</style>\n";
51 print "</head>\n<body>\n";
52 print "<h1>$test_name</h1>\n";
53 flush();
56 /**
57 * Send the headers necessary to ensure the page is
58 * reloaded on every request. Otherwise you could be
59 * scratching your head over out of date test data.
60 * @access public
62 static function sendNoCacheHeaders() {
63 if (! headers_sent()) {
64 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
65 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
66 header("Cache-Control: no-store, no-cache, must-revalidate");
67 header("Cache-Control: post-check=0, pre-check=0", false);
68 header("Pragma: no-cache");
72 /**
73 * Paints the CSS. Add additional styles here.
74 * @return string CSS code as text.
75 * @access protected
77 protected function getCss() {
78 return ".fail { background-color: inherit; color: red; }" .
79 ".pass { background-color: inherit; color: green; }" .
80 " pre { background-color: lightgray; color: inherit; }";
83 /**
84 * Paints the end of the test with a summary of
85 * the passes and failures.
86 * @param string $test_name Name class of test.
87 * @access public
89 function paintFooter($test_name) {
90 $colour = ($this->getFailCount() + $this->getExceptionCount() > 0 ? "red" : "green");
91 print "<div style=\"";
92 print "padding: 8px; margin-top: 1em; background-color: $colour; color: white;";
93 print "\">";
94 print $this->getTestCaseProgress() . "/" . $this->getTestCaseCount();
95 print " test cases complete:\n";
96 print "<strong>" . $this->getPassCount() . "</strong> passes, ";
97 print "<strong>" . $this->getFailCount() . "</strong> fails and ";
98 print "<strong>" . $this->getExceptionCount() . "</strong> exceptions.";
99 print "</div>\n";
100 print "</body>\n</html>\n";
104 * Paints the test failure with a breadcrumbs
105 * trail of the nesting test suites below the
106 * top level test.
107 * @param string $message Failure message displayed in
108 * the context of the other tests.
110 function paintFail($message) {
111 parent::paintFail($message);
112 print "<span class=\"fail\">Fail</span>: ";
113 $breadcrumb = $this->getTestList();
114 array_shift($breadcrumb);
115 print implode(" -&gt; ", $breadcrumb);
116 print " -&gt; " . $this->htmlEntities($message) . "<br />\n";
120 * Paints a PHP error.
121 * @param string $message Message is ignored.
122 * @access public
124 function paintError($message) {
125 parent::paintError($message);
126 print "<span class=\"fail\">Exception</span>: ";
127 $breadcrumb = $this->getTestList();
128 array_shift($breadcrumb);
129 print implode(" -&gt; ", $breadcrumb);
130 print " -&gt; <strong>" . $this->htmlEntities($message) . "</strong><br />\n";
134 * Paints a PHP exception.
135 * @param Exception $exception Exception to display.
136 * @access public
138 function paintException($exception) {
139 parent::paintException($exception);
140 print "<span class=\"fail\">Exception</span>: ";
141 $breadcrumb = $this->getTestList();
142 array_shift($breadcrumb);
143 print implode(" -&gt; ", $breadcrumb);
144 $message = 'Unexpected exception of type [' . get_class($exception) .
145 '] with message ['. $exception->getMessage() .
146 '] in ['. $exception->getFile() .
147 ' line ' . $exception->getLine() . ']';
148 print " -&gt; <strong>" . $this->htmlEntities($message) . "</strong><br />\n";
152 * Prints the message for skipping tests.
153 * @param string $message Text of skip condition.
154 * @access public
156 function paintSkip($message) {
157 parent::paintSkip($message);
158 print "<span class=\"pass\">Skipped</span>: ";
159 $breadcrumb = $this->getTestList();
160 array_shift($breadcrumb);
161 print implode(" -&gt; ", $breadcrumb);
162 print " -&gt; " . $this->htmlEntities($message) . "<br />\n";
166 * Paints formatted text such as dumped privateiables.
167 * @param string $message Text to show.
168 * @access public
170 function paintFormattedMessage($message) {
171 print '<pre>' . $this->htmlEntities($message) . '</pre>';
175 * Character set adjusted entity conversion.
176 * @param string $message Plain text or Unicode message.
177 * @return string Browser readable message.
178 * @access protected
180 protected function htmlEntities($message) {
181 return htmlentities($message, ENT_COMPAT, $this->character_set);
186 * Sample minimal test displayer. Generates only
187 * failure messages and a pass count. For command
188 * line use. I've tried to make it look like JUnit,
189 * but I wanted to output the errors as they arrived
190 * which meant dropping the dots.
191 * @package SimpleTest
192 * @subpackage UnitTester
194 class TextReporter extends SimpleReporter {
197 * Does nothing yet. The first output will
198 * be sent on the first test start.
200 function __construct() {
201 parent::__construct();
205 * Paints the title only.
206 * @param string $test_name Name class of test.
207 * @access public
209 function paintHeader($test_name) {
210 if (! SimpleReporter::inCli()) {
211 header('Content-type: text/plain');
213 print "$test_name\n";
214 flush();
218 * Paints the end of the test with a summary of
219 * the passes and failures.
220 * @param string $test_name Name class of test.
221 * @access public
223 function paintFooter($test_name) {
224 if ($this->getFailCount() + $this->getExceptionCount() == 0) {
225 print "OK\n";
226 } else {
227 print "FAILURES!!!\n";
229 print "Test cases run: " . $this->getTestCaseProgress() .
230 "/" . $this->getTestCaseCount() .
231 ", Passes: " . $this->getPassCount() .
232 ", Failures: " . $this->getFailCount() .
233 ", Exceptions: " . $this->getExceptionCount() . "\n";
237 * Paints the test failure as a stack trace.
238 * @param string $message Failure message displayed in
239 * the context of the other tests.
240 * @access public
242 function paintFail($message) {
243 parent::paintFail($message);
244 print $this->getFailCount() . ") $message\n";
245 $breadcrumb = $this->getTestList();
246 array_shift($breadcrumb);
247 print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
248 print "\n";
252 * Paints a PHP error or exception.
253 * @param string $message Message to be shown.
254 * @access public
255 * @abstract
257 function paintError($message) {
258 parent::paintError($message);
259 print "Exception " . $this->getExceptionCount() . "!\n$message\n";
260 $breadcrumb = $this->getTestList();
261 array_shift($breadcrumb);
262 print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
263 print "\n";
267 * Paints a PHP error or exception.
268 * @param Exception $exception Exception to describe.
269 * @access public
270 * @abstract
272 function paintException($exception) {
273 parent::paintException($exception);
274 $message = 'Unexpected exception of type [' . get_class($exception) .
275 '] with message ['. $exception->getMessage() .
276 '] in ['. $exception->getFile() .
277 ' line ' . $exception->getLine() . ']';
278 print "Exception " . $this->getExceptionCount() . "!\n$message\n";
279 $breadcrumb = $this->getTestList();
280 array_shift($breadcrumb);
281 print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
282 print "\n";
286 * Prints the message for skipping tests.
287 * @param string $message Text of skip condition.
288 * @access public
290 function paintSkip($message) {
291 parent::paintSkip($message);
292 print "Skip: $message\n";
296 * Paints formatted text such as dumped privateiables.
297 * @param string $message Text to show.
298 * @access public
300 function paintFormattedMessage($message) {
301 print "$message\n";
302 flush();
307 * Runs just a single test group, a single case or
308 * even a single test within that case.
309 * @package SimpleTest
310 * @subpackage UnitTester
312 class SelectiveReporter extends SimpleReporterDecorator {
313 private $just_this_case = false;
314 private $just_this_test = false;
315 private $on;
318 * Selects the test case or group to be run,
319 * and optionally a specific test.
320 * @param SimpleScorer $reporter Reporter to receive events.
321 * @param string $just_this_case Only this case or group will run.
322 * @param string $just_this_test Only this test method will run.
324 function __construct($reporter, $just_this_case = false, $just_this_test = false) {
325 if (isset($just_this_case) && $just_this_case) {
326 $this->just_this_case = strtolower($just_this_case);
327 $this->off();
328 } else {
329 $this->on();
331 if (isset($just_this_test) && $just_this_test) {
332 $this->just_this_test = strtolower($just_this_test);
334 parent::__construct($reporter);
338 * Compares criteria to actual the case/group name.
339 * @param string $test_case The incoming test.
340 * @return boolean True if matched.
341 * @access protected
343 protected function matchesTestCase($test_case) {
344 return $this->just_this_case == strtolower($test_case);
348 * Compares criteria to actual the test name. If no
349 * name was specified at the beginning, then all tests
350 * can run.
351 * @param string $method The incoming test method.
352 * @return boolean True if matched.
353 * @access protected
355 protected function shouldRunTest($test_case, $method) {
356 if ($this->isOn() || $this->matchesTestCase($test_case)) {
357 if ($this->just_this_test) {
358 return $this->just_this_test == strtolower($method);
359 } else {
360 return true;
363 return false;
367 * Switch on testing for the group or subgroup.
368 * @access private
370 protected function on() {
371 $this->on = true;
375 * Switch off testing for the group or subgroup.
376 * @access private
378 protected function off() {
379 $this->on = false;
383 * Is this group actually being tested?
384 * @return boolean True if the current test group is active.
385 * @access private
387 protected function isOn() {
388 return $this->on;
392 * Veto everything that doesn't match the method wanted.
393 * @param string $test_case Name of test case.
394 * @param string $method Name of test method.
395 * @return boolean True if test should be run.
396 * @access public
398 function shouldInvoke($test_case, $method) {
399 if ($this->shouldRunTest($test_case, $method)) {
400 return $this->reporter->shouldInvoke($test_case, $method);
402 return false;
406 * Paints the start of a group test.
407 * @param string $test_case Name of test or other label.
408 * @param integer $size Number of test cases starting.
409 * @access public
411 function paintGroupStart($test_case, $size) {
412 if ($this->just_this_case && $this->matchesTestCase($test_case)) {
413 $this->on();
415 $this->reporter->paintGroupStart($test_case, $size);
419 * Paints the end of a group test.
420 * @param string $test_case Name of test or other label.
421 * @access public
423 function paintGroupEnd($test_case) {
424 $this->reporter->paintGroupEnd($test_case);
425 if ($this->just_this_case && $this->matchesTestCase($test_case)) {
426 $this->off();
432 * Suppresses skip messages.
433 * @package SimpleTest
434 * @subpackage UnitTester
436 class NoSkipsReporter extends SimpleReporterDecorator {
439 * Does nothing.
440 * @param string $message Text of skip condition.
441 * @access public
443 function paintSkip($message) { }