14
14
use Symfony \Component \Security \Core \Exception \AuthenticationException ;
15
15
use Symfony \Component \Security \Http \Firewall \RememberMeListener ;
16
16
use Symfony \Component \HttpFoundation \Request ;
17
+ use Symfony \Component \Security \Http \SecurityEvents ;
17
18
18
19
class RememberMeListenerTest extends \PHPUnit_Framework_TestCase
19
20
{
20
21
public function testOnCoreSecurityDoesNotTryToPopulateNonEmptySecurityContext ()
21
22
{
22
- list ($ listener , $ context , $ service ,,) = $ this ->getListener ();
23
+ list ($ listener , $ context ,, ,,) = $ this ->getListener ();
23
24
24
25
$ context
25
26
->expects ($ this ->once ())
@@ -99,6 +100,48 @@ public function testOnCoreSecurityIgnoresAuthenticationExceptionThrownByAuthenti
99
100
$ listener ->handle ($ event );
100
101
}
101
102
103
+ /**
104
+ * @expectedException Symfony\Component\Security\Core\Exception\AuthenticationException
105
+ * @expectedExceptionMessage Authentication failed.
106
+ */
107
+ public function testOnCoreSecurityIgnoresAuthenticationOptionallyRethrowsExceptionThrownAuthenticationManagerImplementation ()
108
+ {
109
+ list ($ listener , $ context , $ service , $ manager ,) = $ this ->getListener (false , false );
110
+
111
+ $ context
112
+ ->expects ($ this ->once ())
113
+ ->method ('getToken ' )
114
+ ->will ($ this ->returnValue (null ))
115
+ ;
116
+
117
+ $ service
118
+ ->expects ($ this ->once ())
119
+ ->method ('autoLogin ' )
120
+ ->will ($ this ->returnValue ($ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )))
121
+ ;
122
+
123
+ $ service
124
+ ->expects ($ this ->once ())
125
+ ->method ('loginFail ' )
126
+ ;
127
+
128
+ $ exception = new AuthenticationException ('Authentication failed. ' );
129
+ $ manager
130
+ ->expects ($ this ->once ())
131
+ ->method ('authenticate ' )
132
+ ->will ($ this ->throwException ($ exception ))
133
+ ;
134
+
135
+ $ event = $ this ->getGetResponseEvent ();
136
+ $ event
137
+ ->expects ($ this ->once ())
138
+ ->method ('getRequest ' )
139
+ ->will ($ this ->returnValue (new Request ()))
140
+ ;
141
+
142
+ $ listener ->handle ($ event );
143
+ }
144
+
102
145
public function testOnCoreSecurity ()
103
146
{
104
147
list ($ listener , $ context , $ service , $ manager ,) = $ this ->getListener ();
@@ -138,6 +181,55 @@ public function testOnCoreSecurity()
138
181
$ listener ->handle ($ event );
139
182
}
140
183
184
+ public function testOnCoreSecurityInteractiveLoginEventIsDispatchedIfDispatcherIsPresent ()
185
+ {
186
+ list ($ listener , $ context , $ service , $ manager ,, $ dispatcher ) = $ this ->getListener (true );
187
+
188
+ $ context
189
+ ->expects ($ this ->once ())
190
+ ->method ('getToken ' )
191
+ ->will ($ this ->returnValue (null ))
192
+ ;
193
+
194
+ $ token = $ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' );
195
+ $ service
196
+ ->expects ($ this ->once ())
197
+ ->method ('autoLogin ' )
198
+ ->will ($ this ->returnValue ($ token ))
199
+ ;
200
+
201
+ $ context
202
+ ->expects ($ this ->once ())
203
+ ->method ('setToken ' )
204
+ ->with ($ this ->equalTo ($ token ))
205
+ ;
206
+
207
+ $ manager
208
+ ->expects ($ this ->once ())
209
+ ->method ('authenticate ' )
210
+ ->will ($ this ->returnValue ($ token ))
211
+ ;
212
+
213
+ $ event = $ this ->getGetResponseEvent ();
214
+ $ request = new Request ();
215
+ $ event
216
+ ->expects ($ this ->once ())
217
+ ->method ('getRequest ' )
218
+ ->will ($ this ->returnValue ($ request ))
219
+ ;
220
+
221
+ $ dispatcher
222
+ ->expects ($ this ->once ())
223
+ ->method ('dispatch ' )
224
+ ->with (
225
+ SecurityEvents::INTERACTIVE_LOGIN ,
226
+ $ this ->isInstanceOf ('Symfony\Component\Security\Http\Event\InteractiveLoginEvent ' )
227
+ )
228
+ ;
229
+
230
+ $ listener ->handle ($ event );
231
+ }
232
+
141
233
protected function getGetResponseEvent ()
142
234
{
143
235
return $ this ->getMock ('Symfony\Component\HttpKernel\Event\GetResponseEvent ' , array (), array (), '' , false );
@@ -148,16 +240,18 @@ protected function getFilterResponseEvent()
148
240
return $ this ->getMock ('Symfony\Component\HttpKernel\Event\FilterResponseEvent ' , array (), array (), '' , false );
149
241
}
150
242
151
- protected function getListener ()
243
+ protected function getListener ($ withDispatcher = false , $ catchExceptions = true )
152
244
{
153
245
$ listener = new RememberMeListener (
154
246
$ context = $ this ->getContext (),
155
247
$ service = $ this ->getService (),
156
248
$ manager = $ this ->getManager (),
157
- $ logger = $ this ->getLogger ()
249
+ $ logger = $ this ->getLogger (),
250
+ $ dispatcher = ($ withDispatcher ? $ this ->getDispatcher () : null ),
251
+ $ catchExceptions
158
252
);
159
253
160
- return array ($ listener , $ context , $ service , $ manager , $ logger );
254
+ return array ($ listener , $ context , $ service , $ manager , $ logger, $ dispatcher );
161
255
}
162
256
163
257
protected function getLogger ()
@@ -177,8 +271,11 @@ protected function getService()
177
271
178
272
protected function getContext ()
179
273
{
180
- return $ this ->getMockBuilder ('Symfony\Component\Security\Core\SecurityContext ' )
181
- ->disableOriginalConstructor ()
182
- ->getMock ();
274
+ return $ this ->getMock ('Symfony\Component\Security\Core\SecurityContextInterface ' );
275
+ }
276
+
277
+ protected function getDispatcher ()
278
+ {
279
+ return $ this ->getMock ('Symfony\Component\EventDispatcher\EventDispatcherInterface ' );
183
280
}
184
281
}
0 commit comments