mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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:
parent
54132cba79
commit
22fbc24560
2 changed files with 71 additions and 14 deletions
|
@ -68,26 +68,30 @@ abstract class ObjectCollection {
|
||||||
* the method you are calling.
|
* the method you are calling.
|
||||||
* @param array $params Array of parameters for the triggered callback.
|
* @param array $params Array of parameters for the triggered callback.
|
||||||
* @param array $options Array of options.
|
* @param array $options Array of options.
|
||||||
* @return Returns true.
|
* @return mixed true.
|
||||||
*/
|
*/
|
||||||
public function trigger($callback, $params = array(), $options = array()) {
|
public function trigger($callback, $params = array(), $options = array()) {
|
||||||
if (empty($this->_enabled)) {
|
if (empty($this->_enabled)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
$options = array_merge(
|
$options = array_merge(
|
||||||
array('break' => false, 'breakOn' => false),
|
array('break' => false, 'breakOn' => false, 'collectReturn' => false),
|
||||||
$options
|
$options
|
||||||
);
|
);
|
||||||
|
$collected = array();
|
||||||
foreach ($this->_enabled as $name) {
|
foreach ($this->_enabled as $name) {
|
||||||
$result = call_user_func_array(array(&$this->_loaded[$name], $callback), $params);
|
$result = call_user_func_array(array(&$this->_loaded[$name], $callback), $params);
|
||||||
|
if ($options['collectReturn'] === true) {
|
||||||
|
$collected[] = $result;
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
$options['break'] && ($result === $options['breakOn'] ||
|
$options['break'] && ($result === $options['breakOn'] ||
|
||||||
(is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
|
(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -118,6 +118,18 @@ class ComponentCollectionTest extends CakeTestCase {
|
||||||
$this->assertEquals(array('Security'), $result, 'enabled components is wrong');
|
$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.
|
* test triggering callbacks.
|
||||||
*
|
*
|
||||||
|
@ -125,10 +137,7 @@ class ComponentCollectionTest extends CakeTestCase {
|
||||||
*/
|
*/
|
||||||
function testTrigger() {
|
function testTrigger() {
|
||||||
$controller = null;
|
$controller = null;
|
||||||
if (!class_exists('TriggerMockCookieComponent')) {
|
$this->_makeMockClasses();
|
||||||
$this->getMock('CookieComponent', array(), array(), 'TriggerMockCookieComponent');
|
|
||||||
$this->getMock('SecurityComponent', array(), array(), 'TriggerMockSecurityComponent');
|
|
||||||
}
|
|
||||||
$this->Components->load('TriggerMockCookie');
|
$this->Components->load('TriggerMockCookie');
|
||||||
$this->Components->load('TriggerMockSecurity');
|
$this->Components->load('TriggerMockSecurity');
|
||||||
|
|
||||||
|
@ -137,7 +146,7 @@ class ComponentCollectionTest extends CakeTestCase {
|
||||||
$this->Components->TriggerMockSecurity->expects($this->once())->method('startup')
|
$this->Components->TriggerMockSecurity->expects($this->once())->method('startup')
|
||||||
->with(null);
|
->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() {
|
function testTriggerWithDisabledComponents() {
|
||||||
$controller = null;
|
$controller = null;
|
||||||
if (!class_exists('TriggerMockCookieComponent')) {
|
$this->_makeMockClasses();
|
||||||
$this->getMock('CookieComponent', array(), array(), 'TriggerMockCookieComponent');
|
|
||||||
$this->getMock('SecurityComponent', array(), array(), 'TriggerMockSecurityComponent');
|
|
||||||
}
|
|
||||||
$this->Components->load('TriggerMockCookie');
|
$this->Components->load('TriggerMockCookie');
|
||||||
$this->Components->load('TriggerMockSecurity');
|
$this->Components->load('TriggerMockSecurity');
|
||||||
|
|
||||||
|
@ -161,7 +167,54 @@ class ComponentCollectionTest extends CakeTestCase {
|
||||||
|
|
||||||
$this->Components->disable('TriggerMockSecurity');
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue