Skip to content

Commit

Permalink
Ticket 4059
Browse files Browse the repository at this point in the history
-  Added EventManager::prioritisedListeners() function to alow the global event manager to return unprocessed listeners array
  • Loading branch information
Andy Hobbs authored and Andy Hobbs committed Sep 13, 2013
1 parent e1db5b4 commit 699d5ff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
14 changes: 13 additions & 1 deletion lib/Cake/Event/CakeEventManager.php
Expand Up @@ -263,7 +263,7 @@ public function dispatch($event) {
public function listeners($eventKey) {
$globalListeners = array();
if (!$this->_isGlobal) {
$globalListeners = self::instance()->listeners($eventKey);
$globalListeners = self::instance()->prioritisedListeners($eventKey);
}

if (empty($this->_listeners[$eventKey]) && empty($globalListeners)) {
Expand All @@ -288,4 +288,16 @@ public function listeners($eventKey) {
return $result;
}

/**
* Returns the listeners for the specified event key indexed by priority
*
* @param string $eventKey
* @return array
*/
public function prioritisedListeners($eventKey) {
if (empty($this->_listeners[$eventKey])) {
return array();
}
return $this->_listeners[$eventKey];
}
}
31 changes: 27 additions & 4 deletions lib/Cake/Test/Case/Event/CakeEventManagerTest.php
Expand Up @@ -213,6 +213,29 @@ public function testDispatch() {
$manager->dispatch($event);
}

/**
* Tests event dispatching
*
* @return void
*/
public function testDispatchClosure() {
$this->skipIf(
version_compare(PHP_VERSION, '5.3.0', '<'),
'These tests fail in PHP version < 5.3'
);

$manager = new CakeEventManager;
$listener = $this->getMock('CakeEventTestListener');
$anotherListener = $this->getMock('CakeEventTestListener');

$manager->attach(function($testEvent) use ($listener) { $listener->listenerFunction($testEvent); }, 'fake.event');

$event = new CakeEvent('fake.event');

$listener->expects($this->once())->method('listenerFunction')->with($event);
$manager->dispatch($event);
}

/**
* Tests event dispatching using event key name
*
Expand Down Expand Up @@ -388,12 +411,12 @@ public function testGlobalDispatcherGetter() {
* @return void
*/
public function testDispatchWithGlobal() {
$generalManager = $this->getMock('CakeEventManager', array('listeners'));
$generalManager = $this->getMock('CakeEventManager', array('prioritisedListeners'));
$manager = new CakeEventManager;
$event = new CakeEvent('fake.event');
CakeEventManager::instance($generalManager);

$generalManager->expects($this->once())->method('listeners')->with('fake.event');
$generalManager->expects($this->once())->method('prioritisedListeners')->with('fake.event');
$manager->dispatch($event);
}

Expand All @@ -409,7 +432,7 @@ public function testStopPropagation() {

CakeEventManager::instance($generalManager);
$generalManager->expects($this->any())
->method('listeners')
->method('prioritisedListeners')
->with('fake.event')
->will($this->returnValue(array()));

Expand All @@ -436,7 +459,7 @@ public function testDispatchPrioritizedWithGlobal() {

CakeEventManager::instance($generalManager);
$generalManager->expects($this->any())
->method('listeners')
->method('prioritisedListeners')
->with('fake.event')
->will($this->returnValue(
array(11 => array(
Expand Down

0 comments on commit 699d5ff

Please sign in to comment.