3 * base include file for SimpleTest
5 * @subpackage UnitTester
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');
18 require_once(dirname(__FILE__
) . '/test_case.php');
19 require_once(dirname(__FILE__
) . '/expectation.php');
22 * Extension that traps errors into an error queue.
24 * @subpackage UnitTester
26 class SimpleErrorTrappingInvoker
extends SimpleInvokerDecorator
{
29 * Stores the invoker to wrap.
30 * @param SimpleInvoker $invoker Test method runner.
32 function SimpleErrorTrappingInvoker(&$invoker) {
33 $this->SimpleInvokerDecorator($invoker);
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.
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();
59 * Singleton error queue used to record trapped
62 * @subpackage UnitTester
64 class SimpleErrorQueue
{
66 var $_expectation_queue;
70 * Starts with an empty queue.
72 function SimpleErrorQueue() {
77 * Sets the currently running test case.
78 * @param SimpleTestCase $test Test case to send messages to.
81 function setTestCase(&$test) {
82 $this->_test
= &$test;
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.
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);
100 array($severity, $content, $filename, $line));
105 * Tests the error against the most recent expected
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.
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(
119 sprintf($message, "%s -> PHP error [$content] severity [$severity] in [$filename] line [$line]"));
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.
135 if (count($this->_queue
)) {
136 return array_shift($this->_queue
);
142 * Discards the contents of the error queue.
146 $this->_queue
= array();
147 $this->_expectation_queue
= array();
153 function assertNoErrors($message) {
154 return $this->_test
->assert(
155 new TrueExpectation(),
156 count($this->_queue
) == 0,
157 sprintf($message, 'Should be no errors'));
163 function assertError($expected, $message) {
164 if (count($this->_queue
) == 0) {
165 $this->_test
->fail(sprintf($message, 'Expected error not found'));
168 list($severity, $content, $file, $line) = $this->extract();
169 $severity = $this->getSeverityAsString($severity);
170 return $this->_test
->assert(
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.
185 function expectError($expected, $message) {
187 $this->_expectation_queue
,
188 array($expected, $message));
192 * Converts an error code into it's string
194 * @param $severity PHP integer error code.
195 * @return String version of error code.
199 function getSeverityAsString($severity) {
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.
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');