1818use Symfony \Component \Security \Http \Event \InteractiveLoginEvent ;
1919use Symfony \Component \Security \Http \SecurityEvents ;
2020
21- /**
22- * @author Ryan Weaver <weaverryan@gmail.com>
23- */
2421class GuardAuthenticatorHandlerTest extends \PHPUnit_Framework_TestCase
2522{
2623 private $ tokenStorage ;
@@ -63,7 +60,41 @@ public function testHandleAuthenticationSuccess()
6360
6461 public function testHandleAuthenticationFailure ()
6562 {
63+ // setToken() not called - getToken() will return null, so there's nothing to clear
64+ $ this ->tokenStorage ->expects ($ this ->never ())
65+ ->method ('setToken ' )
66+ ->with (null );
67+ $ authException = new AuthenticationException ('Bad password! ' );
68+
69+ $ response = new Response ('Try again, but with the right password! ' );
70+ $ this ->guardAuthenticator ->expects ($ this ->once ())
71+ ->method ('onAuthenticationFailure ' )
72+ ->with ($ this ->request , $ authException )
73+ ->will ($ this ->returnValue ($ response ));
74+
75+ $ handler = new GuardAuthenticatorHandler ($ this ->tokenStorage , $ this ->dispatcher );
76+ $ actualResponse = $ handler ->handleAuthenticationFailure ($ authException , $ this ->request , $ this ->guardAuthenticator , 'firewall_provider_key ' );
77+ $ this ->assertSame ($ response , $ actualResponse );
78+ }
79+
80+ /**
81+ * @dataProvider getTokenClearingTests
82+ */
83+ public function testHandleAuthenticationClearsToken ($ tokenClass , $ tokenProviderKey , $ actualProviderKey , $ shouldTokenBeCleared )
84+ {
85+ $ token = $ this ->getMockBuilder ($ tokenClass )
86+ ->disableOriginalConstructor ()
87+ ->getMock ();
88+ $ token ->expects ($ this ->any ())
89+ ->method ('getProviderKey ' )
90+ ->will ($ this ->returnValue ($ tokenProviderKey ));
91+
92+ // make the $token be the current token
6693 $ this ->tokenStorage ->expects ($ this ->once ())
94+ ->method ('getToken ' )
95+ ->will ($ this ->returnValue ($ token ));
96+
97+ $ this ->tokenStorage ->expects ($ shouldTokenBeCleared ? $ this ->once () : $ this ->never ())
6798 ->method ('setToken ' )
6899 ->with (null );
69100 $ authException = new AuthenticationException ('Bad password! ' );
@@ -75,10 +106,21 @@ public function testHandleAuthenticationFailure()
75106 ->will ($ this ->returnValue ($ response ));
76107
77108 $ handler = new GuardAuthenticatorHandler ($ this ->tokenStorage , $ this ->dispatcher );
78- $ actualResponse = $ handler ->handleAuthenticationFailure ($ authException , $ this ->request , $ this ->guardAuthenticator );
109+ $ actualResponse = $ handler ->handleAuthenticationFailure ($ authException , $ this ->request , $ this ->guardAuthenticator , $ actualProviderKey );
79110 $ this ->assertSame ($ response , $ actualResponse );
80111 }
81112
113+ public function getTokenClearingTests ()
114+ {
115+ $ tests = array ();
116+ // correct token class and matching firewall => clear the token
117+ $ tests [] = array ('Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken ' , 'the_firewall_key ' , 'the_firewall_key ' , true );
118+ $ tests [] = array ('Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken ' , 'the_firewall_key ' , 'different_key ' , false );
119+ $ tests [] = array ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' , 'the_firewall_key ' , 'the_firewall_key ' , false );
120+
121+ return $ tests ;
122+ }
123+
82124 protected function setUp ()
83125 {
84126 $ this ->tokenStorage = $ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' );
0 commit comments