Source for file cli_reporter.php

Documentation is available at cli_reporter.php

  1. <?php
  2. /**
  3. * Swiped from the WACT test suite
  4. * @author Jon Bangoid
  5. * @see http://www.phpwact.org
  6. * @version $Id: cli_reporter.php,v 1.1 2007/09/09 20:39:50 pitlinz Exp $
  7. */
  8. if (! defined('ST_FAILDETAIL_SEPARATOR')) {
  9.     define('ST_FAILDETAIL_SEPARATOR', "->");
  10. }
  11.  
  12. if (! defined('ST_FAILS_RETURN_CODE')) {
  13.     define('ST_FAILS_RETURN_CODE', 1);
  14. }
  15.  
  16. if (version_compare(phpversion(), '4.3.0', '<') ||
  17.     php_sapi_name() == 'cgi') {
  18.     define('STDOUT', fopen('php://stdout', 'w'));
  19.     define('STDERR', fopen('php://stderr', 'w'));
  20.         create_function('', 'fclose(STDOUT); fclose(STDERR); return true;'));
  21. }
  22.  
  23. /**
  24.  * Minimal command line test displayer. Writes fail details to STDERR. Returns 0
  25.  * to the shell if all tests pass, ST_FAILS_RETURN_CODE if any test fails.
  26.  */
  27. class CLIReporter extends SimpleReporter {
  28.  
  29.     var $faildetail_separator = ST_FAILDETAIL_SEPARATOR;
  30.  
  31.     function CLIReporter($faildetail_separator = NULL) {
  32.         $this->SimpleReporter();
  33.         if (! is_null($faildetail_separator)) {
  34.             $this->setFailDetailSeparator($faildetail_separator);
  35.         }
  36.     }
  37.  
  38.     function setFailDetailSeparator($separator) {
  39.         $this->faildetail_separator = $separator;
  40.     }
  41.  
  42.     /**
  43.      * Return a formatted faildetail for printing.
  44.      */
  45.     function &_paintTestFailDetail(&$message) {
  46.         $buffer = '';
  47.         $faildetail = $this->getTestList();
  48.         array_shift($faildetail);
  49.         $buffer .= implode($this->faildetail_separator, $faildetail);
  50.         $buffer .= $this->faildetail_separator . "$message\n";
  51.         return $buffer;
  52.     }
  53.  
  54.     /**
  55.      * Paint fail faildetail to STDERR.
  56.      */
  57.     function paintFail($message) {
  58.         parent::paintFail($message);
  59.         fwrite(STDERR, 'FAIL' . $this->faildetail_separator .
  60.                $this->_paintTestFailDetail($message));
  61.     }
  62.  
  63.     /**
  64.      * Paint exception faildetail to STDERR.
  65.      */
  66.     function paintException($message) {
  67.         parent::paintException($message);
  68.         fwrite(STDERR, 'EXCEPTION' . $this->faildetail_separator .
  69.                $this->_paintTestFailDetail($message));
  70.     }
  71.  
  72.     /**
  73.      * Paint a footer with test case name, timestamp, counts of fails and
  74.      * exceptions.
  75.      */
  76.     function paintFooter($test_name) {
  77.         $buffer = $this->getTestCaseProgress() . '/' .
  78.             $this->getTestCaseCount() . ' test cases complete: ';
  79.  
  80.         if (0 < ($this->getFailCount() + $this->getExceptionCount())) {
  81.             $buffer .= $this->getPassCount() . " passes";
  82.             if (0 < $this->getFailCount()) {
  83.                 $buffer .= ", " . $this->getFailCount() . " fails";
  84.             }
  85.             if (0 < $this->getExceptionCount()) {
  86.                 $buffer .= ", " . $this->getExceptionCount() . " exceptions";
  87.             }
  88.             $buffer .= ".\n";
  89.             fwrite(STDOUT, $buffer);
  90.             exit(ST_FAILS_RETURN_CODE);
  91.         } else {
  92.             fwrite(STDOUT, $buffer . $this->getPassCount() . " passes.\n");
  93.         }
  94.     }
  95. }

Documentation generated on Thu, 08 Jan 2009 17:38:14 +0100 by phpDocumentor 1.4.0a2