forked from getsentry/sentry-php
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ErrorHandlerTest.php
203 lines (164 loc) · 6.6 KB
/
ErrorHandlerTest.php
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
<?php
/*
* This file is part of Raven.
*
* (c) Sentry Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Raven_Tests_ErrorHandlerTest extends PHPUnit_Framework_TestCase
{
private $errorLevel;
public function setUp()
{
$this->errorLevel = error_reporting();
$this->errorHandlerCalled = false;
$this->existingErrorHandler = set_error_handler(array($this, 'errorHandler'), -1);
// improves the reliability of tests
if (function_exists('error_clear_last')) {
error_clear_last();
}
}
public function errorHandler()
{
$this->errorHandlerCalled = true;
}
public function tearDown()
{
restore_exception_handler();
set_error_handler($this->existingErrorHandler);
// // XXX(dcramer): this isn't great as it doesnt restore the old error reporting level
// set_error_handler(array($this, 'errorHandler'), error_reporting());
error_reporting($this->errorLevel);
}
public function testErrorsAreLoggedAsExceptions()
{
$client = $this->getMockBuilder('Client')
->setMethods(array('captureException', 'sendUnsentErrors'))
->getMock();
$client->expects($this->once())
->method('captureException')
->with($this->isInstanceOf('ErrorException'));
$handler = new Raven_ErrorHandler($client, E_ALL);
$handler->handleError(E_WARNING, 'message');
}
public function testExceptionsAreLogged()
{
$client = $this->getMockBuilder('Client')
->setMethods(array('captureException'))
->getMock();
$client->expects($this->once())
->method('captureException')
->with($this->isInstanceOf('ErrorException'));
$e = new ErrorException('message', 0, E_WARNING, '', 0);
$handler = new Raven_ErrorHandler($client);
$handler->handleException($e);
}
public function testErrorHandlerPassErrorReportingPass()
{
$client = $this->getMockBuilder('Client')
->setMethods(array('captureException'))
->getMock();
$client->expects($this->once())
->method('captureException');
$handler = new Raven_ErrorHandler($client);
$handler->registerErrorHandler(false, -1);
error_reporting(E_USER_WARNING);
trigger_error('Warning', E_USER_WARNING);
}
public function testErrorHandlerPropagates()
{
$client = $this->getMockBuilder('Client')
->setMethods(array('captureException'))
->getMock();
$client->expects($this->never())
->method('captureException');
$handler = new Raven_ErrorHandler($client);
$handler->registerErrorHandler(true, E_DEPRECATED);
error_reporting(E_USER_WARNING);
trigger_error('Warning', E_USER_WARNING);
$this->assertEquals($this->errorHandlerCalled, 1);
}
public function testErrorHandlerRespectsErrorReportingDefault()
{
$client = $this->getMockBuilder('Client')
->setMethods(array('captureException'))
->getMock();
$client->expects($this->once())
->method('captureException');
error_reporting(E_DEPRECATED);
$handler = new Raven_ErrorHandler($client);
$handler->registerErrorHandler(true);
error_reporting(E_ALL);
trigger_error('Warning', E_USER_WARNING);
$this->assertEquals($this->errorHandlerCalled, 1);
}
// Because we cannot **know** that a user silenced an error, we always
// defer to respecting the error reporting settings.
public function testSilentErrorsAreNotReportedWithGlobal()
{
$client = $this->getMockBuilder('Client')
->setMethods(array('captureException'))
->getMock();
$client->expects($this->never())
->method('captureException');
error_reporting(E_ALL);
$handler = new Raven_ErrorHandler($client);
$handler->registerErrorHandler(true);
@$undefined;
// also ensure it doesnt get reported by the fatal handler
$handler->handleFatalError();
}
// Because we cannot **know** that a user silenced an error, we always
// defer to respecting the error reporting settings.
public function testSilentErrorsAreNotReportedWithLocal()
{
$client = $this->getMockBuilder('Client')
->setMethods(array('captureException'))
->getMock();
$client->expects($this->never())
->method('captureException');
$handler = new Raven_ErrorHandler($client);
$handler->registerErrorHandler(true, E_ALL);
@$my_array[2];
// also ensure it doesnt get reported by the fatal handler
$handler->handleFatalError();
}
public function testShouldCaptureFatalErrorBehavior()
{
$client = $this->getMockBuilder('Client')
->setMethods(array('captureException'))
->getMock();
$handler = new Raven_ErrorHandler($client);
$this->assertEquals($handler->shouldCaptureFatalError(E_ERROR), true);
$this->assertEquals($handler->shouldCaptureFatalError(E_WARNING), false);
}
public function testErrorHandlerDefaultsErrorReporting()
{
$client = $this->getMockBuilder('Client')
->setMethods(array('captureException'))
->getMock();
$client->expects($this->never())
->method('captureException');
error_reporting(E_USER_ERROR);
$handler = new Raven_ErrorHandler($client);
$handler->registerErrorHandler(false);
trigger_error('Warning', E_USER_WARNING);
}
public function testFluidInterface()
{
$client = $this->getMockBuilder('Client')
->setMethods(array('captureException'))
->getMock();
$handler = new Raven_ErrorHandler($client);
$result = $handler->registerErrorHandler();
$this->assertEquals($result, $handler);
$result = $handler->registerExceptionHandler();
$this->assertEquals($result, $handler);
// TODO(dcramer): cant find a great way to test resetting the shutdown
// handler
// $result = $handler->registerShutdownHandler();
// $this->assertEquals($result, $handler);
}
}