PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/simpletest/reporter.php

https://github.com/quarkness/piwik
PHP | 447 lines | 212 code | 34 blank | 201 comment | 21 complexity | 7c3a9bf08f9a7351b347a976cd7a1327 MD5 | raw file
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage UnitTester
  6. * @version $Id: reporter.php 1702 2008-03-25 00:08:04Z lastcraft $
  7. */
  8. /**#@+
  9. * include other SimpleTest class files
  10. */
  11. require_once(dirname(__FILE__) . '/scorer.php');
  12. /**#@-*/
  13. /**
  14. * Sample minimal test displayer. Generates only
  15. * failure messages and a pass count.
  16. * @package SimpleTest
  17. * @subpackage UnitTester
  18. */
  19. class HtmlReporter extends SimpleReporter {
  20. var $_character_set;
  21. /**
  22. * Does nothing yet. The first output will
  23. * be sent on the first test start. For use
  24. * by a web browser.
  25. * @access public
  26. */
  27. function HtmlReporter($character_set = 'ISO-8859-1') {
  28. $this->SimpleReporter();
  29. $this->_character_set = $character_set;
  30. }
  31. /**
  32. * Paints the top of the web page setting the
  33. * title to the name of the starting test.
  34. * @param string $test_name Name class of test.
  35. * @access public
  36. */
  37. function paintHeader($test_name) {
  38. $this->sendNoCacheHeaders();
  39. print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
  40. print "<html>\n<head>\n<title>$test_name</title>\n";
  41. print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" .
  42. $this->_character_set . "\" />\n";
  43. print "<style type=\"text/css\">\n";
  44. print $this->_getCss() . "\n";
  45. print "</style>\n";
  46. print "</head>\n<body>\n";
  47. print "<h1>$test_name</h1>\n";
  48. flush();
  49. }
  50. /**
  51. * Send the headers necessary to ensure the page is
  52. * reloaded on every request. Otherwise you could be
  53. * scratching your head over out of date test data.
  54. * @access public
  55. * @static
  56. */
  57. function sendNoCacheHeaders() {
  58. if (! headers_sent()) {
  59. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  60. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  61. header("Cache-Control: no-store, no-cache, must-revalidate");
  62. header("Cache-Control: post-check=0, pre-check=0", false);
  63. header("Pragma: no-cache");
  64. }
  65. }
  66. /**
  67. * Paints the CSS. Add additional styles here.
  68. * @return string CSS code as text.
  69. * @access protected
  70. */
  71. function _getCss() {
  72. return ".fail { background-color: inherit; color: red; }" .
  73. ".pass { background-color: inherit; color: green; }" .
  74. " pre { background-color: lightgray; color: inherit; }";
  75. }
  76. /**
  77. * Paints the end of the test with a summary of
  78. * the passes and failures.
  79. * @param string $test_name Name class of test.
  80. * @access public
  81. */
  82. function paintFooter($test_name) {
  83. $colour = ($this->getFailCount() + $this->getExceptionCount() > 0 ? "red" : "green");
  84. print "<div style=\"";
  85. print "padding: 8px; margin-top: 1em; background-color: $colour; color: white;";
  86. print "\">";
  87. print $this->getTestCaseProgress() . "/" . $this->getTestCaseCount();
  88. print " test cases complete:\n";
  89. print "<strong>" . $this->getPassCount() . "</strong> passes, ";
  90. print "<strong>" . $this->getFailCount() . "</strong> fails and ";
  91. print "<strong>" . $this->getExceptionCount() . "</strong> exceptions.";
  92. print "</div>\n";
  93. print "</body>\n</html>\n";
  94. }
  95. /**
  96. * Paints the test failure with a breadcrumbs
  97. * trail of the nesting test suites below the
  98. * top level test.
  99. * @param string $message Failure message displayed in
  100. * the context of the other tests.
  101. * @access public
  102. */
  103. function paintFail($message) {
  104. parent::paintFail($message);
  105. print "<span class=\"fail\">Fail</span>: ";
  106. $breadcrumb = $this->getTestList();
  107. array_shift($breadcrumb);
  108. print implode(" -&gt; ", $breadcrumb);
  109. print " -&gt; " . $this->_htmlEntities($message) . "<br />\n";
  110. }
  111. /**
  112. * Paints a PHP error.
  113. * @param string $message Message is ignored.
  114. * @access public
  115. */
  116. function paintError($message) {
  117. parent::paintError($message);
  118. print "<span class=\"fail\">Exception</span>: ";
  119. $breadcrumb = $this->getTestList();
  120. array_shift($breadcrumb);
  121. print implode(" -&gt; ", $breadcrumb);
  122. print " -&gt; <strong>" . $this->_htmlEntities($message) . "</strong><br />\n";
  123. }
  124. /**
  125. * Paints a PHP exception.
  126. * @param Exception $exception Exception to display.
  127. * @access public
  128. */
  129. function paintException($exception) {
  130. parent::paintException($exception);
  131. print "<span class=\"fail\">Exception</span>: ";
  132. $breadcrumb = $this->getTestList();
  133. array_shift($breadcrumb);
  134. print implode(" -&gt; ", $breadcrumb);
  135. $message = 'Unexpected exception of type [' . get_class($exception) .
  136. '] with message ['. $exception->getMessage() .
  137. '] in ['. $exception->getFile() .
  138. ' line ' . $exception->getLine() . ']';
  139. print " -&gt; <strong>" . $this->_htmlEntities($message) . "</strong><br />\n";
  140. }
  141. /**
  142. * Prints the message for skipping tests.
  143. * @param string $message Text of skip condition.
  144. * @access public
  145. */
  146. function paintSkip($message) {
  147. parent::paintSkip($message);
  148. print "<span class=\"pass\">Skipped</span>: ";
  149. $breadcrumb = $this->getTestList();
  150. array_shift($breadcrumb);
  151. print implode(" -&gt; ", $breadcrumb);
  152. print " -&gt; " . $this->_htmlEntities($message) . "<br />\n";
  153. }
  154. /**
  155. * Paints formatted text such as dumped variables.
  156. * @param string $message Text to show.
  157. * @access public
  158. */
  159. function paintFormattedMessage($message) {
  160. print '<pre>' . $this->_htmlEntities($message) . '</pre>';
  161. }
  162. /**
  163. * Character set adjusted entity conversion.
  164. * @param string $message Plain text or Unicode message.
  165. * @return string Browser readable message.
  166. * @access protected
  167. */
  168. function _htmlEntities($message) {
  169. return htmlentities($message, ENT_COMPAT, $this->_character_set);
  170. }
  171. }
  172. /**
  173. * Sample minimal test displayer. Generates only
  174. * failure messages and a pass count. For command
  175. * line use. I've tried to make it look like JUnit,
  176. * but I wanted to output the errors as they arrived
  177. * which meant dropping the dots.
  178. * @package SimpleTest
  179. * @subpackage UnitTester
  180. */
  181. class TextReporter extends SimpleReporter {
  182. /**
  183. * Does nothing yet. The first output will
  184. * be sent on the first test start.
  185. * @access public
  186. */
  187. function TextReporter() {
  188. $this->SimpleReporter();
  189. }
  190. /**
  191. * Paints the title only.
  192. * @param string $test_name Name class of test.
  193. * @access public
  194. */
  195. function paintHeader($test_name) {
  196. if (! SimpleReporter::inCli()) {
  197. header('Content-type: text/plain');
  198. }
  199. print "$test_name\n";
  200. flush();
  201. }
  202. /**
  203. * Paints the end of the test with a summary of
  204. * the passes and failures.
  205. * @param string $test_name Name class of test.
  206. * @access public
  207. */
  208. function paintFooter($test_name) {
  209. if ($this->getFailCount() + $this->getExceptionCount() == 0) {
  210. print "OK\n";
  211. } else {
  212. print "FAILURES!!!\n";
  213. }
  214. print "Test cases run: " . $this->getTestCaseProgress() .
  215. "/" . $this->getTestCaseCount() .
  216. ", Passes: " . $this->getPassCount() .
  217. ", Failures: " . $this->getFailCount() .
  218. ", Exceptions: " . $this->getExceptionCount() . "\n";
  219. }
  220. /**
  221. * Paints the test failure as a stack trace.
  222. * @param string $message Failure message displayed in
  223. * the context of the other tests.
  224. * @access public
  225. */
  226. function paintFail($message) {
  227. parent::paintFail($message);
  228. print $this->getFailCount() . ") $message\n";
  229. $breadcrumb = $this->getTestList();
  230. array_shift($breadcrumb);
  231. print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
  232. print "\n";
  233. }
  234. /**
  235. * Paints a PHP error or exception.
  236. * @param string $message Message to be shown.
  237. * @access public
  238. * @abstract
  239. */
  240. function paintError($message) {
  241. parent::paintError($message);
  242. print "Exception " . $this->getExceptionCount() . "!\n$message\n";
  243. $breadcrumb = $this->getTestList();
  244. array_shift($breadcrumb);
  245. print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
  246. print "\n";
  247. }
  248. /**
  249. * Paints a PHP error or exception.
  250. * @param Exception $exception Exception to describe.
  251. * @access public
  252. * @abstract
  253. */
  254. function paintException($exception) {
  255. parent::paintException($exception);
  256. $message = 'Unexpected exception of type [' . get_class($exception) .
  257. '] with message ['. $exception->getMessage() .
  258. '] in ['. $exception->getFile() .
  259. ' line ' . $exception->getLine() . ']';
  260. print "Exception " . $this->getExceptionCount() . "!\n$message\n";
  261. $breadcrumb = $this->getTestList();
  262. array_shift($breadcrumb);
  263. print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
  264. print "\n";
  265. }
  266. /**
  267. * Prints the message for skipping tests.
  268. * @param string $message Text of skip condition.
  269. * @access public
  270. */
  271. function paintSkip($message) {
  272. parent::paintSkip($message);
  273. print "Skip: $message\n";
  274. }
  275. /**
  276. * Paints formatted text such as dumped variables.
  277. * @param string $message Text to show.
  278. * @access public
  279. */
  280. function paintFormattedMessage($message) {
  281. print "$message\n";
  282. flush();
  283. }
  284. }
  285. /**
  286. * Runs just a single test group, a single case or
  287. * even a single test within that case.
  288. * @package SimpleTest
  289. * @subpackage UnitTester
  290. */
  291. class SelectiveReporter extends SimpleReporterDecorator {
  292. var $_just_this_case = false;
  293. var $_just_this_test = false;
  294. var $_on;
  295. /**
  296. * Selects the test case or group to be run,
  297. * and optionally a specific test.
  298. * @param SimpleScorer $reporter Reporter to receive events.
  299. * @param string $just_this_case Only this case or group will run.
  300. * @param string $just_this_test Only this test method will run.
  301. */
  302. function SelectiveReporter(&$reporter, $just_this_case = false, $just_this_test = false) {
  303. if (isset($just_this_case) && $just_this_case) {
  304. $this->_just_this_case = strtolower($just_this_case);
  305. $this->_off();
  306. } else {
  307. $this->_on();
  308. }
  309. if (isset($just_this_test) && $just_this_test) {
  310. $this->_just_this_test = strtolower($just_this_test);
  311. }
  312. $this->SimpleReporterDecorator($reporter);
  313. }
  314. /**
  315. * Compares criteria to actual the case/group name.
  316. * @param string $test_case The incoming test.
  317. * @return boolean True if matched.
  318. * @access protected
  319. */
  320. function _matchesTestCase($test_case) {
  321. return $this->_just_this_case == strtolower($test_case);
  322. }
  323. /**
  324. * Compares criteria to actual the test name. If no
  325. * name was specified at the beginning, then all tests
  326. * can run.
  327. * @param string $method The incoming test method.
  328. * @return boolean True if matched.
  329. * @access protected
  330. */
  331. function _shouldRunTest($test_case, $method) {
  332. if ($this->_isOn() || $this->_matchesTestCase($test_case)) {
  333. if ($this->_just_this_test) {
  334. return $this->_just_this_test == strtolower($method);
  335. } else {
  336. return true;
  337. }
  338. }
  339. return false;
  340. }
  341. /**
  342. * Switch on testing for the group or subgroup.
  343. * @access private
  344. */
  345. function _on() {
  346. $this->_on = true;
  347. }
  348. /**
  349. * Switch off testing for the group or subgroup.
  350. * @access private
  351. */
  352. function _off() {
  353. $this->_on = false;
  354. }
  355. /**
  356. * Is this group actually being tested?
  357. * @return boolean True if the current test group is active.
  358. * @access private
  359. */
  360. function _isOn() {
  361. return $this->_on;
  362. }
  363. /**
  364. * Veto everything that doesn't match the method wanted.
  365. * @param string $test_case Name of test case.
  366. * @param string $method Name of test method.
  367. * @return boolean True if test should be run.
  368. * @access public
  369. */
  370. function shouldInvoke($test_case, $method) {
  371. if ($this->_shouldRunTest($test_case, $method)) {
  372. return $this->_reporter->shouldInvoke($test_case, $method);
  373. }
  374. return false;
  375. }
  376. /**
  377. * Paints the start of a group test.
  378. * @param string $test_case Name of test or other label.
  379. * @param integer $size Number of test cases starting.
  380. * @access public
  381. */
  382. function paintGroupStart($test_case, $size) {
  383. if ($this->_just_this_case && $this->_matchesTestCase($test_case)) {
  384. $this->_on();
  385. }
  386. $this->_reporter->paintGroupStart($test_case, $size);
  387. }
  388. /**
  389. * Paints the end of a group test.
  390. * @param string $test_case Name of test or other label.
  391. * @access public
  392. */
  393. function paintGroupEnd($test_case) {
  394. $this->_reporter->paintGroupEnd($test_case);
  395. if ($this->_just_this_case && $this->_matchesTestCase($test_case)) {
  396. $this->_off();
  397. }
  398. }
  399. }
  400. /**
  401. * Suppresses skip messages.
  402. * @package SimpleTest
  403. * @subpackage UnitTester
  404. */
  405. class NoSkipsReporter extends SimpleReporterDecorator {
  406. /**
  407. * Does nothing.
  408. * @param string $message Text of skip condition.
  409. * @access public
  410. */
  411. function paintSkip($message) { }
  412. }
  413. ?>