Skip to content

Commit

Permalink
Fixing test and implementing return values for events
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 26, 2011
1 parent 078a2df commit ffa12f4
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 26 deletions.
6 changes: 2 additions & 4 deletions lib/Cake/Controller/ComponentCollection.php
Expand Up @@ -117,10 +117,8 @@ public function load($component, $settings = array()) {
*/
public function implementedEvents() {
return array(
'Controller.startup' => array(
array('callable' => 'trigger', 'priority' => 9),
array('callable' => 'trigger')
),
'Controller.initialize' => array('callable' => 'trigger'),
'Controller.startup' => array('callable' => 'trigger'),
'Controller.beforeRender' => array('callable' => 'trigger'),
'Controller.beforeRedirect' => array('callable' => 'trigger'),
'Controller.shutdown' => array('callable' => 'trigger', 'priority' => 9),
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Controller/Controller.php
Expand Up @@ -591,7 +591,6 @@ public function implementedEvents() {
return array(
'Controller.startup' => 'beforeFilter',
'Controller.beforeRender' => 'beforeRender',
'Controller.beforeRedirect' => array('callable' => 'beforeRedirect', 'passParams' => true),
'Controller.shutdown' => 'afterFilter'
);
}
Expand Down Expand Up @@ -643,6 +642,7 @@ public function getEventManager() {
* @return void
*/
public function startupProcess() {
$this->getEventManager()->dispatch(new CakeEvent('Controller.initialize', $this));
$this->getEventManager()->dispatch(new CakeEvent('Controller.startup', $this));
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Cake/Event/CakeEvent.php
Expand Up @@ -46,6 +46,12 @@ class CakeEvent {
*/
public $data = null;

/**
* Property used to retain the result value of the event listeners
*
* @var mixed $result
*/
public $result = null;

/**
* Flags an event as stopped or not, default is false
Expand Down
10 changes: 8 additions & 2 deletions lib/Cake/Event/CakeEventManager.php
Expand Up @@ -240,9 +240,15 @@ public function dispatch($event) {
break;
}
if ($listener['passParams'] === true) {
call_user_func_array($listener['callable'], $event->data);
$result = call_user_func_array($listener['callable'], $event->data);
} else {
call_user_func($listener['callable'], $event);
$result = call_user_func($listener['callable'], $event);
}
if ($result === false) {
$event->stopPropagation();
}
if ($result !== null) {
$event->result = $result;
}
continue;
}
Expand Down
36 changes: 19 additions & 17 deletions lib/Cake/Test/Case/Console/Command/ApiShellTest.php
Expand Up @@ -65,23 +65,25 @@ public function testMethodNameDetection () {
'8. constructClasses()',
'9. disableCache()',
'10. flash($message, $url, $pause = 1, $layout = \'flash\')',
'11. header($status)',
'12. httpCodes($code = NULL)',
'13. invokeAction($request)',
'14. loadModel($modelClass = NULL, $id = NULL)',
'15. paginate($object = NULL, $scope = array (), $whitelist = array ())',
'16. postConditions($data = array (), $op = NULL, $bool = \'AND\', $exclusive = false)',
'17. redirect($url, $status = NULL, $exit = true)',
'18. referer($default = NULL, $local = false)',
'19. render($view = NULL, $layout = NULL)',
'20. scaffoldError($method)',
'21. set($one, $two = NULL)',
'22. setAction($action)',
'23. setRequest($request)',
'24. shutdownProcess()',
'25. startupProcess()',
'26. validate()',
'27. validateErrors()'
'11. getEventManager()',
'12. header($status)',
'13. httpCodes($code = NULL)',
'14. implementedEvents()',
'15. invokeAction($request)',
'16. loadModel($modelClass = NULL, $id = NULL)',
'17. paginate($object = NULL, $scope = array (), $whitelist = array ())',
'18. postConditions($data = array (), $op = NULL, $bool = \'AND\', $exclusive = false)',
'19. redirect($url, $status = NULL, $exit = true)',
'20. referer($default = NULL, $local = false)',
'21. render($view = NULL, $layout = NULL)',
'22. scaffoldError($method)',
'23. set($one, $two = NULL)',
'24. setAction($action)',
'25. setRequest($request)',
'26. shutdownProcess()',
'27. startupProcess()',
'28. validate()',
'29. validateErrors()'
);
$this->Shell->expects($this->at(2))->method('out')->with($expected);

Expand Down
12 changes: 10 additions & 2 deletions lib/Cake/Test/Case/Controller/ControllerTest.php
Expand Up @@ -1114,15 +1114,23 @@ public function testStartupProcess() {
$Controller = $this->getMock('Controller', array('getEventManager'));

$eventManager = $this->getMock('CakeEventManager');
$eventManager->expects($this->once())->method('dispatch')
$eventManager->expects($this->at(0))->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('CakeEvent'),
$this->attributeEqualTo('_name', 'Controller.initialize'),
$this->attributeEqualTo('_subject', $Controller)
)
);
$eventManager->expects($this->at(1))->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('CakeEvent'),
$this->attributeEqualTo('_name', 'Controller.startup'),
$this->attributeEqualTo('_subject', $Controller)
)
);
$Controller->expects($this->once())->method('getEventManager')
$Controller->expects($this->exactly(2))->method('getEventManager')
->will($this->returnValue($eventManager));
$Controller->startupProcess();
}
Expand Down
45 changes: 45 additions & 0 deletions lib/Cake/Test/Case/Event/CakeEventManagerTest.php
Expand Up @@ -210,6 +210,51 @@ public function testDispatch() {
$manager->dispatch($event);
}

/**
* Tests event dispatching with a return value
*
* @return void
*/
public function testDispatchReturnValue() {
$manager = new CakeEventManager;
$listener = $this->getMock('CakeEventTestListener');
$anotherListener = $this->getMock('CakeEventTestListener');
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
$manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
$event = new CakeEvent('fake.event');

$firstStep = clone $event;
$listener->expects($this->at(0))->method('listenerFunction')
->with($firstStep)
->will($this->returnValue('something special'));
$anotherListener->expects($this->at(0))->method('listenerFunction')->with($event);
$manager->dispatch($event);
$this->assertEquals('something special', $event->result);
}

/**
* Tests that returning false in a callback stops the event
*
* @return void
*/
public function testDispatchFalseStopsEvent() {
$manager = new CakeEventManager;
$listener = $this->getMock('CakeEventTestListener');
$anotherListener = $this->getMock('CakeEventTestListener');
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
$manager->attach(array($anotherListener, 'listenerFunction'), 'fake.event');
$event = new CakeEvent('fake.event');

$originalEvent = clone $event;
$listener->expects($this->at(0))->method('listenerFunction')
->with($originalEvent)
->will($this->returnValue(false));
$anotherListener->expects($this->never())->method('listenerFunction');
$manager->dispatch($event);
$this->assertTrue($event->isStopped());
}


/**
* Tests event dispatching using priorities
*
Expand Down

0 comments on commit ffa12f4

Please sign in to comment.