Skip to content

Commit f5b6fa2

Browse files
javernicolas-grekas
authored andcommitted
[EventDispatcher] Fix removing listeners when using first-class callable syntax
1 parent 36736d8 commit f5b6fa2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Tests/Firewall/ContextListenerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,30 @@ public function testWithPreviousNotStartedSession()
342342
$this->assertSame($usageIndex, $session->getUsageIndex());
343343
}
344344

345+
public function testOnKernelResponseRemoveListener()
346+
{
347+
$tokenStorage = new TokenStorage();
348+
$tokenStorage->setToken(new UsernamePasswordToken('test1', 'pass1', 'phpunit', ['ROLE_USER']));
349+
350+
$request = new Request();
351+
$request->attributes->set('_security_firewall_run', '_security_session');
352+
353+
$session = new Session(new MockArraySessionStorage());
354+
$request->setSession($session);
355+
356+
$dispatcher = new EventDispatcher();
357+
$httpKernel = $this->createMock(HttpKernelInterface::class);
358+
359+
$listener = new ContextListener($tokenStorage, [], 'session', null, $dispatcher, null, \Closure::fromCallable([$tokenStorage, 'getToken']));
360+
$this->assertEmpty($dispatcher->getListeners());
361+
362+
$listener(new RequestEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST));
363+
$this->assertNotEmpty($dispatcher->getListeners());
364+
365+
$listener->onKernelResponse(new ResponseEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST, new Response()));
366+
$this->assertEmpty($dispatcher->getListeners());
367+
}
368+
345369
protected function runSessionOnKernelResponse($newToken, $original = null)
346370
{
347371
$session = new Session(new MockArraySessionStorage());

Tests/Firewall/ExceptionListenerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Http\Tests\Firewall;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\EventDispatcher\EventDispatcher;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
@@ -170,6 +171,18 @@ public function testLogoutException()
170171
$this->assertEquals(403, $event->getThrowable()->getStatusCode());
171172
}
172173

174+
public function testUnregister()
175+
{
176+
$listener = $this->createExceptionListener();
177+
$dispatcher = new EventDispatcher();
178+
179+
$listener->register($dispatcher);
180+
$this->assertNotEmpty($dispatcher->getListeners());
181+
182+
$listener->unregister($dispatcher);
183+
$this->assertEmpty($dispatcher->getListeners());
184+
}
185+
173186
public function getAccessDeniedExceptionProvider()
174187
{
175188
return [

0 commit comments

Comments
 (0)