CakeHtmlReporter.php 11 KB

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