PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/simpletest/reporter.php

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