Navigation Menu

Skip to content

Commit

Permalink
[Security] Remove ContextListener's onKernelResponse listener as it i…
Browse files Browse the repository at this point in the history
…s used
  • Loading branch information
davedevelopment authored and fabpot committed Feb 5, 2015
1 parent b8e4b4a commit 380d805
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Expand Up @@ -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');
}
Expand Down
Expand Up @@ -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
{
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 380d805

Please sign in to comment.