Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletestlib/reporter.php
Go to the documentation of this file.
00001 <?php
00012 require_once(dirname(__FILE__) . '/scorer.php');
00021 class HtmlReporter extends SimpleReporter {
00022     var $_character_set;
00023 
00030     function HtmlReporter($character_set = 'ISO-8859-1') {
00031         $this->SimpleReporter();
00032         $this->_character_set = $character_set;
00033     }
00034 
00041     function paintHeader($test_name) {
00042         $this->sendNoCacheHeaders();
00043         print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
00044         print "<html>\n<head>\n<title>$test_name</title>\n";
00045         print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" .
00046                 $this->_character_set . "\">\n";
00047         print "<style type=\"text/css\">\n";
00048         print $this->_getCss() . "\n";
00049         print "</style>\n";
00050         print "</head>\n<body>\n";
00051         print "<h1>$test_name</h1>\n";
00052         flush();
00053     }
00054 
00062     function sendNoCacheHeaders() {
00063         if (! headers_sent()) {
00064             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
00065             header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
00066             header("Cache-Control: no-store, no-cache, must-revalidate");
00067             header("Cache-Control: post-check=0, pre-check=0", false);
00068             header("Pragma: no-cache");
00069         }
00070     }
00071 
00077     function _getCss() {
00078         return ".fail { background-color: inherit; color: red; }" .
00079                 ".pass { background-color: inherit; color: green; }" .
00080                 " pre { background-color: lightgray; color: inherit; }";
00081     }
00082 
00089     function paintFooter($test_name) {
00090         $colour = ($this->getFailCount() + $this->getExceptionCount() > 0 ? "red" : "green");
00091         print "<div style=\"";
00092         print "padding: 8px; margin-top: 1em; background-color: $colour; color: white;";
00093         print "\">";
00094         print $this->getTestCaseProgress() . "/" . $this->getTestCaseCount();
00095         print " test cases complete:\n";
00096         print "<strong>" . $this->getPassCount() . "</strong> passes, ";
00097         print "<strong>" . $this->getFailCount() . "</strong> fails and ";
00098         print "<strong>" . $this->getExceptionCount() . "</strong> exceptions.";
00099         print "</div>\n";
00100         print "</body>\n</html>\n";
00101     }
00102 
00111     function paintFail($message) {
00112         parent::paintFail($message);
00113         print "<span class=\"fail\">Fail</span>: ";
00114         $breadcrumb = $this->getTestList();
00115         array_shift($breadcrumb);
00116         print implode(" -&gt; ", $breadcrumb);
00117         print " -&gt; " . $this->_htmlEntities($message) . "<br />\n";
00118     }
00119 
00125     function paintError($message) {
00126         parent::paintError($message);
00127         print "<span class=\"fail\">Exception</span>: ";
00128         $breadcrumb = $this->getTestList();
00129         array_shift($breadcrumb);
00130         print implode(" -&gt; ", $breadcrumb);
00131         print " -&gt; <strong>" . $this->_htmlEntities($message) . "</strong><br />\n";
00132     }
00133 
00139     function paintException($exception) {
00140         parent::paintException($exception);
00141         print "<span class=\"fail\">Exception</span>: ";
00142         $breadcrumb = $this->getTestList();
00143         array_shift($breadcrumb);
00144         print implode(" -&gt; ", $breadcrumb);
00145         $message = 'Unexpected exception of type [' . get_class($exception) .
00146                 '] with message ['. $exception->getMessage() .
00147                 '] in ['. $exception->getFile() .
00148                 ' line ' . $exception->getLine() . ']';
00149         print " -&gt; <strong>" . $this->_htmlEntities($message) . "</strong><br />\n";
00150     }
00151     
00157     function paintSkip($message) {
00158         parent::paintSkip($message);
00159         print "<span class=\"pass\">Skipped</span>: ";
00160         $breadcrumb = $this->getTestList();
00161         array_shift($breadcrumb);
00162         print implode(" -&gt; ", $breadcrumb);
00163         print " -&gt; " . $this->_htmlEntities($message) . "<br />\n";
00164     }
00165 
00171     function paintFormattedMessage($message) {
00172         print '<pre>' . $this->_htmlEntities($message) . '</pre>';
00173     }
00174 
00181     function _htmlEntities($message) {
00182         return htmlentities($message, ENT_COMPAT, $this->_character_set);
00183     }
00184 }
00185 
00195 class TextReporter extends SimpleReporter {
00196 
00202     function TextReporter() {
00203         $this->SimpleReporter();
00204     }
00205 
00211     function paintHeader($test_name) {
00212         if (! SimpleReporter::inCli()) {
00213             header('Content-type: text/plain');
00214         }
00215         print "$test_name\n";
00216         flush();
00217     }
00218 
00225     function paintFooter($test_name) {
00226         if ($this->getFailCount() + $this->getExceptionCount() == 0) {
00227             print "OK\n";
00228         } else {
00229             print "FAILURES!!!\n";
00230         }
00231         print "Test cases run: " . $this->getTestCaseProgress() .
00232                 "/" . $this->getTestCaseCount() .
00233                 ", Passes: " . $this->getPassCount() .
00234                 ", Failures: " . $this->getFailCount() .
00235                 ", Exceptions: " . $this->getExceptionCount() . "\n";
00236     }
00237 
00244     function paintFail($message) {
00245         parent::paintFail($message);
00246         print $this->getFailCount() . ") $message\n";
00247         $breadcrumb = $this->getTestList();
00248         array_shift($breadcrumb);
00249         print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
00250         print "\n";
00251     }
00252 
00259     function paintError($message) {
00260         parent::paintError($message);
00261         print "Exception " . $this->getExceptionCount() . "!\n$message\n";
00262         $breadcrumb = $this->getTestList();
00263         array_shift($breadcrumb);
00264         print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
00265         print "\n";
00266     }
00267 
00274     function paintException($exception) {
00275         parent::paintException($exception);
00276         $message = 'Unexpected exception of type [' . get_class($exception) .
00277                 '] with message ['. $exception->getMessage() .
00278                 '] in ['. $exception->getFile() .
00279                 ' line ' . $exception->getLine() . ']';
00280         print "Exception " . $this->getExceptionCount() . "!\n$message\n";
00281         $breadcrumb = $this->getTestList();
00282         array_shift($breadcrumb);
00283         print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
00284         print "\n";
00285     }
00286     
00292     function paintSkip($message) {
00293         parent::paintSkip($message);
00294         print "Skip: $message\n";
00295     }
00296 
00302     function paintFormattedMessage($message) {
00303         print "$message\n";
00304         flush();
00305     }
00306 }
00307 
00314 class SelectiveReporter extends SimpleReporterDecorator {
00315     var $_just_this_case = false;
00316     var $_just_this_test = false;
00317     var $_on;
00318     
00326     function SelectiveReporter(&$reporter, $just_this_case = false, $just_this_test = false) {
00327         if (isset($just_this_case) && $just_this_case) {
00328             $this->_just_this_case = strtolower($just_this_case);
00329             $this->_off();
00330         } else {
00331             $this->_on();
00332         }
00333         if (isset($just_this_test) && $just_this_test) {
00334             $this->_just_this_test = strtolower($just_this_test);
00335         }
00336         $this->SimpleReporterDecorator($reporter);
00337     }
00338 
00345     function _matchesTestCase($test_case) {
00346         return $this->_just_this_case == strtolower($test_case);
00347     }
00348 
00357     function _shouldRunTest($test_case, $method) {
00358         if ($this->_isOn() || $this->_matchesTestCase($test_case)) {
00359             if ($this->_just_this_test) {
00360                 return $this->_just_this_test == strtolower($method);
00361             } else {
00362                 return true;
00363             }
00364         }
00365         return false;
00366     }
00367     
00372     function _on() {
00373         $this->_on = true;
00374     }
00375     
00380     function _off() {
00381         $this->_on = false;
00382     }
00383     
00389     function _isOn() {
00390         return $this->_on;
00391     }
00392 
00400     function shouldInvoke($test_case, $method) {
00401         if ($this->_shouldRunTest($test_case, $method)) {
00402             return $this->_reporter->shouldInvoke($test_case, $method);
00403         }
00404         return false;
00405     }
00406 
00413     function paintGroupStart($test_case, $size) {
00414         if ($this->_just_this_case && $this->_matchesTestCase($test_case)) {
00415             $this->_on();
00416         }
00417         $this->_reporter->paintGroupStart($test_case, $size);
00418     }
00419 
00425     function paintGroupEnd($test_case) {
00426         $this->_reporter->paintGroupEnd($test_case);
00427         if ($this->_just_this_case && $this->_matchesTestCase($test_case)) {
00428             $this->_off();
00429         }
00430     }
00431 }
00432 
00438 class NoSkipsReporter extends SimpleReporterDecorator {
00439     
00445     function paintSkip($message) { }
00446 }
00447 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations