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 1784 2008-04-26 13:07:14Z pp11 $
  7.  */
  8.  
  9. /**#@+
  10.  * Includes SimpleTest files.
  11.  */
  12. require_once dirname(__FILE__'/invoker.php';
  13. require_once dirname(__FILE__'/test_case.php';
  14. require_once dirname(__FILE__'/expectation.php';
  15. /**#@-*/
  16.  
  17. /**
  18.  *    Extension that traps errors into an error queue.
  19.  *    @package SimpleTest
  20.  *    @subpackage UnitTester
  21.  */
  22.  
  23.     /**
  24.      *    Stores the invoker to wrap.
  25.      *    @param SimpleInvoker $invoker  Test method runner.
  26.      */
  27.     function __construct($invoker{
  28.         parent::__construct($invoker);
  29.     }
  30.  
  31.     /**
  32.      *    Invokes a test method and dispatches any
  33.      *    untrapped errors. Called back from
  34.      *    the visiting runner.
  35.      *    @param string $method    Test method to call.
  36.      *    @access public
  37.      */
  38.     function invoke($method{
  39.         $queue $this->createErrorQueue();
  40.         set_error_handler('SimpleTestErrorHandler');
  41.         parent::invoke($method);
  42.         restore_error_handler();
  43.         $queue->tally();
  44.     }
  45.     
  46.     /**
  47.      *    Wires up the error queue for a single test.
  48.      *    @return SimpleErrorQueue    Queue connected to the test.
  49.      *    @access private
  50.      */
  51.     protected function createErrorQueue({
  52.         $context SimpleTest::getContext();
  53.         $test $this->getTestCase();
  54.         $queue $context->get('SimpleErrorQueue');
  55.         $queue->setTestCase($test);
  56.         return $queue;
  57.     }
  58. }
  59.  
  60. /**
  61.  *    Error queue used to record trapped
  62.  *    errors.
  63.  *    @package  SimpleTest
  64.  *    @subpackage   UnitTester
  65.  */
  66.     private $queue;
  67.     private $expectation_queue;
  68.     private $test;
  69.     private $using_expect_style false;
  70.  
  71.     /**
  72.      *    Starts with an empty queue.
  73.      */
  74.     function __construct({
  75.         $this->clear();
  76.     }
  77.  
  78.     /**
  79.      *    Discards the contents of the error queue.
  80.      *    @access public
  81.      */
  82.     function clear({
  83.         $this->queue array();
  84.         $this->expectation_queue array();
  85.     }
  86.  
  87.     /**
  88.      *    Sets the currently running test case.
  89.      *    @param SimpleTestCase $test    Test case to send messages to.
  90.      *    @access public
  91.      */
  92.     function setTestCase($test{
  93.         $this->test $test;
  94.     }
  95.  
  96.     /**
  97.      *    Sets up an expectation of an error. If this is
  98.      *    not fulfilled at the end of the test, a failure
  99.      *    will occour. If the error does happen, then this
  100.      *    will cancel it out and send a pass message.
  101.      *    @param SimpleExpectation $expected    Expected error match.
  102.      *    @param string $message                Message to display.
  103.      *    @access public
  104.      */
  105.     function expectError($expected$message{
  106.         array_push($this->expectation_queuearray($expected$message));
  107.     }
  108.  
  109.     /**
  110.      *    Adds an error to the front of the queue.
  111.      *    @param integer $severity       PHP error code.
  112.      *    @param string $content         Text of error.
  113.      *    @param string $filename        File error occoured in.
  114.      *    @param integer $line           Line number of error.
  115.      *    @access public
  116.      */
  117.     function add($severity$content$filename$line{
  118.         $content str_replace('%''%%'$content);
  119.         $this->testLatestError($severity$content$filename$line);
  120.     }
  121.     
  122.     /**
  123.      *    Any errors still in the queue are sent to the test
  124.      *    case. Any unfulfilled expectations trigger failures.
  125.      *    @access public
  126.      */
  127.     function tally({
  128.         while (list($severity$message$file$line$this->extract()) {
  129.             $severity $this->getSeverityAsString($severity);
  130.             $this->test->error($severity$message$file$line);
  131.         }
  132.         while (list($expected$message$this->extractExpectation()) {
  133.             $this->test->assert($expectedfalse"%s -> Expected error not caught");
  134.         }
  135.     }
  136.  
  137.     /**
  138.      *    Tests the error against the most recent expected
  139.      *    error.
  140.      *    @param integer $severity       PHP error code.
  141.      *    @param string $content         Text of error.
  142.      *    @param string $filename        File error occoured in.
  143.      *    @param integer $line           Line number of error.
  144.      *    @access private
  145.      */
  146.     protected function testLatestError($severity$content$filename$line{
  147.         if ($expectation $this->extractExpectation()) {
  148.             list($expected$message$expectation;
  149.             $this->test->assert($expected$contentsprintf(
  150.                     $message,
  151.                     "%s -> PHP error [$content] severity [.
  152.                             $this->getSeverityAsString($severity.
  153.                             "] in [$filename] line [$line]"));
  154.         else {
  155.             $this->test->error($severity$content$filename$line);
  156.         }
  157.     }
  158.  
  159.     /**
  160.      *    Pulls the earliest error from the queue.
  161.      *    @return  mixed    False if none, or a list of error
  162.      *                       information. Elements are: severity
  163.      *                       as the PHP error code, the error message,
  164.      *                       the file with the error, the line number
  165.      *                       and a list of PHP super global arrays.
  166.      *    @access public
  167.      */
  168.     function extract({
  169.         if (count($this->queue)) {
  170.             return array_shift($this->queue);
  171.         }
  172.         return false;
  173.     }
  174.  
  175.     /**
  176.      *    Pulls the earliest expectation from the queue.
  177.      *    @return     SimpleExpectation    False if none.
  178.      *    @access private
  179.      */
  180.     protected function extractExpectation({
  181.         if (count($this->expectation_queue)) {
  182.             return array_shift($this->expectation_queue);
  183.         }
  184.         return false;
  185.     }
  186.  
  187.     /**
  188.      *    Converts an error code into it's string
  189.      *    representation.
  190.      *    @param $severity  PHP integer error code.
  191.      *    @return           String version of error code.
  192.      *    @access public
  193.      */
  194.     static function getSeverityAsString($severity{
  195.         static $map array(
  196.                 E_STRICT => 'E_STRICT',
  197.                 E_ERROR => 'E_ERROR',
  198.                 E_WARNING => 'E_WARNING',
  199.                 E_PARSE => 'E_PARSE',
  200.                 E_NOTICE => 'E_NOTICE',
  201.                 E_CORE_ERROR => 'E_CORE_ERROR',
  202.                 E_CORE_WARNING => 'E_CORE_WARNING',
  203.                 E_COMPILE_ERROR => 'E_COMPILE_ERROR',
  204.                 E_COMPILE_WARNING => 'E_COMPILE_WARNING',
  205.                 E_USER_ERROR => 'E_USER_ERROR',
  206.                 E_USER_WARNING => 'E_USER_WARNING',
  207.                 E_USER_NOTICE => 'E_USER_NOTICE');
  208.         if (defined('E_RECOVERABLE_ERROR')) {
  209.             $map[E_RECOVERABLE_ERROR'E_RECOVERABLE_ERROR';
  210.         }
  211.         if (defined('E_DEPRECATED')) {
  212.             $map[E_DEPRECATED'E_DEPRECATED';
  213.         }
  214.         return $map[$severity];
  215.     }
  216. }
  217.  
  218. /**
  219.  *    Error handler that simply stashes any errors into the global
  220.  *    error queue. Simulates the existing behaviour with respect to
  221.  *    logging errors, but this feature may be removed in future.
  222.  *    @param $severity        PHP error code.
  223.  *    @param $message         Text of error.
  224.  *    @param $filename        File error occoured in.
  225.  *    @param $line            Line number of error.
  226.  *    @param $super_globals   Hash of PHP super global arrays.
  227.  *    @access public
  228.  */
  229. function SimpleTestErrorHandler($severity$message$filename null$line null$super_globals null$mask null{
  230.     $severity $severity error_reporting();
  231.     if ($severity{
  232.         restore_error_handler();
  233.         if (IsNotCausedBySimpleTest($message)) {
  234.             if (ini_get('log_errors')) {
  235.                 $label SimpleErrorQueue::getSeverityAsString($severity);
  236.                 error_log("$label$message in $filename on line $line");
  237.             }
  238.             $queue SimpleTest::getContext()->get('SimpleErrorQueue');
  239.             $queue->add($severity$message$filename$line);
  240.         }
  241.         set_error_handler('SimpleTestErrorHandler');
  242.     }
  243.     return true;
  244. }
  245.  
  246. /**
  247.  *  Certain messages can be caused by the unit tester itself.
  248.  *  These have to be filtered.
  249.  *  @param string $message      Message to filter.
  250.  *  @return boolean             True if genuine failure.
  251.  */
  252. function IsNotCausedBySimpleTest($message{
  253.     return preg_match('/returned by reference/'$message);
  254. }
  255. ?>

Documentation generated on Thu, 01 Oct 2009 20:54:44 -0500 by phpDocumentor 1.4.2