|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Dziki\MonologSentryBundle\Tests\Unit\SubscribedProcessor; |
|
6
|
|
|
|
|
7
|
|
|
use Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending; |
|
8
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @covers \Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending |
|
17
|
|
|
*/ |
|
18
|
|
|
class UserDataAppendingTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @test |
|
22
|
|
|
*/ |
|
23
|
|
|
public function isSubscribedToKernelRequest(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$subscribedEvents = UserDataAppending::getSubscribedEvents(); |
|
26
|
|
|
$this->assertArrayHasKey(KernelEvents::REQUEST, $subscribedEvents); |
|
27
|
|
|
$this->assertSame(['onKernelRequest', 7], $subscribedEvents[KernelEvents::REQUEST]); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @test |
|
32
|
|
|
*/ |
|
33
|
|
|
public function doNothingIfMissingToken(): void |
|
34
|
|
|
{ |
|
35
|
|
|
/** @var TokenStorageInterface $tokenStorage */ |
|
36
|
|
|
$tokenStorage = $this->createMock(TokenStorageInterface::class); |
|
37
|
|
|
$userDataAppendingProcessor = new UserDataAppending($tokenStorage); |
|
38
|
|
|
|
|
39
|
|
|
$userDataAppendingProcessor->onKernelRequest(); |
|
40
|
|
|
|
|
41
|
|
|
$record = $userDataAppendingProcessor([]); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertSame([], $record); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @test |
|
48
|
|
|
*/ |
|
49
|
|
|
public function obtainUsernameIfUserImplementsUserInterface(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$user = $this->createMock(UserInterface::class); |
|
52
|
|
|
$user |
|
53
|
|
|
->expects($this->once()) |
|
54
|
|
|
->method('getUsername') |
|
55
|
|
|
->willReturn('user') |
|
56
|
|
|
; |
|
57
|
|
|
|
|
58
|
|
|
$token = $this->createMock(TokenInterface::class); |
|
59
|
|
|
$token |
|
60
|
|
|
->method('getUser') |
|
61
|
|
|
->willReturn($user) |
|
62
|
|
|
; |
|
63
|
|
|
|
|
64
|
|
|
/** @var TokenStorageInterface|MockObject $tokenStorage */ |
|
65
|
|
|
$tokenStorage = $this->createMock(TokenStorageInterface::class); |
|
66
|
|
|
$tokenStorage |
|
|
|
|
|
|
67
|
|
|
->method('getToken') |
|
68
|
|
|
->willReturn($token) |
|
69
|
|
|
; |
|
70
|
|
|
|
|
71
|
|
|
$userDataAppendingProcessor = new UserDataAppending($tokenStorage); |
|
72
|
|
|
$userDataAppendingProcessor->onKernelRequest(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @test |
|
77
|
|
|
*/ |
|
78
|
|
View Code Duplication |
public function appendToLogsUsernameIfUserIsString(): void |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$user = 'user'; |
|
81
|
|
|
|
|
82
|
|
|
$token = $this->createMock(TokenInterface::class); |
|
83
|
|
|
$token |
|
84
|
|
|
->method('getUser') |
|
85
|
|
|
->willReturn($user) |
|
86
|
|
|
; |
|
87
|
|
|
|
|
88
|
|
|
/** @var TokenStorageInterface|MockObject $tokenStorage */ |
|
89
|
|
|
$tokenStorage = $this->createMock(TokenStorageInterface::class); |
|
90
|
|
|
$tokenStorage |
|
|
|
|
|
|
91
|
|
|
->method('getToken') |
|
92
|
|
|
->willReturn($token) |
|
93
|
|
|
; |
|
94
|
|
|
|
|
95
|
|
|
$userDataAppendingProcessor = new UserDataAppending($tokenStorage); |
|
96
|
|
|
|
|
97
|
|
|
$userDataAppendingProcessor->onKernelRequest(); |
|
98
|
|
|
|
|
99
|
|
|
$record = $userDataAppendingProcessor([]); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertSame( |
|
102
|
|
|
['context' => [ |
|
103
|
|
|
'user' => [ |
|
104
|
|
|
'username' => 'user', |
|
105
|
|
|
], |
|
106
|
|
|
], |
|
107
|
|
|
], |
|
108
|
|
|
$record |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @test |
|
114
|
|
|
*/ |
|
115
|
|
View Code Duplication |
public function appendToLogsUsernameIfUserHasToStringMethod(): void |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
$user = new class() { |
|
118
|
|
|
public function __toString() |
|
119
|
|
|
{ |
|
120
|
|
|
return 'user'; |
|
121
|
|
|
} |
|
122
|
|
|
}; |
|
123
|
|
|
|
|
124
|
|
|
$token = $this->createMock(TokenInterface::class); |
|
125
|
|
|
$token |
|
126
|
|
|
->method('getUser') |
|
127
|
|
|
->willReturn($user) |
|
128
|
|
|
; |
|
129
|
|
|
|
|
130
|
|
|
/** @var TokenStorageInterface|MockObject $tokenStorage */ |
|
131
|
|
|
$tokenStorage = $this->createMock(TokenStorageInterface::class); |
|
132
|
|
|
$tokenStorage |
|
|
|
|
|
|
133
|
|
|
->method('getToken') |
|
134
|
|
|
->willReturn($token) |
|
135
|
|
|
; |
|
136
|
|
|
|
|
137
|
|
|
$userDataAppendingProcessor = new UserDataAppending($tokenStorage); |
|
138
|
|
|
$userDataAppendingProcessor->onKernelRequest(); |
|
139
|
|
|
|
|
140
|
|
|
$record = $userDataAppendingProcessor([]); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertSame( |
|
143
|
|
|
['context' => [ |
|
144
|
|
|
'user' => [ |
|
145
|
|
|
'username' => 'user', |
|
146
|
|
|
], |
|
147
|
|
|
], |
|
148
|
|
|
], |
|
149
|
|
|
$record |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.