[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/tests_runner/lib/simpletest/ -> reporter.php (source)

   1  <?php
   2      /**

   3       *    base include file for SimpleTest

   4       *    @package    SimpleTest

   5       *    @subpackage    UnitTester

   6       *    @version    $Id: reporter.php 5999 2007-06-18 13:13:08Z pachanga $

   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(" -&gt; ", $breadcrumb);
 117              print " -&gt; " . $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(" -&gt; ", $breadcrumb);
 131              print " -&gt; <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(" -&gt; ", $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 " -&gt; <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(" -&gt; ", $breadcrumb);
 163              print " -&gt; " . $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              $breadcrumb = $this->getTestList();
 263              array_shift($breadcrumb);
 264              print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
 265              print "\n";
 266          }
 267  
 268          /**

 269           *    Paints a PHP error or exception.

 270           *    @param Exception $exception      Exception to describe.

 271           *    @access public

 272           *    @abstract

 273           */
 274          function paintException($exception) {
 275              parent::paintException($exception);
 276              $message = 'Unexpected exception of type [' . get_class($exception) .
 277                      '] with message ['. $exception->getMessage() .
 278                      '] in ['. $exception->getFile() .
 279                      ' line ' . $exception->getLine() . ']';
 280              print "Exception " . $this->getExceptionCount() . "!\n$message\n";
 281              $breadcrumb = $this->getTestList();
 282              array_shift($breadcrumb);
 283              print "\tin " . implode("\n\tin ", array_reverse($breadcrumb));
 284              print "\n";
 285          }
 286          
 287          /**

 288           *    Prints the message for skipping tests.

 289           *    @param string $message    Text of skip condition.

 290           *    @access public

 291           */
 292  		function paintSkip($message) {
 293              parent::paintSkip($message);
 294              print "Skip: $message\n";
 295          }
 296  
 297          /**

 298           *    Paints formatted text such as dumped variables.

 299           *    @param string $message        Text to show.

 300           *    @access public

 301           */
 302          function paintFormattedMessage($message) {
 303              print "$message\n";
 304              flush();
 305          }
 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          /**

 320           *    Selects the test case or group to be run,

 321           *    and optionally a specific test.

 322           *    @param SimpleScorer $reporter    Reporter to receive events.

 323           *    @param string $just_this_case    Only this case or group will run.

 324           *    @param string $just_this_test    Only this test method will run.

 325           */
 326          function SelectiveReporter(&$reporter, $just_this_case = false, $just_this_test = false) {
 327              if (isset($just_this_case) && $just_this_case) {
 328                  $this->_just_this_case = strtolower($just_this_case);
 329                  $this->_off();
 330              } else {
 331                  $this->_on();
 332              }
 333              if (isset($just_this_test) && $just_this_test) {
 334                  $this->_just_this_test = strtolower($just_this_test);
 335              }
 336              $this->SimpleReporterDecorator($reporter);
 337          }
 338  
 339          /**

 340           *    Compares criteria to actual the case/group name.

 341           *    @param string $test_case    The incoming test.

 342           *    @return boolean             True if matched.

 343           *    @access protected

 344           */
 345          function _matchesTestCase($test_case) {
 346              return $this->_just_this_case == strtolower($test_case);
 347          }
 348  
 349          /**

 350           *    Compares criteria to actual the test name. If no

 351           *    name was specified at the beginning, then all tests

 352           *    can run.

 353           *    @param string $method       The incoming test method.

 354           *    @return boolean             True if matched.

 355           *    @access protected

 356           */
 357          function _shouldRunTest($test_case, $method) {
 358              if ($this->_isOn() || $this->_matchesTestCase($test_case)) {
 359                  if ($this->_just_this_test) {
 360                      return $this->_just_this_test == strtolower($method);
 361                  } else {
 362                      return true;
 363                  }
 364              }
 365              return false;
 366          }
 367          
 368          /**

 369           *    Switch on testing for the group or subgroup.

 370           *    @access private

 371           */
 372          function _on() {
 373              $this->_on = true;
 374          }
 375          
 376          /**

 377           *    Switch off testing for the group or subgroup.

 378           *    @access private

 379           */
 380          function _off() {
 381              $this->_on = false;
 382          }
 383          
 384          /**

 385           *    Is this group actually being tested?

 386           *    @return boolean     True if the current test group is active.

 387           *    @access private

 388           */
 389          function _isOn() {
 390              return $this->_on;
 391          }
 392  
 393          /**

 394           *    Veto everything that doesn't match the method wanted.

 395           *    @param string $test_case       Name of test case.

 396           *    @param string $method          Name of test method.

 397           *    @return boolean                True if test should be run.

 398           *    @access public

 399           */
 400          function shouldInvoke($test_case, $method) {
 401              if ($this->_shouldRunTest($test_case, $method)) {
 402                  return $this->_reporter->shouldInvoke($test_case, $method);
 403              }
 404              return false;
 405          }
 406  
 407          /**

 408           *    Paints the start of a group test.

 409           *    @param string $test_case     Name of test or other label.

 410           *    @param integer $size         Number of test cases starting.

 411           *    @access public

 412           */
 413          function paintGroupStart($test_case, $size) {
 414              if ($this->_just_this_case && $this->_matchesTestCase($test_case)) {
 415                  $this->_on();
 416              }
 417              $this->_reporter->paintGroupStart($test_case, $size);
 418          }
 419  
 420          /**

 421           *    Paints the end of a group test.

 422           *    @param string $test_case     Name of test or other label.

 423           *    @access public

 424           */
 425          function paintGroupEnd($test_case) {
 426              $this->_reporter->paintGroupEnd($test_case);
 427              if ($this->_just_this_case && $this->_matchesTestCase($test_case)) {
 428                  $this->_off();
 429              }
 430          }
 431      }
 432  ?>


Generated: Sat Nov 22 03:48:54 2008 Cross-referenced by PHPXref 0.7