Adding 'collectReturn' option to ObjectCollection::trigger. lets a callback collect the return of each object and return that.

Adding tests.
This commit is contained in:
mark_story 2010-07-04 16:57:13 -04:00
parent 54132cba79
commit 22fbc24560
2 changed files with 71 additions and 14 deletions

View file

@ -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;
}
/**

View file

@ -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);
}
/**