reporter.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. echo '<html style="height:100%;">';
  41. echo '<head>';
  42. echo '<title>Test Suite</title>';
  43. echo '</head>';
  44. echo '<body style="padding:0px;margin:0px;height:100%;">';
  45. echo '<div id="wrapper" style="width:90%;padding-left:5%;padding-bottom:10%;">';//wrapper
  46. echo '<div id="header1">';//header1
  47. echo '<div id="header3" style="min-height:100px;background:url(img/header-logo.png) no-repeat left;"><br/>';//header3
  48. echo '<div id="header4" style="float:right;font-size:25px;color:#01933E;"><br/>';//header4
  49. echo ''. date('Y - m - d').'';
  50. echo '</div>';//fin header 4
  51. echo '</div><br/>';//fin header 3
  52. echo '</div>';//fin header 1
  53. echo '<div id="header2" style="width:100%;border:2px solid #DDDDDD;">';//header2
  54. echo '<div id="content2" style="background-image:url(img/bg-header4.png);background-repeat:repeat-x";>
  55. <h2><div style="font-size:18px;font-family:Tahoma,Geneva,sans-serif;color:#01933E;text-decoration:none;">These results showing how many functions have been tested in the code of chamilo 1.8.6.2</div></h2><br/>
  56. <div style="font-size:12px;font-family:Tahoma,Geneva,sans-serif;color:#01933E;">
  57. <p>This page showing the different types results than have been tested and showing the lines the problems.</p>
  58. <ol><li>This results show the amount of the tests than have been tested.</li>
  59. <li>This results show the amount of the tests than have passed.</li>
  60. <li>This results show the amount of the tests than have failed.</li>
  61. <li>This results show the amount of exceptions in the tests.</li></ol>
  62. <p>You can always find more information about simpletest on the website: http://www.lastcraft.com </p>
  63. <p>"Only a well software like Chamilo, is perfect when has been tested by excellent Testing Engineers".</p>
  64. </div></div>';//content2
  65. echo '</div><br/>';//fin header2
  66. //flush();
  67. }
  68. /**
  69. * Send the headers necessary to ensure the page is
  70. * reloaded on every request. Otherwise you could be
  71. * scratching your head over out of date test data.
  72. * @access public
  73. * @static
  74. */
  75. function sendNoCacheHeaders() {
  76. if (! headers_sent()) {
  77. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  78. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  79. header("Cache-Control: no-store, no-cache, must-revalidate");
  80. header("Cache-Control: post-check=0, pre-check=0", false);
  81. header("Pragma: no-cache");
  82. }
  83. }
  84. /**
  85. * Paints the CSS. Add additional styles here.
  86. * @return string CSS code as text.
  87. * @access protected
  88. */
  89. function _getCss() {
  90. return ".fail { background-color: inherit; color: red; }" .
  91. ".pass { background-color: inherit; color: green; }" .
  92. " pre { background-color: lightgray; color: inherit; }";
  93. }
  94. /**
  95. * Paints the end of the test with a summary of
  96. * the passes and failures.
  97. * @param string $test_name Name class of test.
  98. * @access public
  99. */
  100. function paintFooter($test_name) {
  101. $colour = ($this->getFailCount() + $this->getExceptionCount() > 0 ? "red" : "green");
  102. echo '<div id="header5" style="background-color: '.$colour.';color: white;font-weight:bold;">';//header 5
  103. print "<style type=\"text/css\">\n";
  104. print $this->_getCss() . "\n";
  105. print "</style>\n";
  106. echo ' Test cases complete: ';
  107. echo $this->getTestCaseProgress() . '/' . $this->getTestCaseCount().'<br/><br/>';
  108. echo 'Passes: ' . $this->getPassCount() . ' ';
  109. echo 'Fails: ' . $this->getFailCount() . ' ';
  110. echo 'Exceptions: ' . $this->getExceptionCount() . ' ';
  111. echo '</div>';//fin header5
  112. echo '</div>';//fin wrapper
  113. echo '<div id="footer1" style= "height:6em;background-image:url(img/bg-footer.gif);background-repeat:repeat-x ">';
  114. echo '</div>';//fin footer 1
  115. echo '</body>';
  116. echo '</html>';
  117. }
  118. /**
  119. * Paints the test failure with a breadcrumbs
  120. * trail of the nesting test suites below the
  121. * top level test.
  122. * @param string $message Failure message displayed in
  123. * the context of the other tests.
  124. * @access public
  125. */
  126. function paintFail($message) {
  127. parent::paintFail($message);
  128. print "<span class=\"fail\">Fail</span>: ";
  129. $breadcrumb = $this->getTestList();
  130. array_shift($breadcrumb);
  131. print implode(" -&gt; ", $breadcrumb);
  132. print " -&gt; " . $this->_htmlEntities($message) . "<br />\n";
  133. }
  134. /**
  135. * Paints a PHP error.
  136. * @param string $message Message is ignored.
  137. * @access public
  138. */
  139. function paintError($message) {
  140. parent::paintError($message);
  141. print "<span class=\"fail\">Exception</span>: ";
  142. $breadcrumb = $this->getTestList();
  143. array_shift($breadcrumb);
  144. print implode(" -&gt; ", $breadcrumb);
  145. print " -&gt; <strong>" . $this->_htmlEntities($message) . "</strong><br />\n";
  146. }
  147. /**
  148. * Paints a PHP exception.
  149. * @param Exception $exception Exception to display.
  150. * @access public
  151. */
  152. function paintException($exception) {
  153. parent::paintException($exception);
  154. print "<span class=\"fail\">Exception</span>: ";
  155. $breadcrumb = $this->getTestList();
  156. array_shift($breadcrumb);
  157. print implode(" -&gt; ", $breadcrumb);
  158. $message = 'Unexpected exception of type [' . get_class($exception) .
  159. '] with message ['. $exception->getMessage() .
  160. '] in ['. $exception->getFile() .
  161. ' line ' . $exception->getLine() . ']';
  162. print " -&gt; <strong>" . $this->_htmlEntities($message) . "</strong><br />\n";
  163. }
  164. /**
  165. * Prints the message for skipping tests.
  166. * @param string $message Text of skip condition.
  167. * @access public
  168. */
  169. function paintSkip($message) {
  170. parent::paintSkip($message);
  171. print "<span class=\"pass\">Skipped</span>: ";
  172. $breadcrumb = $this->getTestList();
  173. array_shift($breadcrumb);
  174. print implode(" -&gt; ", $breadcrumb);
  175. print " -&gt; " . $this->_htmlEntities($message) . "<br />\n";
  176. }
  177. /**
  178. * Paints formatted text such as dumped variables.
  179. * @param string $message Text to show.
  180. * @access public
  181. */
  182. function paintFormattedMessage($message) {
  183. print '<pre>' . $this->_htmlEntities($message) . '</pre>';
  184. }
  185. /**
  186. * Character set adjusted entity conversion.
  187. * @param string $message Plain text or Unicode message.
  188. * @return string Browser readable message.
  189. * @access protected
  190. */
  191. function _htmlEntities($message) {
  192. return htmlentities($message, ENT_COMPAT, $this->_character_set);
  193. }
  194. }
  195. /**
  196. * Sample minimal test displayer. Generates only
  197. * failure messages and a pass count. For command
  198. * line use. I've tried to make it look like JUnit,
  199. * but I wanted to output the errors as they arrived
  200. * which meant dropping the dots.
  201. * @package SimpleTest
  202. * @subpackage UnitTester
  203. */
  204. class TextReporter extends SimpleReporter {
  205. /**
  206. * Does nothing yet. The first output will
  207. * be sent on the first test start.
  208. * @access public
  209. */
  210. function TextReporter() {
  211. $this->SimpleReporter();
  212. }
  213. /**
  214. * Paints the title only.
  215. * @param string $test_name Name class of test.
  216. * @access public
  217. */
  218. function paintHeader($test_name) {
  219. if (! SimpleReporter::inCli()) {
  220. header('Content-type: text/plain');
  221. }
  222. print "$test_name\n";
  223. flush();
  224. }
  225. /**
  226. * Paints the end of the test with a summary of
  227. * the passes and failures.
  228. * @param string $test_name Name class of test.
  229. * @access public
  230. */
  231. function paintFooter($test_name) {
  232. if ($this->getFailCount() + $this->getExceptionCount() == 0) {
  233. print "OK\n";
  234. } else {
  235. print "FAILURES!!!\n";
  236. }
  237. print "Test cases run: " . $this->getTestCaseProgress() .
  238. "/" . $this->getTestCaseCount() .
  239. ", Passes: " . $this->getPassCount() .
  240. ", Failures: " . $this->getFailCount() .
  241. ", Exceptions: " . $this->getExceptionCount() . "\n";
  242. }
  243. /**
  244. * Paints the test failure as a stack trace.
  245. * @param string $message Failure message displayed in
  246. * the context of the other tests.
  247. * @access public
  248. */
  249. function paintFail($message) {
  250. parent::paintFail($message);
  251. print $this->getFailCount() . ") $message\n";
  252. $breadcrumb = $this->getTestList();
  253. array_shift($breadcrumb);
  254. print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
  255. print "\n";
  256. }
  257. /**
  258. * Paints a PHP error or exception.
  259. * @param string $message Message to be shown.
  260. * @access public
  261. * @abstract
  262. */
  263. function paintError($message) {
  264. parent::paintError($message);
  265. print "Exception " . $this->getExceptionCount() . "!\n$message\n";
  266. $breadcrumb = $this->getTestList();
  267. array_shift($breadcrumb);
  268. print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
  269. print "\n";
  270. }
  271. /**
  272. * Paints a PHP error or exception.
  273. * @param Exception $exception Exception to describe.
  274. * @access public
  275. * @abstract
  276. */
  277. function paintException($exception) {
  278. parent::paintException($exception);
  279. $message = 'Unexpected exception of type [' . get_class($exception) .
  280. '] with message ['. $exception->getMessage() .
  281. '] in ['. $exception->getFile() .
  282. ' line ' . $exception->getLine() . ']';
  283. print "Exception " . $this->getExceptionCount() . "!\n$message\n";
  284. $breadcrumb = $this->getTestList();
  285. array_shift($breadcrumb);
  286. print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
  287. print "\n";
  288. }
  289. /**
  290. * Prints the message for skipping tests.
  291. * @param string $message Text of skip condition.
  292. * @access public
  293. */
  294. function paintSkip($message) {
  295. parent::paintSkip($message);
  296. print "Skip: $message\n";
  297. }
  298. /**
  299. * Paints formatted text such as dumped variables.
  300. * @param string $message Text to show.
  301. * @access public
  302. */
  303. function paintFormattedMessage($message) {
  304. print "$message\n";
  305. flush();
  306. }
  307. }
  308. /**
  309. * Runs just a single test group, a single case or
  310. * even a single test within that case.
  311. * @package SimpleTest
  312. * @subpackage UnitTester
  313. */
  314. class SelectiveReporter extends SimpleReporterDecorator {
  315. var $_just_this_case = false;
  316. var $_just_this_test = false;
  317. var $_on;
  318. /**
  319. * Selects the test case or group to be run,
  320. * and optionally a specific test.
  321. * @param SimpleScorer $reporter Reporter to receive events.
  322. * @param string $just_this_case Only this case or group will run.
  323. * @param string $just_this_test Only this test method will run.
  324. */
  325. function SelectiveReporter(&$reporter, $just_this_case = false, $just_this_test = false) {
  326. if (isset($just_this_case) && $just_this_case) {
  327. $this->_just_this_case = strtolower($just_this_case);
  328. $this->_off();
  329. } else {
  330. $this->_on();
  331. }
  332. if (isset($just_this_test) && $just_this_test) {
  333. $this->_just_this_test = strtolower($just_this_test);
  334. }
  335. $this->SimpleReporterDecorator($reporter);
  336. }
  337. /**
  338. * Compares criteria to actual the case/group name.
  339. * @param string $test_case The incoming test.
  340. * @return boolean True if matched.
  341. * @access protected
  342. */
  343. function _matchesTestCase($test_case) {
  344. return $this->_just_this_case == strtolower($test_case);
  345. }
  346. /**
  347. * Compares criteria to actual the test name. If no
  348. * name was specified at the beginning, then all tests
  349. * can run.
  350. * @param string $method The incoming test method.
  351. * @return boolean True if matched.
  352. * @access protected
  353. */
  354. function _shouldRunTest($test_case, $method) {
  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. function _on() {
  369. $this->_on = true;
  370. }
  371. /**
  372. * Switch off testing for the group or subgroup.
  373. * @access private
  374. */
  375. function _off() {
  376. $this->_on = false;
  377. }
  378. /**
  379. * Is this group actually being tested?
  380. * @return boolean True if the current test group is active.
  381. * @access private
  382. */
  383. function _isOn() {
  384. return $this->_on;
  385. }
  386. /**
  387. * Veto everything that doesn't match the method wanted.
  388. * @param string $test_case Name of test case.
  389. * @param string $method Name of test method.
  390. * @return boolean True if test should be run.
  391. * @access public
  392. */
  393. function shouldInvoke($test_case, $method) {
  394. if ($this->_shouldRunTest($test_case, $method)) {
  395. return $this->_reporter->shouldInvoke($test_case, $method);
  396. }
  397. return false;
  398. }
  399. /**
  400. * Paints the start of a group test.
  401. * @param string $test_case Name of test or other label.
  402. * @param integer $size Number of test cases starting.
  403. * @access public
  404. */
  405. function paintGroupStart($test_case, $size) {
  406. if ($this->_just_this_case && $this->_matchesTestCase($test_case)) {
  407. $this->_on();
  408. }
  409. $this->_reporter->paintGroupStart($test_case, $size);
  410. }
  411. /**
  412. * Paints the end of a group test.
  413. * @param string $test_case Name of test or other label.
  414. * @access public
  415. */
  416. function paintGroupEnd($test_case) {
  417. $this->_reporter->paintGroupEnd($test_case);
  418. if ($this->_just_this_case && $this->_matchesTestCase($test_case)) {
  419. $this->_off();
  420. }
  421. }
  422. }
  423. /**
  424. * Suppresses skip messages.
  425. * @package SimpleTest
  426. * @subpackage UnitTester
  427. */
  428. class NoSkipsReporter extends SimpleReporterDecorator {
  429. /**
  430. * Does nothing.
  431. * @param string $message Text of skip condition.
  432. * @access public
  433. */
  434. function paintSkip($message) { }
  435. }
  436. ?>