3 * base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: reporter.php,v 1.36 2006/02/06 06:05:18 lastcraft Exp $
10 * include other SimpleTest class files
12 require_once(dirname(__FILE__
) . '/scorer.php');
16 * Sample minimal test displayer. Generates only
17 * failure messages and a pass count.
19 * @subpackage UnitTester
21 class HtmlReporter
extends SimpleReporter
{
25 * Does nothing yet. The first output will
26 * be sent on the first test start. For use
30 function HtmlReporter($character_set = 'ISO-8859-1') {
31 $this->SimpleReporter();
32 $this->_character_set
= $character_set;
36 * Paints the top of the web page setting the
37 * title to the name of the starting test.
38 * @param string $test_name Name class of test.
41 function paintHeader($test_name) {
42 $this->sendNoCacheHeaders();
43 print "<html>\n<head>\n<title>$test_name</title>\n";
44 print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" .
45 $this->_character_set
. "\">\n";
46 print "<style type=\"text/css\">\n";
47 print $this->_getCss() . "\n";
49 print "</head>\n<body>\n";
50 print "<h1>$test_name</h1>\n";
55 * Send the headers necessary to ensure the page is
56 * reloaded on every request. Otherwise you could be
57 * scratching your head over out of date test data.
61 function sendNoCacheHeaders() {
62 if (! headers_sent()) {
63 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
64 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
65 header("Cache-Control: no-store, no-cache, must-revalidate");
66 header("Cache-Control: post-check=0, pre-check=0", false);
67 header("Pragma: no-cache");
72 * Paints the CSS. Add additional styles here.
73 * @return string CSS code as text.
77 return ".fail { color: red; } pre { background-color: lightgray; }";
81 * Paints the end of the test with a summary of
82 * the passes and failures.
83 * @param string $test_name Name class of test.
86 function paintFooter($test_name) {
87 $colour = ($this->getFailCount() +
$this->getExceptionCount() > 0 ?
"red" : "green");
88 print "<div style=\"";
89 print "padding: 8px; margin-top: 1em; background-color: $colour; color: white;";
91 print $this->getTestCaseProgress() . "/" . $this->getTestCaseCount();
92 print " test cases complete:\n";
93 print "<strong>" . $this->getPassCount() . "</strong> passes, ";
94 print "<strong>" . $this->getFailCount() . "</strong> fails and ";
95 print "<strong>" . $this->getExceptionCount() . "</strong> exceptions.";
97 print "</body>\n</html>\n";
101 * Paints the test failure with a breadcrumbs
102 * trail of the nesting test suites below the
104 * @param string $message Failure message displayed in
105 * the context of the other tests.
108 function paintFail($message) {
109 parent
::paintFail($message);
110 print "<span class=\"fail\">Fail</span>: ";
111 $breadcrumb = $this->getTestList();
112 array_shift($breadcrumb);
113 print implode(" -> ", $breadcrumb);
114 print " -> " . $this->_htmlEntities($message) . "<br />\n";
118 * Paints a PHP error or exception.
119 * @param string $message Message is ignored.
123 function paintError($message) {
124 parent
::paintError($message);
125 print "<span class=\"fail\">Exception</span>: ";
126 $breadcrumb = $this->getTestList();
127 array_shift($breadcrumb);
128 print implode(" -> ", $breadcrumb);
129 print " -> <strong>" . $this->_htmlEntities($message) . "</strong><br />\n";
133 * Paints formatted text such as dumped variables.
134 * @param string $message Text to show.
137 function paintFormattedMessage($message) {
138 print '<pre>' . $this->_htmlEntities($message) . '</pre>';
142 * Character set adjusted entity conversion.
143 * @param string $message Plain text or Unicode message.
144 * @return string Browser readable message.
147 function _htmlEntities($message) {
148 return htmlentities($message, ENT_COMPAT
, $this->_character_set
);
153 * Sample minimal test displayer. Generates only
154 * failure messages and a pass count. For command
155 * line use. I've tried to make it look like JUnit,
156 * but I wanted to output the errors as they arrived
157 * which meant dropping the dots.
158 * @package SimpleTest
159 * @subpackage UnitTester
161 class TextReporter
extends SimpleReporter
{
164 * Does nothing yet. The first output will
165 * be sent on the first test start.
168 function TextReporter() {
169 $this->SimpleReporter();
173 * Paints the title only.
174 * @param string $test_name Name class of test.
177 function paintHeader($test_name) {
178 if (! SimpleReporter
::inCli()) {
179 header('Content-type: text/plain');
181 print "$test_name\n";
186 * Paints the end of the test with a summary of
187 * the passes and failures.
188 * @param string $test_name Name class of test.
191 function paintFooter($test_name) {
192 if ($this->getFailCount() +
$this->getExceptionCount() == 0) {
195 print "FAILURES!!!\n";
197 print "Test cases run: " . $this->getTestCaseProgress() .
198 "/" . $this->getTestCaseCount() .
199 ", Passes: " . $this->getPassCount() .
200 ", Failures: " . $this->getFailCount() .
201 ", Exceptions: " . $this->getExceptionCount() . "\n";
205 * Paints the test failure as a stack trace.
206 * @param string $message Failure message displayed in
207 * the context of the other tests.
210 function paintFail($message) {
211 parent
::paintFail($message);
212 print $this->getFailCount() . ") $message\n";
213 $breadcrumb = $this->getTestList();
214 array_shift($breadcrumb);
215 print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
220 * Paints a PHP error or exception.
221 * @param string $message Message is ignored.
225 function paintError($message) {
226 parent
::paintError($message);
227 print "Exception " . $this->getExceptionCount() . "!\n$message\n";
231 * Paints formatted text such as dumped variables.
232 * @param string $message Text to show.
235 function paintFormattedMessage($message) {
242 * Runs just a single test group, a single case or
243 * even a single test within that case.
244 * @package SimpleTest
245 * @subpackage UnitTester
247 class SelectiveReporter
extends SimpleReporterDecorator
{
248 var $_just_this_case =false;
249 var $_just_this_test = false;
250 var $_within_test_case = true;
253 * Selects the test case or group to be run,
254 * and optionally a specific test.
255 * @param SimpleScorer $reporter Reporter to receive events.
256 * @param string $just_this_case Only this case or group will run.
257 * @param string $just_this_test Only this test method will run.
259 function SelectiveReporter(&$reporter, $just_this_case = false, $just_this_test = false) {
260 if (isset($just_this_case) && $just_this_case) {
261 $this->_just_this_case
= strtolower($just_this_case);
262 $this->_within_test_case
= false;
264 if (isset($just_this_test) && $just_this_test) {
265 $this->_just_this_test
= strtolower($just_this_test);
267 $this->SimpleReporterDecorator($reporter);
271 * Compares criteria to actual the case/group name.
272 * @param string $test_case The incoming test.
273 * @return boolean True if matched.
276 function _isCaseMatch($test_case) {
277 if ($this->_just_this_case
) {
278 return $this->_just_this_case
== strtolower($test_case);
284 * Compares criteria to actual the test name.
285 * @param string $method The incoming test method.
286 * @return boolean True if matched.
289 function _isTestMatch($method) {
290 if ($this->_just_this_test
) {
291 return $this->_just_this_test
== strtolower($method);
297 * Veto everything that doesn't match the method wanted.
298 * @param string $test_case Name of test case.
299 * @param string $method Name of test method.
300 * @return boolean True if test should be run.
303 function shouldInvoke($test_case, $method) {
304 if ($this->_within_test_case
&& $this->_isTestMatch($method)) {
305 return $this->_reporter
->shouldInvoke($test_case, $method);
311 * Paints the start of a group test.
312 * @param string $test_case Name of test or other label.
313 * @param integer $size Number of test cases starting.
316 function paintGroupStart($test_case, $size) {
317 if ($this->_isCaseMatch($test_case)) {
318 $this->_within_test_case
= true;
320 if ($this->_within_test_case
) {
321 $this->_reporter
->paintGroupStart($test_case, $size);
326 * Paints the end of a group test.
327 * @param string $test_case Name of test or other label.
330 function paintGroupEnd($test_case) {
331 if ($this->_within_test_case
) {
332 $this->_reporter
->paintGroupEnd($test_case);
334 if ($this->_isCaseMatch($test_case)) {
335 $this->_within_test_case
= false;
340 * Paints the start of a test case.
341 * @param string $test_case Name of test or other label.
344 function paintCaseStart($test_case) {
345 if ($this->_isCaseMatch($test_case)) {
346 $this->_within_test_case
= true;
348 if ($this->_within_test_case
) {
349 $this->_reporter
->paintCaseStart($test_case);
354 * Paints the end of a test case.
355 * @param string $test_case Name of test or other label.
358 function paintCaseEnd($test_case) {
359 if ($this->_within_test_case
) {
360 $this->_reporter
->paintCaseEnd($test_case);
362 if ($this->_isCaseMatch($test_case)) {
363 $this->_within_test_case
= false;