3 * base include file for SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: errors.php,v 1.14 2006/02/06 06:05:18 lastcraft Exp $
9 /** @ignore - PHP5 compatibility fix. */
10 if (! defined('E_STRICT')) {
11 define('E_STRICT', 2048);
15 * Includes SimpleTest files.
17 require_once(dirname(__FILE__
) . '/invoker.php');
20 * Extension that traps errors into an error queue.
22 * @subpackage UnitTester
24 class SimpleErrorTrappingInvoker
extends SimpleInvokerDecorator
{
27 * Stores the invoker to wrap.
28 * @param SimpleInvoker $invoker Test method runner.
30 function SimpleErrorTrappingInvoker(&$invoker) {
31 $this->SimpleInvokerDecorator($invoker);
35 * Invokes a test method and dispatches any
36 * untrapped errors. Called back from
37 * the visiting runner.
38 * @param string $method Test method to call.
41 function invoke($method) {
42 set_error_handler('simpleTestErrorHandler');
43 parent
::invoke($method);
44 $queue = &SimpleErrorQueue
::instance();
45 while (list($severity, $message, $file, $line, $globals) = $queue->extract()) {
46 $severity = SimpleErrorQueue
::getSeverityAsString($severity);
47 $test_case = &$this->getTestCase();
48 $test_case->error($severity, $message, $file, $line);
50 restore_error_handler();
55 * Singleton error queue used to record trapped
58 * @subpackage UnitTester
60 class SimpleErrorQueue
{
64 * Starts with an empty queue.
67 function SimpleErrorQueue() {
72 * Adds an error to the front of the queue.
73 * @param $severity PHP error code.
74 * @param $message Text of error.
75 * @param $filename File error occoured in.
76 * @param $line Line number of error.
77 * @param $super_globals Hash of PHP super global arrays.
80 function add($severity, $message, $filename, $line, $super_globals) {
83 array($severity, $message, $filename, $line, $super_globals));
87 * Pulls the earliest error from the queue.
88 * @return False if none, or a list of error
89 * information. Elements are: severity
90 * as the PHP error code, the error message,
91 * the file with the error, the line number
92 * and a list of PHP super global arrays.
96 if (count($this->_queue
)) {
97 return array_shift($this->_queue
);
103 * Discards the contents of the error queue.
107 $this->_queue
= array();
111 * Tests to see if the queue is empty.
112 * @return True if empty.
115 return (count($this->_queue
) == 0);
119 * Global access to a single error queue.
120 * @return Global error queue object.
124 function &instance() {
125 static $queue = false;
127 $queue = new SimpleErrorQueue();
133 * Converst an error code into it's string
135 * @param $severity PHP integer error code.
136 * @return String version of error code.
140 function getSeverityAsString($severity) {
142 E_STRICT
=> 'E_STRICT',
143 E_ERROR
=> 'E_ERROR',
144 E_WARNING
=> 'E_WARNING',
145 E_PARSE
=> 'E_PARSE',
146 E_NOTICE
=> 'E_NOTICE',
147 E_CORE_ERROR
=> 'E_CORE_ERROR',
148 E_CORE_WARNING
=> 'E_CORE_WARNING',
149 E_COMPILE_ERROR
=> 'E_COMPILE_ERROR',
150 E_COMPILE_WARNING
=> 'E_COMPILE_WARNING',
151 E_USER_ERROR
=> 'E_USER_ERROR',
152 E_USER_WARNING
=> 'E_USER_WARNING',
153 E_USER_NOTICE
=> 'E_USER_NOTICE');
154 return $map[$severity];
159 * Error handler that simply stashes any errors into the global
160 * error queue. Simulates the existing behaviour with respect to
161 * logging errors, but this feature may be removed in future.
162 * @param $severity PHP error code.
163 * @param $message Text of error.
164 * @param $filename File error occoured in.
165 * @param $line Line number of error.
166 * @param $super_globals Hash of PHP super global arrays.
170 function simpleTestErrorHandler($severity, $message, $filename, $line, $super_globals) {
171 if ($severity = $severity & error_reporting()) {
172 restore_error_handler();
173 if (ini_get('log_errors')) {
174 $label = SimpleErrorQueue
::getSeverityAsString($severity);
175 error_log("$label: $message in $filename on line $line");
177 $queue = &SimpleErrorQueue
::instance();
178 $queue->add($severity, $message, $filename, $line, $super_globals);
179 set_error_handler('simpleTestErrorHandler');