3 * base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: reporter.php 2005 2010-11-02 14:09:34Z lastcraft $
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__
) . '/scorer.php');
13 //require_once(dirname(__FILE__) . '/arguments.php');
17 * Sample minimal test displayer. Generates only
18 * failure messages and a pass count.
20 * @subpackage UnitTester
22 class HtmlReporter
extends SimpleReporter
{
23 private $character_set;
26 * Does nothing yet. The first output will
27 * be sent on the first test start. For use
31 function __construct($character_set = 'ISO-8859-1') {
32 parent
::__construct();
33 $this->character_set
= $character_set;
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.
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";
51 print "</head>\n<body>\n";
52 print "<h1>$test_name</h1>\n";
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.
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");
73 * Paints the CSS. Add additional styles here.
74 * @return string CSS code as text.
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; }";
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.
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;";
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.";
100 print "</body>\n</html>\n";
104 * Paints the test failure with a breadcrumbs
105 * trail of the nesting test suites below the
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(" -> ", $breadcrumb);
116 print " -> " . $this->htmlEntities($message) . "<br />\n";
120 * Paints a PHP error.
121 * @param string $message Message is ignored.
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(" -> ", $breadcrumb);
130 print " -> <strong>" . $this->htmlEntities($message) . "</strong><br />\n";
134 * Paints a PHP exception.
135 * @param Exception $exception Exception to display.
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(" -> ", $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 " -> <strong>" . $this->htmlEntities($message) . "</strong><br />\n";
152 * Prints the message for skipping tests.
153 * @param string $message Text of skip condition.
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(" -> ", $breadcrumb);
162 print " -> " . $this->htmlEntities($message) . "<br />\n";
166 * Paints formatted text such as dumped privateiables.
167 * @param string $message Text to show.
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.
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.
209 function paintHeader($test_name) {
210 if (! SimpleReporter
::inCli()) {
211 header('Content-type: text/plain');
213 print "$test_name\n";
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.
223 function paintFooter($test_name) {
224 if ($this->getFailCount() +
$this->getExceptionCount() == 0) {
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.
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));
252 * Paints a PHP error or exception.
253 * @param string $message Message to be shown.
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));
267 * Paints a PHP error or exception.
268 * @param Exception $exception Exception to describe.
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));
286 * Prints the message for skipping tests.
287 * @param string $message Text of skip condition.
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.
300 function paintFormattedMessage($message) {
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;
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);
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.
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
351 * @param string $method The incoming test method.
352 * @return boolean True if matched.
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);
367 * Switch on testing for the group or subgroup.
370 protected function on() {
375 * Switch off testing for the group or subgroup.
378 protected function off() {
383 * Is this group actually being tested?
384 * @return boolean True if the current test group is active.
387 protected function isOn() {
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.
398 function shouldInvoke($test_case, $method) {
399 if ($this->shouldRunTest($test_case, $method)) {
400 return $this->reporter
->shouldInvoke($test_case, $method);
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.
411 function paintGroupStart($test_case, $size) {
412 if ($this->just_this_case
&& $this->matchesTestCase($test_case)) {
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.
423 function paintGroupEnd($test_case) {
424 $this->reporter
->paintGroupEnd($test_case);
425 if ($this->just_this_case
&& $this->matchesTestCase($test_case)) {
432 * Suppresses skip messages.
433 * @package SimpleTest
434 * @subpackage UnitTester
436 class NoSkipsReporter
extends SimpleReporterDecorator
{
440 * @param string $message Text of skip condition.
443 function paintSkip($message) { }