PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/TestSuite/Reporter/CakeHtmlReporter.php

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