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