reporter.php 16 KB

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