diff options
-rw-r--r-- | Http/Firewall/ContextListener.php | 3 | ||||
-rw-r--r-- | Http/Tests/Firewall/ContextListenerTest.php | 36 | ||||
-rw-r--r-- | README.md | 2 |
3 files changed, 37 insertions, 4 deletions
diff --git a/Http/Firewall/ContextListener.php b/Http/Firewall/ContextListener.php index 8df0d34..013586c 100644 --- a/Http/Firewall/ContextListener.php +++ b/Http/Firewall/ContextListener.php @@ -115,6 +115,9 @@ class ContextListener implements ListenerInterface return; } + $this->dispatcher->removeListener(KernelEvents::RESPONSE, array($this, 'onKernelResponse')); + $this->registered = false; + $request = $event->getRequest(); $session = $request->getSession(); diff --git a/Http/Tests/Firewall/ContextListenerTest.php b/Http/Tests/Firewall/ContextListenerTest.php index cb9685a..ae1199a 100644 --- a/Http/Tests/Firewall/ContextListenerTest.php +++ b/Http/Tests/Firewall/ContextListenerTest.php @@ -21,6 +21,7 @@ use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Http\Firewall\ContextListener; +use Symfony\Component\EventDispatcher\EventDispatcher; class ContextListenerTest extends \PHPUnit_Framework_TestCase { @@ -99,7 +100,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase new Response() ); - $listener = new ContextListener($tokenStorage, array(), 'session'); + $listener = new ContextListener($tokenStorage, array(), 'session', null, new EventDispatcher()); $listener->onKernelResponse($event); $this->assertTrue($session->isStarted()); @@ -118,7 +119,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase new Response() ); - $listener = new ContextListener(new TokenStorage(), array(), 'session'); + $listener = new ContextListener(new TokenStorage(), array(), 'session', null, new EventDispatcher()); $listener->onKernelResponse($event); $this->assertFalse($session->isStarted()); @@ -190,6 +191,35 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase $listener->handle($event); } + public function testOnKernelResponseListenerRemovesItself() + { + $tokenStorage = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); + $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent') + ->disableOriginalConstructor() + ->getMock(); + + $listener = new ContextListener($tokenStorage, array(), 'key123', null, $dispatcher); + + $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); + $request->expects($this->any()) + ->method('hasSession') + ->will($this->returnValue(true)); + + $event->expects($this->any()) + ->method('isMasterRequest') + ->will($this->returnValue(true)); + $event->expects($this->any()) + ->method('getRequest') + ->will($this->returnValue($request)); + + $dispatcher->expects($this->once()) + ->method('removeListener') + ->with(KernelEvents::RESPONSE, array($listener, 'onKernelResponse')); + + $listener->onKernelResponse($event); + } + public function testHandleRemovesTokenIfNoPreviousSessionWasFound() { $request = $this->getMock('Symfony\Component\HttpFoundation\Request'); @@ -229,7 +259,7 @@ class ContextListenerTest extends \PHPUnit_Framework_TestCase new Response() ); - $listener = new ContextListener($tokenStorage, array(), 'session'); + $listener = new ContextListener($tokenStorage, array(), 'session', null, new EventDispatcher()); $listener->onKernelResponse($event); return $session; @@ -19,5 +19,5 @@ Tests You can run the unit tests with the following command: $ cd path/to/Symfony/Component/Security/ - $ composer.phar install + $ composer install $ phpunit |