Skip to content
Browse files

Merge branch 'master' of https://github.com/Lansoweb/zf2 into hotfix/…

…2686
  • Loading branch information...
2 parents d89ae32 + 0688eed commit 7c6ab3287d591cd4037c490994eb090ed086b05f @weierophinney weierophinney committed Oct 8, 2012
View
4 library/Zend/EventManager/EventManager.php
@@ -498,6 +498,10 @@ protected function getSharedListeners($event)
}
$identifiers = $this->getIdentifiers();
+ //Add wildcard id to the search, if not already added
+ if (!in_array('*', $identifiers)) {
+ $identifiers[] = '*';
+ }
$sharedListeners = array();
foreach ($identifiers as $id) {
View
4 library/Zend/EventManager/SharedEventManager.php
@@ -101,6 +101,10 @@ public function detach($id, CallbackHandler $listener)
public function getEvents($id)
{
if (!array_key_exists($id, $this->identifiers)) {
+ //Check if there are any id wildcards listeners
+ if ('*' != $id && array_key_exists('*', $this->identifiers)) {
+ return $this->identifiers['*']->getEvents();
+ }
return false;
}
return $this->identifiers[$id]->getEvents();
View
70 tests/ZendTest/EventManager/StaticEventManagerTest.php
@@ -179,6 +179,13 @@ public function testCanGetEventsByResource()
$this->assertEquals(array('bar'), $events->getEvents('foo'));
}
+ public function testCanGetEventsByWildcard()
+ {
+ $events = StaticEventManager::getInstance();
+ $events->attach('*', 'bar', array($this, __FUNCTION__));
+ $this->assertEquals(array('bar'), $events->getEvents('foo'));
+ }
+
public function testCanGetListenersByResourceAndEvent()
{
$events = StaticEventManager::getInstance();
@@ -188,6 +195,23 @@ public function testCanGetListenersByResourceAndEvent()
$this->assertEquals(1, count($listeners));
}
+ public function testCanNotGetListenersByResourceAndEventWithWildcard()
+ {
+ $events = StaticEventManager::getInstance();
+ $events->attach('*', 'bar', array($this, __FUNCTION__));
+ $listeners = $events->getListeners('foo', 'bar');
+ $this->assertFalse($listeners);
+ }
+
+ public function testCanGetListenersByWildcardAndEvent()
+ {
+ $events = StaticEventManager::getInstance();
+ $events->attach('*', 'bar', array($this, __FUNCTION__));
+ $listeners = $events->getListeners('*', 'bar');
+ $this->assertInstanceOf('Zend\Stdlib\PriorityQueue', $listeners);
+ $this->assertEquals(1, count($listeners));
+ }
+
public function testCanClearListenersByResource()
{
$events = StaticEventManager::getInstance();
@@ -237,4 +261,50 @@ public function testListenersAttachedToAnyIdentifierProvidedToEventManagerWillBe
$manager->trigger('bar', $this, array());
$this->assertEquals(2, $test->triggered);
}
+
+ public function testListenersAttachedToWildcardsWillBeTriggered()
+ {
+ $identifiers = array('foo', 'bar');
+ $events = StaticEventManager::getInstance();
+ $manager = new EventManager($identifiers);
+ $manager->setSharedManager($events);
+
+ $test = new \stdClass;
+ $test->triggered = 0;
+ $events->attach('*', 'bar', function($e) use ($test) {
+ $test->triggered++;
+ });
+ //Tests one can have multiple wildcards attached
+ $events->attach('*', 'bar', function($e) use ($test) {
+ $test->triggered++;
+ });
+ $manager->trigger('bar', $this, array());
+ $this->assertEquals(2, $test->triggered);
+ }
+
+ public function testListenersAttachedToAnyIdentifierProvidedToEventManagerOrWildcardsWillBeTriggered()
+ {
+ $identifiers = array('foo', 'bar');
+ $events = StaticEventManager::getInstance();
+ $manager = new EventManager($identifiers);
+ $manager->setSharedManager($events);
+
+ $test = new \stdClass;
+ $test->triggered = 0;
+ $events->attach('foo', 'bar', function($e) use ($test) {
+ $test->triggered++;
+ });
+ $events->attach('bar', 'bar', function($e) use ($test) {
+ $test->triggered++;
+ });
+ $events->attach('*', 'bar', function($e) use ($test) {
+ $test->triggered++;
+ });
+ //Tests one can have multiple wildcards attached
+ $events->attach('*', 'bar', function($e) use ($test) {
+ $test->triggered++;
+ });
+ $manager->trigger('bar', $this, array());
+ $this->assertEquals(4, $test->triggered);
+ }
}

0 comments on commit 7c6ab32

Please sign in to comment.
Something went wrong with that request. Please try again.