Source for file errors.php

Documentation is available at errors.php

  1. <?php
  2. /**
  3.  *  base include file for SimpleTest
  4.  *  @package    SimpleTest
  5.  *  @subpackage UnitTester
  6.  *  @version    $Id: errors.php 1672 2008-03-02 04:47:34Z edwardzyang $
  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.  *    Extension that traps errors into an error queue.
  25.  *    @package SimpleTest
  26.  *    @subpackage UnitTester
  27.  */
  28.  
  29.     /**
  30.      *    Stores the invoker to wrap.
  31.      *    @param SimpleInvoker $invoker  Test method runner.
  32.      */
  33.     function SimpleErrorTrappingInvoker(&$invoker{
  34.         $this->SimpleInvokerDecorator($invoker);
  35.     }
  36.  
  37.     /**
  38.      *    Invokes a test method and dispatches any
  39.      *    untrapped errors. Called back from
  40.      *    the visiting runner.
  41.      *    @param string $method    Test method to call.
  42.      *    @access public
  43.      */
  44.     function invoke($method{
  45.         $queue &$this->_createErrorQueue();
  46.         set_error_handler('SimpleTestErrorHandler');
  47.         parent::invoke($method);
  48.         restore_error_handler();
  49.         $queue->tally();
  50.     }
  51.     
  52.     /**
  53.      *    Wires up the error queue for a single test.
  54.      *    @return SimpleErrorQueue    Queue connected to the test.
  55.      *    @access private
  56.      */
  57.     function &_createErrorQueue({
  58.         $context &SimpleTest::getContext();
  59.         $test &$this->getTestCase();
  60.         $queue &$context->get('SimpleErrorQueue');
  61.         $queue->setTestCase($test);
  62.         return $queue;
  63.     }
  64. }
  65.  
  66. /**
  67.  *    Error queue used to record trapped
  68.  *    errors.
  69.  *    @package  SimpleTest
  70.  *    @subpackage   UnitTester
  71.  */
  72.     var $_queue;
  73.     var $_expectation_queue;
  74.     var $_test;
  75.     var $_using_expect_style = false;
  76.  
  77.     /**
  78.      *    Starts with an empty queue.
  79.      */
  80.     function SimpleErrorQueue({
  81.         $this->clear();
  82.     }
  83.  
  84.     /**
  85.      *    Discards the contents of the error queue.
  86.      *    @access public
  87.      */
  88.     function clear({
  89.         $this->_queue = array();
  90.         $this->_expectation_queue = array();
  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.      *    Sets up an expectation of an error. If this is
  104.      *    not fulfilled at the end of the test, a failure
  105.      *    will occour. If the error does happen, then this
  106.      *    will cancel it out and send a pass message.
  107.      *    @param SimpleExpectation $expected    Expected error match.
  108.      *    @param string $message                Message to display.
  109.      *    @access public
  110.      */
  111.     function expectError($expected$message{
  112.         $this->_using_expect_style = true;
  113.         array_push($this->_expectation_queuearray($expected$message));
  114.     }
  115.  
  116.     /**
  117.      *    Adds an error to the front of the queue.
  118.      *    @param integer $severity       PHP error code.
  119.      *    @param string $content         Text of error.
  120.      *    @param string $filename        File error occoured in.
  121.      *    @param integer $line           Line number of error.
  122.      *    @access public
  123.      */
  124.     function add($severity$content$filename$line{
  125.         $content str_replace('%''%%'$content);
  126.         if ($this->_using_expect_style{
  127.             $this->_testLatestError($severity$content$filename$line);
  128.         else {
  129.             array_push(
  130.                     $this->_queue,
  131.                     array($severity$content$filename$line));
  132.         }
  133.     }
  134.     
  135.     /**
  136.      *    Any errors still in the queue are sent to the test
  137.      *    case. Any unfulfilled expectations trigger failures.
  138.      *    @access public
  139.      */
  140.     function tally({
  141.         while (list($severity$message$file$line$this->extract()) {
  142.             $severity $this->getSeverityAsString($severity);
  143.             $this->_test->error($severity$message$file$line);
  144.         }
  145.         while (list($expected$message$this->_extractExpectation()) {
  146.             $this->_test->assert($expectedfalse"%s -> Expected error not caught");
  147.         }
  148.     }
  149.  
  150.     /**
  151.      *    Tests the error against the most recent expected
  152.      *    error.
  153.      *    @param integer $severity       PHP error code.
  154.      *    @param string $content         Text of error.
  155.      *    @param string $filename        File error occoured in.
  156.      *    @param integer $line           Line number of error.
  157.      *    @access private
  158.      */
  159.     function _testLatestError($severity$content$filename$line{
  160.         if ($expectation $this->_extractExpectation()) {
  161.             list($expected$message$expectation;
  162.             $this->_test->assert($expected$contentsprintf(
  163.                     $message,
  164.                     "%s -> PHP error [$content] severity [.
  165.                             $this->getSeverityAsString($severity.
  166.                             "] in [$filename] line [$line]"));
  167.         else {
  168.             $this->_test->error($severity$content$filename$line);
  169.         }
  170.     }
  171.  
  172.     /**
  173.      *    Pulls the earliest error from the queue.
  174.      *    @return  mixed    False if none, or a list of error
  175.      *                       information. Elements are: severity
  176.      *                       as the PHP error code, the error message,
  177.      *                       the file with the error, the line number
  178.      *                       and a list of PHP super global arrays.
  179.      *    @access public
  180.      */
  181.     function extract({
  182.         if (count($this->_queue)) {
  183.             return array_shift($this->_queue);
  184.         }
  185.         return false;
  186.     }
  187.  
  188.     /**
  189.      *    Pulls the earliest expectation from the queue.
  190.      *    @return     SimpleExpectation    False if none.
  191.      *    @access private
  192.      */
  193.     function _extractExpectation({
  194.         if (count($this->_expectation_queue)) {
  195.             return array_shift($this->_expectation_queue);
  196.         }
  197.         return false;
  198.     }
  199.  
  200.     /**
  201.      *    @deprecated
  202.      */
  203.     function assertNoErrors($message{
  204.         return $this->_test->assert(
  205.                 new TrueExpectation(),
  206.                 count($this->_queue== 0,
  207.                 sprintf($message'Should be no errors'));
  208.     }
  209.  
  210.     /**
  211.      *    @deprecated
  212.      */
  213.     function assertError($expected$message{
  214.         if (count($this->_queue== 0{
  215.             $this->_test->fail(sprintf($message'Expected error not found'));
  216.             return false;
  217.         }
  218.         list($severity$content$file$line$this->extract();
  219.         $severity $this->getSeverityAsString($severity);
  220.         return $this->_test->assert(
  221.                 $expected,
  222.                 $content,
  223.                 sprintf($message"Expected PHP error [$content] severity [$severity] in [$file] line [$line]"));
  224.     }
  225.  
  226.     /**
  227.      *    Converts an error code into it's string
  228.      *    representation.
  229.      *    @param $severity  PHP integer error code.
  230.      *    @return           String version of error code.
  231.      *    @access public
  232.      *    @static
  233.      */
  234.     function getSeverityAsString($severity{
  235.         static $map array(
  236.                 E_STRICT => 'E_STRICT',
  237.                 E_ERROR => 'E_ERROR',
  238.                 E_WARNING => 'E_WARNING',
  239.                 E_PARSE => 'E_PARSE',
  240.                 E_NOTICE => 'E_NOTICE',
  241.                 E_CORE_ERROR => 'E_CORE_ERROR',
  242.                 E_CORE_WARNING => 'E_CORE_WARNING',
  243.                 E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  244.                 E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  245.                 E_USER_ERROR => 'E_USER_ERROR',
  246.                 E_USER_WARNING => 'E_USER_WARNING',
  247.                 E_USER_NOTICE => 'E_USER_NOTICE');
  248.         if (defined('E_RECOVERABLE_ERROR')) {
  249.             $map[E_RECOVERABLE_ERROR'E_RECOVERABLE_ERROR';
  250.         }
  251.         if (defined('E_DEPRECATED')) {
  252.             $map[E_DEPRECATED'E_DEPRECATED';
  253.         }
  254.         return $map[$severity];
  255.     }
  256. }
  257.  
  258. /**
  259.  *    Error handler that simply stashes any errors into the global
  260.  *    error queue. Simulates the existing behaviour with respect to
  261.  *    logging errors, but this feature may be removed in future.
  262.  *    @param $severity        PHP error code.
  263.  *    @param $message         Text of error.
  264.  *    @param $filename        File error occoured in.
  265.  *    @param $line            Line number of error.
  266.  *    @param $super_globals   Hash of PHP super global arrays.
  267.  *    @static
  268.  *    @access public
  269.  */
  270. function SimpleTestErrorHandler($severity$message$filename null$line null$super_globals null$mask null{
  271.     $severity $severity error_reporting();
  272.     if ($severity{
  273.         restore_error_handler();
  274.         if (ini_get('log_errors')) {
  275.             $label SimpleErrorQueue::getSeverityAsString($severity);
  276.             error_log("$label$message in $filename on line $line");
  277.         }
  278.         $context &SimpleTest::getContext();
  279.         $queue &$context->get('SimpleErrorQueue');
  280.         $queue->add($severity$message$filename$line);
  281.         set_error_handler('SimpleTestErrorHandler');
  282.     }
  283.     return true;
  284. }
  285. ?>

Documentation generated on Sun, 04 May 2008 09:21:32 -0500 by phpDocumentor 1.3.0