12
12
namespace Symfony \Component \Security \Http \Tests \Authentication ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \EventDispatcher \EventDispatcher ;
15
16
use Symfony \Component \HttpFoundation \Request ;
16
17
use Symfony \Component \HttpFoundation \Response ;
17
18
use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
18
19
use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
19
- use Symfony \Component \Security \Core \Event \AuthenticationSuccessEvent ;
20
20
use Symfony \Component \Security \Core \Exception \BadCredentialsException ;
21
21
use Symfony \Component \Security \Core \Exception \UsernameNotFoundException ;
22
22
use Symfony \Component \Security \Core \User \UserInterface ;
23
23
use Symfony \Component \Security \Http \Authentication \AuthenticatorManager ;
24
- use Symfony \Component \Security \Http \Authenticator \AuthenticatorInterface ;
25
- use Symfony \Component \Security \Http \Event \LoginSuccessEvent ;
26
- use Symfony \Component \Security \Http \Event \InteractiveLoginEvent ;
24
+ use Symfony \Component \Security \Http \Authenticator \InteractiveAuthenticatorInterface ;
27
25
use Symfony \Component \Security \Http \Event \VerifyAuthenticatorCredentialsEvent ;
28
- use Symfony \Contracts \EventDispatcher \EventDispatcherInterface ;
29
26
30
27
class AuthenticatorManagerTest extends TestCase
31
28
{
@@ -39,7 +36,7 @@ class AuthenticatorManagerTest extends TestCase
39
36
protected function setUp (): void
40
37
{
41
38
$ this ->tokenStorage = $ this ->createMock (TokenStorageInterface::class);
42
- $ this ->eventDispatcher = $ this -> createMock (EventDispatcherInterface::class );
39
+ $ this ->eventDispatcher = new EventDispatcher ( );
43
40
$ this ->request = new Request ();
44
41
$ this ->user = $ this ->createMock (UserInterface::class);
45
42
$ this ->token = $ this ->createMock (TokenInterface::class);
@@ -95,35 +92,22 @@ public function testAuthenticateRequest($matchingAuthenticatorIndex)
95
92
96
93
$ matchingAuthenticator ->expects ($ this ->any ())->method ('getCredentials ' )->willReturn (['password ' => 'pa$$ ' ]);
97
94
$ matchingAuthenticator ->expects ($ this ->any ())->method ('getUser ' )->willReturn ($ this ->user );
98
- $ this ->eventDispatcher ->expects ($ this ->exactly (4 ))
99
- ->method ('dispatch ' )
100
- ->with ($ this ->callback (function ($ event ) use ($ matchingAuthenticator ) {
101
- if ($ event instanceof VerifyAuthenticatorCredentialsEvent) {
102
- return $ event ->getAuthenticator () === $ matchingAuthenticator
103
- && $ event ->getCredentials () === ['password ' => 'pa$$ ' ]
104
- && $ event ->getUser () === $ this ->user ;
105
- }
106
-
107
- return $ event instanceof InteractiveLoginEvent || $ event instanceof LoginSuccessEvent || $ event instanceof AuthenticationSuccessEvent;
108
- }))
109
- ->will ($ this ->returnCallback (function ($ event ) {
110
- if ($ event instanceof VerifyAuthenticatorCredentialsEvent) {
111
- $ event ->setCredentialsValid (true );
112
- }
113
-
114
- return $ event ;
115
- }));
95
+
96
+ $ listenerCalled = false ;
97
+ $ this ->eventDispatcher ->addListener (VerifyAuthenticatorCredentialsEvent::class, function (VerifyAuthenticatorCredentialsEvent $ event ) use (&$ listenerCalled , $ matchingAuthenticator ) {
98
+ if ($ event ->getAuthenticator () === $ matchingAuthenticator && $ event ->getCredentials () === ['password ' => 'pa$$ ' ] && $ event ->getUser () === $ this ->user ) {
99
+ $ listenerCalled = true ;
100
+
101
+ $ event ->setCredentialsValid (true );
102
+ }
103
+ });
116
104
$ matchingAuthenticator ->expects ($ this ->any ())->method ('createAuthenticatedToken ' )->willReturn ($ this ->token );
117
105
118
106
$ this ->tokenStorage ->expects ($ this ->once ())->method ('setToken ' )->with ($ this ->token );
119
107
120
- $ matchingAuthenticator ->expects ($ this ->any ())
121
- ->method ('onAuthenticationSuccess ' )
122
- ->with ($ this ->anything (), $ this ->token , 'main ' )
123
- ->willReturn ($ this ->response );
124
-
125
108
$ manager = $ this ->createManager ($ authenticators );
126
- $ this ->assertSame ($ this ->response , $ manager ->authenticateRequest ($ this ->request ));
109
+ $ this ->assertNull ($ manager ->authenticateRequest ($ this ->request ));
110
+ $ this ->assertTrue ($ listenerCalled , 'The VerifyAuthenticatorCredentialsEvent listener is not called ' );
127
111
}
128
112
129
113
public function provideMatchingAuthenticatorIndex ()
@@ -174,15 +158,9 @@ public function testEraseCredentials($eraseCredentials)
174
158
175
159
$ authenticator ->expects ($ this ->any ())->method ('getCredentials ' )->willReturn (['username ' => 'john ' ]);
176
160
$ authenticator ->expects ($ this ->any ())->method ('getUser ' )->willReturn ($ this ->user );
177
- $ this ->eventDispatcher ->expects ($ this ->any ())
178
- ->method ('dispatch ' )
179
- ->will ($ this ->returnCallback (function ($ event ) {
180
- if ($ event instanceof VerifyAuthenticatorCredentialsEvent) {
181
- $ event ->setCredentialsValid (true );
182
- }
183
-
184
- return $ event ;
185
- }));
161
+ $ this ->eventDispatcher ->addListener (VerifyAuthenticatorCredentialsEvent::class, function (VerifyAuthenticatorCredentialsEvent $ event ) {
162
+ $ event ->setCredentialsValid (true );
163
+ });
186
164
187
165
$ authenticator ->expects ($ this ->any ())->method ('createAuthenticatedToken ' )->willReturn ($ this ->token );
188
166
@@ -207,12 +185,38 @@ public function testAuthenticateUser()
207
185
$ this ->tokenStorage ->expects ($ this ->once ())->method ('setToken ' )->with ($ this ->token );
208
186
209
187
$ manager = $ this ->createManager ([$ authenticator ]);
210
- $ this ->assertSame ($ this ->response , $ manager ->authenticateUser ($ this ->user , $ authenticator , $ this ->request ));
188
+ $ manager ->authenticateUser ($ this ->user , $ authenticator , $ this ->request );
189
+ }
190
+
191
+ public function testInteractiveAuthenticator ()
192
+ {
193
+ $ authenticator = $ this ->createMock (InteractiveAuthenticatorInterface::class);
194
+ $ authenticator ->expects ($ this ->any ())->method ('isInteractive ' )->willReturn (true );
195
+ $ this ->request ->attributes ->set ('_guard_authenticators ' , [$ authenticator ]);
196
+
197
+ $ authenticator ->expects ($ this ->any ())->method ('getCredentials ' )->willReturn (['password ' => 'pa$$ ' ]);
198
+ $ authenticator ->expects ($ this ->any ())->method ('getUser ' )->willReturn ($ this ->user );
199
+
200
+ $ this ->eventDispatcher ->addListener (VerifyAuthenticatorCredentialsEvent::class, function (VerifyAuthenticatorCredentialsEvent $ event ) {
201
+ $ event ->setCredentialsValid (true );
202
+ });
203
+ $ authenticator ->expects ($ this ->any ())->method ('createAuthenticatedToken ' )->willReturn ($ this ->token );
204
+
205
+ $ this ->tokenStorage ->expects ($ this ->once ())->method ('setToken ' )->with ($ this ->token );
206
+
207
+ $ authenticator ->expects ($ this ->any ())
208
+ ->method ('onAuthenticationSuccess ' )
209
+ ->with ($ this ->anything (), $ this ->token , 'main ' )
210
+ ->willReturn ($ this ->response );
211
+
212
+ $ manager = $ this ->createManager ([$ authenticator ]);
213
+ $ response = $ manager ->authenticateRequest ($ this ->request );
214
+ $ this ->assertSame ($ this ->response , $ response );
211
215
}
212
216
213
217
private function createAuthenticator ($ supports = true )
214
218
{
215
- $ authenticator = $ this ->createMock (AuthenticatorInterface ::class);
219
+ $ authenticator = $ this ->createMock (InteractiveAuthenticatorInterface ::class);
216
220
$ authenticator ->expects ($ this ->any ())->method ('supports ' )->willReturn ($ supports );
217
221
218
222
return $ authenticator ;
0 commit comments