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.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 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
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • 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/2.0/en/development/testing.html>
  8:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * For full copyright and license information, please see the LICENSE.txt
 12:  * Redistributions of files must retain the above copyright notice
 13:  *
 14:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @since         CakePHP(tm) v 1.2.0.4433
 17:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 18:  */
 19: 
 20: App::uses('CakeBaseReporter', 'TestSuite/Reporter');
 21: 
 22: /**
 23:  * CakeHtmlReporter Reports Results of TestSuites and Test Cases
 24:  * in an HTML format / context.
 25:  *
 26:  * @package       Cake.TestSuite.Reporter
 27:  */
 28: class CakeHtmlReporter extends CakeBaseReporter {
 29: 
 30: /**
 31:  * Paints the top of the web page setting the
 32:  * title to the name of the starting test.
 33:  *
 34:  * @return void
 35:  */
 36:     public function paintHeader() {
 37:         $this->_headerSent = true;
 38:         $this->sendContentType();
 39:         $this->sendNoCacheHeaders();
 40:         $this->paintDocumentStart();
 41:         $this->paintTestMenu();
 42:         echo "<ul class='tests'>\n";
 43:     }
 44: 
 45: /**
 46:  * Set the content-type header so it is in the correct encoding.
 47:  *
 48:  * @return void
 49:  */
 50:     public function sendContentType() {
 51:         if (!headers_sent()) {
 52:             header('Content-Type: text/html; charset=' . Configure::read('App.encoding'));
 53:         }
 54:     }
 55: 
 56: /**
 57:  * Paints the document start content contained in header.php
 58:  *
 59:  * @return void
 60:  */
 61:     public function paintDocumentStart() {
 62:         ob_start();
 63:         $baseDir = $this->params['baseDir'];
 64:         include CAKE . 'TestSuite' . DS . 'templates' . DS . 'header.php';
 65:     }
 66: 
 67: /**
 68:  * Paints the menu on the left side of the test suite interface.
 69:  * Contains all of the various plugin, core, and app buttons.
 70:  *
 71:  * @return void
 72:  */
 73:     public function paintTestMenu() {
 74:         $cases = $this->baseUrl() . '?show=cases';
 75:         $plugins = App::objects('plugin', null, false);
 76:         sort($plugins);
 77:         include CAKE . 'TestSuite' . DS . 'templates' . DS . 'menu.php';
 78:     }
 79: 
 80: /**
 81:  * Retrieves and paints the list of tests cases in an HTML format.
 82:  *
 83:  * @return void
 84:  */
 85:     public function testCaseList() {
 86:         $testCases = parent::testCaseList();
 87:         $core = $this->params['core'];
 88:         $plugin = $this->params['plugin'];
 89: 
 90:         $buffer = "<h3>App Test Cases:</h3>\n<ul>";
 91:         $urlExtra = null;
 92:         if ($core) {
 93:             $buffer = "<h3>Core Test Cases:</h3>\n<ul>";
 94:             $urlExtra = '&core=true';
 95:         } elseif ($plugin) {
 96:             $buffer = "<h3>" . Inflector::humanize($plugin) . " Test Cases:</h3>\n<ul>";
 97:             $urlExtra = '&plugin=' . $plugin;
 98:         }
 99: 
100:         if (1 > count($testCases)) {
101:             $buffer .= "<strong>EMPTY</strong>";
102:         }
103: 
104:         foreach ($testCases as $testCase) {
105:             $title = explode(DS, str_replace('.test.php', '', $testCase));
106:             $title[count($title) - 1] = Inflector::camelize($title[count($title) - 1]);
107:             $title = implode(' / ', $title);
108:                 $buffer .= "<li><a href='" . $this->baseUrl() . "?case=" . urlencode($testCase) . $urlExtra . "'>" . $title . "</a></li>\n";
109:         }
110:         $buffer .= "</ul>\n";
111:         echo $buffer;
112:     }
113: 
114: /**
115:  * Send the headers necessary to ensure the page is
116:  * reloaded on every request. Otherwise you could be
117:  * scratching your head over out of date test data.
118:  *
119:  * @return void
120:  */
121:     public function sendNoCacheHeaders() {
122:         if (!headers_sent()) {
123:             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
124:             header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
125:             header("Cache-Control: no-store, no-cache, must-revalidate");
126:             header("Cache-Control: post-check=0, pre-check=0", false);
127:             header("Pragma: no-cache");
128:         }
129:     }
130: 
131: /**
132:  * Paints the end of the test with a summary of
133:  * the passes and failures.
134:  *
135:  * @param PHPUnit_Framework_TestResult $result Result object
136:  * @return void
137:  */
138:     public function paintFooter($result) {
139:         ob_end_flush();
140:         $colour = ($result->failureCount() + $result->errorCount() > 0 ? "red" : "green");
141:         echo "</ul>\n";
142:         echo "<div style=\"";
143:         echo "padding: 8px; margin: 1em 0; background-color: $colour; color: white;";
144:         echo "\">";
145:         echo ($result->count() - $result->skippedCount()) . "/" . $result->count();
146:         echo " test methods complete:\n";
147:         echo "<strong>" . count($result->passed()) . "</strong> passes, ";
148:         echo "<strong>" . $result->failureCount() . "</strong> fails, ";
149:         echo "<strong>" . $this->numAssertions . "</strong> assertions and ";
150:         echo "<strong>" . $result->errorCount() . "</strong> exceptions.";
151:         echo "</div>\n";
152:         echo '<div style="padding:0 0 5px;">';
153:         echo '<p><strong>Time:</strong> ' . $result->time() . ' seconds</p>';
154:         echo '<p><strong>Peak memory:</strong> ' . number_format(memory_get_peak_usage()) . ' bytes</p>';
155:         echo $this->_paintLinks();
156:         echo '</div>';
157:         if (isset($this->params['codeCoverage']) && $this->params['codeCoverage']) {
158:             $coverage = $result->getCodeCoverage();
159:             if (method_exists($coverage, 'getSummary')) {
160:                 $report = $coverage->getSummary();
161:                 echo $this->paintCoverage($report);
162:             }
163:             if (method_exists($coverage, 'getData')) {
164:                 $report = $coverage->getData();
165:                 echo $this->paintCoverage($report);
166:             }
167:         }
168:         $this->paintDocumentEnd();
169:     }
170: 
171: /**
172:  * Paints a code coverage report.
173:  *
174:  * @param array $coverage
175:  * @return void
176:  */
177:     public function paintCoverage(array $coverage) {
178:         App::uses('HtmlCoverageReport', 'TestSuite/Coverage');
179: 
180:         $reporter = new HtmlCoverageReport($coverage, $this);
181:         echo $reporter->report();
182:     }
183: 
184: /**
185:  * Renders the links that for accessing things in the test suite.
186:  *
187:  * @return void
188:  */
189:     protected function _paintLinks() {
190:         $show = $query = array();
191:         if (!empty($this->params['case'])) {
192:             $show['show'] = 'cases';
193:         }
194: 
195:         if (!empty($this->params['core'])) {
196:             $show['core'] = $query['core'] = 'true';
197:         }
198:         if (!empty($this->params['plugin'])) {
199:             $show['plugin'] = $query['plugin'] = $this->params['plugin'];
200:         }
201:         if (!empty($this->params['case'])) {
202:             $query['case'] = $this->params['case'];
203:         }
204:         $show = $this->_queryString($show);
205:         $query = $this->_queryString($query);
206: 
207:         echo "<p><a href='" . $this->baseUrl() . $show . "'>Run more tests</a> | <a href='" . $this->baseUrl() . $query . "&amp;show_passes=1'>Show Passes</a> | \n";
208:         echo "<a href='" . $this->baseUrl() . $query . "&amp;debug=1'>Enable Debug Output</a> | \n";
209:         echo "<a href='" . $this->baseUrl() . $query . "&amp;code_coverage=true'>Analyze Code Coverage</a></p>\n";
210:     }
211: 
212: /**
213:  * Convert an array of parameters into a query string url
214:  *
215:  * @param array $url Url hash to be converted
216:  * @return string Converted url query string
217:  */
218:     protected function _queryString($url) {
219:         $out = '?';
220:         $params = array();
221:         foreach ($url as $key => $value) {
222:             $params[] = "$key=$value";
223:         }
224:         $out .= implode('&amp;', $params);
225:         return $out;
226:     }
227: 
228: /**
229:  * Paints the end of the document html.
230:  *
231:  * @return void
232:  */
233:     public function paintDocumentEnd() {
234:         $baseDir = $this->params['baseDir'];
235:         include CAKE . 'TestSuite' . DS . 'templates' . DS . 'footer.php';
236:         if (ob_get_length()) {
237:             ob_end_flush();
238:         }
239:     }
240: 
241: /**
242:  * Paints the test failure with a breadcrumbs
243:  * trail of the nesting test suites below the
244:  * top level test.
245:  *
246:  * @param PHPUnit_Framework_AssertionFailedError $message Failure object displayed in
247:  *   the context of the other tests.
248:  * @param mixed $test
249:  * @return void
250:  */
251:     public function paintFail($message, $test) {
252:         $trace = $this->_getStackTrace($message);
253:         $testName = get_class($test) . '(' . $test->getName() . ')';
254: 
255:         $actualMsg = $expectedMsg = null;
256:         if (method_exists($message, 'getComparisonFailure')) {
257:             $failure = $message->getComparisonFailure();
258:             if (is_object($failure)) {
259:                 $actualMsg = $failure->getActualAsString();
260:                 $expectedMsg = $failure->getExpectedAsString();
261:             }
262:         }
263: 
264:         echo "<li class='fail'>\n";
265:         echo "<span>Failed</span>";
266:         echo "<div class='msg'><pre>" . $this->_htmlEntities($message->toString());
267: 
268:         if ((is_string($actualMsg) && is_string($expectedMsg)) || (is_array($actualMsg) && is_array($expectedMsg))) {
269:             echo "<br />" . PHPUnit_Util_Diff::diff($expectedMsg, $actualMsg);
270:         }
271: 
272:         echo "</pre></div>\n";
273:         echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
274:         echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
275:         echo "</li>\n";
276:     }
277: 
278: /**
279:  * Paints the test pass with a breadcrumbs
280:  * trail of the nesting test suites below the
281:  * top level test.
282:  *
283:  * @param PHPUnit_Framework_Test test method that just passed
284:  * @param float $time time spent to run the test method
285:  * @return void
286:  */
287:     public function paintPass(PHPUnit_Framework_Test $test, $time = null) {
288:         if (isset($this->params['showPasses']) && $this->params['showPasses']) {
289:             echo "<li class='pass'>\n";
290:             echo "<span>Passed</span> ";
291: 
292:             echo "<br />" . $this->_htmlEntities($test->getName()) . " ($time seconds)\n";
293:             echo "</li>\n";
294:         }
295:     }
296: 
297: /**
298:  * Paints a PHP exception.
299:  *
300:  * @param Exception $exception Exception to display.
301:  * @param mixed $test
302:  * @return void
303:  */
304:     public function paintException($message, $test) {
305:         $trace = $this->_getStackTrace($message);
306:         $testName = get_class($test) . '(' . $test->getName() . ')';
307: 
308:         echo "<li class='fail'>\n";
309:         echo "<span>" . get_class($message) . "</span>";
310: 
311:         echo "<div class='msg'>" . $this->_htmlEntities($message->getMessage()) . "</div>\n";
312:         echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
313:         echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
314:         echo "</li>\n";
315:     }
316: 
317: /**
318:  * Prints the message for skipping tests.
319:  *
320:  * @param string $message Text of skip condition.
321:  * @param PHPUnit_Framework_TestCase $test the test method skipped
322:  * @return void
323:  */
324:     public function paintSkip($message, $test) {
325:         echo "<li class='skipped'>\n";
326:         echo "<span>Skipped</span> ";
327:         echo $test->getName() . ': ' . $this->_htmlEntities($message->getMessage());
328:         echo "</li>\n";
329:     }
330: 
331: /**
332:  * Paints formatted text such as dumped variables.
333:  *
334:  * @param string $message Text to show.
335:  * @return void
336:  */
337:     public function paintFormattedMessage($message) {
338:         echo '<pre>' . $this->_htmlEntities($message) . '</pre>';
339:     }
340: 
341: /**
342:  * Character set adjusted entity conversion.
343:  *
344:  * @param string $message Plain text or Unicode message.
345:  * @return string Browser readable message.
346:  */
347:     protected function _htmlEntities($message) {
348:         return htmlentities($message, ENT_COMPAT, $this->_characterSet);
349:     }
350: 
351: /**
352:  * Gets a formatted stack trace.
353:  *
354:  * @param Exception $e Exception to get a stack trace for.
355:  * @return string Generated stack trace.
356:  */
357:     protected function _getStackTrace(Exception $e) {
358:         $trace = $e->getTrace();
359:         $out = array();
360:         foreach ($trace as $frame) {
361:             if (isset($frame['file']) && isset($frame['line'])) {
362:                 $out[] = $frame['file'] . ' : ' . $frame['line'];
363:             } elseif (isset($frame['class']) && isset($frame['function'])) {
364:                 $out[] = $frame['class'] . '::' . $frame['function'];
365:             } else {
366:                 $out[] = '[internal]';
367:             }
368:         }
369:         return implode('<br />', $out);
370:     }
371: 
372: /**
373:  * A test suite started.
374:  *
375:  * @param PHPUnit_Framework_TestSuite $suite
376:  * @return void
377:  */
378:     public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
379:         if (!$this->_headerSent) {
380:             echo $this->paintHeader();
381:         }
382:         echo '<h2>' . __d('cake_dev', 'Running  %s', $suite->getName()) . '</h2>';
383:     }
384: 
385: }
386: 
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