1212namespace Symfony \Component \Security \Http \Tests \Authentication ;
1313
1414use PHPUnit \Framework \TestCase ;
15+ use Symfony \Component \EventDispatcher \EventDispatcher ;
1516use Symfony \Component \HttpFoundation \Request ;
1617use Symfony \Component \HttpFoundation \Response ;
1718use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
1819use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
19- use Symfony \Component \Security \Core \Event \AuthenticationSuccessEvent ;
2020use Symfony \Component \Security \Core \Exception \BadCredentialsException ;
2121use Symfony \Component \Security \Core \Exception \UsernameNotFoundException ;
2222use Symfony \Component \Security \Core \User \UserInterface ;
2323use 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 ;
2725use Symfony \Component \Security \Http \Event \VerifyAuthenticatorCredentialsEvent ;
28- use Symfony \Contracts \EventDispatcher \EventDispatcherInterface ;
2926
3027class AuthenticatorManagerTest extends TestCase
3128{
@@ -39,7 +36,7 @@ class AuthenticatorManagerTest extends TestCase
3936 protected function setUp (): void
4037 {
4138 $ this ->tokenStorage = $ this ->createMock (TokenStorageInterface::class);
42- $ this ->eventDispatcher = $ this -> createMock (EventDispatcherInterface::class );
39+ $ this ->eventDispatcher = new EventDispatcher ( );
4340 $ this ->request = new Request ();
4441 $ this ->user = $ this ->createMock (UserInterface::class);
4542 $ this ->token = $ this ->createMock (TokenInterface::class);
@@ -95,35 +92,22 @@ public function testAuthenticateRequest($matchingAuthenticatorIndex)
9592
9693 $ matchingAuthenticator ->expects ($ this ->any ())->method ('getCredentials ' )->willReturn (['password ' => 'pa$$ ' ]);
9794 $ 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+ });
116104 $ matchingAuthenticator ->expects ($ this ->any ())->method ('createAuthenticatedToken ' )->willReturn ($ this ->token );
117105
118106 $ this ->tokenStorage ->expects ($ this ->once ())->method ('setToken ' )->with ($ this ->token );
119107
120- $ matchingAuthenticator ->expects ($ this ->any ())
121- ->method ('onAuthenticationSuccess ' )
122- ->with ($ this ->anything (), $ this ->token , 'main ' )
123- ->willReturn ($ this ->response );
124-
125108 $ 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 ' );
127111 }
128112
129113 public function provideMatchingAuthenticatorIndex ()
@@ -174,15 +158,9 @@ public function testEraseCredentials($eraseCredentials)
174158
175159 $ authenticator ->expects ($ this ->any ())->method ('getCredentials ' )->willReturn (['username ' => 'john ' ]);
176160 $ 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+ });
186164
187165 $ authenticator ->expects ($ this ->any ())->method ('createAuthenticatedToken ' )->willReturn ($ this ->token );
188166
@@ -207,12 +185,38 @@ public function testAuthenticateUser()
207185 $ this ->tokenStorage ->expects ($ this ->once ())->method ('setToken ' )->with ($ this ->token );
208186
209187 $ 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 );
211215 }
212216
213217 private function createAuthenticator ($ supports = true )
214218 {
215- $ authenticator = $ this ->createMock (AuthenticatorInterface ::class);
219+ $ authenticator = $ this ->createMock (InteractiveAuthenticatorInterface ::class);
216220 $ authenticator ->expects ($ this ->any ())->method ('supports ' )->willReturn ($ supports );
217221
218222 return $ authenticator ;
0 commit comments