PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/lib/reporter/cake_html_reporter.php

https://bitbucket.org/webpolis/hurli
PHP | 378 lines | 205 code | 26 blank | 147 comment | 16 complexity | 37e880430fb7c4fa96f31cc12cecd815 MD5 | raw file
  1. <?php
  2. /**
  3. * CakeHtmlReporter
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.tests.libs.reporter
  17. * @since CakePHP(tm) v 1.2.0.4433
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. include_once dirname(__FILE__) . DS . 'cake_base_reporter.php';
  21. /**
  22. * CakeHtmlReporter Reports Results of TestSuites and Test Cases
  23. * in an HTML format / context.
  24. *
  25. * @package cake
  26. * @subpackage cake.tests.lib
  27. */
  28. class CakeHtmlReporter extends CakeBaseReporter {
  29. /**
  30. * Constructor
  31. *
  32. * @param string $charset
  33. * @param string $params
  34. * @return void
  35. */
  36. function CakeHtmlReporter($charset = 'utf-8', $params = array()) {
  37. $params = array_map(array($this, '_htmlEntities'), $params);
  38. $this->CakeBaseReporter($charset, $params);
  39. }
  40. /**
  41. * Paints the top of the web page setting the
  42. * title to the name of the starting test.
  43. *
  44. * @param string $test_name Name class of test.
  45. * @return void
  46. * @access public
  47. */
  48. function paintHeader($testName) {
  49. $this->sendNoCacheHeaders();
  50. $this->paintDocumentStart();
  51. $this->paintTestMenu();
  52. printf("<h2>%s</h2>\n", $this->_htmlEntities($testName));
  53. echo "<ul class='tests'>\n";
  54. }
  55. /**
  56. * Paints the document start content contained in header.php
  57. *
  58. * @return void
  59. */
  60. function paintDocumentStart() {
  61. ob_start();
  62. $baseDir = $this->params['baseDir'];
  63. include CAKE_TESTS_LIB . 'templates' . DS . 'header.php';
  64. }
  65. /**
  66. * Paints the menu on the left side of the test suite interface.
  67. * Contains all of the various plugin, core, and app buttons.
  68. *
  69. * @return void
  70. */
  71. function paintTestMenu() {
  72. $groups = $this->baseUrl() . '?show=groups';
  73. $cases = $this->baseUrl() . '?show=cases';
  74. $plugins = App::objects('plugin');
  75. sort($plugins);
  76. include CAKE_TESTS_LIB . 'templates' . DS . 'menu.php';
  77. }
  78. /**
  79. * Retrieves and paints the list of tests cases in an HTML format.
  80. *
  81. * @return void
  82. */
  83. function testCaseList() {
  84. $testCases = parent::testCaseList();
  85. $app = $this->params['app'];
  86. $plugin = $this->params['plugin'];
  87. $buffer = "<h3>Core Test Cases:</h3>\n<ul>";
  88. $urlExtra = null;
  89. if ($app) {
  90. $buffer = "<h3>App Test Cases:</h3>\n<ul>";
  91. $urlExtra = '&app=true';
  92. } elseif ($plugin) {
  93. $buffer = "<h3>" . Inflector::humanize($plugin) . " Test Cases:</h3>\n<ul>";
  94. $urlExtra = '&plugin=' . $plugin;
  95. }
  96. if (1 > count($testCases)) {
  97. $buffer .= "<strong>EMPTY</strong>";
  98. }
  99. foreach ($testCases as $testCaseFile => $testCase) {
  100. $title = explode(strpos($testCase, '\\') ? '\\' : '/', str_replace('.test.php', '', $testCase));
  101. $title[count($title) - 1] = Inflector::camelize($title[count($title) - 1]);
  102. $title = implode(' / ', $title);
  103. $buffer .= "<li><a href='" . $this->baseUrl() . "?case=" . urlencode($testCase) . $urlExtra ."'>" . $title . "</a></li>\n";
  104. }
  105. $buffer .= "</ul>\n";
  106. echo $buffer;
  107. }
  108. /**
  109. * Retrieves and paints the list of group tests in an HTML format.
  110. *
  111. * @return void
  112. */
  113. function groupTestList() {
  114. $groupTests = parent::groupTestList();
  115. $app = $this->params['app'];
  116. $plugin = $this->params['plugin'];
  117. $buffer = "<h3>Core Test Groups:</h3>\n<ul>";
  118. $urlExtra = null;
  119. if ($app) {
  120. $buffer = "<h3>App Test Groups:</h3>\n<ul>";
  121. $urlExtra = '&app=true';
  122. } else if ($plugin) {
  123. $buffer = "<h3>" . Inflector::humanize($plugin) . " Test Groups:</h3>\n<ul>";
  124. $urlExtra = '&plugin=' . $plugin;
  125. }
  126. $buffer .= "<li><a href='" . $this->baseURL() . "?group=all$urlExtra'>All tests</a></li>\n";
  127. foreach ($groupTests as $groupTest) {
  128. $buffer .= "<li><a href='" . $this->baseURL() . "?group={$groupTest}" . "{$urlExtra}'>" . $groupTest . "</a></li>\n";
  129. }
  130. $buffer .= "</ul>\n";
  131. echo $buffer;
  132. }
  133. /**
  134. * Send the headers necessary to ensure the page is
  135. * reloaded on every request. Otherwise you could be
  136. * scratching your head over out of date test data.
  137. *
  138. * @return void
  139. * @access public
  140. */
  141. function sendNoCacheHeaders() {
  142. if (!headers_sent()) {
  143. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  144. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  145. header("Cache-Control: no-store, no-cache, must-revalidate");
  146. header("Cache-Control: post-check=0, pre-check=0", false);
  147. header("Pragma: no-cache");
  148. }
  149. }
  150. /**
  151. * Paints the end of the test with a summary of
  152. * the passes and failures.
  153. *
  154. * @param string $test_name Name class of test.
  155. * @return void
  156. * @access public
  157. */
  158. function paintFooter($test_name) {
  159. $colour = ($this->getFailCount() + $this->getExceptionCount() > 0 ? "red" : "green");
  160. echo "</ul>\n";
  161. echo "<div style=\"";
  162. echo "padding: 8px; margin: 1em 0; background-color: $colour; color: white;";
  163. echo "\">";
  164. echo $this->getTestCaseProgress() . "/" . $this->getTestCaseCount();
  165. echo " test cases complete:\n";
  166. echo "<strong>" . $this->getPassCount() . "</strong> passes, ";
  167. echo "<strong>" . $this->getFailCount() . "</strong> fails and ";
  168. echo "<strong>" . $this->getExceptionCount() . "</strong> exceptions.";
  169. echo "</div>\n";
  170. echo '<div style="padding:0 0 5px;">';
  171. echo '<p><strong>Time taken by tests (in seconds):</strong> ' . $this->_timeDuration . '</p>';
  172. if (function_exists('memory_get_peak_usage')) {
  173. echo '<p><strong>Peak memory use: (in bytes):</strong> ' . number_format(memory_get_peak_usage()) . '</p>';
  174. }
  175. echo $this->_paintLinks();
  176. echo '</div>';
  177. if (
  178. isset($this->params['codeCoverage']) &&
  179. $this->params['codeCoverage'] &&
  180. class_exists('CodeCoverageManager')
  181. ) {
  182. CodeCoverageManager::report();
  183. }
  184. $this->paintDocumentEnd();
  185. }
  186. /**
  187. * Renders the links that for accessing things in the test suite.
  188. *
  189. * @return void
  190. */
  191. function _paintLinks() {
  192. $show = $query = array();
  193. if (!empty($this->params['group'])) {
  194. $show['show'] = 'groups';
  195. } elseif (!empty($this->params['case'])) {
  196. $show['show'] = 'cases';
  197. }
  198. if (!empty($this->params['app'])) {
  199. $show['app'] = $query['app'] = 'true';
  200. }
  201. if (!empty($this->params['plugin'])) {
  202. $show['plugin'] = $query['plugin'] = $this->params['plugin'];
  203. }
  204. if (!empty($this->params['case'])) {
  205. $query['case'] = $this->params['case'];
  206. } elseif (!empty($this->params['group'])) {
  207. $query['group'] = $this->params['group'];
  208. }
  209. $show = $this->_queryString($show);
  210. $query = $this->_queryString($query);
  211. echo "<p><a href='" . $this->baseUrl() . $show . "'>Run more tests</a> | <a href='" . $this->baseUrl() . $query . "&show_passes=1'>Show Passes</a> | \n";
  212. echo " <a href='" . $this->baseUrl() . $query . "&amp;code_coverage=true'>Analyze Code Coverage</a></p>\n";
  213. }
  214. /**
  215. * Convert an array of parameters into a query string url
  216. *
  217. * @param array $url Url hash to be converted
  218. * @return string Converted url query string
  219. */
  220. function _queryString($url) {
  221. $out = '?';
  222. $params = array();
  223. foreach ($url as $key => $value) {
  224. $params[] = "$key=$value";
  225. }
  226. $out .= implode('&amp;', $params);
  227. return $out;
  228. }
  229. /**
  230. * Paints the end of the document html.
  231. *
  232. * @return void
  233. */
  234. function paintDocumentEnd() {
  235. $baseDir = $this->params['baseDir'];
  236. include CAKE_TESTS_LIB . 'templates' . DS . 'footer.php';
  237. ob_end_flush();
  238. }
  239. /**
  240. * Paints the test failure with a breadcrumbs
  241. * trail of the nesting test suites below the
  242. * top level test.
  243. *
  244. * @param string $message Failure message displayed in
  245. * the context of the other tests.
  246. * @return void
  247. * @access public
  248. */
  249. function paintFail($message) {
  250. parent::paintFail($message);
  251. echo "<li class='fail'>\n";
  252. echo "<span>Failed</span>";
  253. echo "<div class='msg'>" . $this->_htmlEntities($message) . "</div>\n";
  254. $breadcrumb = $this->getTestList();
  255. array_shift($breadcrumb);
  256. echo "<div>" . implode(" -&gt; ", $breadcrumb) . "</div>\n";
  257. echo "</li>\n";
  258. }
  259. /**
  260. * Paints the test pass with a breadcrumbs
  261. * trail of the nesting test suites below the
  262. * top level test.
  263. *
  264. * @param string $message Pass message displayed in the context of the other tests.
  265. * @return void
  266. * @access public
  267. */
  268. function paintPass($message) {
  269. parent::paintPass($message);
  270. if (isset($this->params['show_passes']) && $this->params['show_passes']) {
  271. echo "<li class='pass'>\n";
  272. echo "<span>Passed</span> ";
  273. $breadcrumb = $this->getTestList();
  274. array_shift($breadcrumb);
  275. echo implode(" -&gt; ", $breadcrumb);
  276. echo "<br />" . $this->_htmlEntities($message) . "\n";
  277. echo "</li>\n";
  278. }
  279. }
  280. /**
  281. * Paints a PHP error.
  282. *
  283. * @param string $message Message is ignored.
  284. * @return void
  285. * @access public
  286. */
  287. function paintError($message) {
  288. parent::paintError($message);
  289. echo "<li class='error'>\n";
  290. echo "<span>Error</span>";
  291. echo "<div class='msg'>" . $this->_htmlEntities($message) . "</div>\n";
  292. $breadcrumb = $this->getTestList();
  293. array_shift($breadcrumb);
  294. echo "<div>" . implode(" -&gt; ", $breadcrumb) . "</div>\n";
  295. echo "</li>\n";
  296. }
  297. /**
  298. * Paints a PHP exception.
  299. *
  300. * @param Exception $exception Exception to display.
  301. * @return void
  302. * @access public
  303. */
  304. function paintException($exception) {
  305. parent::paintException($exception);
  306. echo "<li class='fail'>\n";
  307. echo "<span>Exception</span>";
  308. $message = 'Unexpected exception of type [' . get_class($exception) .
  309. '] with message ['. $exception->getMessage() .
  310. '] in ['. $exception->getFile() .
  311. ' line ' . $exception->getLine() . ']';
  312. echo "<div class='msg'>" . $this->_htmlEntities($message) . "</div>\n";
  313. $breadcrumb = $this->getTestList();
  314. array_shift($breadcrumb);
  315. echo "<div>" . implode(" -&gt; ", $breadcrumb) . "</div>\n";
  316. echo "</li>\n";
  317. }
  318. /**
  319. * Prints the message for skipping tests.
  320. *
  321. * @param string $message Text of skip condition.
  322. * @return void
  323. * @access public
  324. */
  325. function paintSkip($message) {
  326. parent::paintSkip($message);
  327. echo "<li class='skipped'>\n";
  328. echo "<span>Skipped</span> ";
  329. echo $this->_htmlEntities($message);
  330. echo "</li>\n";
  331. }
  332. /**
  333. * Paints formatted text such as dumped variables.
  334. *
  335. * @param string $message Text to show.
  336. * @return void
  337. * @access public
  338. */
  339. function paintFormattedMessage($message) {
  340. echo '<pre>' . $this->_htmlEntities($message) . '</pre>';
  341. }
  342. /**
  343. * Character set adjusted entity conversion.
  344. *
  345. * @param string $message Plain text or Unicode message.
  346. * @return string Browser readable message.
  347. * @access protected
  348. */
  349. function _htmlEntities($message) {
  350. return htmlentities($message, ENT_COMPAT, $this->_characterSet);
  351. }
  352. }