Passed
Pull Request — master (#3)
by Bastien
02:20
created

DeamonLoggerExtraWebProcessorTest::testProcessorWithNullContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
164
{
165
    const SCOPE_CONTAINER = 'scope';
166
167
    private $parameters = array();
168
169
    public function set($id, $service, $scope = self::SCOPE_CONTAINER)
170
    {
171
    }
172
173
    public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
174
    {
175
        switch ($id) {
176
            case 'kernel':
177
                return new KernelForTest('env_foo', false);
178
            case 'deamon.logger_extra.context':
179
                return new DeamonLoggerExtraContext('fr', 'foo_app');
180
            case 'request_stack':
181
                $request = new Request([], [], [
182
                    '_route' => 'requested_route',
183
                ], [], [], [
184
                    'HTTP_ACCEPT-ENCODING' => 'Bar-Encoding',
185
                    'HTTP_USER_AGENT' => 'user_agent_string',
186
                    'REQUEST_URI' => 'requested_uri',
187
                    'REMOTE_ADDR' => '123.456.789.123',
188
                ]);
189
                $stack = new RequestStack();
190
                $stack->push($request);
191
192
                return $stack;
193
            case 'security.token_storage':
194
                $storage = new TokenStorage();
195
                $storage->setToken(new MyToken($this->parameters['user']));
196
197
                return $storage;
198
            default:
199
                return null;
200
        }
201
    }
202
203
    public function has($id)
204
    {
205
    }
206
207
    public function initialized($id)
208
    {
209
    }
210
211
    public function getParameter($name)
212
    {
213
    }
214
215
    public function hasParameter($name)
216
    {
217
    }
218
219
    public function setParameter($name, $value)
220
    {
221
        $this->parameters[$name] = $value;
222
    }
223
}
224
225
class MyUserWithOnlyUsername implements UserInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
226
{
227
    private $userName;
228
229
    public function __construct($userName = 'foo')
230
    {
231
        $this->userName = $userName;
232
    }
233
234
    public function getRoles()
235
    {
236
    }
237
238
    public function getPassword()
239
    {
240
    }
241
242
    public function getSalt()
243
    {
244
    }
245
246
    public function eraseCredentials()
247
    {
248
    }
249
250
    public function getUsername()
251
    {
252
        return $this->userName;
253
    }
254
}
255
256
class MyUserWithAllFields implements UserInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
257
{
258
    private $id;
259
    private $email;
260
    private $userName;
261
262
    public function __construct($id = 1, $email = '[email protected]', $userName = 'foo')
263
    {
264
        $this->id = $id;
265
        $this->email = $email;
266
        $this->userName = $userName;
267
    }
268
269
    public function getRoles()
270
    {
271
    }
272
273
    public function getPassword()
274
    {
275
    }
276
277
    public function getSalt()
278
    {
279
    }
280
281
    public function eraseCredentials()
282
    {
283
    }
284
285
    public function getUsername()
286
    {
287
        return $this->userName;
288
    }
289
290
    public function getId()
291
    {
292
        return $this->id;
293
    }
294
295
    public function getEmail()
296
    {
297
        return $this->email;
298
    }
299
}
300
301
class MyToken implements TokenInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
302
{
303
    private $user;
304
305
    public function __construct($user = null)
306
    {
307
        $this->user = $user;
308
    }
309
310
    public function serialize()
311
    {
312
    }
313
314
    public function unserialize($serialized)
315
    {
316
    }
317
318
    public function __toString()
319
    {
320
    }
321
322
    public function getRoles()
323
    {
324
    }
325
326
    public function getCredentials()
327
    {
328
    }
329
330
    public function getUser()
331
    {
332
        return $this->user;
333
    }
334
335
    public function setUser($user)
336
    {
337
    }
338
339
    public function getUsername()
340
    {
341
    }
342
343
    public function isAuthenticated()
344
    {
345
    }
346
347
    public function setAuthenticated($isAuthenticated)
348
    {
349
    }
350
351
    public function eraseCredentials()
352
    {
353
    }
354
355
    public function getAttributes()
356
    {
357
    }
358
359
    public function setAttributes(array $attributes)
360
    {
361
    }
362
363
    public function hasAttribute($name)
364
    {
365
    }
366
367
    public function getAttribute($name)
368
    {
369
    }
370
371
    public function setAttribute($name, $value)
372
    {
373
    }
374
}
375