1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Deamon\LoggerExtraBundle\Tests\Processors\Monolog; |
4
|
|
|
|
5
|
|
|
use Deamon\LoggerExtraBundle\Processors\Monolog\DeamonLoggerExtraWebProcessor; |
6
|
|
|
use Deamon\LoggerExtraBundle\Services\DeamonLoggerExtraContext; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Bridge\Monolog\Logger; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
12
|
|
|
use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest; |
13
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
14
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
15
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
16
|
|
|
|
17
|
|
|
class DeamonLoggerExtraWebProcessorTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function testProcessorWithNullContainer() |
20
|
|
|
{ |
21
|
|
|
$processor = new DeamonLoggerExtraWebProcessor(); |
22
|
|
|
$originalRecord = $this->getRecord(); |
23
|
|
|
$record = $processor->__invoke($originalRecord); |
24
|
|
|
|
25
|
|
|
$this->assertEquals($originalRecord, $record); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @runInSeparateProcess |
30
|
|
|
*/ |
31
|
|
|
public function testAddContextInfo() |
32
|
|
|
{ |
33
|
|
|
$config = $this->getDisplayConfig([ |
34
|
|
|
'env' => true, |
35
|
|
|
'locale' => true, |
36
|
|
|
'application_name' => true, |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
$processor = new DeamonLoggerExtraWebProcessor(new MyContainerForTests(), $config); |
40
|
|
|
$record = $processor->__invoke($this->getRecord()); |
41
|
|
|
|
42
|
|
|
$this->assertArrayHasKeyAndEquals('env', $record['extra'], 'env_foo'); |
43
|
|
|
$this->assertArrayHasKeyAndEquals('locale', $record['extra'], 'fr'); |
44
|
|
|
$this->assertArrayHasKeyAndEquals('application', $record['extra'], 'foo_app'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testAddRequestInfo() |
48
|
|
|
{ |
49
|
|
|
$config = $this->getDisplayConfig( |
50
|
|
|
[ |
51
|
|
|
'url' => true, |
52
|
|
|
'route' => true, |
53
|
|
|
'user_agent' => true, |
54
|
|
|
'accept_encoding' => true, |
55
|
|
|
'client_ip' => true, |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$processor = new DeamonLoggerExtraWebProcessor(new MyContainerForTests(), $config); |
60
|
|
|
$record = $processor->__invoke($this->getRecord()); |
61
|
|
|
|
62
|
|
|
$this->assertArrayHasKeyAndEquals('url', $record['extra'], 'requested_uri'); |
63
|
|
|
$this->assertArrayHasKeyAndEquals('route', $record['extra'], 'requested_route'); |
64
|
|
|
$this->assertArrayHasKeyAndEquals('user_agent', $record['extra'], 'user_agent_string'); |
65
|
|
|
$this->assertArrayHasKeyAndEquals('accept_encoding', $record['extra'], 'Bar-Encoding'); |
66
|
|
|
$this->assertArrayHasKeyAndEquals('client_ip', $record['extra'], '123.456.789.123'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testAddUserInfo() |
70
|
|
|
{ |
71
|
|
|
$config = $this->getDisplayConfig([ |
72
|
|
|
'user' => true, |
73
|
|
|
'user_id' => true, |
74
|
|
|
'user_email' => true, |
75
|
|
|
'user_name' => true, |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
$processor = new DeamonLoggerExtraWebProcessor(null, $config); |
79
|
|
|
$container = new MyContainerForTests(); |
80
|
|
|
$container->setParameter('user', new MyUserWithAllFields()); |
81
|
|
|
$processor->setContainer($container); |
82
|
|
|
$record = $processor->__invoke($this->getRecord()); |
83
|
|
|
|
84
|
|
|
$this->assertArrayHasKeyAndEquals('user_id', $record['extra'], 1); |
85
|
|
|
$this->assertArrayHasKeyAndEquals('user_email', $record['extra'], '[email protected]'); |
86
|
|
|
$this->assertArrayHasKeyAndEquals('user_name', $record['extra'], 'foo'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testAddChannelInfoWithoutChannelPrefix() |
90
|
|
|
{ |
91
|
|
|
$config = $this->getDisplayConfig(['global_channel' => true]); |
92
|
|
|
$processor = new DeamonLoggerExtraWebProcessor(new MyContainerForTests(), $config); |
93
|
|
|
$originalRecord = $this->getRecord(); |
94
|
|
|
$record = $processor->__invoke($originalRecord); |
95
|
|
|
|
96
|
|
|
$this->assertArrayHasKeyAndEquals('global_channel', $record['extra'], $originalRecord['channel']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testAddChannelInfoWithChannelPrefix() |
100
|
|
|
{ |
101
|
|
|
$config = $this->getDisplayConfig(['global_channel' => true], 'prefix'); |
102
|
|
|
$processor = new DeamonLoggerExtraWebProcessor(new MyContainerForTests(), $config); |
103
|
|
|
$originalRecord = $this->getRecord(); |
104
|
|
|
$record = $processor->__invoke($originalRecord); |
105
|
|
|
|
106
|
|
|
$this->assertArrayHasKeyAndEquals('global_channel', $record['extra'], $originalRecord['channel']); |
107
|
|
|
$this->assertArrayHasKeyAndEquals('channel', $record, sprintf('prefix.%s', $originalRecord['channel'])); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function getDisplayConfig($trueValues, $channelPrefix = null) |
111
|
|
|
{ |
112
|
|
|
$ret = array_merge( |
113
|
|
|
[ |
114
|
|
|
'env' => false, |
115
|
|
|
'locale' => false, |
116
|
|
|
'application_name' => false, |
117
|
|
|
'url' => false, |
118
|
|
|
'route' => false, |
119
|
|
|
'user_agent' => false, |
120
|
|
|
'accept_encoding' => false, |
121
|
|
|
'client_ip' => false, |
122
|
|
|
'user_id' => false, |
123
|
|
|
'user_email' => false, |
124
|
|
|
'user_name' => false, |
125
|
|
|
'global_channel' => false, |
126
|
|
|
], |
127
|
|
|
$trueValues |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return [ |
131
|
|
|
'channel_prefix' => $channelPrefix, |
132
|
|
|
'display' => $ret, |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function assertArrayHasKeyAndEquals($key, $array, $value) |
137
|
|
|
{ |
138
|
|
|
$this->assertArrayHasKey($key, $array); |
139
|
|
|
$this->assertEquals($value, $array[$key]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param int $level |
144
|
|
|
* @param string $message |
145
|
|
|
* @param array $context |
146
|
|
|
* |
147
|
|
|
* @return array Record |
148
|
|
|
*/ |
149
|
|
|
protected function getRecord($level = Logger::WARNING, $message = 'test', $context = array()) |
150
|
|
|
{ |
151
|
|
|
return array( |
152
|
|
|
'message' => $message, |
153
|
|
|
'context' => $context, |
154
|
|
|
'level' => $level, |
155
|
|
|
'level_name' => Logger::getLevelName($level), |
156
|
|
|
'channel' => 'test', |
157
|
|
|
'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true))), |
158
|
|
|
'extra' => array(), |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
class MyContainerForTests implements ContainerInterface |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
private $parameters = array(); |
166
|
|
|
|
167
|
|
|
public function set($id, $service) |
168
|
|
|
{ |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) |
172
|
|
|
{ |
173
|
|
|
switch ($id) { |
174
|
|
|
case 'kernel': |
175
|
|
|
return new KernelForTest('env_foo', false); |
176
|
|
|
case 'deamon.logger_extra.context': |
177
|
|
|
return new DeamonLoggerExtraContext('fr', 'foo_app'); |
178
|
|
|
case 'request_stack': |
179
|
|
|
$request = new Request([], [], [ |
180
|
|
|
'_route' => 'requested_route', |
181
|
|
|
], [], [], [ |
182
|
|
|
'HTTP_ACCEPT-ENCODING' => 'Bar-Encoding', |
183
|
|
|
'HTTP_USER_AGENT' => 'user_agent_string', |
184
|
|
|
'REQUEST_URI' => 'requested_uri', |
185
|
|
|
'REMOTE_ADDR' => '123.456.789.123', |
186
|
|
|
]); |
187
|
|
|
$stack = new RequestStack(); |
188
|
|
|
$stack->push($request); |
189
|
|
|
|
190
|
|
|
return $stack; |
191
|
|
|
case 'security.token_storage': |
192
|
|
|
$storage = new TokenStorage(); |
193
|
|
|
$storage->setToken(new MyToken($this->parameters['user'])); |
194
|
|
|
|
195
|
|
|
return $storage; |
196
|
|
|
default: |
197
|
|
|
return null; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function has($id) |
202
|
|
|
{ |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function initialized($id) |
206
|
|
|
{ |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function getParameter($name) |
210
|
|
|
{ |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function hasParameter($name) |
214
|
|
|
{ |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function setParameter($name, $value) |
218
|
|
|
{ |
219
|
|
|
$this->parameters[$name] = $value; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
class MyUserWithOnlyUsername implements UserInterface |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
private $userName; |
226
|
|
|
|
227
|
|
|
public function __construct($userName = 'foo') |
228
|
|
|
{ |
229
|
|
|
$this->userName = $userName; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function getRoles() |
233
|
|
|
{ |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function getPassword() |
237
|
|
|
{ |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function getSalt() |
241
|
|
|
{ |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function eraseCredentials() |
245
|
|
|
{ |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function getUsername() |
249
|
|
|
{ |
250
|
|
|
return $this->userName; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
class MyUserWithAllFields implements UserInterface |
|
|
|
|
255
|
|
|
{ |
256
|
|
|
private $id; |
257
|
|
|
private $email; |
258
|
|
|
private $userName; |
259
|
|
|
|
260
|
|
|
public function __construct($id = 1, $email = '[email protected]', $userName = 'foo') |
261
|
|
|
{ |
262
|
|
|
$this->id = $id; |
263
|
|
|
$this->email = $email; |
264
|
|
|
$this->userName = $userName; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function getRoles() |
268
|
|
|
{ |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function getPassword() |
272
|
|
|
{ |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function getSalt() |
276
|
|
|
{ |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function eraseCredentials() |
280
|
|
|
{ |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function getUsername() |
284
|
|
|
{ |
285
|
|
|
return $this->userName; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function getId() |
289
|
|
|
{ |
290
|
|
|
return $this->id; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
public function getEmail() |
294
|
|
|
{ |
295
|
|
|
return $this->email; |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
class MyToken implements TokenInterface |
|
|
|
|
300
|
|
|
{ |
301
|
|
|
private $user; |
302
|
|
|
|
303
|
|
|
public function __construct($user = null) |
304
|
|
|
{ |
305
|
|
|
$this->user = $user; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function serialize() |
309
|
|
|
{ |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function unserialize($serialized) |
313
|
|
|
{ |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function __toString() |
317
|
|
|
{ |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
public function getRoles() |
321
|
|
|
{ |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
public function getCredentials() |
325
|
|
|
{ |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function getUser() |
329
|
|
|
{ |
330
|
|
|
return $this->user; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function setUser($user) |
334
|
|
|
{ |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function getUsername() |
338
|
|
|
{ |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
public function isAuthenticated() |
342
|
|
|
{ |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function setAuthenticated($isAuthenticated) |
346
|
|
|
{ |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
public function eraseCredentials() |
350
|
|
|
{ |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function getAttributes() |
354
|
|
|
{ |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
public function setAttributes(array $attributes) |
358
|
|
|
{ |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
public function hasAttribute($name) |
362
|
|
|
{ |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function getAttribute($name) |
366
|
|
|
{ |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function setAttribute($name, $value) |
370
|
|
|
{ |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.