forked from dotkernel/dot-errorhandler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogErrorHandlerTest.php
More file actions
377 lines (326 loc) · 12.9 KB
/
LogErrorHandlerTest.php
File metadata and controls
377 lines (326 loc) · 12.9 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
declare(strict_types=1);
namespace DotTest\ErrorHandler;
use Dot\ErrorHandler\Extra\ExtraProvider;
use Dot\ErrorHandler\Extra\Provider\CookieProvider;
use Dot\ErrorHandler\Extra\Provider\HeaderProvider;
use Dot\ErrorHandler\Extra\Provider\RequestProvider;
use Dot\ErrorHandler\Extra\Provider\ServerProvider;
use Dot\ErrorHandler\Extra\Provider\SessionProvider;
use Dot\ErrorHandler\Extra\Provider\TraceProvider;
use Dot\ErrorHandler\LogErrorHandler;
use Dot\ErrorHandler\LogErrorHandler as Subject;
use Dot\Log\Formatter\Json;
use Dot\Log\Logger;
use ErrorException;
use Laminas\Stratigility\Middleware\ErrorResponseGenerator;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use ReflectionObject;
use RuntimeException;
use Throwable;
use function error_reporting;
use function file_get_contents;
class LogErrorHandlerTest extends TestCase
{
private Subject $subject;
private MockObject&ServerRequestInterface $serverRequest;
private MockObject&ResponseInterface $response;
/** @var callable():ResponseInterface $responseFactory */
private $responseFactory;
private MockObject&StreamInterface $body;
private MockObject&RequestHandlerInterface $handler;
private Throwable $exception;
private ErrorResponseGenerator $errorResponseGenerator;
private vfsStreamDirectory $fileSystem;
/**
* @throws Exception
*/
public function setUp(): void
{
$this->response = $this->createMock(ResponseInterface::class);
$this->serverRequest = $this->createMock(ServerRequestInterface::class);
$this->body = $this->createMock(StreamInterface::class);
$this->handler = $this->createMock(RequestHandlerInterface::class);
$this->responseFactory = fn(): ResponseInterface => $this->response;
$this->errorResponseGenerator = new ErrorResponseGenerator();
$this->subject = new LogErrorHandler(
$this->responseFactory,
$this->errorResponseGenerator,
$this->createMock(LoggerInterface::class)
);
$this->exception = new RuntimeException('Not Implemented', 501);
$this->fileSystem = vfsStream::setup('root', 0644, ['log']);
}
public function testWillCreateWithDefaultParameters(): void
{
$this->assertContainsOnlyInstancesOf(Subject::class, [$this->subject]);
}
public function testCreateErrorHandlerReturnsCallable(): void
{
$this->assertIsCallable($this->subject->createErrorHandler());
}
public function testCreateErrorHandlerRaisesErrorException(): void
{
$callableErrorHandler = $this->subject->createErrorHandler();
$this->expectException(ErrorException::class);
$callableErrorHandler(error_reporting(), ErrorException::class, 'testErrorFile', 0);
}
public function testCreateErrorHandlerSkipsErrorsOutsideErrorReportingMask(): void
{
$callableErrorHandler = $this->subject->createErrorHandler();
$this->assertNull($callableErrorHandler(-(error_reporting() + 1), ErrorException::class, 'testErrorFile', 0));
}
/**
* @throws ContainerExceptionInterface
*/
public function testLogErrorHandlerWillInitiateWhenExtraProviderIsMissing(): void
{
$logErrorHandler = new LogErrorHandler(
$this->responseFactory,
$this->errorResponseGenerator,
new Logger($this->getConfig()),
);
$this->assertSame(LogErrorHandler::class, $logErrorHandler::class);
$this->assertNull($logErrorHandler->getExtraProvider());
}
/**
* @throws ContainerExceptionInterface
*/
public function testLogErrorHandlerWillInitiateWhenExtraProviderIsNull(): void
{
$logErrorHandler = new LogErrorHandler(
$this->responseFactory,
$this->errorResponseGenerator,
new Logger($this->getConfig()),
null
);
$this->assertSame(LogErrorHandler::class, $logErrorHandler::class);
$this->assertNull($logErrorHandler->getExtraProvider());
}
/**
* @throws ContainerExceptionInterface
*/
public function testLogErrorHandlerWillInitiateWhenExtraProviderIsProvided(): void
{
$logErrorHandler = new LogErrorHandler(
$this->responseFactory,
$this->errorResponseGenerator,
new Logger($this->getConfig()),
new ExtraProvider()
);
$this->assertSame(LogErrorHandler::class, $logErrorHandler::class);
$this->assertInstanceOf(ExtraProvider::class, $logErrorHandler->getExtraProvider());
}
/**
* @throws ContainerExceptionInterface
*/
public function testLogErrorHandlerLogsWillOnlyContainDefaultKeysWhenNoProvidersAreEnabled(): void
{
$logErrorHandler = new LogErrorHandler(
$this->responseFactory,
$this->errorResponseGenerator,
new Logger($this->getConfig()),
new ExtraProvider()
);
$log = $logErrorHandler->provideExtra(new \Exception('test'), $this->serverRequest);
$this->assertCount(2, $log);
$this->assertArrayHasKey('file', $log);
$this->assertArrayHasKey('line', $log);
}
/**
* @throws ContainerExceptionInterface
*/
public function testLogErrorHandlerLogsWillContainExtraKeysWhenProvidersAreEnabled(): void
{
$logErrorHandler = new LogErrorHandler(
$this->responseFactory,
$this->errorResponseGenerator,
new Logger($this->getConfig()),
new ExtraProvider([
CookieProvider::class => ['enabled' => true],
HeaderProvider::class => ['enabled' => true],
RequestProvider::class => ['enabled' => true],
ServerProvider::class => ['enabled' => true],
SessionProvider::class => ['enabled' => true],
TraceProvider::class => ['enabled' => true],
])
);
$this->serverRequest->method('getCookieParams')->willReturn([]);
$this->serverRequest->method('getHeaders')->willReturn([]);
$this->serverRequest->method('getParsedBody')->willReturn([]);
$this->serverRequest->method('getServerParams')->willReturn([]);
$extra = $logErrorHandler->provideExtra(new \Exception('test'), $this->serverRequest);
$this->assertCount(8, $extra);
$this->assertArrayHasKey('file', $extra);
$this->assertArrayHasKey('line', $extra);
$this->assertArrayHasKey('cookie', $extra);
$this->assertArrayHasKey('header', $extra);
$this->assertArrayHasKey('request', $extra);
$this->assertArrayHasKey('server', $extra);
$this->assertArrayHasKey('session', $extra);
$this->assertArrayHasKey('trace', $extra);
}
public function testAttachListenerDoesNotAttachDuplicates(): void
{
$listener = static function (): void {
};
$this->subject->attachListener($listener);
$this->subject->attachListener($listener);
$ref = new ReflectionObject($this->subject);
$listeners = $ref->getProperty('listeners');
$this->assertContains($listener, $listeners->getValue($this->subject));
$this->assertCount(1, $listeners->getValue($this->subject));
}
public function testTriggerListenersWithTwoListeners(): void
{
$listener1 = function (
Throwable $error,
ServerRequestInterface $request,
ResponseInterface $response
): void {
$this->assertSame($this->exception, $error);
$this->assertSame($this->serverRequest, $request);
$this->assertSame($this->response, $response);
};
$listener2 = clone $listener1;
$this->subject->attachListener($listener1);
$this->subject->attachListener($listener2);
$this->subject->triggerListeners($this->exception, $this->serverRequest, $this->response);
}
public function testHandleThrowable(): void
{
$responseGenerator = new ErrorResponseGenerator();
$this->body
->expects(self::once())
->method('write')
->with('Not Implemented')
->willReturn(0);
$this->response
->method('getStatusCode')
->willReturn(501);
$this->response
->method('withStatus')
->with(501)
->willReturnSelf();
$this->response
->method('getBody')
->willReturn($this->body);
$this->response
->method('getReasonPhrase')
->willReturn('Not Implemented');
$response = $responseGenerator($this->exception, $this->serverRequest, ($this->responseFactory)());
$this->assertContainsOnlyInstancesOf(ResponseInterface::class, [$response]);
}
public function testErrorHandlingTriggersListeners(): void
{
$this->handler
->method('handle')
->with($this->serverRequest)
->willThrowException($this->exception);
$this->body
->expects(self::once())
->method('write')
->with('Not Implemented')
->willReturn(0);
$this->response
->method('getStatusCode')
->willReturn(501);
$this->response
->method('withStatus')
->with(501)
->willReturnSelf();
$this->response
->method('getBody')
->willReturn($this->body);
$this->response
->method('getReasonPhrase')
->willReturn('Not Implemented');
$listener = function (
Throwable $error,
ServerRequestInterface $request,
ResponseInterface $response
): void {
$this->assertSame($this->exception, $error, 'Listener did not receive same exception as was raised');
$this->assertSame($this->serverRequest, $request, 'Listener did not receive same request');
$this->assertSame($this->response, $response, 'Listener did not receive same response');
};
$listener2 = clone $listener;
$this->subject->attachListener($listener);
$this->subject->attachListener($listener2);
$result = $this->subject->process($this->serverRequest, $this->handler);
$this->assertSame($this->response, $result);
}
/**
* @throws ContainerExceptionInterface
*/
public function testHandleThrowableLogsError(): void
{
$config = $this->getConfig();
$logErrorHandler = new LogErrorHandler(
$this->responseFactory,
$this->errorResponseGenerator,
new Logger($config)
);
$this->handler
->method('handle')
->with($this->serverRequest)
->willThrowException($this->exception);
$this->body
->expects(self::once())
->method('write')
->with('Not Implemented')
->willReturn(0);
$this->response
->method('getStatusCode')
->willReturn(501);
$this->response
->method('withStatus')
->with(501)
->willReturnSelf();
$this->response
->method('getBody')
->willReturn($this->body);
$this->response
->method('getReasonPhrase')
->willReturn('Not Implemented');
$logErrorHandler->process($this->serverRequest, $this->handler);
$this->assertTrue($this->fileSystem->hasChild('test-error-log.log'));
$this->assertNotEmpty(file_get_contents($this->fileSystem->url() . '/test-error-log.log'));
}
private function getConfig(): array
{
return [
'writers' => [
'FileWriter' => [
'name' => 'stream',
'level' => Logger::ALERT,
'options' => [
'stream' => $this->fileSystem->url() . '/test-error-log.log',
'filters' => [
'allMessages' => [
'name' => 'level',
'options' => [
'operator' => '>=',
'level' => Logger::EMERG,
],
],
],
'formatter' => [
'name' => Json::class,
],
],
],
],
];
}
}