2525use Symfony \Component \Security \Http \Authentication \AuthenticationSuccessHandlerInterface ;
2626use Symfony \Component \Security \Http \Firewall \UsernamePasswordJsonAuthenticationListener ;
2727use Symfony \Component \Security \Http \HttpUtils ;
28+ use Symfony \Component \Translation \Loader \ArrayLoader ;
29+ use Symfony \Component \Translation \Translator ;
2830
2931/**
3032 * @author Kévin Dunglas <dunglas@gmail.com>
@@ -36,7 +38,7 @@ class UsernamePasswordJsonAuthenticationListenerTest extends TestCase
3638 */
3739 private $ listener ;
3840
39- private function createListener (array $ options = [], $ success = true , $ matchCheckPath = true )
41+ private function createListener (array $ options = [], $ success = true , $ matchCheckPath = true , $ withMockedHandler = true )
4042 {
4143 $ tokenStorage = $ this ->getMockBuilder (TokenStorageInterface::class)->getMock ();
4244 $ httpUtils = $ this ->getMockBuilder (HttpUtils::class)->getMock ();
@@ -55,10 +57,15 @@ private function createListener(array $options = [], $success = true, $matchChec
5557 $ authenticationManager ->method ('authenticate ' )->willThrowException (new AuthenticationException ());
5658 }
5759
58- $ authenticationSuccessHandler = $ this ->getMockBuilder (AuthenticationSuccessHandlerInterface::class)->getMock ();
59- $ authenticationSuccessHandler ->method ('onAuthenticationSuccess ' )->willReturn (new Response ('ok ' ));
60- $ authenticationFailureHandler = $ this ->getMockBuilder (AuthenticationFailureHandlerInterface::class)->getMock ();
61- $ authenticationFailureHandler ->method ('onAuthenticationFailure ' )->willReturn (new Response ('ko ' ));
60+ $ authenticationSuccessHandler = null ;
61+ $ authenticationFailureHandler = null ;
62+
63+ if ($ withMockedHandler ) {
64+ $ authenticationSuccessHandler = $ this ->getMockBuilder (AuthenticationSuccessHandlerInterface::class)->getMock ();
65+ $ authenticationSuccessHandler ->method ('onAuthenticationSuccess ' )->willReturn (new Response ('ok ' ));
66+ $ authenticationFailureHandler = $ this ->getMockBuilder (AuthenticationFailureHandlerInterface::class)->getMock ();
67+ $ authenticationFailureHandler ->method ('onAuthenticationFailure ' )->willReturn (new Response ('ko ' ));
68+ }
6269
6370 $ this ->listener = new UsernamePasswordJsonAuthenticationListener ($ tokenStorage , $ authenticationManager , $ httpUtils , 'providerKey ' , $ authenticationSuccessHandler , $ authenticationFailureHandler , $ options );
6471 }
@@ -86,12 +93,28 @@ public function testSuccessIfRequestFormatIsJsonLD()
8693
8794 public function testHandleFailure ()
8895 {
89- $ this ->createListener ([], false );
96+ $ this ->createListener ([], false , true , false );
97+ $ request = new Request ([], [], [], [], [], ['HTTP_CONTENT_TYPE ' => 'application/json ' ], '{"username": "dunglas", "password": "foo"} ' );
98+ $ event = new RequestEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
99+
100+ ($ this ->listener )($ event );
101+ $ this ->assertSame (['error ' => 'An authentication exception occurred. ' ], json_decode ($ event ->getResponse ()->getContent (), true ));
102+ }
103+
104+ public function testTranslatedHandleFailure ()
105+ {
106+ $ translator = new Translator ('en ' );
107+ $ translator ->addLoader ('array ' , new ArrayLoader ());
108+ $ translator ->addResource ('array ' , ['An authentication exception occurred. ' => 'foo ' ], 'en ' , 'security ' );
109+
110+ $ this ->createListener ([], false , true , false );
111+ $ this ->listener ->setTranslator ($ translator );
112+
90113 $ request = new Request ([], [], [], [], [], ['HTTP_CONTENT_TYPE ' => 'application/json ' ], '{"username": "dunglas", "password": "foo"} ' );
91114 $ event = new RequestEvent ($ this ->getMockBuilder (KernelInterface::class)->getMock (), $ request , KernelInterface::MASTER_REQUEST );
92115
93116 ($ this ->listener )($ event );
94- $ this ->assertEquals ( ' ko ' , $ event ->getResponse ()->getContent ());
117+ $ this ->assertSame ([ ' error ' => ' foo ' ], json_decode ( $ event ->getResponse ()->getContent (), true ));
95118 }
96119
97120 public function testUsePath ()
0 commit comments