1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VM\RequestLogger\Tests\Middlewares; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use TypeError; |
10
|
|
|
use VM\Psr15Mocks\Middleware; |
11
|
|
|
use VM\RequestLogger\Interfaces\LogMessageFormatter; |
12
|
|
|
use VM\RequestLogger\Middlewares\RequestLogger; |
13
|
|
|
|
14
|
|
|
class RequestLoggerTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
use Middleware; |
17
|
|
|
|
18
|
|
|
public function setUp(): void |
19
|
|
|
{ |
20
|
|
|
$this->buildRequest(); |
21
|
|
|
$this->buildResponse(); |
22
|
|
|
$this->buildRequestHandler(); |
23
|
|
|
$this->buildFormatter(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function tearDown(): void |
27
|
|
|
{ |
28
|
|
|
$this->destroyFormatter(); |
29
|
|
|
$this->destroyRequestHandler(); |
30
|
|
|
$this->destroyResponse(); |
31
|
|
|
$this->destroyRequest(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testProcessShouldLogANormalResponse(): void |
35
|
|
|
{ |
36
|
|
|
$this->formatter->expects($this->once())->method('logRequest')->with($this->request); |
37
|
|
|
$this->formatter->expects($this->once())->method('logResponse')->with($this->response); |
38
|
|
|
$this->formatter->expects($this->never())->method('logError'); |
39
|
|
|
$this->formatter->expects($this->never())->method('logException'); |
40
|
|
|
$this->requestHandler->expects($this->once())->method('handle')->with($this->request)->willReturn($this->response); |
41
|
|
|
|
42
|
|
|
$middleware = new RequestLogger($this->formatter); |
43
|
|
|
$middleware->process($this->request, $this->requestHandler); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testProcessShouldLogErrorsAndRethrow(): void |
47
|
|
|
{ |
48
|
|
|
$throwable = new TypeError('wheee!'); |
49
|
|
|
$this->formatter->expects($this->once())->method('logRequest')->with($this->request); |
50
|
|
|
$this->formatter->expects($this->once())->method('logError')->with($throwable); |
51
|
|
|
$this->formatter->expects($this->never())->method('logResponse'); |
52
|
|
|
$this->formatter->expects($this->never())->method('logException'); |
53
|
|
|
$this->requestHandler->expects($this->once())->method('handle')->with($this->request)->willThrowException($throwable); |
54
|
|
|
|
55
|
|
|
$middleware = new RequestLogger($this->formatter); |
56
|
|
|
$hasException = false; |
57
|
|
|
try { |
58
|
|
|
$middleware->process($this->request, $this->requestHandler); |
59
|
|
|
} catch (TypeError $e) { |
60
|
|
|
$hasException = true; |
61
|
|
|
} |
62
|
|
|
$this->assertTrue($hasException, "Exception was not rethrown"); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testProcessShouldLogExceptionsAndRethrow(): void |
66
|
|
|
{ |
67
|
|
|
$throwable = new RuntimeException('yaarrrr!'); |
68
|
|
|
$this->formatter->expects($this->once())->method('logRequest')->with($this->request); |
69
|
|
|
$this->formatter->expects($this->once())->method('logException')->with($throwable); |
70
|
|
|
$this->formatter->expects($this->never())->method('logResponse'); |
71
|
|
|
$this->formatter->expects($this->never())->method('logError'); |
72
|
|
|
$this->requestHandler->expects($this->once())->method('handle')->with($this->request)->willThrowException($throwable); |
73
|
|
|
|
74
|
|
|
$middleware = new RequestLogger($this->formatter); |
75
|
|
|
$hasException = false; |
76
|
|
|
try { |
77
|
|
|
$middleware->process($this->request, $this->requestHandler); |
78
|
|
|
} catch (RuntimeException $e) { |
79
|
|
|
$hasException = true; |
80
|
|
|
} |
81
|
|
|
$this->assertTrue($hasException, "Exception was not rethrown"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function buildFormatter(): void |
85
|
|
|
{ |
86
|
|
|
$this->formatter = $this->getMockBuilder(LogMessageFormatter::class) |
|
|
|
|
87
|
|
|
->disableOriginalConstructor() |
88
|
|
|
->setMethods([ |
89
|
|
|
'logRequest', |
90
|
|
|
'logResponse', |
91
|
|
|
'logError', |
92
|
|
|
'logException', |
93
|
|
|
]) |
94
|
|
|
->getMock() |
95
|
|
|
; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function destroyFormatter(): void |
99
|
|
|
{ |
100
|
|
|
$this->formatter = null; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|