21
21
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
22
22
use Symfony \Component \Security \Core \SecurityContext ;
23
23
use Symfony \Component \Security \Http \Firewall \ContextListener ;
24
+ use Symfony \Component \EventDispatcher \EventDispatcher ;
24
25
25
26
class ContextListenerTest extends \PHPUnit_Framework_TestCase
26
27
{
@@ -111,7 +112,7 @@ public function testOnKernelResponseWithoutSession()
111
112
new Response ()
112
113
);
113
114
114
- $ listener = new ContextListener ($ this ->securityContext , array (), 'session ' );
115
+ $ listener = new ContextListener ($ this ->securityContext , array (), 'session ' , null , new EventDispatcher () );
115
116
$ listener ->onKernelResponse ($ event );
116
117
117
118
$ this ->assertTrue ($ session ->isStarted ());
@@ -130,7 +131,7 @@ public function testOnKernelResponseWithoutSessionNorToken()
130
131
new Response ()
131
132
);
132
133
133
- $ listener = new ContextListener ($ this ->securityContext , array (), 'session ' );
134
+ $ listener = new ContextListener ($ this ->securityContext , array (), 'session ' , null , new EventDispatcher () );
134
135
$ listener ->onKernelResponse ($ event );
135
136
136
137
$ this ->assertFalse ($ session ->isStarted ());
@@ -202,6 +203,35 @@ public function testHandleAddsKernelResponseListener()
202
203
$ listener ->handle ($ event );
203
204
}
204
205
206
+ public function testOnKernelResponseListenerRemovesItself ()
207
+ {
208
+ $ context = $ this ->getMock ('Symfony\Component\Security\Core\SecurityContextInterface ' );
209
+ $ dispatcher = $ this ->getMock ('Symfony\Component\EventDispatcher\EventDispatcherInterface ' );
210
+ $ event = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Event\FilterResponseEvent ' )
211
+ ->disableOriginalConstructor ()
212
+ ->getMock ();
213
+
214
+ $ listener = new ContextListener ($ context , array (), 'key123 ' , null , $ dispatcher );
215
+
216
+ $ request = $ this ->getMock ('Symfony\Component\HttpFoundation\Request ' );
217
+ $ request ->expects ($ this ->any ())
218
+ ->method ('hasSession ' )
219
+ ->will ($ this ->returnValue (true ));
220
+
221
+ $ event ->expects ($ this ->any ())
222
+ ->method ('getRequestType ' )
223
+ ->will ($ this ->returnValue (HttpKernelInterface::MASTER_REQUEST ));
224
+ $ event ->expects ($ this ->any ())
225
+ ->method ('getRequest ' )
226
+ ->will ($ this ->returnValue ($ request ));
227
+
228
+ $ dispatcher ->expects ($ this ->once ())
229
+ ->method ('removeListener ' )
230
+ ->with (KernelEvents::RESPONSE , array ($ listener , 'onKernelResponse ' ));
231
+
232
+ $ listener ->onKernelResponse ($ event );
233
+ }
234
+
205
235
public function testHandleRemovesTokenIfNoPreviousSessionWasFound ()
206
236
{
207
237
$ request = $ this ->getMock ('Symfony\Component\HttpFoundation\Request ' );
@@ -240,7 +270,7 @@ protected function runSessionOnKernelResponse($newToken, $original = null)
240
270
new Response ()
241
271
);
242
272
243
- $ listener = new ContextListener ($ this ->securityContext , array (), 'session ' );
273
+ $ listener = new ContextListener ($ this ->securityContext , array (), 'session ' , null , new EventDispatcher () );
244
274
$ listener ->onKernelResponse ($ event );
245
275
246
276
return $ session ;
0 commit comments