diff --git a/src/Kdyby/Events/EventManager.php b/src/Kdyby/Events/EventManager.php index cc8c1b0..771285e 100644 --- a/src/Kdyby/Events/EventManager.php +++ b/src/Kdyby/Events/EventManager.php @@ -218,12 +218,16 @@ public function removeEventListener($unsubscribe, $subscriber = NULL) foreach ($this->listeners[$eventName] as $priority => $listeners) { foreach ($listeners as $k => $listener) { if (!($listener == $subscriber || (is_array($listener) && $listener[0] == $subscriber))) { - continue(2); + continue; } $key = $k; break; } + if (!isset($key)) { + continue; + } + unset($this->listeners[$eventName][$priority][$key]); if (empty($this->listeners[$eventName][$priority])) { unset($this->listeners[$eventName][$priority]); @@ -237,6 +241,7 @@ public function removeEventListener($unsubscribe, $subscriber = NULL) unset($this->sorted[$eventName]); } + unset($key); } } } diff --git a/tests/KdybyTests/Events/EventManager.phpt b/tests/KdybyTests/Events/EventManager.phpt index 6872e34..8b7ef49 100644 --- a/tests/KdybyTests/Events/EventManager.phpt +++ b/tests/KdybyTests/Events/EventManager.phpt @@ -161,6 +161,21 @@ class EventManagerTest extends Tester\TestCase } + + public function testRemovingListenersIssue90() + { + $listener1 = new EventListenerMock(); + $listener2 = new EventListenerMock2(); + $this->manager->addEventListener('onFoo', $listener1); + $this->manager->addEventListener('onFoo', $listener2); + Assert::count(2, $this->manager->getListeners('onFoo')); + + $this->manager->removeEventListener('onFoo', $listener2); + Assert::count(1, $this->manager->getListeners('onFoo')); + Assert::same(['onFoo' => [$listener1]], $this->manager->getListeners()); + } + + public function testListenerDontHaveRequiredMethodException() { $evm = $this->manager;