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

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

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