[EventDispatcher] don't count empty listeners
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.
This commit is contained in:
parent
c548bd861a
commit
fdbb04a6ac
@ -74,7 +74,7 @@ class EventDispatcher implements EventDispatcherInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->sorted;
|
return array_filter($this->sorted);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -23,6 +23,9 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
|
|||||||
const preBar = 'pre.bar';
|
const preBar = 'pre.bar';
|
||||||
const postBar = 'post.bar';
|
const postBar = 'post.bar';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Symfony\Component\EventDispatcher\EventDispatcher
|
||||||
|
*/
|
||||||
private $dispatcher;
|
private $dispatcher;
|
||||||
|
|
||||||
private $listener;
|
private $listener;
|
||||||
@ -261,6 +264,28 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
|
|||||||
$dispatcher->removeListener('bug.62976', function () {});
|
$dispatcher->removeListener('bug.62976', function () {});
|
||||||
$this->assertTrue($dispatcher->hasListeners('bug.62976'));
|
$this->assertTrue($dispatcher->hasListeners('bug.62976'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testHasListenersWhenAddedCallbackListenerIsRemoved()
|
||||||
|
{
|
||||||
|
$listener = function () {};
|
||||||
|
$this->dispatcher->addListener('foo', $listener);
|
||||||
|
$this->dispatcher->removeListener('foo', $listener);
|
||||||
|
$this->assertFalse($this->dispatcher->hasListeners());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetListenersWhenAddedCallbackListenerIsRemoved()
|
||||||
|
{
|
||||||
|
$listener = function () {};
|
||||||
|
$this->dispatcher->addListener('foo', $listener);
|
||||||
|
$this->dispatcher->removeListener('foo', $listener);
|
||||||
|
$this->assertSame(array(), $this->dispatcher->getListeners());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled()
|
||||||
|
{
|
||||||
|
$this->assertFalse($this->dispatcher->hasListeners('foo'));
|
||||||
|
$this->assertFalse($this->dispatcher->hasListeners());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CallableClass
|
class CallableClass
|
||||||
|
Reference in New Issue
Block a user