comparison cake/tests/lib/reporter/cake_html_reporter.php @ 0:261e66bd5a0c

hg init
author Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp>
date Sun, 24 Jul 2011 21:08:31 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:261e66bd5a0c
1 <?php
2 /**
3 * CakeHtmlReporter
4 *
5 * PHP versions 4 and 5
6 *
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9 *
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
12 *
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://cakephp.org CakePHP(tm) Project
15 * @package cake
16 * @subpackage cake.tests.libs.reporter
17 * @since CakePHP(tm) v 1.2.0.4433
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
19 */
20 include_once dirname(__FILE__) . DS . 'cake_base_reporter.php';
21
22 /**
23 * CakeHtmlReporter Reports Results of TestSuites and Test Cases
24 * in an HTML format / context.
25 *
26 * @package cake
27 * @subpackage cake.tests.lib
28 */
29 class CakeHtmlReporter extends CakeBaseReporter {
30 /**
31 * Constructor
32 *
33 * @param string $charset
34 * @param string $params
35 * @return void
36 */
37 function CakeHtmlReporter($charset = 'utf-8', $params = array()) {
38 $params = array_map(array($this, '_htmlEntities'), $params);
39 $this->CakeBaseReporter($charset, $params);
40 }
41 /**
42 * Paints the top of the web page setting the
43 * title to the name of the starting test.
44 *
45 * @param string $test_name Name class of test.
46 * @return void
47 * @access public
48 */
49 function paintHeader($testName) {
50 $this->sendNoCacheHeaders();
51 $this->paintDocumentStart();
52 $this->paintTestMenu();
53 printf("<h2>%s</h2>\n", $this->_htmlEntities($testName));
54 echo "<ul class='tests'>\n";
55 }
56
57 /**
58 * Paints the document start content contained in header.php
59 *
60 * @return void
61 */
62 function paintDocumentStart() {
63 ob_start();
64 $baseDir = $this->params['baseDir'];
65 include CAKE_TESTS_LIB . 'templates' . DS . 'header.php';
66 }
67
68 /**
69 * Paints the menu on the left side of the test suite interface.
70 * Contains all of the various plugin, core, and app buttons.
71 *
72 * @return void
73 */
74 function paintTestMenu() {
75 $groups = $this->baseUrl() . '?show=groups';
76 $cases = $this->baseUrl() . '?show=cases';
77 $plugins = App::objects('plugin', null, false);
78 sort($plugins);
79 include CAKE_TESTS_LIB . 'templates' . DS . 'menu.php';
80 }
81
82 /**
83 * Retrieves and paints the list of tests cases in an HTML format.
84 *
85 * @return void
86 */
87 function testCaseList() {
88 $testCases = parent::testCaseList();
89 $app = $this->params['app'];
90 $plugin = $this->params['plugin'];
91
92 $buffer = "<h3>Core Test Cases:</h3>\n<ul>";
93 $urlExtra = null;
94 if ($app) {
95 $buffer = "<h3>App Test Cases:</h3>\n<ul>";
96 $urlExtra = '&app=true';
97 } elseif ($plugin) {
98 $buffer = "<h3>" . Inflector::humanize($plugin) . " Test Cases:</h3>\n<ul>";
99 $urlExtra = '&plugin=' . $plugin;
100 }
101
102 if (1 > count($testCases)) {
103 $buffer .= "<strong>EMPTY</strong>";
104 }
105
106 foreach ($testCases as $testCaseFile => $testCase) {
107 $title = explode(strpos($testCase, '\\') ? '\\' : '/', str_replace('.test.php', '', $testCase));
108 $title[count($title) - 1] = Inflector::camelize($title[count($title) - 1]);
109 $title = implode(' / ', $title);
110 $buffer .= "<li><a href='" . $this->baseUrl() . "?case=" . urlencode($testCase) . $urlExtra ."'>" . $title . "</a></li>\n";
111 }
112 $buffer .= "</ul>\n";
113 echo $buffer;
114 }
115
116 /**
117 * Retrieves and paints the list of group tests in an HTML format.
118 *
119 * @return void
120 */
121 function groupTestList() {
122 $groupTests = parent::groupTestList();
123 $app = $this->params['app'];
124 $plugin = $this->params['plugin'];
125
126 $buffer = "<h3>Core Test Groups:</h3>\n<ul>";
127 $urlExtra = null;
128 if ($app) {
129 $buffer = "<h3>App Test Groups:</h3>\n<ul>";
130 $urlExtra = '&app=true';
131 } else if ($plugin) {
132 $buffer = "<h3>" . Inflector::humanize($plugin) . " Test Groups:</h3>\n<ul>";
133 $urlExtra = '&plugin=' . $plugin;
134 }
135
136 $buffer .= "<li><a href='" . $this->baseURL() . "?group=all$urlExtra'>All tests</a></li>\n";
137
138 foreach ($groupTests as $groupTest) {
139 $buffer .= "<li><a href='" . $this->baseURL() . "?group={$groupTest}" . "{$urlExtra}'>" . $groupTest . "</a></li>\n";
140 }
141 $buffer .= "</ul>\n";
142 echo $buffer;
143 }
144
145 /**
146 * Send the headers necessary to ensure the page is
147 * reloaded on every request. Otherwise you could be
148 * scratching your head over out of date test data.
149 *
150 * @return void
151 * @access public
152 */
153 function sendNoCacheHeaders() {
154 if (!headers_sent()) {
155 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
156 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
157 header("Cache-Control: no-store, no-cache, must-revalidate");
158 header("Cache-Control: post-check=0, pre-check=0", false);
159 header("Pragma: no-cache");
160 }
161 }
162
163 /**
164 * Paints the end of the test with a summary of
165 * the passes and failures.
166 *
167 * @param string $test_name Name class of test.
168 * @return void
169 * @access public
170 */
171 function paintFooter($test_name) {
172 $colour = ($this->getFailCount() + $this->getExceptionCount() > 0 ? "red" : "green");
173 echo "</ul>\n";
174 echo "<div style=\"";
175 echo "padding: 8px; margin: 1em 0; background-color: $colour; color: white;";
176 echo "\">";
177 echo $this->getTestCaseProgress() . "/" . $this->getTestCaseCount();
178 echo " test cases complete:\n";
179 echo "<strong>" . $this->getPassCount() . "</strong> passes, ";
180 echo "<strong>" . $this->getFailCount() . "</strong> fails and ";
181 echo "<strong>" . $this->getExceptionCount() . "</strong> exceptions.";
182 echo "</div>\n";
183 echo '<div style="padding:0 0 5px;">';
184 echo '<p><strong>Time taken by tests (in seconds):</strong> ' . $this->_timeDuration . '</p>';
185 if (function_exists('memory_get_peak_usage')) {
186 echo '<p><strong>Peak memory use: (in bytes):</strong> ' . number_format(memory_get_peak_usage()) . '</p>';
187 }
188 echo $this->_paintLinks();
189 echo '</div>';
190 if (
191 isset($this->params['codeCoverage']) &&
192 $this->params['codeCoverage'] &&
193 class_exists('CodeCoverageManager')
194 ) {
195 CodeCoverageManager::report();
196 }
197 $this->paintDocumentEnd();
198 }
199
200 /**
201 * Renders the links that for accessing things in the test suite.
202 *
203 * @return void
204 */
205 function _paintLinks() {
206 $show = $query = array();
207 if (!empty($this->params['group'])) {
208 $show['show'] = 'groups';
209 } elseif (!empty($this->params['case'])) {
210 $show['show'] = 'cases';
211 }
212
213 if (!empty($this->params['app'])) {
214 $show['app'] = $query['app'] = 'true';
215 }
216 if (!empty($this->params['plugin'])) {
217 $show['plugin'] = $query['plugin'] = $this->params['plugin'];
218 }
219 if (!empty($this->params['case'])) {
220 $query['case'] = $this->params['case'];
221 } elseif (!empty($this->params['group'])) {
222 $query['group'] = $this->params['group'];
223 }
224 $show = $this->_queryString($show);
225 $query = $this->_queryString($query);
226
227 echo "<p><a href='" . $this->baseUrl() . $show . "'>Run more tests</a> | <a href='" . $this->baseUrl() . $query . "&show_passes=1'>Show Passes</a> | \n";
228 echo " <a href='" . $this->baseUrl() . $query . "&amp;code_coverage=true'>Analyze Code Coverage</a></p>\n";
229 }
230
231 /**
232 * Convert an array of parameters into a query string url
233 *
234 * @param array $url Url hash to be converted
235 * @return string Converted url query string
236 */
237 function _queryString($url) {
238 $out = '?';
239 $params = array();
240 foreach ($url as $key => $value) {
241 $params[] = "$key=$value";
242 }
243 $out .= implode('&amp;', $params);
244 return $out;
245 }
246
247 /**
248 * Paints the end of the document html.
249 *
250 * @return void
251 */
252 function paintDocumentEnd() {
253 $baseDir = $this->params['baseDir'];
254 include CAKE_TESTS_LIB . 'templates' . DS . 'footer.php';
255 ob_end_flush();
256 }
257
258 /**
259 * Paints the test failure with a breadcrumbs
260 * trail of the nesting test suites below the
261 * top level test.
262 *
263 * @param string $message Failure message displayed in
264 * the context of the other tests.
265 * @return void
266 * @access public
267 */
268 function paintFail($message) {
269 parent::paintFail($message);
270 echo "<li class='fail'>\n";
271 echo "<span>Failed</span>";
272 echo "<div class='msg'>" . $this->_htmlEntities($message) . "</div>\n";
273 $breadcrumb = $this->getTestList();
274 array_shift($breadcrumb);
275 echo "<div>" . implode(" -&gt; ", $breadcrumb) . "</div>\n";
276 echo "</li>\n";
277 }
278
279 /**
280 * Paints the test pass with a breadcrumbs
281 * trail of the nesting test suites below the
282 * top level test.
283 *
284 * @param string $message Pass message displayed in the context of the other tests.
285 * @return void
286 * @access public
287 */
288 function paintPass($message) {
289 parent::paintPass($message);
290
291 if (isset($this->params['show_passes']) && $this->params['show_passes']) {
292 echo "<li class='pass'>\n";
293 echo "<span>Passed</span> ";
294 $breadcrumb = $this->getTestList();
295 array_shift($breadcrumb);
296 echo implode(" -&gt; ", $breadcrumb);
297 echo "<br />" . $this->_htmlEntities($message) . "\n";
298 echo "</li>\n";
299 }
300 }
301
302 /**
303 * Paints a PHP error.
304 *
305 * @param string $message Message is ignored.
306 * @return void
307 * @access public
308 */
309 function paintError($message) {
310 parent::paintError($message);
311 echo "<li class='error'>\n";
312 echo "<span>Error</span>";
313 echo "<div class='msg'>" . $this->_htmlEntities($message) . "</div>\n";
314 $breadcrumb = $this->getTestList();
315 array_shift($breadcrumb);
316 echo "<div>" . implode(" -&gt; ", $breadcrumb) . "</div>\n";
317 echo "</li>\n";
318 }
319
320 /**
321 * Paints a PHP exception.
322 *
323 * @param Exception $exception Exception to display.
324 * @return void
325 * @access public
326 */
327 function paintException($exception) {
328 parent::paintException($exception);
329 echo "<li class='fail'>\n";
330 echo "<span>Exception</span>";
331 $message = 'Unexpected exception of type [' . get_class($exception) .
332 '] with message ['. $exception->getMessage() .
333 '] in ['. $exception->getFile() .
334 ' line ' . $exception->getLine() . ']';
335 echo "<div class='msg'>" . $this->_htmlEntities($message) . "</div>\n";
336 $breadcrumb = $this->getTestList();
337 array_shift($breadcrumb);
338 echo "<div>" . implode(" -&gt; ", $breadcrumb) . "</div>\n";
339 echo "</li>\n";
340 }
341
342 /**
343 * Prints the message for skipping tests.
344 *
345 * @param string $message Text of skip condition.
346 * @return void
347 * @access public
348 */
349 function paintSkip($message) {
350 parent::paintSkip($message);
351 echo "<li class='skipped'>\n";
352 echo "<span>Skipped</span> ";
353 echo $this->_htmlEntities($message);
354 echo "</li>\n";
355 }
356
357 /**
358 * Paints formatted text such as dumped variables.
359 *
360 * @param string $message Text to show.
361 * @return void
362 * @access public
363 */
364 function paintFormattedMessage($message) {
365 echo '<pre>' . $this->_htmlEntities($message) . '</pre>';
366 }
367
368 /**
369 * Character set adjusted entity conversion.
370 *
371 * @param string $message Plain text or Unicode message.
372 * @return string Browser readable message.
373 * @access protected
374 */
375 function _htmlEntities($message) {
376 return htmlentities($message, ENT_COMPAT, $this->_characterSet);
377 }
378 }