summaryrefslogtreecommitdiff
path: root/tests/test_tools/simpletest/errors.php
blob: b32cd5fe11b4f03274500d08b9200939c1e4525d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
    /**
     *	base include file for SimpleTest
     *	@package	SimpleTest
     *	@subpackage	UnitTester
     *	@version	$Id: errors.php,v 1.14 2006/02/06 06:05:18 lastcraft Exp $
     */

    /** @ignore - PHP5 compatibility fix. */
    if (! defined('E_STRICT')) {
        define('E_STRICT', 2048);
    }

    /**#@+
     * Includes SimpleTest files.
     */
    require_once(dirname(__FILE__) . '/invoker.php');

    /**
     *    Extension that traps errors into an error queue.
	 *	  @package SimpleTest
	 *	  @subpackage UnitTester
     */
    class SimpleErrorTrappingInvoker extends SimpleInvokerDecorator {

        /**
         *    Stores the invoker to wrap.
         *    @param SimpleInvoker $invoker  Test method runner.
         */
        function SimpleErrorTrappingInvoker($invoker) {
            $this->SimpleInvokerDecorator($invoker);
        }

        /**
         *    Invokes a test method and dispatches any
         *    untrapped errors. Called back from
         *    the visiting runner.
         *    @param string $method    Test method to call.
         *    @access public
         */
        function invoke($method) {
            set_error_handler('simpleTestErrorHandler');
            parent::invoke($method);
            $queue = SimpleErrorQueue::instance();
            while (list($severity, $message, $file, $line, $globals) = $queue->extract()) {
                $severity = SimpleErrorQueue::getSeverityAsString($severity);
                $test_case = $this->getTestCase();
                $test_case->error($severity, $message, $file, $line);
            }
            restore_error_handler();
        }
    }

    /**
     *    Singleton error queue used to record trapped
     *    errors.
	 *	  @package	SimpleTest
	 *	  @subpackage	UnitTester
     */
    class SimpleErrorQueue {
        protected $_queue;

        /**
         *    Starts with an empty queue.
         *    @access public
         */
        function SimpleErrorQueue() {
            $this->clear();
        }

        /**
         *    Adds an error to the front of the queue.
         *    @param $severity        PHP error code.
         *    @param $message         Text of error.
         *    @param $filename        File error occoured in.
         *    @param $line            Line number of error.
         *    @param $super_globals   Hash of PHP super global arrays.
         *    @access public
         */
        function add($severity, $message, $filename, $line, $super_globals) {
            array_push(
                    $this->_queue,
                    array($severity, $message, $filename, $line, $super_globals));
        }

        /**
         *    Pulls the earliest error from the queue.
         *    @return     False if none, or a list of error
         *                information. Elements are: severity
         *                as the PHP error code, the error message,
         *                the file with the error, the line number
         *                and a list of PHP super global arrays.
         *    @access public
         */
        function extract() {
            if (count($this->_queue)) {
                return array_shift($this->_queue);
            }
            return false;
        }

        /**
         *    Discards the contents of the error queue.
         *    @access public
         */
        function clear() {
            $this->_queue = array();
        }

        /**
         *    Tests to see if the queue is empty.
         *    @return        True if empty.
         */
        function isEmpty() {
            return (count($this->_queue) == 0);
        }

        /**
         *    Global access to a single error queue.
         *    @return        Global error queue object.
         *    @access public
         *    @static
         */
        static function instance() {
            static $queue = false;
            if (! $queue) {
                $queue = new SimpleErrorQueue();
            }
            return $queue;
        }

        /**
         *    Converst an error code into it's string
         *    representation.
         *    @param $severity  PHP integer error code.
         *    @return           String version of error code.
         *    @access public
         *    @static
         */
         static function getSeverityAsString($severity) {
            static $map = array(
                    E_STRICT => 'E_STRICT',
                    E_ERROR => 'E_ERROR',
                    E_WARNING => 'E_WARNING',
                    E_PARSE => 'E_PARSE',
                    E_NOTICE => 'E_NOTICE',
                    E_CORE_ERROR => 'E_CORE_ERROR',
                    E_CORE_WARNING => 'E_CORE_WARNING',
                    E_COMPILE_ERROR => 'E_COMPILE_ERROR',
                    E_COMPILE_WARNING => 'E_COMPILE_WARNING',
                    E_USER_ERROR => 'E_USER_ERROR',
                    E_USER_WARNING => 'E_USER_WARNING',
                    E_USER_NOTICE => 'E_USER_NOTICE');
            return $map[$severity];
        }
    }

    /**
     *    Error handler that simply stashes any errors into the global
     *    error queue. Simulates the existing behaviour with respect to
     *    logging errors, but this feature may be removed in future.
     *    @param $severity        PHP error code.
     *    @param $message         Text of error.
     *    @param $filename        File error occoured in.
     *    @param $line            Line number of error.
     *    @param $super_globals   Hash of PHP super global arrays.
     *    @static
     *    @access public
     */
    function simpleTestErrorHandler($severity, $message, $filename, $line, $super_globals) {
        if ($severity = $severity & error_reporting()) {
            restore_error_handler();
            if (ini_get('log_errors')) {
                $label = SimpleErrorQueue::getSeverityAsString($severity);
                error_log("$label: $message in $filename on line $line");
            }
            $queue = SimpleErrorQueue::instance();
            $queue->add($severity, $message, $filename, $line, $super_globals);
            set_error_handler('simpleTestErrorHandler');
        }
    }
?>