CakeHtmlReporter.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /**
  3. * CakeHtmlReporter
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 1.2.0.4433
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. App::uses('CakeBaseReporter', 'TestSuite/Reporter');
  18. /**
  19. * CakeHtmlReporter Reports Results of TestSuites and Test Cases
  20. * in an HTML format / context.
  21. *
  22. * @package Cake.TestSuite.Reporter
  23. */
  24. class CakeHtmlReporter extends CakeBaseReporter {
  25. /**
  26. * Paints the top of the web page setting the
  27. * title to the name of the starting test.
  28. *
  29. * @return void
  30. */
  31. public function paintHeader() {
  32. $this->_headerSent = true;
  33. $this->sendContentType();
  34. $this->sendNoCacheHeaders();
  35. $this->paintDocumentStart();
  36. $this->paintTestMenu();
  37. echo "<ul class='tests'>\n";
  38. }
  39. /**
  40. * Set the content-type header so it is in the correct encoding.
  41. *
  42. * @return void
  43. */
  44. public function sendContentType() {
  45. if (!headers_sent()) {
  46. header('Content-Type: text/html; charset=' . Configure::read('App.encoding'));
  47. }
  48. }
  49. /**
  50. * Paints the document start content contained in header.php
  51. *
  52. * @return void
  53. */
  54. public function paintDocumentStart() {
  55. ob_start();
  56. $baseDir = $this->params['baseDir'];
  57. include CAKE . 'TestSuite' . DS . 'templates' . DS . 'header.php';
  58. }
  59. /**
  60. * Paints the menu on the left side of the test suite interface.
  61. * Contains all of the various plugin, core, and app buttons.
  62. *
  63. * @return void
  64. */
  65. public function paintTestMenu() {
  66. $cases = $this->baseUrl() . '?show=cases';
  67. $plugins = App::objects('plugin', null, false);
  68. sort($plugins);
  69. include CAKE . 'TestSuite' . DS . 'templates' . DS . 'menu.php';
  70. }
  71. /**
  72. * Retrieves and paints the list of tests cases in an HTML format.
  73. *
  74. * @return void
  75. */
  76. public function testCaseList() {
  77. $testCases = parent::testCaseList();
  78. $core = $this->params['core'];
  79. $plugin = $this->params['plugin'];
  80. $buffer = "<h3>App Test Cases:</h3>\n<ul>";
  81. $urlExtra = null;
  82. if ($core) {
  83. $buffer = "<h3>Core Test Cases:</h3>\n<ul>";
  84. $urlExtra = '&core=true';
  85. } elseif ($plugin) {
  86. $buffer = "<h3>" . Inflector::humanize($plugin) . " Test Cases:</h3>\n<ul>";
  87. $urlExtra = '&plugin=' . $plugin;
  88. }
  89. if (count($testCases) < 1) {
  90. $buffer .= "<strong>EMPTY</strong>";
  91. }
  92. foreach ($testCases as $testCase) {
  93. $title = explode(DS, str_replace('.test.php', '', $testCase));
  94. $title[count($title) - 1] = Inflector::camelize($title[count($title) - 1]);
  95. $title = implode(' / ', $title);
  96. $buffer .= "<li><a href='" . $this->baseUrl() . "?case=" . urlencode($testCase) . $urlExtra . "'>" . $title . "</a></li>\n";
  97. }
  98. $buffer .= "</ul>\n";
  99. echo $buffer;
  100. }
  101. /**
  102. * Send the headers necessary to ensure the page is
  103. * reloaded on every request. Otherwise you could be
  104. * scratching your head over out of date test data.
  105. *
  106. * @return void
  107. */
  108. public function sendNoCacheHeaders() {
  109. if (!headers_sent()) {
  110. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  111. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  112. header("Cache-Control: no-store, no-cache, must-revalidate");
  113. header("Cache-Control: post-check=0, pre-check=0", false);
  114. header("Pragma: no-cache");
  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. * Paints a code coverage report.
  158. *
  159. * @param array $coverage
  160. * @return void
  161. */
  162. public function paintCoverage(array $coverage) {
  163. App::uses('HtmlCoverageReport', 'TestSuite/Coverage');
  164. $reporter = new HtmlCoverageReport($coverage, $this);
  165. echo $reporter->report();
  166. }
  167. /**
  168. * Renders the links that for accessing things in the test suite.
  169. *
  170. * @return void
  171. */
  172. protected function _paintLinks() {
  173. $show = $query = array();
  174. if (!empty($this->params['case'])) {
  175. $show['show'] = 'cases';
  176. }
  177. if (!empty($this->params['core'])) {
  178. $show['core'] = $query['core'] = 'true';
  179. }
  180. if (!empty($this->params['plugin'])) {
  181. $show['plugin'] = $query['plugin'] = $this->params['plugin'];
  182. }
  183. if (!empty($this->params['case'])) {
  184. $query['case'] = $this->params['case'];
  185. }
  186. $show = $this->_queryString($show);
  187. $query = $this->_queryString($query);
  188. echo "<p><a href='" . $this->baseUrl() . $show . "'>Run more tests</a> | <a href='" . $this->baseUrl() . $query . "&amp;show_passes=1'>Show Passes</a> | \n";
  189. echo "<a href='" . $this->baseUrl() . $query . "&amp;debug=1'>Enable Debug Output</a> | \n";
  190. echo "<a href='" . $this->baseUrl() . $query . "&amp;code_coverage=true'>Analyze Code Coverage</a></p>\n";
  191. }
  192. /**
  193. * Convert an array of parameters into a query string url
  194. *
  195. * @param array $url Url hash to be converted
  196. * @return string Converted url query string
  197. */
  198. protected function _queryString($url) {
  199. $out = '?';
  200. $params = array();
  201. foreach ($url as $key => $value) {
  202. $params[] = "$key=$value";
  203. }
  204. $out .= implode('&amp;', $params);
  205. return $out;
  206. }
  207. /**
  208. * Paints the end of the document html.
  209. *
  210. * @return void
  211. */
  212. public function paintDocumentEnd() {
  213. $baseDir = $this->params['baseDir'];
  214. include CAKE . 'TestSuite' . DS . 'templates' . DS . 'footer.php';
  215. if (ob_get_length()) {
  216. ob_end_flush();
  217. }
  218. }
  219. /**
  220. * Paints the test failure with a breadcrumbs
  221. * trail of the nesting test suites below the
  222. * top level test.
  223. *
  224. * @param PHPUnit_Framework_AssertionFailedError $message Failure object displayed in
  225. * the context of the other tests.
  226. * @param mixed $test
  227. * @return void
  228. */
  229. public function paintFail($message, $test) {
  230. $trace = $this->_getStackTrace($message);
  231. $testName = get_class($test) . '(' . $test->getName() . ')';
  232. $actualMsg = $expectedMsg = null;
  233. if (method_exists($message, 'getComparisonFailure')) {
  234. $failure = $message->getComparisonFailure();
  235. if (is_object($failure)) {
  236. $actualMsg = $failure->getActualAsString();
  237. $expectedMsg = $failure->getExpectedAsString();
  238. }
  239. }
  240. echo "<li class='fail'>\n";
  241. echo "<span>Failed</span>";
  242. echo "<div class='msg'><pre>" . $this->_htmlEntities($message->toString());
  243. if ((is_string($actualMsg) && is_string($expectedMsg)) || (is_array($actualMsg) && is_array($expectedMsg))) {
  244. echo "<br />" . $this->_htmlEntities(PHPUnit_Util_Diff::diff($expectedMsg, $actualMsg));
  245. }
  246. echo "</pre></div>\n";
  247. echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
  248. echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
  249. echo "</li>\n";
  250. }
  251. /**
  252. * Paints the test pass with a breadcrumbs
  253. * trail of the nesting test suites below the
  254. * top level test.
  255. *
  256. * @param PHPUnit_Framework_Test test method that just passed
  257. * @param float $time time spent to run the test method
  258. * @return void
  259. */
  260. public function paintPass(PHPUnit_Framework_Test $test, $time = null) {
  261. if (isset($this->params['showPasses']) && $this->params['showPasses']) {
  262. echo "<li class='pass'>\n";
  263. echo "<span>Passed</span> ";
  264. echo "<br />" . $this->_htmlEntities($test->getName()) . " ($time seconds)\n";
  265. echo "</li>\n";
  266. }
  267. }
  268. /**
  269. * Paints a PHP exception.
  270. *
  271. * @param Exception $exception Exception to display.
  272. * @param mixed $test
  273. * @return void
  274. */
  275. public function paintException($message, $test) {
  276. $trace = $this->_getStackTrace($message);
  277. $testName = get_class($test) . '(' . $test->getName() . ')';
  278. echo "<li class='fail'>\n";
  279. echo "<span>" . get_class($message) . "</span>";
  280. echo "<div class='msg'>" . $this->_htmlEntities($message->getMessage()) . "</div>\n";
  281. echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
  282. echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
  283. echo "</li>\n";
  284. }
  285. /**
  286. * Prints the message for skipping tests.
  287. *
  288. * @param string $message Text of skip condition.
  289. * @param PHPUnit_Framework_TestCase $test the test method skipped
  290. * @return void
  291. */
  292. public function paintSkip($message, $test) {
  293. echo "<li class='skipped'>\n";
  294. echo "<span>Skipped</span> ";
  295. echo $test->getName() . ': ' . $this->_htmlEntities($message->getMessage());
  296. echo "</li>\n";
  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. * Character set adjusted entity conversion.
  309. *
  310. * @param string $message Plain text or Unicode message.
  311. * @return string Browser readable message.
  312. */
  313. protected function _htmlEntities($message) {
  314. return htmlentities($message, ENT_COMPAT, $this->_characterSet);
  315. }
  316. /**
  317. * Gets a formatted stack trace.
  318. *
  319. * @param Exception $e Exception to get a stack trace for.
  320. * @return string Generated stack trace.
  321. */
  322. protected function _getStackTrace(Exception $e) {
  323. $trace = $e->getTrace();
  324. $out = array();
  325. foreach ($trace as $frame) {
  326. if (isset($frame['file']) && isset($frame['line'])) {
  327. $out[] = $frame['file'] . ' : ' . $frame['line'];
  328. } elseif (isset($frame['class']) && isset($frame['function'])) {
  329. $out[] = $frame['class'] . '::' . $frame['function'];
  330. } else {
  331. $out[] = '[internal]';
  332. }
  333. }
  334. return implode('<br />', $out);
  335. }
  336. /**
  337. * A test suite started.
  338. *
  339. * @param PHPUnit_Framework_TestSuite $suite
  340. * @return void
  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. }