From ac536c333f676c6f55336fcfad2cb414a5a7c206 Mon Sep 17 00:00:00 2001 From: Dave Marshall Date: Tue, 20 Jan 2015 13:50:10 +0000 Subject: [PATCH 1/2] [Security] Remove ContextListener's onKernelResponse listener as it is used --- Http/Firewall/ContextListener.php | 3 ++ Tests/Http/Firewall/ContextListenerTest.php | 36 +++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Http/Firewall/ContextListener.php b/Http/Firewall/ContextListener.php index 3ea651238..c80fff331 100644 --- a/Http/Firewall/ContextListener.php +++ b/Http/Firewall/ContextListener.php @@ -114,6 +114,9 @@ public function onKernelResponse(FilterResponseEvent $event) return; } + $this->dispatcher->removeListener(KernelEvents::RESPONSE, array($this, 'onKernelResponse')); + $this->registered = false; + if (null !== $this->logger) { $this->logger->debug('Write SecurityContext in the session'); } diff --git a/Tests/Http/Firewall/ContextListenerTest.php b/Tests/Http/Firewall/ContextListenerTest.php index f44c60aa0..6b4ef7310 100644 --- a/Tests/Http/Firewall/ContextListenerTest.php +++ b/Tests/Http/Firewall/ContextListenerTest.php @@ -21,6 +21,7 @@ use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\SecurityContext; use Symfony\Component\Security\Http\Firewall\ContextListener; +use Symfony\Component\EventDispatcher\EventDispatcher; class ContextListenerTest extends \PHPUnit_Framework_TestCase { @@ -111,7 +112,7 @@ public function testOnKernelResponseWithoutSession() new Response() ); - $listener = new ContextListener($this->securityContext, array(), 'session'); + $listener = new ContextListener($this->securityContext, array(), 'session', null, new EventDispatcher()); $listener->onKernelResponse($event); $this->assertTrue($session->isStarted()); @@ -130,7 +131,7 @@ public function testOnKernelResponseWithoutSessionNorToken() new Response() ); - $listener = new ContextListener($this->securityContext, array(), 'session'); + $listener = new ContextListener($this->securityContext, array(), 'session', null, new EventDispatcher()); $listener->onKernelResponse($event); $this->assertFalse($session->isStarted()); @@ -202,6 +203,35 @@ public function testHandleAddsKernelResponseListener() $listener->handle($event); } + public function testOnKernelResponseListenerRemovesItself() + { + $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface'); + $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent') + ->disableOriginalConstructor() + ->getMock(); + + $listener = new ContextListener($context, 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('getRequestType') + ->will($this->returnValue(HttpKernelInterface::MASTER_REQUEST)); + $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'); @@ -240,7 +270,7 @@ protected function runSessionOnKernelResponse($newToken, $original = null) new Response() ); - $listener = new ContextListener($this->securityContext, array(), 'session'); + $listener = new ContextListener($this->securityContext, array(), 'session', null, new EventDispatcher()); $listener->onKernelResponse($event); return $session; From 41890dcab00f2e1de2080472bd9d57e242e3db76 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 8 Feb 2015 08:41:14 +0100 Subject: [PATCH 2/2] renamed composer.phar to composer to be consistent with the Symfony docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 53efa5eec..21f8a24d3 100644 --- a/README.md +++ b/README.md @@ -19,5 +19,5 @@ Resources You can run the unit tests with the following command: $ cd path/to/Symfony/Component/Security/ - $ composer.phar install + $ composer install $ phpunit