1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SubjectivePHPTest\Psr\Log; |
4
|
|
|
|
5
|
|
|
use MongoDB\BSON\UTCDateTime; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Psr\Log\LogLevel; |
8
|
|
|
use SubjectivePHP\Psr\Log\MongoLogger; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Unit tests for the \SubjectivePHP\Psr\Log\MongoLogger class. |
12
|
|
|
* |
13
|
|
|
* @coversDefaultClass \SubjectivePHP\Psr\Log\MongoLogger |
14
|
|
|
* @covers ::<private> |
15
|
|
|
* @covers ::__construct |
16
|
|
|
*/ |
17
|
|
|
final class MongoLoggerTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Verify basic behavior of log(). |
21
|
|
|
* |
22
|
|
|
* @test |
23
|
|
|
* @covers ::log |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
public function log() |
28
|
|
|
{ |
29
|
|
|
$collectionMock = $this->getMongoCollectionMockWithAsserts( |
30
|
|
|
LogLevel::WARNING, |
31
|
|
|
'this is a test', |
32
|
|
|
['some' => ['nested' => ['data']]], |
33
|
|
|
null |
34
|
|
|
); |
35
|
|
|
(new MongoLogger($collectionMock))->log( |
|
|
|
|
36
|
|
|
LogLevel::WARNING, |
37
|
|
|
'this is a test', |
38
|
|
|
['some' => ['nested' => ['data']]] |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Verify behavior of log() with message interpolation. |
44
|
|
|
* |
45
|
|
|
* @test |
46
|
|
|
* @covers ::log |
47
|
|
|
* |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function logWithInterpolation() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$collectionMock = $this->getMongoCollectionMockWithAsserts( |
53
|
|
|
LogLevel::INFO, |
54
|
|
|
'user chadicus created', |
55
|
|
|
['username' => 'chadicus'], |
56
|
|
|
null |
57
|
|
|
); |
58
|
|
|
(new MongoLogger($collectionMock))->log(LogLevel::INFO, 'user {username} created', ['username' => 'chadicus']); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Verify behavior of log() when $level is not known. |
63
|
|
|
* |
64
|
|
|
* @test |
65
|
|
|
* @covers ::log |
66
|
|
|
* @expectedException \Psr\Log\InvalidArgumentException |
67
|
|
|
* @expectedExceptionMessage Given $level was not a known LogLevel |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function logUnknownLevel() |
72
|
|
|
{ |
73
|
|
|
$collectionMock = $this->getMockBuilder('\\MongoDB\\Collection')->disableOriginalConstructor()->getMock(); |
74
|
|
|
$collectionMock->method('insertOne')->will($this->throwException(new \Exception('insertOne was called.'))); |
75
|
|
|
(new MongoLogger($collectionMock))->log('unknown', 'this is a test'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Verify behavior of log() when $message is not a valid string value. |
80
|
|
|
* |
81
|
|
|
* @test |
82
|
|
|
* @covers ::log |
83
|
|
|
* @expectedException \Psr\Log\InvalidArgumentException |
84
|
|
|
* @expectedExceptionMessage Given $message was a valid string value |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function logNonStringMessage() |
89
|
|
|
{ |
90
|
|
|
$collectionMock = $this->getMockBuilder('\\MongoDB\\Collection')->disableOriginalConstructor()->getMock(); |
91
|
|
|
$collectionMock->method('insertOne')->will($this->throwException(new \Exception('insertOne was called.'))); |
92
|
|
|
(new MongoLogger($collectionMock))->log(LogLevel::INFO, new \StdClass()); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Verify behavior of log() when $message is an object with __toString(). |
97
|
|
|
* |
98
|
|
|
* @test |
99
|
|
|
* @covers ::log |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function logObjectMessage() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$collectionMock = $this->getMongoCollectionMockWithAsserts( |
106
|
|
|
LogLevel::INFO, |
107
|
|
|
__FILE__, |
108
|
|
|
['foo' => 'bar'], |
109
|
|
|
null |
110
|
|
|
); |
111
|
|
|
(new MongoLogger($collectionMock))->log( |
|
|
|
|
112
|
|
|
LogLevel::INFO, |
113
|
|
|
new \SplFileInfo(__FILE__), |
114
|
|
|
['foo' => 'bar'] |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Verify context is normalized when log(). |
120
|
|
|
* |
121
|
|
|
* @test |
122
|
|
|
* @covers ::log |
123
|
|
|
* |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function logNormalizesContext() |
127
|
|
|
{ |
128
|
|
|
$collectionMock = $this->getMongoCollectionMockWithAsserts( |
129
|
|
|
LogLevel::INFO, |
130
|
|
|
'this is a test', |
131
|
|
|
[ |
132
|
|
|
'stdout' => 'resource', |
133
|
|
|
'object' => 'stdClass', |
134
|
|
|
'file' => __FILE__, |
135
|
|
|
], |
136
|
|
|
null |
137
|
|
|
); |
138
|
|
|
(new MongoLogger($collectionMock))->log( |
|
|
|
|
139
|
|
|
LogLevel::INFO, |
140
|
|
|
'this is a test', |
141
|
|
|
[ |
142
|
|
|
'stdout' => STDOUT, |
143
|
|
|
'object' => new \StdClass(), |
144
|
|
|
'file' => new \SplFileInfo(__FILE__), |
145
|
|
|
] |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Verify exception is handled properly. |
151
|
|
|
* |
152
|
|
|
* @test |
153
|
|
|
* @covers ::log |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function logWithException() |
158
|
|
|
{ |
159
|
|
|
$exception = new \RuntimeException('a message', 21); |
160
|
|
|
$collectionMock = $this->getMongoCollectionMockWithAsserts( |
161
|
|
|
LogLevel::INFO, |
162
|
|
|
'this is a test', |
163
|
|
|
[], |
164
|
|
|
[ |
165
|
|
|
'type' => 'RuntimeException', |
166
|
|
|
'message' => 'a message', |
167
|
|
|
'code' => 21, |
168
|
|
|
'file' => __FILE__, |
169
|
|
|
'line' => $exception->getLine(), |
170
|
|
|
'trace' => $exception->getTraceAsString(), |
171
|
|
|
'previous' => null, |
172
|
|
|
] |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
(new MongoLogger($collectionMock))->log( |
|
|
|
|
176
|
|
|
LogLevel::INFO, |
177
|
|
|
'this is a test', |
178
|
|
|
[ |
179
|
|
|
'exception' => $exception, |
180
|
|
|
] |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
private function getMongoCollectionMockWithAsserts($level, $message, $extra, $exception) |
185
|
|
|
{ |
186
|
|
|
$test = $this; |
187
|
|
|
$insertOneCallback = function ($document, $options) use ($test, $level, $message, $extra, $exception) { |
188
|
|
|
$test->assertInstanceOf('\\MongoDB\\BSON\\UTCDateTime', $document['timestamp']); |
189
|
|
|
$test->assertLessThanOrEqual(time(), $document['timestamp']->toDateTime()->getTimestamp()); |
190
|
|
|
$test->assertSame( |
191
|
|
|
[ |
192
|
|
|
'timestamp' => $document['timestamp'], |
193
|
|
|
'level' => $level, |
194
|
|
|
'message' => $message, |
195
|
|
|
'exception' => $exception, |
196
|
|
|
'extra' => $extra, |
197
|
|
|
], |
198
|
|
|
$document |
199
|
|
|
); |
200
|
|
|
$test->assertSame(['w' => 0], $options); |
201
|
|
|
}; |
202
|
|
|
$collectionMock = $this->getMockBuilder('\\MongoDB\\Collection')->disableOriginalConstructor()->getMock(); |
203
|
|
|
$collectionMock->expects($this->once())->method('insertOne')->will($this->returnCallback($insertOneCallback)); |
204
|
|
|
return $collectionMock; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: