Browse Source

Fix offing all event listeners for a specific event

Robbert Noordzij 10 years ago
parent
commit
7e67fe6409
2 changed files with 22 additions and 0 deletions
  1. 4 0
      src/Event/EventManager.php
  2. 18 0
      tests/TestCase/Event/EventManagerTest.php

+ 4 - 0
src/Event/EventManager.php

@@ -277,6 +277,10 @@ class EventManager
             $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);

+ 18 - 0
tests/TestCase/Event/EventManagerTest.php

@@ -227,6 +227,24 @@ class EventManagerTest extends TestCase
     }
 
     /**
+     * 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
      *
      * @return void