File: errors_test.php

package info (click to toggle)
postfixadmin 2.3.5-2%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,200 kB
  • sloc: php: 25,767; xml: 14,485; perl: 964; sh: 664; python: 169; makefile: 84
file content (235 lines) | stat: -rw-r--r-- 8,170 bytes parent folder | download | duplicates (2)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
    require_once(dirname(__FILE__) . '/../errors.php');
    require_once(dirname(__FILE__) . '/../expectation.php');
    require_once(dirname(__FILE__) . '/../test_case.php');
    Mock::generate('SimpleTestCase');
    Mock::generate('SimpleExpectation');

    class TestOfErrorQueue extends UnitTestCase {

        function setUp() {
            $context = &SimpleTest::getContext();
            $queue = &$context->get('SimpleErrorQueue');
            $queue->clear();
        }

        function tearDown() {
            $context = &SimpleTest::getContext();
            $queue = &$context->get('SimpleErrorQueue');
            $queue->clear();
        }

        function testOrder() {
            $context = &SimpleTest::getContext();
            $queue = &$context->get('SimpleErrorQueue');
            $queue->add(1024, 'Ouch', 'here.php', 100);
            $queue->add(512, 'Yuk', 'there.php', 101);
            $this->assertEqual(
                    $queue->extract(),
                    array(1024, 'Ouch', 'here.php', 100));
            $this->assertEqual(
                    $queue->extract(),
                    array(512, 'Yuk', 'there.php', 101));
            $this->assertFalse($queue->extract());
        }

        function testAssertNoErrorsGivesTrueWhenNoErrors() {
            $test = &new MockSimpleTestCase();
            $test->expectOnce('assert', array(new TrueExpectation(), true, 'Should be no errors'));
            $test->setReturnValue('assert', true);
            $queue = &new SimpleErrorQueue();
            $queue->setTestCase($test);
            $this->assertTrue($queue->assertNoErrors('%s'));
        }

        function testAssertNoErrorsIssuesFailWhenErrors() {
            $test = &new MockSimpleTestCase();
            $test->expectOnce('assert', array(new TrueExpectation(), false, 'Should be no errors'));
            $test->setReturnValue('assert', false);
            $queue = &new SimpleErrorQueue();
            $queue->setTestCase($test);
            $queue->add(1024, 'Ouch', 'here.php', 100);
            $this->assertFalse($queue->assertNoErrors('%s'));
        }

        function testAssertErrorFailsWhenNoError() {
            $test = &new MockSimpleTestCase();
            $test->expectOnce('fail', array('Expected error not found'));
            $test->setReturnValue('assert', false);
            $queue = &new SimpleErrorQueue();
            $queue->setTestCase($test);
            $this->assertFalse($queue->assertError(false, '%s'));
        }

        function testAssertErrorFailsWhenErrorDoesntMatch() {
            $test = &new MockSimpleTestCase();
            $test->expectOnce('assert', array(
                    new MockSimpleExpectation(),
                    'B',
                    'Expected PHP error [B] severity [E_USER_NOTICE] in [b.php] line [100]'));
            $test->setReturnValue('assert', false);
            $queue = &new SimpleErrorQueue();
            $queue->setTestCase($test);
            $queue->add(1024, 'B', 'b.php', 100);
            $this->assertFalse($queue->assertError(new MockSimpleExpectation(), '%s'));
        }

        function testExpectationMatchCancelsIncomingError() {
            $test = &new MockSimpleTestCase();
            $test->expectOnce('assert', array(new MockSimpleExpectation(), 'B', 'a message'));
            $test->setReturnValue('assert', true);
            $test->expectNever('error');
            $queue = &new SimpleErrorQueue();
            $queue->setTestCase($test);
            $queue->expectError(new MockSimpleExpectation(), 'a message');
            $queue->add(1024, 'B', 'b.php', 100);
        }

        function testExpectationMissTriggersError() {
            $test = &new MockSimpleTestCase();
            $test->expectOnce('assert', array(new MockSimpleExpectation(), 'B', 'a message'));
            $test->setReturnValue('assert', false);
            $test->expectOnce('error');
            $queue = &new SimpleErrorQueue();
            $queue->setTestCase($test);
            $queue->expectError(new MockSimpleExpectation(), 'a message');
            $queue->add(1024, 'B', 'b.php', 100);
        }
    }

    class TestOfErrorTrap extends UnitTestCase {
        var $_old;

        function setUp() {
            $this->_old = error_reporting(E_ALL);
            set_error_handler('SimpleTestErrorHandler');
        }

        function tearDown() {
            restore_error_handler();
            error_reporting($this->_old);
        }

        function testQueueStartsEmpty() {
            $context = &SimpleTest::getContext();
            $queue = &$context->get('SimpleErrorQueue');
            $this->assertFalse($queue->extract());
        }

        function testTrappedErrorPlacedInQueue() {
            trigger_error('Ouch!');
            $context = &SimpleTest::getContext();
            $queue = &$context->get('SimpleErrorQueue');
            list($severity, $message, $file, $line) = $queue->extract();
            $this->assertEqual($message, 'Ouch!');
            $this->assertEqual($file, __FILE__);
            $this->assertFalse($queue->extract());
        }

        function testErrorsAreSwallowedByMatchingExpectation() {
            $this->expectError('Ouch!');
            trigger_error('Ouch!');
        }

        function testErrorsAreSwallowedInOrder() {
            $this->expectError('a');
            $this->expectError('b');
            trigger_error('a');
            trigger_error('b');
        }

        function testAnyErrorCanBeSwallowed() {
            $this->expectError();
            trigger_error('Ouch!');
        }

        function testErrorCanBeSwallowedByPatternMatching() {
            $this->expectError(new PatternExpectation('/ouch/i'));
            trigger_error('Ouch!');
        }
		
		function testErrorWithPercentsPassesWithNoSprintfError() {
			$this->expectError("%");
			trigger_error('%');
		}
    }

    class TestOfErrors extends UnitTestCase {
        var $_old;

        function setUp() {
            $this->_old = error_reporting(E_ALL);
        }

        function tearDown() {
            error_reporting($this->_old);
        }

        function testDefaultWhenAllReported() {
            error_reporting(E_ALL);
            trigger_error('Ouch!');
            $this->assertError('Ouch!');
        }

        function testNoticeWhenReported() {
            error_reporting(E_ALL);
            trigger_error('Ouch!', E_USER_NOTICE);
            $this->assertError('Ouch!');
        }

        function testWarningWhenReported() {
            error_reporting(E_ALL);
            trigger_error('Ouch!', E_USER_WARNING);
            $this->assertError('Ouch!');
        }

        function testErrorWhenReported() {
            error_reporting(E_ALL);
            trigger_error('Ouch!', E_USER_ERROR);
            $this->assertError('Ouch!');
        }

        function testNoNoticeWhenNotReported() {
            error_reporting(0);
            trigger_error('Ouch!', E_USER_NOTICE);
        }

        function testNoWarningWhenNotReported() {
            error_reporting(0);
            trigger_error('Ouch!', E_USER_WARNING);
        }

        function testNoticeSuppressedWhenReported() {
            error_reporting(E_ALL);
            @trigger_error('Ouch!', E_USER_NOTICE);
        }

        function testWarningSuppressedWhenReported() {
            error_reporting(E_ALL);
            @trigger_error('Ouch!', E_USER_WARNING);
        }

		function testErrorWithPercentsReportedWithNoSprintfError() {
			trigger_error('%');
			$this->assertError('%');
		}
    }
    
    class TestOfErrorsExcludingPHP52AndAbove extends UnitTestCase {
        function skip() {
            $this->skipIf(version_compare(phpversion(), '5.2', '>='), 'E_USER_ERROR not tested for PHP 5.2 and above');
        }

        function testNoErrorWhenNotReported() {
            error_reporting(0);
            trigger_error('Ouch!', E_USER_ERROR);
        }

        function testErrorSuppressedWhenReported() {
            error_reporting(E_ALL);
            @trigger_error('Ouch!', E_USER_ERROR);
        }
    }
    
// TODO: Add stacked error handler test
?>