* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package cake.tests.libs.reporter
* @since CakePHP(tm) v 1.2.0.4433
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
include_once dirname(__FILE__) . DS . 'cake_base_reporter.php';
PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT');
/**
* CakeHtmlReporter Reports Results of TestSuites and Test Cases
* in an HTML format / context.
*
* @package cake
* @package cake.tests.lib
*/
class CakeHtmlReporter extends CakeBaseReporter {
/**
* Paints the top of the web page setting the
* title to the name of the starting test.
*
* @return void
*/
public function paintHeader() {
$this->_headerSent = true;
$this->sendNoCacheHeaders();
$this->paintDocumentStart();
$this->paintTestMenu();
echo "
\n";
}
/**
* Paints the document start content contained in header.php
*
* @return void
*/
public function paintDocumentStart() {
ob_start();
$baseDir = $this->params['baseDir'];
include CAKE_TESTS_LIB . 'templates' . DS . 'header.php';
}
/**
* Paints the menu on the left side of the test suite interface.
* Contains all of the various plugin, core, and app buttons.
*
* @return void
*/
public function paintTestMenu() {
$cases = $this->baseUrl() . '?show=cases';
$plugins = App::objects('plugin', null, false);
sort($plugins);
include CAKE_TESTS_LIB . 'templates' . DS . 'menu.php';
}
/**
* Retrieves and paints the list of tests cases in an HTML format.
*
* @return void
*/
public function testCaseList() {
$testCases = parent::testCaseList();
$app = $this->params['app'];
$plugin = $this->params['plugin'];
$buffer = "Core Test Cases:
\n";
$urlExtra = null;
if ($app) {
$buffer = "App Test Cases:
\n";
$urlExtra = '&app=true';
} elseif ($plugin) {
$buffer = "" . Inflector::humanize($plugin) . " Test Cases:
\n";
$urlExtra = '&plugin=' . $plugin;
}
if (1 > count($testCases)) {
$buffer .= "EMPTY";
}
foreach ($testCases as $testCaseFile => $testCase) {
$title = explode(DS, str_replace('.test.php', '', $testCase));
$title[count($title) - 1] = Inflector::camelize($title[count($title) - 1]);
$title = implode(' / ', $title);
$buffer .= "- " . $title . "
\n";
}
$buffer .= "
\n";
echo $buffer;
}
/**
* Send the headers necessary to ensure the page is
* reloaded on every request. Otherwise you could be
* scratching your head over out of date test data.
*
* @return void
*/
public function sendNoCacheHeaders() {
if (!headers_sent()) {
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
}
/**
* Paints the end of the test with a summary of
* the passes and failures.
*
* @param PHPUnit_Framework_TestResult $result Result object
* @return void
*/
public function paintFooter($result) {
ob_end_flush();
$colour = ($result->failureCount() + $result->errorCount() > 0 ? "red" : "green");
echo "
\n";
echo "";
echo ($result->count() - $result->skippedCount()) . "/" . $result->count();
echo " test methods complete:\n";
echo "" . count($result->passed()) . " passes, ";
echo "" . $result->failureCount() . " fails, ";
echo "" . $this->numAssertions . " assertions and ";
echo "" . $result->errorCount() . " exceptions.";
echo "
\n";
echo '';
echo '
Time: ' . $result->time() . ' seconds
';
echo '
Peak memory: ' . number_format(memory_get_peak_usage()) . ' bytes
';
echo $this->_paintLinks();
echo '
';
if (isset($this->params['codeCoverage']) && $this->params['codeCoverage']) {
$coverage = $result->getCodeCoverage()->getSummary();
echo $this->paintCoverage($coverage);
}
$this->paintDocumentEnd();
}
/**
* Paints a code coverage report.
*
* @return void
*/
public function paintCoverage(array $coverage) {
$file = dirname(dirname(__FILE__)) . '/coverage/html_coverage_report.php';
include_once $file;
$reporter = new HtmlCoverageReport($coverage, $this);
echo $reporter->report();
}
/**
* Renders the links that for accessing things in the test suite.
*
* @return void
*/
protected function _paintLinks() {
$show = $query = array();
if (!empty($this->params['case'])) {
$show['show'] = 'cases';
}
if (!empty($this->params['app'])) {
$show['app'] = $query['app'] = 'true';
}
if (!empty($this->params['plugin'])) {
$show['plugin'] = $query['plugin'] = $this->params['plugin'];
}
if (!empty($this->params['case'])) {
$query['case'] = $this->params['case'];
}
$show = $this->_queryString($show);
$query = $this->_queryString($query);
echo "Run more tests | Show Passes | \n";
echo " Analyze Code Coverage
\n";
}
/**
* Convert an array of parameters into a query string url
*
* @param array $url Url hash to be converted
* @return string Converted url query string
*/
protected function _queryString($url) {
$out = '?';
$params = array();
foreach ($url as $key => $value) {
$params[] = "$key=$value";
}
$out .= implode('&', $params);
return $out;
}
/**
* Paints the end of the document html.
*
* @return void
*/
public function paintDocumentEnd() {
$baseDir = $this->params['baseDir'];
include CAKE_TESTS_LIB . 'templates/footer.php';
if (ob_get_length()) {
ob_end_flush();
}
}
/**
* Paints the test failure with a breadcrumbs
* trail of the nesting test suites below the
* top level test.
*
* @param PHPUnit_Framework_AssertionFailedError $message Failure object displayed in
* the context of the other tests.
* @return void
*/
public function paintFail($message, $test) {
$trace = $this->_getStackTrace($message);
$testName = get_class($test) . '(' . $test->getName() . ')';
echo "- \n";
echo "Failed";
echo "
" . $this->_htmlEntities($message->toString()) . "
\n";
echo "" . __('Test case: %s', $testName) . "
\n";
echo "" . __('Stack trace:') . '
' . $trace . "
\n";
echo " \n";
}
/**
* Paints the test pass with a breadcrumbs
* trail of the nesting test suites below the
* top level test.
*
* @param PHPUnit_Framework_Test test method that just passed
* @param float $time time spent to run the test method
* @return void
*/
public function paintPass(PHPUnit_Framework_Test $test, $time = null) {
if (isset($this->params['show_passes']) && $this->params['show_passes']) {
echo "- \n";
echo "Passed ";
echo "
" . $this->_htmlEntities($test->getName()) . " ($time seconds)\n";
echo " \n";
}
}
/**
* Paints a PHP exception.
*
* @param Exception $exception Exception to display.
* @return void
*/
public function paintException($message, $test) {
$trace = $this->_getStackTrace($message);
$testName = get_class($test) . '(' . $test->getName() . ')';
echo "- \n";
echo "" . get_class($message) . "";
echo "
" . $this->_htmlEntities($message->getMessage()) . "
\n";
echo "" . __('Test case: %s', $testName) . "
\n";
echo "" . __('Stack trace:') . '
' . $trace . "
\n";
echo " \n";
}
/**
* Prints the message for skipping tests.
*
* @param string $message Text of skip condition.
* @param PHPUnit_Framework_TestCase $test the test method skipped
* @return void
*/
public function paintSkip($message, $test) {
echo "- \n";
echo "Skipped ";
echo $test->getName() . ': ' . $this->_htmlEntities($message->getMessage());
echo "
\n";
}
/**
* Paints formatted text such as dumped variables.
*
* @param string $message Text to show.
* @return void
*/
public function paintFormattedMessage($message) {
echo '
' . $this->_htmlEntities($message) . '
';
}
/**
* Character set adjusted entity conversion.
*
* @param string $message Plain text or Unicode message.
* @return string Browser readable message.
*/
protected function _htmlEntities($message) {
return htmlentities($message, ENT_COMPAT, $this->_characterSet);
}
/**
* Gets a formatted stack trace.
*
* @param Exception $e Exception to get a stack trace for.
* @return string Generated stack trace.
*/
protected function _getStackTrace(Exception $e) {
$trace = $e->getTrace();
$out = array();
foreach ($trace as $frame) {
if (isset($frame['file']) && isset($frame['line'])) {
$out[] = $frame['file'] . ' : ' . $frame['line'];
} elseif (isset($frame['class']) && isset($frame['function'])) {
$out[] = $frame['class'] . '::' . $frame['function'];
} else {
$out[] = '[internal]';
}
}
return implode('
', $out);
}
/**
* A test suite started.
*
* @param PHPUnit_Framework_TestSuite $suite
*/
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
if (!$this->_headerSent) {
echo $this->paintHeader();
}
echo '' . __('Running %s', $suite->getName()) . '
';
}
}