8000 Merge branch '6.0' into 6.1 · symfony/symfony@99fd22a · GitHub
[go: up one dir, main page]

Skip to content

Commit 99fd22a

Browse files
Merge branch '6.0' into 6.1
* 6.0: [Console] Better required argument check in InputArgument [EventDispatcher] Fix removing listeners when using first-class callable syntax
2 parents 0e6fc82 + cf09781 commit 99fd22a

File tree

8 files changed

+80
-6
lines changed

8 files changed

+80
-6
lines changed

src/Symfony/Component/Console/Input/InputArgument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function isArray(): bool
9595
*/
9696
public function setDefault(string|bool|int|float|array $default = null)
9797
{
98-
if (self::REQUIRED === $this->mode && null !== $default) {
98+
if ($this->isRequired() && null !== $default) {
9999
throw new LogicException('Cannot set a default value except for InputArgument::OPTIONAL mode.');
100100
}
101101

src/Symfony/Component/Console/Tests/Input/InputArgumentTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ public function testSetDefaultWithRequiredArgument()
9292
$argument->setDefault('default');
9393
}
9494

95+
public function testSetDefaultWithRequiredArrayArgument()
96+
{
97+
$this->expectException(\LogicException::class);
98+
$this->expectExceptionMessage('Cannot set a default value except for InputArgument::OPTIONAL mode.');
99+
$argument = new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY);
100+
$argument->setDefault([]);
101+
}
102+
95103
public function testSetDefaultWithArrayArgument()
96104
{
97105
$this->expectException(\LogicException::class);

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function removeListener(string $eventName, callable|array $listener)
7474
{
7575
if (isset($this->wrappedListeners[$eventName])) {
7676
foreach ($this->wrappedListeners[$eventName] as $index => $wrappedListener) {
77-
if ($wrappedListener->getWrappedListener() === $listener) {
77+
if ($wrappedListener->getWrappedListener() === $listener || ($listener instanceof \Closure && $wrappedListener->getWrappedListener() == $listener)) {
7878
$listener = $wrappedListener;
7979
unset($this->wrappedListeners[$eventName][$index]);
8080
break;
@@ -110,7 +110,7 @@ public function getListenerPriority(string $eventName, callable|array $listener)
110110
// in that case get the priority by wrapper
111111
if (isset($this->wrappedListeners[$eventName])) {
112112
foreach ($this->wrappedListeners[$eventName] as $wrappedListener) {
113-
if ($wrappedListener->getWrappedListener() === $listener) {
113+
if ($wrappedListener->getWrappedListener() === $listener || ($listener instanceof \Closure && $wrappedListener->getWrappedListener() == $listener)) {
114114
return $this->dispatcher->getListenerPriority($eventName, $wrappedListener);
115115
}
116116
}

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function getListenerPriority(string $eventName, callable|array $listener)
108108
$v[0] = $v[0]();
109109
$v[1] ??= '__invoke';
110110
}
111-
if ($v === $listener) {
111+
if ($v === $listener || ($listener instanceof \Closure && $v == $listener)) {
112112
return $priority;
113113
}
114114
}
@@ -164,7 +164,7 @@ public function removeListener(string $eventName, callable|array $listener)
164164
$v[0] = $v[0]();
165165
$v[1] ??= '__invoke';
166166
}
167-
if ($v === $listener) {
167+
if ($v === $listener || ($listener instanceof \Closure && $v == $listener)) {
168168
unset($listeners[$k], $this->sorted[$eventName], $this->optimized[$eventName]);
169169
}
170170
}

src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,35 @@ public function testMutatingWhilePropagationIsStopped()
405405

406406
$this->assertTrue($testLoaded);
407407
}
408+
409+
/**
410+
* @requires PHP 8.1
411+
*/
412+
public function testNamedClosures()
413+
{
414+
$listener = new TestEventListener();
415+
416+
$callback1 = $listener(...);
417+
$callback2 = $listener(...);
418+
$callback3 = (new TestEventListener())(...);
419+
420+
$this->assertNotSame($callback1, $callback2);
421+
$this->assertNotSame($callback1, $callback3);
422+
$this->assertNotSame($callback2, $callback3);
423+
$this->assertTrue($callback1 == $callback2);
424+
$this->assertFalse($callback1 == $callback3);
425+
426+
$this->dispatcher->addListener('foo', $callback1, 3);
427+
$this->dispatcher->addListener('foo', $callback2, 2);
428+
$this->dispatcher->addListener('foo', $callback3, 1);
429+
430+
$this->assertSame(3, $this->dispatcher->getListenerPriority('foo', $callback1));
431+
$this->assertSame(3, $this->dispatcher->getListenerPriority('foo', $callback2));
432+
433+
$this->dispatcher->removeListener('foo', $callback1);
434+
435+
$this->assertSame(['foo' => [$callback3]], $this->dispatcher->getListeners());
436+
}
408437
}
409438

410439
class CallableClass

src/Symfony/Component/Security/Http/Tests/Firewall/ContextListenerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,30 @@ public function testSessionIsNotReported()
322322
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST));
323323
}
324324

325+
public function testOnKernelResponseRemoveListener()
326+
{
327+
$tokenStorage = new TokenStorage();
328+
$tokenStorage->setToken(new UsernamePasswordToken(new InMemoryUser('test1', 'pass1'), 'phpunit', ['ROLE_USER']));
329+
330+
$request = new Request();
331+
$request->attributes->set('_security_firewall_run', '_security_session');
332+
333+
$session = new Session(new MockArraySessionStorage());
334+
$request->setSession($session);
335+
336+
$dispatcher = new EventDispatcher();
337+
$httpKernel = $this->createMock(HttpKernelInterface::class);
338+
339+
$listener = new ContextListener($tokenStorage, [], 'session', null, $dispatcher, null, $tokenStorage->getToken(...)));
340+
$this->assertEmpty($dispatcher->getListeners());
341+
342+
$listener(new RequestEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST));
343+
$this->assertNotEmpty($dispatcher->getListeners());
344+
345+
$listener->onKernelResponse(new ResponseEvent($httpKernel, $request, HttpKernelInterface::MASTER_REQUEST, new Response()));
346+
$this->assertEmpty($dispatcher->getListeners());
347+
}
348+
325349
protected function runSessionOnKernelResponse($newToken, $original = null)
326350
{
327351
$session = new Session(new MockArraySessionStorage());

src/Symfony/Component/Security/Http/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;
@@ -179,6 +180,18 @@ public function testLogoutException()
179180
$this->assertEquals(403, $event->getThrowable()->getStatusCode());
180181
}
181182

183+
public function testUnregister()
184+
{
185+
$listener = $this->createExceptionListener();
186+
$dispatcher = new EventDispatcher();
187+
188+
$listener->register($dispatcher);
189+
$this->assertNotEmpty($dispatcher->getListeners());
190+
191+
$listener->unregister($dispatcher);
192+
$this->assertEmpty($dispatcher->getListeners());
193+
}
194+
182195
public function getAccessDeniedExceptionProvider()
183196
{
184197
return [

src/Symfony/Component/Security/Http/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"psr/log": "^1|^2|^3"
3333
},
3434
"conflict": {
35-
"symfony/event-dispatcher": "<5.4",
35+
"symfony/event-dispatcher": "<5.4.9",
3636
"symfony/security-bundle": "<5.4",
3737
"symfony/security-csrf": "<5.4"
3838
},

0 commit comments

Comments
 (0)
0