CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.0 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.0
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Auth
    • Core
    • Error
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeBaseReporter
  • CakeHtmlReporter
  • CakeTextReporter
  1: <?php
  2: /**
  3:  * CakeHtmlReporter
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8:  * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice
 12:  *
 13:  * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @since         CakePHP(tm) v 1.2.0.4433
 16:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 17:  */
 18: App::uses('CakeBaseReporter', 'TestSuite/Reporter');
 19: 
 20: /**
 21:  * CakeHtmlReporter Reports Results of TestSuites and Test Cases
 22:  * in an HTML format / context.
 23:  *
 24:  * @package       Cake.TestSuite.Reporter
 25:  */
 26: class CakeHtmlReporter extends CakeBaseReporter {
 27: 
 28: /**
 29:  * Paints the top of the web page setting the
 30:  * title to the name of the starting test.
 31:  *
 32:  * @return void
 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:  * Paints the document start content contained in header.php
 44:  *
 45:  * @return void
 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:  * Paints the menu on the left side of the test suite interface.
 55:  * Contains all of the various plugin, core, and app buttons.
 56:  *
 57:  * @return void
 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:  * Retrieves and paints the list of tests cases in an HTML format.
 68:  *
 69:  * @return void
 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:  * Send the headers necessary to ensure the page is
102:  * reloaded on every request. Otherwise you could be
103:  * scratching your head over out of date test data.
104:  *
105:  * @return void
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:  * Paints the end of the test with a summary of
119:  * the passes and failures.
120:  *
121:  * @param PHPUnit_Framework_TestResult $result Result object
122:  * @return void
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:  * Paints a code coverage report.
159:  *
160:  * @return void
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:  * Renders the links that for accessing things in the test suite.
171:  *
172:  * @return void
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 . "&amp;code_coverage=true'>Analyze Code Coverage</a></p>\n";
194:     }
195: 
196: /**
197:  * Convert an array of parameters into a query string url
198:  *
199:  * @param array $url Url hash to be converted
200:  * @return string Converted url query string
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('&amp;', $params);
209:         return $out;
210:     }
211: 
212: /**
213:  * Paints the end of the document html.
214:  *
215:  * @return void
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:  * Paints the test failure with a breadcrumbs
227:  * trail of the nesting test suites below the
228:  * top level test.
229:  *
230:  * @param PHPUnit_Framework_AssertionFailedError $message Failure object displayed in
231:  *   the context of the other tests.
232:  * @return void
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:  * Paints the test pass with a breadcrumbs
248:  * trail of the nesting test suites below the
249:  * top level test.
250:  *
251:  * @param PHPUnit_Framework_Test test method that just passed
252:  * @param float $time time spent to run the test method
253:  * @return void
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:  * Paints a PHP exception.
267:  *
268:  * @param Exception $exception Exception to display.
269:  * @return void
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:  * Prints the message for skipping tests.
286:  *
287:  * @param string $message Text of skip condition.
288:  * @param PHPUnit_Framework_TestCase $test the test method skipped
289:  * @return void
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:  * Paints formatted text such as dumped variables.
300:  *
301:  * @param string $message Text to show.
302:  * @return void
303:  */
304:     public function paintFormattedMessage($message) {
305:         echo '<pre>' . $this->_htmlEntities($message) . '</pre>';
306:     }
307: 
308: /**
309:  * Character set adjusted entity conversion.
310:  *
311:  * @param string $message Plain text or Unicode message.
312:  * @return string Browser readable message.
313:  */
314:     protected function _htmlEntities($message) {
315:         return htmlentities($message, ENT_COMPAT, $this->_characterSet);
316:     }
317: 
318: /**
319:  * Gets a formatted stack trace.
320:  *
321:  * @param Exception $e Exception to get a stack trace for.
322:  * @return string Generated stack trace.
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:  * A test suite started.
341:  *
342:  * @param  PHPUnit_Framework_TestSuite $suite
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: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs