| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
* base include file for SimpleTest |
|---|
| 4 |
* @package SimpleTest |
|---|
| 5 |
* @subpackage UnitTester |
|---|
| 6 |
* @version $Id: errors.php,v 1.14 2006/02/06 06:05:18 lastcraft Exp $ |
|---|
| 7 |
*/ |
|---|
| 8 |
|
|---|
| 9 |
/** @ignore - PHP5 compatibility fix. */ |
|---|
| 10 |
if (! defined('E_STRICT')) { |
|---|
| 11 |
define('E_STRICT', 2048); |
|---|
| 12 |
} |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
* Includes SimpleTest files. |
|---|
| 16 |
*/ |
|---|
| 17 |
require_once(dirname(__FILE__) . '/invoker.php'); |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
* Extension that traps errors into an error queue. |
|---|
| 21 |
* @package SimpleTest |
|---|
| 22 |
* @subpackage UnitTester |
|---|
| 23 |
*/ |
|---|
| 24 |
class SimpleErrorTrappingInvoker extends SimpleInvokerDecorator { |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
* Stores the invoker to wrap. |
|---|
| 28 |
* @param SimpleInvoker $invoker Test method runner. |
|---|
| 29 |
*/ |
|---|
| 30 |
function SimpleErrorTrappingInvoker(&$invoker) { |
|---|
| 31 |
$this->SimpleInvokerDecorator($invoker); |
|---|
| 32 |
} |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 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. |
|---|
| 39 |
* @access public |
|---|
| 40 |
*/ |
|---|
| 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); |
|---|
| 49 |
} |
|---|
| 50 |
restore_error_handler(); |
|---|
| 51 |
} |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
* Singleton error queue used to record trapped |
|---|
| 56 |
* errors. |
|---|
| 57 |
* @package SimpleTest |
|---|
| 58 |
* @subpackage UnitTester |
|---|
| 59 |
*/ |
|---|
| 60 |
class SimpleErrorQueue { |
|---|
| 61 |
var $_queue; |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
* Starts with an empty queue. |
|---|
| 65 |
* @access public |
|---|
| 66 |
*/ |
|---|
| 67 |
function SimpleErrorQueue() { |
|---|
| 68 |
$this->clear(); |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 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. |
|---|
| 78 |
* @access public |
|---|
| 79 |
*/ |
|---|
| 80 |
function add($severity, $message, $filename, $line, $super_globals) { |
|---|
| 81 |
array_push( |
|---|
| 82 |
$this->_queue, |
|---|
| 83 |
array($severity, $message, $filename, $line, $super_globals)); |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 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. |
|---|
| 93 |
* @access public |
|---|
| 94 |
*/ |
|---|
| 95 |
function extract() { |
|---|
| 96 |
if (count($this->_queue)) { |
|---|
| 97 |
return array_shift($this->_queue); |
|---|
| 98 |
} |
|---|
| 99 |
return false; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
* Discards the contents of the error queue. |
|---|
| 104 |
* @access public |
|---|
| 105 |
*/ |
|---|
| 106 |
function clear() { |
|---|
| 107 |
$this->_queue = array(); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
* Tests to see if the queue is empty. |
|---|
| 112 |
* @return True if empty. |
|---|
| 113 |
*/ |
|---|
| 114 |
function isEmpty() { |
|---|
| 115 |
return (count($this->_queue) == 0); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
* Global access to a single error queue. |
|---|
| 120 |
* @return Global error queue object. |
|---|
| 121 |
* @access public |
|---|
| 122 |
* @static |
|---|
| 123 |
*/ |
|---|
| 124 |
function &instance() { |
|---|
| 125 |
static $queue = false; |
|---|
| 126 |
if (! $queue) { |
|---|
| 127 |
$queue = new SimpleErrorQueue(); |
|---|
| 128 |
} |
|---|
| 129 |
return $queue; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
* Converst an error code into it's string |
|---|
| 134 |
* representation. |
|---|
| 135 |
* @param $severity PHP integer error code. |
|---|
| 136 |
* @return String version of error code. |
|---|
| 137 |
* @access public |
|---|
| 138 |
* @static |
|---|
| 139 |
*/ |
|---|
| 140 |
function getSeverityAsString($severity) { |
|---|
| 141 |
static $map = array( |
|---|
| 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]; |
|---|
| 155 |
} |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 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. |
|---|
| 167 |
* @static |
|---|
| 168 |
* @access public |
|---|
| 169 |
*/ |
|---|
| 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"); |
|---|
| 176 |
} |
|---|
| 177 |
$queue = &SimpleErrorQueue::instance(); |
|---|
| 178 |
$queue->add($severity, $message, $filename, $line, $super_globals); |
|---|
| 179 |
set_error_handler('simpleTestErrorHandler'); |
|---|
| 180 |
} |
|---|
| 181 |
} |
|---|
| 182 |
?> |
|---|