merged branch jmikola/remove-subscriber-with-priorities (PR #2014)

Commits
-------

39fabab [EventDispatcher] Fix removeSubscriber() to work with priority syntax

Discussion
----------

[EventDispatcher] Fix removeSubscriber() to work with priority syntax

Previously only addSubscriber() was being tested with priority syntax. This adds a unit test for removeSubscriber() and fixes a bug that would have caused it to fail.
This commit is contained in:
Fabien Potencier 2011-08-23 22:51:37 +02:00
commit 9231f1d1dd
2 changed files with 11 additions and 2 deletions

View File

@ -127,8 +127,8 @@ class EventDispatcher implements EventDispatcherInterface
*/
public function removeSubscriber(EventSubscriberInterface $subscriber)
{
foreach ($subscriber->getSubscribedEvents() as $eventName => $method) {
$this->removeListener($eventName, array($subscriber, $method));
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
$this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0]));
}
}

View File

@ -198,6 +198,15 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
$this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
}
public function testRemoveSubscriberWithPriorities()
{
$eventSubscriber = new TestEventSubscriberWithPriorities();
$this->dispatcher->addSubscriber($eventSubscriber);
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
$this->dispatcher->removeSubscriber($eventSubscriber);
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
}
}
class TestEventListener