|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VM\RequestLogger\Tests\Services; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use TypeError; |
|
10
|
|
|
use VM\Psr15Mocks\Middleware; |
|
11
|
|
|
use VM\RequestLogger\Services\LogLevel; |
|
12
|
|
|
use VM\RequestLogger\Services\StandardLogFormatter; |
|
13
|
|
|
use VM\RequestLogger\Tests\Traits\Logger; |
|
14
|
|
|
|
|
15
|
|
|
class StandardLogFormatterTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
use Logger; |
|
18
|
|
|
use Middleware; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$this->buildLogger(); |
|
23
|
|
|
$this->buildRequest(); |
|
24
|
|
|
$this->buildUri(); |
|
25
|
|
|
$this->buildBody(); |
|
26
|
|
|
$this->buildResponse(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function tearDown(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->destroyResponse(); |
|
32
|
|
|
$this->destroyBody(); |
|
33
|
|
|
$this->destroyUri(); |
|
34
|
|
|
$this->destroyRequest(); |
|
35
|
|
|
$this->destroyLogger(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testLogErrorShouldLogErrorsWithDefaultMethods(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->logger->expects($this->once())->method('error'); |
|
41
|
|
|
$this->logger->expects($this->once())->method('critical'); |
|
42
|
|
|
|
|
43
|
|
|
$logLevel = new LogLevel(LogLevel::LEVEL_DEBUG); |
|
44
|
|
|
$service = new StandardLogFormatter($this->logger, $logLevel); |
|
45
|
|
|
$service->logError(new TypeError('foo!')); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testLogErrorShouldNotLogWhenTheLevelIsNotHighEnough(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$this->logger->expects($this->never())->method('error'); |
|
51
|
|
|
$this->logger->expects($this->never())->method('critical'); |
|
52
|
|
|
|
|
53
|
|
|
$logLevel = new LogLevel(LogLevel::LEVEL_EMERGENCY); |
|
54
|
|
|
$service = new StandardLogFormatter($this->logger, $logLevel); |
|
55
|
|
|
$service->logError(new TypeError('foo!')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testLogLevelShouldBeConfigurable(): void |
|
59
|
|
|
{ |
|
60
|
|
|
$this->logger->expects($this->once())->method('emergency'); |
|
61
|
|
|
$this->logger->expects($this->never())->method('critical'); |
|
62
|
|
|
|
|
63
|
|
|
$logLevel = new LogLevel(LogLevel::LEVEL_EMERGENCY); |
|
64
|
|
|
$service = new StandardLogFormatter($this->logger, $logLevel, [StandardLogFormatter::MESSAGE_ERROR => $logLevel]); |
|
65
|
|
|
$service->logError(new TypeError('foo!')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testLogExceptionShouldLogExceptionsWithDefaultMethods(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$this->logger->expects($this->once())->method('error'); |
|
71
|
|
|
$this->logger->expects($this->once())->method('critical'); |
|
72
|
|
|
|
|
73
|
|
|
$logLevel = new LogLevel(LogLevel::LEVEL_DEBUG); |
|
74
|
|
|
$service = new StandardLogFormatter($this->logger, $logLevel); |
|
75
|
|
|
$service->logException(new RuntimeException('foo!')); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testLogRequestShouldLogAllRequestLogTypes(): void |
|
79
|
|
|
{ |
|
80
|
|
|
$this->logger->expects($this->once())->method('notice'); |
|
81
|
|
|
$this->logger->expects($this->once())->method('debug'); |
|
82
|
|
|
$this->logger->expects($this->once())->method('info'); |
|
83
|
|
|
|
|
84
|
|
|
$this->request->expects($this->once())->method('getUri')->willReturn($this->uri); |
|
85
|
|
|
$this->request->expects($this->once())->method('getHeaders')->willReturn(['X-SOME' => 'header']); |
|
86
|
|
|
$this->request->expects($this->once())->method('getBody')->willReturn($this->body); |
|
87
|
|
|
|
|
88
|
|
|
$this->uri->expects($this->once())->method('getHost')->willReturn('www.host.de'); |
|
89
|
|
|
$this->uri->expects($this->once())->method('getPath')->willReturn('/some/path'); |
|
90
|
|
|
$this->uri->expects($this->exactly(2))->method('getQuery')->willReturn('some=query'); |
|
91
|
|
|
|
|
92
|
|
|
$this->body->expects($this->once())->method('__toString')->willReturn('some body content'); |
|
93
|
|
|
|
|
94
|
|
|
$logLevel = new LogLevel(LogLevel::LEVEL_DEBUG); |
|
95
|
|
|
$service = new StandardLogFormatter($this->logger, $logLevel); |
|
96
|
|
|
$service->logRequest($this->request); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testLogResponseShouldLogAllResponseLogTypes(): void |
|
100
|
|
|
{ |
|
101
|
|
|
$this->logger->expects($this->once())->method('notice'); |
|
102
|
|
|
$this->logger->expects($this->once())->method('debug'); |
|
103
|
|
|
$this->logger->expects($this->once())->method('info'); |
|
104
|
|
|
|
|
105
|
|
|
$this->response->expects($this->once())->method('getStatusCode')->willReturn(200); |
|
106
|
|
|
$this->response->expects($this->once())->method('getHeaders')->willReturn(['X-SOME' => 'header']); |
|
107
|
|
|
$this->response->expects($this->once())->method('getBody')->willReturn($this->body); |
|
108
|
|
|
|
|
109
|
|
|
$this->body->expects($this->once())->method('__toString')->willReturn('some response body content'); |
|
110
|
|
|
|
|
111
|
|
|
$logLevel = new LogLevel(LogLevel::LEVEL_DEBUG); |
|
112
|
|
|
$service = new StandardLogFormatter($this->logger, $logLevel); |
|
113
|
|
|
$service->logResponse($this->response); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function testMayInstantiateWithoutSecondParam(): void |
|
117
|
|
|
{ |
|
118
|
|
|
$service = new StandardLogFormatter($this->logger); |
|
119
|
|
|
$this->assertSame(StandardLogFormatter::class, get_class($service)); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|