[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

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

   1  <?php
   2  /**

   3   *    base include file for SimpleTest

   4   *    @package    SimpleTest

   5   *    @subpackage    UnitTester

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

   7   */
   8  
   9  /**

  10   * @ignore - PHP5 compatibility fix.

  11   */
  12  if (! defined('E_STRICT')) {
  13      define('E_STRICT', 2048);
  14  }
  15  
  16  /**#@+

  17   * Includes SimpleTest files.

  18   */
  19  require_once dirname(__FILE__) . '/invoker.php';
  20  require_once dirname(__FILE__) . '/test_case.php';
  21  require_once dirname(__FILE__) . '/expectation.php';
  22  /**#@-*/

  23  
  24  /**

  25   *    Extension that traps errors into an error queue.

  26   *      @package SimpleTest

  27   *      @subpackage UnitTester

  28   */
  29  class SimpleErrorTrappingInvoker extends SimpleInvokerDecorator {
  30  
  31      /**

  32       *    Stores the invoker to wrap.

  33       *    @param SimpleInvoker $invoker  Test method runner.

  34       */
  35  	function SimpleErrorTrappingInvoker(&$invoker) {
  36          $this->SimpleInvokerDecorator($invoker);
  37      }
  38  
  39      /**

  40       *    Invokes a test method and dispatches any

  41       *    untrapped errors. Called back from

  42       *    the visiting runner.

  43       *    @param string $method    Test method to call.

  44       *    @access public

  45       */
  46  	function invoke($method) {
  47          $context = &SimpleTest::getContext();
  48          $queue = &$context->get('SimpleErrorQueue');
  49          $queue->setTestCase($this->GetTestCase());
  50          set_error_handler('SimpleTestErrorHandler');
  51          parent::invoke($method);
  52          while (list($severity, $message, $file, $line) = $queue->extract()) {
  53              $severity = SimpleErrorQueue::getSeverityAsString($severity);
  54              $test = &$this->getTestCase();
  55              $test->error($severity, $message, $file, $line);
  56          }
  57          restore_error_handler();
  58      }
  59  
  60  	function after($method) {
  61          $context = &SimpleTest::getContext();
  62          $queue = &$context->get('SimpleErrorQueue');
  63          $queue->setTestCase($this->getTestCase());
  64          while (list($expected, $message) = $queue->extractExpectation()) {
  65              $testCase = &$this->getTestCase();
  66  
  67              $testCase->fail(
  68                  sprintf('Expected PHP error [%s] not caught', $expected->_value)
  69              );
  70          }
  71          parent::after($method);
  72      }
  73  }
  74  
  75  /**

  76   *    Singleton error queue used to record trapped

  77   *    errors.

  78   *      @package    SimpleTest

  79   *      @subpackage    UnitTester

  80   */
  81  class SimpleErrorQueue {
  82      var $_queue;
  83      var $_expectation_queue;
  84      var $_test;
  85  
  86      /**

  87       *    Starts with an empty queue.

  88       */
  89  	function SimpleErrorQueue() {
  90          $this->clear();
  91      }
  92  
  93      /**

  94       *    Sets the currently running test case.

  95       *    @param SimpleTestCase $test    Test case to send messages to.

  96       *    @access public

  97       */
  98  	function setTestCase(&$test) {
  99          $this->_test = &$test;
 100      }
 101  
 102      /**

 103       *    Adds an error to the front of the queue.

 104       *    @param integer $severity       PHP error code.

 105       *    @param string $content         Text of error.

 106       *    @param string $filename        File error occoured in.

 107       *    @param integer $line           Line number of error.

 108       *    @access public

 109       */
 110  	function add($severity, $content, $filename, $line) {
 111          $content = str_replace('%', '%%', $content);
 112          if (count($this->_expectation_queue)) {
 113              $this->_testLatestError($severity, $content, $filename, $line);
 114          } else {
 115              array_push(
 116                      $this->_queue,
 117                      array($severity, $content, $filename, $line));
 118          }
 119      }
 120  
 121      /**

 122       *    Tests the error against the most recent expected

 123       *    error.

 124       *    @param integer $severity       PHP error code.

 125       *    @param string $content         Text of error.

 126       *    @param string $filename        File error occoured in.

 127       *    @param integer $line           Line number of error.

 128       *    @access private

 129       */
 130  	function _testLatestError($severity, $content, $filename, $line) {
 131          list($expected, $message) = array_shift($this->_expectation_queue);
 132          $severity = $this->getSeverityAsString($severity);
 133          $is_match = $this->_test->assert(
 134                  $expected,
 135                  $content,
 136                  sprintf($message, "%s -> PHP error [$content] severity [$severity] in [$filename] line [$line]"));
 137          if (! $is_match) {
 138              $this->_test->error($severity, $content, $filename, $line);
 139          }
 140      }
 141  
 142      /**

 143       *    Pulls the earliest error from the queue.

 144       *    @return     False if none, or a list of error

 145       *                information. Elements are: severity

 146       *                as the PHP error code, the error message,

 147       *                the file with the error, the line number

 148       *                and a list of PHP super global arrays.

 149       *    @access public

 150       */
 151  	function extract() {
 152          if (count($this->_queue)) {
 153              return array_shift($this->_queue);
 154          }
 155          return false;
 156      }
 157  
 158  	function extractExpectation() {
 159          if (count($this->_expectation_queue)) {
 160              return array_shift($this->_expectation_queue);
 161          }
 162          return false;
 163      }
 164  
 165      /**

 166       *    Discards the contents of the error queue.

 167       *    @access public

 168       */
 169  	function clear() {
 170          $this->_queue = array();
 171          $this->_expectation_queue = array();
 172      }
 173  
 174      /**

 175       *    @deprecated

 176       */
 177  	function assertNoErrors($message) {
 178          return $this->_test->assert(
 179                  new TrueExpectation(),
 180                  count($this->_queue) == 0,
 181                  sprintf($message, 'Should be no errors'));
 182      }
 183  
 184      /**

 185       *    @deprecated

 186       */
 187  	function assertError($expected, $message) {
 188          if (count($this->_queue) == 0) {
 189              $this->_test->fail(sprintf($message, 'Expected error not found'));
 190              return false;
 191          }
 192          list($severity, $content, $file, $line) = $this->extract();
 193          $severity = $this->getSeverityAsString($severity);
 194          return $this->_test->assert(
 195                  $expected,
 196                  $content,
 197                  sprintf($message, "Expected PHP error [$content] severity [$severity] in [$file] line [$line]"));
 198      }
 199  
 200      /**

 201       *    Sets up an expectation of an error. If this is

 202       *    not fulfilled at the end of the test, a failure

 203       *    will occour. If the error does happen, then this

 204       *    will cancel it out and send a pass message.

 205       *    @param SimpleExpectation $expected    Expected error match.

 206       *    @param string $message                Message to display.

 207       *    @access public

 208       */
 209  	function expectError($expected, $message) {
 210          array_push($this->_expectation_queue, array($expected, $message));
 211      }
 212  
 213      /**

 214       *    Converts an error code into it's string

 215       *    representation.

 216       *    @param $severity  PHP integer error code.

 217       *    @return           String version of error code.

 218       *    @access public

 219       *    @static

 220       */
 221  	function getSeverityAsString($severity) {
 222          static $map = array(
 223                  E_STRICT => 'E_STRICT',
 224                  E_ERROR => 'E_ERROR',
 225                  E_WARNING => 'E_WARNING',
 226                  E_PARSE => 'E_PARSE',
 227                  E_NOTICE => 'E_NOTICE',
 228                  E_CORE_ERROR => 'E_CORE_ERROR',
 229                  E_CORE_WARNING => 'E_CORE_WARNING',
 230                  E_COMPILE_ERROR => 'E_COMPILE_ERROR',
 231                  E_COMPILE_WARNING => 'E_COMPILE_WARNING',
 232                  E_USER_ERROR => 'E_USER_ERROR',
 233                  E_USER_WARNING => 'E_USER_WARNING',
 234                  E_USER_NOTICE => 'E_USER_NOTICE');
 235      if(version_compare(phpversion(), '5.2.0', '>=')) {
 236         $map[E_RECOVERABLE_ERROR] = 'E_RECOVERABLE_ERROR';
 237      }
 238          return $map[$severity];
 239      }
 240  }
 241  
 242  /**

 243   *    Error handler that simply stashes any errors into the global

 244   *    error queue. Simulates the existing behaviour with respect to

 245   *    logging errors, but this feature may be removed in future.

 246   *    @param $severity        PHP error code.

 247   *    @param $message         Text of error.

 248   *    @param $filename        File error occoured in.

 249   *    @param $line            Line number of error.

 250   *    @param $super_globals   Hash of PHP super global arrays.

 251   *    @static

 252   *    @access public

 253   */
 254  function SimpleTestErrorHandler($severity, $message, $filename = null, $line = null, $super_globals = null, $mask = null) {
 255      $severity = $severity & error_reporting();
 256      if ($severity) {
 257          restore_error_handler();
 258          if (ini_get('log_errors')) {
 259              $label = SimpleErrorQueue::getSeverityAsString($severity);
 260              error_log("$label: $message in $filename on line $line");
 261          }
 262          $context = &SimpleTest::getContext();
 263          $queue = &$context->get('SimpleErrorQueue');
 264          $queue->add($severity, $message, $filename, $line);
 265          set_error_handler('SimpleTestErrorHandler');
 266      }
 267      return true;
 268  }
 269  ?>


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