From 22fbc24560dc1a38f9d417504e38234b14ad6c6d Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 4 Jul 2010 16:57:13 -0400 Subject: [PATCH] Adding 'collectReturn' option to ObjectCollection::trigger. lets a callback collect the return of each object and return that. Adding tests. --- cake/libs/object_collection.php | 12 ++- .../controller/component_collection.test.php | 73 ++++++++++++++++--- 2 files changed, 71 insertions(+), 14 deletions(-) diff --git a/cake/libs/object_collection.php b/cake/libs/object_collection.php index e4983b8cd..5526f771c 100644 --- a/cake/libs/object_collection.php +++ b/cake/libs/object_collection.php @@ -68,26 +68,30 @@ abstract class ObjectCollection { * the method you are calling. * @param array $params Array of parameters for the triggered callback. * @param array $options Array of options. - * @return Returns true. + * @return mixed true. */ public function trigger($callback, $params = array(), $options = array()) { if (empty($this->_enabled)) { return true; } $options = array_merge( - array('break' => false, 'breakOn' => false), + array('break' => false, 'breakOn' => false, 'collectReturn' => false), $options ); + $collected = array(); foreach ($this->_enabled as $name) { $result = call_user_func_array(array(&$this->_loaded[$name], $callback), $params); + if ($options['collectReturn'] === true) { + $collected[] = $result; + } if ( $options['break'] && ($result === $options['breakOn'] || (is_array($options['breakOn']) && in_array($result, $options['breakOn'], true))) ) { - return $result; + return ($options['collectReturn'] === true) ? $collected : $result; } } - return true; + return $options['collectReturn'] ? $collected : true; } /** diff --git a/cake/tests/cases/libs/controller/component_collection.test.php b/cake/tests/cases/libs/controller/component_collection.test.php index 3f91cdfcd..a44e33e63 100644 --- a/cake/tests/cases/libs/controller/component_collection.test.php +++ b/cake/tests/cases/libs/controller/component_collection.test.php @@ -118,6 +118,18 @@ class ComponentCollectionTest extends CakeTestCase { $this->assertEquals(array('Security'), $result, 'enabled components is wrong'); } +/** + * creates mock classes for testing + * + * @return void + */ + protected function _makeMockClasses() { + if (!class_exists('TriggerMockCookieComponent')) { + $this->getMock('CookieComponent', array(), array(), 'TriggerMockCookieComponent', false); + $this->getMock('SecurityComponent', array(), array(), 'TriggerMockSecurityComponent', false); + } + } + /** * test triggering callbacks. * @@ -125,10 +137,7 @@ class ComponentCollectionTest extends CakeTestCase { */ function testTrigger() { $controller = null; - if (!class_exists('TriggerMockCookieComponent')) { - $this->getMock('CookieComponent', array(), array(), 'TriggerMockCookieComponent'); - $this->getMock('SecurityComponent', array(), array(), 'TriggerMockSecurityComponent'); - } + $this->_makeMockClasses(); $this->Components->load('TriggerMockCookie'); $this->Components->load('TriggerMockSecurity'); @@ -137,7 +146,7 @@ class ComponentCollectionTest extends CakeTestCase { $this->Components->TriggerMockSecurity->expects($this->once())->method('startup') ->with(null); - $this->Components->trigger('startup', array(&$controller)); + $this->assertTrue($this->Components->trigger('startup', array(&$controller))); } /** @@ -147,10 +156,7 @@ class ComponentCollectionTest extends CakeTestCase { */ function testTriggerWithDisabledComponents() { $controller = null; - if (!class_exists('TriggerMockCookieComponent')) { - $this->getMock('CookieComponent', array(), array(), 'TriggerMockCookieComponent'); - $this->getMock('SecurityComponent', array(), array(), 'TriggerMockSecurityComponent'); - } + $this->_makeMockClasses(); $this->Components->load('TriggerMockCookie'); $this->Components->load('TriggerMockSecurity'); @@ -161,7 +167,54 @@ class ComponentCollectionTest extends CakeTestCase { $this->Components->disable('TriggerMockSecurity'); - $this->Components->trigger('startup', array(&$controller)); + $this->assertTrue($this->Components->trigger('startup', array(&$controller))); + } + +/** + * test that the collectReturn option works. + * + * @return void + */ + function testTriggerWithCollectReturn() { + $controller = null; + $this->_makeMockClasses(); + $this->Components->load('TriggerMockCookie'); + $this->Components->load('TriggerMockSecurity'); + + $this->Components->TriggerMockCookie->expects($this->once())->method('startup') + ->will($this->returnValue(array('one', 'two'))); + $this->Components->TriggerMockSecurity->expects($this->once())->method('startup') + ->will($this->returnValue(array('three', 'four'))); + + $result = $this->Components->trigger('startup', array(&$controller), array('collectReturn' => true)); + $expected = array( + array('one', 'two'), + array('three', 'four') + ); + $this->assertEquals($expected, $result); + } + +/** + * test that trigger with break & breakOn works. + * + * @return void + */ + function testTriggerWithBreak() { + $controller = null; + $this->_makeMockClasses(); + $this->Components->load('TriggerMockCookie'); + $this->Components->load('TriggerMockSecurity'); + + $this->Components->TriggerMockCookie->expects($this->once())->method('startup') + ->will($this->returnValue(false)); + $this->Components->TriggerMockSecurity->expects($this->never())->method('startup'); + + $result = $this->Components->trigger( + 'startup', + array(&$controller), + array('break' => true, 'breakOn' => false) + ); + $this->assertFalse($result); } /**