diff --git a/src/Event/EventManager.php b/src/Event/EventManager.php index a22674eec1f..012dfecee79 100644 --- a/src/Event/EventManager.php +++ b/src/Event/EventManager.php @@ -277,6 +277,10 @@ public function off($eventKey, $callable = null) $this->_detachSubscriber($callable, $eventKey); return; } + if ($callable === null && is_string($eventKey)) { + unset($this->_listeners[$eventKey]); + return; + } if ($callable === null) { foreach (array_keys($this->_listeners) as $name) { $this->off($name, $eventKey); diff --git a/tests/TestCase/Event/EventManagerTest.php b/tests/TestCase/Event/EventManagerTest.php index 01e426efb45..34818480c99 100644 --- a/tests/TestCase/Event/EventManagerTest.php +++ b/tests/TestCase/Event/EventManagerTest.php @@ -226,6 +226,24 @@ public function testOffFromAll() $this->assertEquals([], $manager->listeners('fake.event')); } + /** + * Tests off'ing all listeners for an event + */ + public function testRemoveAllListeners() + { + $manager = new EventManager(); + $manager->on('fake.event', ['AClass', 'aMethod']); + $manager->on('another.event', ['priority' => 1], 'fakeFunction'); + + $manager->off('fake.event'); + + $expected = [ + ['callable' => 'fakeFunction'] + ]; + $this->assertEquals($expected, $manager->listeners('another.event')); + $this->assertEquals([], $manager->listeners('fake.event')); + } + /** * Tests detaching an event from a event key queue *