18
18
use Symfony \Component \Security \Http \Event \InteractiveLoginEvent ;
19
19
use Symfony \Component \Security \Http \SecurityEvents ;
20
20
21
- /**
22
- * @author Ryan Weaver <weaverryan@gmail.com>
23
- */
24
21
class GuardAuthenticatorHandlerTest extends \PHPUnit_Framework_TestCase
25
22
{
26
23
private $ tokenStorage ;
@@ -63,7 +60,41 @@ public function testHandleAuthenticationSuccess()
63
60
64
61
public function testHandleAuthenticationFailure ()
65
62
{
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
66
93
$ this ->tokenStorage ->expects ($ this ->once ())
94
+ ->method ('getToken ' )
95
+ ->will ($ this ->returnValue ($ token ));
96
+
97
+ $ this ->tokenStorage ->expects ($ shouldTokenBeCleared ? $ this ->once () : $ this ->never ())
67
98
->method ('setToken ' )
68
99
->with (null );
69
100
$ authException = new AuthenticationException ('Bad password! ' );
@@ -75,10 +106,21 @@ public function testHandleAuthenticationFailure()
75
106
->will ($ this ->returnValue ($ response ));
76
107
77
108
$ 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 );
79
110
$ this ->assertSame ($ response , $ actualResponse );
80
111
}
81
112
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
+
82
124
protected function setUp ()
83
125
{
84
126
$ this ->tokenStorage = $ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface ' );
0 commit comments