Skip to content
Permalink
Browse files

Moving the modParams option into ObjectCollection, so it can replace …

…the specific trigger method in BehaviorCollection.

Changed how break behaves, so it is works better with modParams and collectReturn options.
Tests updated and expanded.
  • Loading branch information...
markstory committed Dec 12, 2010
1 parent d14d148 commit 96968f7194708519980f2ac9e1411aca2ce5e793
Showing with 101 additions and 16 deletions.
  1. +30 −7 cake/libs/object_collection.php
  2. +71 −9 cake/tests/cases/libs/object_collection.test.php
@@ -60,12 +60,20 @@ abstract class ObjectCollection {
* ### Options
*
* - `breakOn` Set to the value or values you want the callback propagation to stop on.
* Defaults to `false`
* - `break` Set to true to enabled breaking. Defaults to `false`.
* Can either be a scalar value, or an array of values to break on. Defaults to `false`.
*
* - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
* will be returned. If used in combination with `collectReturn` the collected results will be returned.
* Defaults to `false`.
*
* - `collectReturn` Set to true to collect the return of each object into an array.
* This array of return values will be returned from the trigger() call. Defaults to `false`.
*
* - `triggerDisabled` Will trigger the callback on all objects in the collection even the non-enabled
* objects. Defaults to false.
* objects. Defaults to false.
*
* - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
* Setting modParams to an integer value will allow you to modify the parameter with that index. Defaults to false.
*
* @param string $callback Method to fire on all the objects. Its assumed all the objects implement
* the method you are calling.
@@ -78,27 +86,42 @@ public function trigger($callback, $params = array(), $options = array()) {
return true;
}
$options = array_merge(
array('break' => false, 'breakOn' => false, 'collectReturn' => false, 'triggerDisabled' => false),
array(
'break' => false,
'breakOn' => false,
'collectReturn' => false,
'triggerDisabled' => false,
'modParams' => false
),
$options
);
$collected = array();
$list = $this->_enabled;
if ($options['triggerDisabled'] === true) {
$list = array_keys($this->_loaded);
}
if ($options['modParams'] !== false && !isset($params[$options['modParams']])) {
throw new CakeException(__('Cannot use modParams with indexes that do not exist.'));
}
foreach ($list 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 (
$options['break'] && ($result === $options['breakOn'] ||
(is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
) {
return ($options['collectReturn'] === true) ? $collected : $result;
break;
} elseif ($options['modParams'] !== false) {
$params[$options['modParams']] = $result;
}
}
return $options['collectReturn'] ? $collected : true;
if ($options['modParams'] !== false) {
return $params[$options['modParams']];
}
return $options['collectReturn'] ? $collected : $result;
}
/**
@@ -158,9 +158,11 @@ function testTrigger() {
$this->Objects->load('TriggerMockSecond');
$this->Objects->TriggerMockFirst->expects($this->once())
->method('callback');
->method('callback')
->will($this->returnValue(true));
$this->Objects->TriggerMockSecond->expects($this->once())
->method('callback');
->method('callback')
->will($this->returnValue(true));
$this->assertTrue($this->Objects->trigger('callback'));
}
@@ -170,15 +172,17 @@ function testTrigger() {
*
* @return void
*/
function testTriggerWithTriggerDisabledObjects() {
function testTriggerWithTriggerDisabledObjects() {
$this->_makeMockClasses();
$this->Objects->load('TriggerMockFirst', array(), false);
$this->Objects->load('TriggerMockSecond');
$this->Objects->TriggerMockFirst->expects($this->once())
->method('callback');
->method('callback')
->will($this->returnValue(true));
$this->Objects->TriggerMockSecond->expects($this->once())
->method('callback');
->method('callback')
->will($this->returnValue(true));
$result = $this->Objects->trigger('callback', array(), array('triggerDisabled' => true));
$this->assertTrue($result);
@@ -195,9 +199,11 @@ function testTriggerWithDisabledComponents() {
$this->Objects->load('TriggerMockSecond');
$this->Objects->TriggerMockFirst->expects($this->once())
->method('callback');
->method('callback')
->will($this->returnValue(true));
$this->Objects->TriggerMockSecond->expects($this->never())
->method('callback');
->method('callback')
->will($this->returnValue(true));
$this->Objects->disable('TriggerMockSecond');
@@ -247,12 +253,68 @@ function testTriggerWithBreak() {
$result = $this->Objects->trigger(
'callback',
array(&$controller),
array(),
array('break' => true, 'breakOn' => false)
);
$this->assertFalse($result);
}
/**
* test that trigger with modParams works.
*
* @return void
*/
function testTriggerWithModParams() {
$this->_makeMockClasses();
$this->Objects->load('TriggerMockFirst');
$this->Objects->load('TriggerMockSecond');
$this->Objects->TriggerMockFirst->expects($this->once())
->method('callback')
->with(array('value'))
->will($this->returnValue(array('new value')));
$this->Objects->TriggerMockSecond->expects($this->once())
->method('callback')
->with(array('new value'))
->will($this->returnValue(array('newer value')));
$result = $this->Objects->trigger(
'callback',
array(array('value')),
array('modParams' => 0)
);
$this->assertEquals(array('newer value'), $result);
}
/**
* test that setting modParams to an index that doesn't exist doesn't cause errors.
*
* @expectedException CakeException
* @return void
*/
function testTriggerModParamsInvalidIndex() {
$this->_makeMockClasses();
$this->Objects->load('TriggerMockFirst');
$this->Objects->load('TriggerMockSecond');
$this->Objects->TriggerMockFirst->expects($this->once())
->method('callback')
->with(array('value'))
->will($this->returnValue(array('new value')));
$this->Objects->TriggerMockSecond->expects($this->once())
->method('callback')
->with(array('value'))
->will($this->returnValue(array('newer value')));
$result = $this->Objects->trigger(
'callback',
array(array('value')),
array('modParams' => 2)
);
}
/**
* test normalizeObjectArray
*
@@ -265,7 +327,7 @@ function testnormalizeObjectArray() {
'Something',
'Banana.Apple' => array('foo' => 'bar')
);
$result = ComponentCollection::normalizeObjectArray($components);
$result = ObjectCollection::normalizeObjectArray($components);
$expected = array(
'Html' => array('class' => 'Html', 'settings' => array()),
'Bar' => array('class' => 'Foo.Bar', 'settings' => array('one', 'two')),

0 comments on commit 96968f7

Please sign in to comment.
You can’t perform that action at this time.