premier commit
[bazdig.git] / test / simpletest / errors.php
blob1d977c839f9e32ec4bc059cd23e7fbdf5095bdef
1 <?php
2 /**
3 * base include file for SimpleTest
4 * @package SimpleTest
5 * @subpackage UnitTester
6 * @version $Id: errors.php,v 1.20 2006/11/15 21:45:26 maugrim_t_r Exp $
7 */
9 /** @ignore - PHP5 compatibility fix. */
10 if (! defined('E_STRICT')) {
11 define('E_STRICT', 2048);
14 /**#@+
15 * Includes SimpleTest files.
17 require_once(dirname(__FILE__) . '/invoker.php');
18 require_once(dirname(__FILE__) . '/test_case.php');
19 require_once(dirname(__FILE__) . '/expectation.php');
21 /**
22 * Extension that traps errors into an error queue.
23 * @package SimpleTest
24 * @subpackage UnitTester
26 class SimpleErrorTrappingInvoker extends SimpleInvokerDecorator {
28 /**
29 * Stores the invoker to wrap.
30 * @param SimpleInvoker $invoker Test method runner.
32 function SimpleErrorTrappingInvoker(&$invoker) {
33 $this->SimpleInvokerDecorator($invoker);
36 /**
37 * Invokes a test method and dispatches any
38 * untrapped errors. Called back from
39 * the visiting runner.
40 * @param string $method Test method to call.
41 * @access public
43 function invoke($method) {
44 $context = &SimpleTest::getContext();
45 $queue = &$context->get('SimpleErrorQueue');
46 $queue->setTestCase($this->GetTestCase());
47 set_error_handler('SimpleTestErrorHandler');
48 parent::invoke($method);
49 while (list($severity, $message, $file, $line) = $queue->extract()) {
50 $severity = SimpleErrorQueue::getSeverityAsString($severity);
51 $test = &$this->getTestCase();
52 $test->error($severity, $message, $file, $line);
54 restore_error_handler();
58 /**
59 * Singleton error queue used to record trapped
60 * errors.
61 * @package SimpleTest
62 * @subpackage UnitTester
64 class SimpleErrorQueue {
65 var $_queue;
66 var $_expectation_queue;
67 var $_test;
69 /**
70 * Starts with an empty queue.
72 function SimpleErrorQueue() {
73 $this->clear();
76 /**
77 * Sets the currently running test case.
78 * @param SimpleTestCase $test Test case to send messages to.
79 * @access public
81 function setTestCase(&$test) {
82 $this->_test = &$test;
85 /**
86 * Adds an error to the front of the queue.
87 * @param integer $severity PHP error code.
88 * @param string $content Text of error.
89 * @param string $filename File error occoured in.
90 * @param integer $line Line number of error.
91 * @access public
93 function add($severity, $content, $filename, $line) {
94 $content = str_replace('%', '%%', $content);
95 if (count($this->_expectation_queue)) {
96 $this->_testLatestError($severity, $content, $filename, $line);
97 } else {
98 array_push(
99 $this->_queue,
100 array($severity, $content, $filename, $line));
105 * Tests the error against the most recent expected
106 * error.
107 * @param integer $severity PHP error code.
108 * @param string $content Text of error.
109 * @param string $filename File error occoured in.
110 * @param integer $line Line number of error.
111 * @access private
113 function _testLatestError($severity, $content, $filename, $line) {
114 list($expected, $message) = array_shift($this->_expectation_queue);
115 $severity = $this->getSeverityAsString($severity);
116 $is_match = $this->_test->assert(
117 $expected,
118 $content,
119 sprintf($message, "%s -> PHP error [$content] severity [$severity] in [$filename] line [$line]"));
120 if (! $is_match) {
121 $this->_test->error($severity, $content, $filename, $line);
126 * Pulls the earliest error from the queue.
127 * @return False if none, or a list of error
128 * information. Elements are: severity
129 * as the PHP error code, the error message,
130 * the file with the error, the line number
131 * and a list of PHP super global arrays.
132 * @access public
134 function extract() {
135 if (count($this->_queue)) {
136 return array_shift($this->_queue);
138 return false;
142 * Discards the contents of the error queue.
143 * @access public
145 function clear() {
146 $this->_queue = array();
147 $this->_expectation_queue = array();
151 * @deprecated
153 function assertNoErrors($message) {
154 return $this->_test->assert(
155 new TrueExpectation(),
156 count($this->_queue) == 0,
157 sprintf($message, 'Should be no errors'));
161 * @deprecated
163 function assertError($expected, $message) {
164 if (count($this->_queue) == 0) {
165 $this->_test->fail(sprintf($message, 'Expected error not found'));
166 return false;
168 list($severity, $content, $file, $line) = $this->extract();
169 $severity = $this->getSeverityAsString($severity);
170 return $this->_test->assert(
171 $expected,
172 $content,
173 sprintf($message, "Expected PHP error [$content] severity [$severity] in [$file] line [$line]"));
177 * Sets up an expectation of an error. If this is
178 * not fulfilled at the end of the test, a failure
179 * will occour. If the error does happen, then this
180 * will cancel it out and send a pass message.
181 * @param SimpleExpectation $expected Expected error match.
182 * @param string $message Message to display.
183 * @access public
185 function expectError($expected, $message) {
186 array_push(
187 $this->_expectation_queue,
188 array($expected, $message));
192 * Converts an error code into it's string
193 * representation.
194 * @param $severity PHP integer error code.
195 * @return String version of error code.
196 * @access public
197 * @static
199 function getSeverityAsString($severity) {
200 static $map = array(
201 E_STRICT => 'E_STRICT',
202 E_ERROR => 'E_ERROR',
203 E_WARNING => 'E_WARNING',
204 E_PARSE => 'E_PARSE',
205 E_NOTICE => 'E_NOTICE',
206 E_CORE_ERROR => 'E_CORE_ERROR',
207 E_CORE_WARNING => 'E_CORE_WARNING',
208 E_COMPILE_ERROR => 'E_COMPILE_ERROR',
209 E_COMPILE_WARNING => 'E_COMPILE_WARNING',
210 E_USER_ERROR => 'E_USER_ERROR',
211 E_USER_WARNING => 'E_USER_WARNING',
212 E_USER_NOTICE => 'E_USER_NOTICE');
213 return $map[$severity];
218 * Error handler that simply stashes any errors into the global
219 * error queue. Simulates the existing behaviour with respect to
220 * logging errors, but this feature may be removed in future.
221 * @param $severity PHP error code.
222 * @param $message Text of error.
223 * @param $filename File error occoured in.
224 * @param $line Line number of error.
225 * @param $super_globals Hash of PHP super global arrays.
226 * @static
227 * @access public
229 function SimpleTestErrorHandler($severity, $message, $filename, $line, $super_globals) {
230 if ($severity = $severity & error_reporting()) {
231 restore_error_handler();
232 if (ini_get('log_errors')) {
233 $label = SimpleErrorQueue::getSeverityAsString($severity);
234 error_log("$label: $message in $filename on line $line");
236 $context = &SimpleTest::getContext();
237 $queue = &$context->get('SimpleErrorQueue');
238 $queue->add($severity, $message, $filename, $line);
239 set_error_handler('SimpleTestErrorHandler');