reporter.php 14 KB

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