0337d704 |
1 | <?php |
2 | /** |
3 | * base include file for SimpleTest |
4 | * @package SimpleTest |
5 | * @subpackage UnitTester |
6 | * @version $Id: reporter.php,v 1.31 2004/08/04 22:09:39 lastcraft Exp $ |
7 | */ |
8 | |
9 | /**#@+ |
10 | * include other SimpleTest class files |
11 | */ |
12 | require_once(dirname(__FILE__) . '/scorer.php'); |
13 | /**#@-*/ |
14 | |
15 | /** |
16 | * Sample minimal test displayer. Generates only |
17 | * failure messages and a pass count. |
18 | * @package SimpleTest |
19 | * @subpackage UnitTester |
20 | */ |
21 | class HtmlReporter extends SimpleReporter { |
22 | |
23 | /** |
24 | * Does nothing yet. The first output will |
25 | * be sent on the first test start. For use |
26 | * by a web browser. |
27 | * @access public |
28 | */ |
29 | function HtmlReporter() { |
30 | $this->SimpleReporter(); |
31 | } |
32 | |
33 | /** |
34 | * Paints the top of the web page setting the |
35 | * title to the name of the starting test. |
36 | * @param string $test_name Name class of test. |
37 | * @access public |
38 | */ |
39 | function paintHeader($test_name) { |
40 | $this->sendNoCacheHeaders(); |
41 | print "<html>\n<head>\n<title>$test_name</title>\n"; |
42 | print "<style type=\"text/css\">\n"; |
43 | print $this->_getCss() . "\n"; |
44 | print "</style>\n"; |
45 | print "</head>\n<body>\n"; |
46 | print "<h1>$test_name</h1>\n"; |
47 | flush(); |
48 | } |
49 | |
50 | /** |
51 | * Send the headers necessary to ensure the page is |
52 | * reloaded on every request. Otherwise you could be |
53 | * scratching your head over out of date test data. |
54 | * @access public |
55 | * @static |
56 | */ |
57 | function sendNoCacheHeaders() { |
58 | if (! headers_sent()) { |
59 | header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); |
60 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
61 | header("Cache-Control: no-store, no-cache, must-revalidate"); |
62 | header("Cache-Control: post-check=0, pre-check=0", false); |
63 | header("Pragma: no-cache"); |
64 | } |
65 | } |
66 | |
67 | /** |
68 | * Paints the CSS. Add additional styles here. |
69 | * @return string CSS code as text. |
70 | * @access protected |
71 | */ |
72 | function _getCss() { |
73 | return ".fail { color: red; } pre { background-color: lightgray; }"; |
74 | } |
75 | |
76 | /** |
77 | * Paints the end of the test with a summary of |
78 | * the passes and failures. |
79 | * @param string $test_name Name class of test. |
80 | * @access public |
81 | */ |
82 | function paintFooter($test_name) { |
83 | $colour = ($this->getFailCount() + $this->getExceptionCount() > 0 ? "red" : "green"); |
84 | print "<div style=\""; |
85 | print "padding: 8px; margin-top: 1em; background-color: $colour; color: white;"; |
86 | print "\">"; |
87 | print $this->getTestCaseProgress() . "/" . $this->getTestCaseCount(); |
88 | print " test cases complete:\n"; |
89 | print "<strong>" . $this->getPassCount() . "</strong> passes, "; |
90 | print "<strong>" . $this->getFailCount() . "</strong> fails and "; |
91 | print "<strong>" . $this->getExceptionCount() . "</strong> exceptions."; |
92 | print "</div>\n"; |
93 | print "</body>\n</html>\n"; |
94 | } |
95 | |
96 | /** |
97 | * Paints the test failure with a breadcrumbs |
98 | * trail of the nesting test suites below the |
99 | * top level test. |
100 | * @param string $message Failure message displayed in |
101 | * the context of the other tests. |
102 | * @access public |
103 | */ |
104 | function paintFail($message) { |
105 | parent::paintFail($message); |
106 | print "<span class=\"fail\">Fail</span>: "; |
107 | $breadcrumb = $this->getTestList(); |
108 | array_shift($breadcrumb); |
109 | print implode(" -> ", $breadcrumb); |
110 | print " -> " . htmlentities($message) . "<br />\n"; |
111 | } |
112 | |
113 | /** |
114 | * Paints a PHP error or exception. |
115 | * @param string $message Message is ignored. |
116 | * @access public |
117 | * @abstract |
118 | */ |
119 | function paintException($message) { |
120 | parent::paintException($message); |
121 | print "<span class=\"fail\">Exception</span>: "; |
122 | $breadcrumb = $this->getTestList(); |
123 | array_shift($breadcrumb); |
124 | print implode(" -> ", $breadcrumb); |
125 | print " -> <strong>" . htmlentities($message) . "</strong><br />\n"; |
126 | } |
127 | |
128 | /** |
129 | * Paints formatted text such as dumped variables. |
130 | * @param string $message Text to show. |
131 | * @access public |
132 | */ |
133 | function paintFormattedMessage($message) { |
134 | echo '<pre>', htmlentities($message), '</pre>'; |
135 | } |
136 | } |
137 | |
138 | /** |
139 | * Sample minimal test displayer. Generates only |
140 | * failure messages and a pass count. For command |
141 | * line use. I've tried to make it look like JUnit, |
142 | * but I wanted to output the errors as they arrived |
143 | * which meant dropping the dots. |
144 | * @package SimpleTest |
145 | * @subpackage UnitTester |
146 | */ |
147 | class TextReporter extends SimpleReporter { |
148 | |
149 | /** |
150 | * Does nothing yet. The first output will |
151 | * be sent on the first test start. |
152 | * @access public |
153 | */ |
154 | function TextReporter() { |
155 | $this->SimpleReporter(); |
156 | } |
157 | |
158 | /** |
159 | * Paints the title only. |
160 | * @param string $test_name Name class of test. |
161 | * @access public |
162 | */ |
163 | function paintHeader($test_name) { |
164 | if (!SimpleReporter::inCli()) { |
165 | header('Content-type: text/plain'); |
166 | } |
167 | print "$test_name\n"; |
168 | flush(); |
169 | } |
170 | |
171 | /** |
172 | * Paints the end of the test with a summary of |
173 | * the passes and failures. |
174 | * @param string $test_name Name class of test. |
175 | * @access public |
176 | */ |
177 | function paintFooter($test_name) { |
178 | if ($this->getFailCount() + $this->getExceptionCount() == 0) { |
179 | print "OK\n"; |
180 | } else { |
181 | print "FAILURES!!!\n"; |
182 | } |
183 | print "Test cases run: " . $this->getTestCaseProgress() . |
184 | "/" . $this->getTestCaseCount() . |
185 | ", Passes: " . $this->getPassCount() . |
186 | ", Failures: " . $this->getFailCount() . |
187 | ", Exceptions: " . $this->getExceptionCount() . "\n"; |
188 | |
189 | } |
190 | |
191 | /** |
192 | * Paints the test failure as a stack trace. |
193 | * @param string $message Failure message displayed in |
194 | * the context of the other tests. |
195 | * @access public |
196 | */ |
197 | function paintFail($message) { |
198 | parent::paintFail($message); |
199 | print $this->getFailCount() . ") $message\n"; |
200 | $breadcrumb = $this->getTestList(); |
201 | array_shift($breadcrumb); |
202 | print "\tin " . implode("\n\tin ", array_reverse($breadcrumb)); |
203 | print "\n"; |
204 | } |
205 | |
206 | /** |
207 | * Paints a PHP error or exception. |
208 | * @param string $message Message is ignored. |
209 | * @access public |
210 | * @abstract |
211 | */ |
212 | function paintException($message) { |
213 | parent::paintException($message); |
214 | print "Exception " . $this->getExceptionCount() . "!\n$message\n"; |
215 | } |
216 | |
217 | /** |
218 | * Paints formatted text such as dumped variables. |
219 | * @param string $message Text to show. |
220 | * @access public |
221 | */ |
222 | function paintFormattedMessage($message) { |
223 | print "$message\n"; |
224 | flush(); |
225 | } |
226 | } |
227 | ?> |