1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18: App::uses('CakeBaseReporter', 'TestSuite/Reporter');
19:
20: 21: 22: 23: 24: 25:
26: class CakeHtmlReporter extends CakeBaseReporter {
27:
28: 29: 30: 31: 32: 33:
34: public function paintHeader() {
35: $this->_headerSent = true;
36: $this->sendNoCacheHeaders();
37: $this->paintDocumentStart();
38: $this->paintTestMenu();
39: echo "<ul class='tests'>\n";
40: }
41:
42: 43: 44: 45: 46:
47: public function paintDocumentStart() {
48: ob_start();
49: $baseDir = $this->params['baseDir'];
50: include CAKE . 'TestSuite' . DS . 'templates' . DS . 'header.php';
51: }
52:
53: 54: 55: 56: 57: 58:
59: public function paintTestMenu() {
60: $cases = $this->baseUrl() . '?show=cases';
61: $plugins = App::objects('plugin', null, false);
62: sort($plugins);
63: include CAKE . 'TestSuite' . DS . 'templates' . DS . 'menu.php';
64: }
65:
66: 67: 68: 69: 70:
71: public function testCaseList() {
72: $testCases = parent::testCaseList();
73: $app = $this->params['app'];
74: $plugin = $this->params['plugin'];
75:
76: $buffer = "<h3>Core Test Cases:</h3>\n<ul>";
77: $urlExtra = null;
78: if ($app) {
79: $buffer = "<h3>App Test Cases:</h3>\n<ul>";
80: $urlExtra = '&app=true';
81: } elseif ($plugin) {
82: $buffer = "<h3>" . Inflector::humanize($plugin) . " Test Cases:</h3>\n<ul>";
83: $urlExtra = '&plugin=' . $plugin;
84: }
85:
86: if (1 > count($testCases)) {
87: $buffer .= "<strong>EMPTY</strong>";
88: }
89:
90: foreach ($testCases as $testCaseFile => $testCase) {
91: $title = explode(DS, str_replace('.test.php', '', $testCase));
92: $title[count($title) - 1] = Inflector::camelize($title[count($title) - 1]);
93: $title = implode(' / ', $title);
94: $buffer .= "<li><a href='" . $this->baseUrl() . "?case=" . urlencode($testCase) . $urlExtra ."'>" . $title . "</a></li>\n";
95: }
96: $buffer .= "</ul>\n";
97: echo $buffer;
98: }
99:
100: 101: 102: 103: 104: 105: 106:
107: public function sendNoCacheHeaders() {
108: if (!headers_sent()) {
109: header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
110: header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
111: header("Cache-Control: no-store, no-cache, must-revalidate");
112: header("Cache-Control: post-check=0, pre-check=0", false);
113: header("Pragma: no-cache");
114: }
115: }
116:
117: 118: 119: 120: 121: 122: 123:
124: public function paintFooter($result) {
125: ob_end_flush();
126: $colour = ($result->failureCount() + $result->errorCount() > 0 ? "red" : "green");
127: echo "</ul>\n";
128: echo "<div style=\"";
129: echo "padding: 8px; margin: 1em 0; background-color: $colour; color: white;";
130: echo "\">";
131: echo ($result->count() - $result->skippedCount()) . "/" . $result->count();
132: echo " test methods complete:\n";
133: echo "<strong>" . count($result->passed()) . "</strong> passes, ";
134: echo "<strong>" . $result->failureCount() . "</strong> fails, ";
135: echo "<strong>" . $this->numAssertions . "</strong> assertions and ";
136: echo "<strong>" . $result->errorCount() . "</strong> exceptions.";
137: echo "</div>\n";
138: echo '<div style="padding:0 0 5px;">';
139: echo '<p><strong>Time:</strong> ' . $result->time() . ' seconds</p>';
140: echo '<p><strong>Peak memory:</strong> ' . number_format(memory_get_peak_usage()) . ' bytes</p>';
141: echo $this->_paintLinks();
142: echo '</div>';
143: if (isset($this->params['codeCoverage']) && $this->params['codeCoverage']) {
144: $coverage = $result->getCodeCoverage();
145: if (method_exists($coverage, 'getSummary')) {
146: $report = $coverage->getSummary();
147: echo $this->paintCoverage($report);
148: }
149: if (method_exists($coverage, 'getData')) {
150: $report = $coverage->getData();
151: echo $this->paintCoverage($report);
152: }
153: }
154: $this->paintDocumentEnd();
155: }
156:
157: 158: 159: 160: 161:
162: public function paintCoverage(array $coverage) {
163: App::uses('HtmlCoverageReport', 'TestSuite/Coverage');
164:
165: $reporter = new HtmlCoverageReport($coverage, $this);
166: echo $reporter->report();
167: }
168:
169: 170: 171: 172: 173:
174: protected function _paintLinks() {
175: $show = $query = array();
176: if (!empty($this->params['case'])) {
177: $show['show'] = 'cases';
178: }
179:
180: if (!empty($this->params['app'])) {
181: $show['app'] = $query['app'] = 'true';
182: }
183: if (!empty($this->params['plugin'])) {
184: $show['plugin'] = $query['plugin'] = $this->params['plugin'];
185: }
186: if (!empty($this->params['case'])) {
187: $query['case'] = $this->params['case'];
188: }
189: $show = $this->_queryString($show);
190: $query = $this->_queryString($query);
191:
192: echo "<p><a href='" . $this->baseUrl() . $show . "'>Run more tests</a> | <a href='" . $this->baseUrl() . $query . "&show_passes=1'>Show Passes</a> | \n";
193: echo " <a href='" . $this->baseUrl() . $query . "&code_coverage=true'>Analyze Code Coverage</a></p>\n";
194: }
195:
196: 197: 198: 199: 200: 201:
202: protected function _queryString($url) {
203: $out = '?';
204: $params = array();
205: foreach ($url as $key => $value) {
206: $params[] = "$key=$value";
207: }
208: $out .= implode('&', $params);
209: return $out;
210: }
211:
212: 213: 214: 215: 216:
217: public function paintDocumentEnd() {
218: $baseDir = $this->params['baseDir'];
219: include CAKE . 'TestSuite' . DS . 'templates' . DS . 'footer.php';
220: if (ob_get_length()) {
221: ob_end_flush();
222: }
223: }
224:
225: 226: 227: 228: 229: 230: 231: 232: 233:
234: public function paintFail($message, $test) {
235: $trace = $this->_getStackTrace($message);
236: $testName = get_class($test) . '(' . $test->getName() . ')';
237:
238: echo "<li class='fail'>\n";
239: echo "<span>Failed</span>";
240: echo "<div class='msg'><pre>" . $this->_htmlEntities($message->toString()) . "</pre></div>\n";
241: echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
242: echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
243: echo "</li>\n";
244: }
245:
246: 247: 248: 249: 250: 251: 252: 253: 254:
255: public function paintPass(PHPUnit_Framework_Test $test, $time = null) {
256: if (isset($this->params['showPasses']) && $this->params['showPasses']) {
257: echo "<li class='pass'>\n";
258: echo "<span>Passed</span> ";
259:
260: echo "<br />" . $this->_htmlEntities($test->getName()) . " ($time seconds)\n";
261: echo "</li>\n";
262: }
263: }
264:
265: 266: 267: 268: 269: 270:
271: public function paintException($message, $test) {
272: $trace = $this->_getStackTrace($message);
273: $testName = get_class($test) . '(' . $test->getName() . ')';
274:
275: echo "<li class='fail'>\n";
276: echo "<span>" . get_class($message) . "</span>";
277:
278: echo "<div class='msg'>" . $this->_htmlEntities($message->getMessage()) . "</div>\n";
279: echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
280: echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
281: echo "</li>\n";
282: }
283:
284: 285: 286: 287: 288: 289: 290:
291: public function paintSkip($message, $test) {
292: echo "<li class='skipped'>\n";
293: echo "<span>Skipped</span> ";
294: echo $test->getName() . ': ' . $this->_htmlEntities($message->getMessage());
295: echo "</li>\n";
296: }
297:
298: 299: 300: 301: 302: 303:
304: public function paintFormattedMessage($message) {
305: echo '<pre>' . $this->_htmlEntities($message) . '</pre>';
306: }
307:
308: 309: 310: 311: 312: 313:
314: protected function _htmlEntities($message) {
315: return htmlentities($message, ENT_COMPAT, $this->_characterSet);
316: }
317:
318: 319: 320: 321: 322: 323:
324: protected function _getStackTrace(Exception $e) {
325: $trace = $e->getTrace();
326: $out = array();
327: foreach ($trace as $frame) {
328: if (isset($frame['file']) && isset($frame['line'])) {
329: $out[] = $frame['file'] . ' : ' . $frame['line'];
330: } elseif (isset($frame['class']) && isset($frame['function'])) {
331: $out[] = $frame['class'] . '::' . $frame['function'];
332: } else {
333: $out[] = '[internal]';
334: }
335: }
336: return implode('<br />', $out);
337: }
338:
339: 340: 341: 342: 343:
344: public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
345: if (!$this->_headerSent) {
346: echo $this->paintHeader();
347: }
348: echo '<h2>' . __d('cake_dev', 'Running %s', $suite->getName()) . '</h2>';
349: }
350: }
351: