Skip to content

Commit

Permalink
Merge pull request #9334 from jeremyharris/global-event-lists
Browse files Browse the repository at this point in the history
Tracking events on global list
  • Loading branch information
markstory committed Aug 25, 2016
2 parents 8a3b47f + 2a9ff7e commit cc1cbdb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/Event/EventManager.php
Expand Up @@ -371,11 +371,16 @@ public function dispatch($event)
}

$listeners = $this->listeners($event->name());
if (empty($listeners)) {
if ($this->_trackEvents) {
$this->addEventToList($event);
}

if ($this->_trackEvents) {
$this->addEventToList($event);
}

if (!$this->_isGlobal && static::instance()->isTrackingEvents()) {
static::instance()->addEventToList($event);
}

if (empty($listeners)) {
return $event;
}

Expand All @@ -392,10 +397,6 @@ public function dispatch($event)
}
}

if ($this->_trackEvents) {
$this->addEventToList($event);
}

return $event;
}

Expand Down Expand Up @@ -533,6 +534,16 @@ public function trackEvents($enabled)
$this->_trackEvents = (bool)$enabled;
}

/**
* Returns whether this manager is set up to track events
*
* @return bool
*/
public function isTrackingEvents()
{
return $this->_trackEvents && $this->_eventList;
}

/**
* Enables the listing of dispatched events.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/TestCase/Event/EventManagerTest.php
Expand Up @@ -760,4 +760,52 @@ public function testGetDispatchedEvents()
$result = $manager->getEventList();
$this->assertNull($result);
}

/**
* Test that locally dispatched events are also added to the global manager's event list
*
* @return void
* @triggers Event $this
*/
public function testGetLocallyDispatchedEventsFromGlobal()
{
$localList = new EventList();
$globalList = new EventList();

$globalManager = EventManager::instance();
$globalManager->setEventList($globalList);

$localManager = new EventManager();
$localManager->setEventList($localList);

$globalEvent = new Event('GlobalEvent', $this);
$globalManager->dispatch($globalEvent);

$localEvent = new Event('LocalEvent', $this);
$localManager->dispatch($localEvent);

$this->assertTrue($globalList->hasEvent('GlobalEvent'));
$this->assertFalse($localList->hasEvent('GlobalEvent'));
$this->assertTrue($localList->hasEvent('LocalEvent'));
$this->assertTrue($globalList->hasEvent('LocalEvent'));
}

/**
* Test isTrackingEvents
*
* @return void
*/
public function testIsTrackingEvents()
{
$this->assertFalse(EventManager::instance()->isTrackingEvents());

$manager = new EventManager();
$manager->setEventList(new EventList());

$this->assertTrue($manager->isTrackingEvents());

$manager->trackEvents(false);

$this->assertFalse($manager->isTrackingEvents());
}
}

0 comments on commit cc1cbdb

Please sign in to comment.