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

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.10
      • 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
  • None

Classes

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