8000 bug #11475 [EventDispatcher] don't count empty listeners (xabbuh) · symfony/symfony@24cd425 · GitHub
[go: up one dir, main page]

Skip to content

Commit 24cd425

Browse files
committed
bug #11475 [EventDispatcher] don't count empty listeners (xabbuh)
This PR was merged into the 2.3 branch. Discussion ---------- [EventDispatcher] don't count empty listeners | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11444 | License | MIT | Doc PR | When event listeners for certain events are removed from the event dispatcher, empty arrays are not being removed. Therefore, counting on empty arrays leads to wrong results of the hasListeners() method. Thanks to @mlindenb for discovering this an proposing a solution. Commits ------- fdbb04a [EventDispatcher] don't count empty listeners
2 parents ff4a37f + fdbb04a commit 24cd425

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getListeners($eventName = null)
7474
}
7575
}
7676

77-
return $this->sorted;
77+
return array_filter($this->sorted);
7878
}
7979

8080
/**

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
2323
const preBar = 'pre.bar';
2424
const postBar = 'post.bar';
2525

26+
/**
27+
* @var \Symfony\Component\EventDispatcher\EventDispatcher
28+
*/
2629
private $dispatcher;
2730

2831
private $listener;
@@ -261,6 +264,28 @@ public function testWorkaroundForPhpBug62976()
261264
$dispatcher->removeListener('bug.62976', function () {});
262265
$this->assertTrue($dispatcher->hasListeners('bug.62976'));
263266
}
267+
268+
public function testHasListenersWhenAddedCallbackListenerIsRemoved()
269+
{
270+
$listener = function () {};
271+
$this->dispatcher->addListener('foo', $listener);
272+
$this->dispatcher->removeListener('foo', $listener);
273+
$this->assertFalse($this->dispatcher->hasListeners());
274+
}
275+
276+
public function testGetListenersWhenAddedCallbackListenerIsRemoved()
277+
{
278+
$listener = function () {};
279+
$this->dispatcher->addListener('foo', $listener);
280+
$this->dispatcher->removeListener('foo', $listener);
281+
$this->assertSame(array(), $this->dispatcher->getListeners());
282+
}
283+
284+
public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled()
285+
{
286+
$this->assertFalse($this->dispatcher->hasListeners('foo'));
287+
$this->assertFalse($this->dispatcher->hasListeners());
288+
}
264289
}
265290

266291
class CallableClass

0 commit comments

Comments
 (0)
0