Skip to content

Commit

Permalink
Making modParams ignore null results.
Browse files Browse the repository at this point in the history
Test added.
  • Loading branch information
markstory committed Dec 12, 2010
1 parent a05baaa commit 10c3587
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cake/libs/object_collection.php
Expand Up @@ -73,7 +73,10 @@ abstract public function load($name, $options = array());
* 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.
* Setting modParams to an integer value will allow you to modify the parameter with that index.
* Any non-null value will modify the parameter index indicated.
* Defaults to false.
*
*
* @param string $callback Method to fire on all the objects. Its assumed all the objects implement
* the method you are calling.
Expand Down Expand Up @@ -115,7 +118,7 @@ public function trigger($callback, $params = array(), $options = array()) {
(is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
) {
break;
} elseif ($options['modParams'] !== false) {
} elseif ($options['modParams'] !== false && $result !== null) {
$params[$options['modParams']] = $result;
}
}
Expand Down
29 changes: 29 additions & 0 deletions cake/tests/cases/libs/object_collection.test.php
Expand Up @@ -315,6 +315,35 @@ function testTriggerModParamsInvalidIndex() {
);
}

/**
* test that returrning null doesn't modify parameters.
*
* @expectedException CakeException
* @return void
*/
function testTriggerModParamsNullIgnored() {
$this->_makeMockClasses();
$this->Objects->load('TriggerMockFirst');
$this->Objects->load('TriggerMockSecond');

$this->Objects->TriggerMockFirst->expects($this->once())
->method('callback')
->with(array('value'))
->will($this->returnValue(null));

$this->Objects->TriggerMockSecond->expects($this->once())
->method('callback')
->with(array('value'))
->will($this->returnValue('new value'));

$result = $this->Objects->trigger(
'callback',
array(array('value')),
array('modParams' => 2)
);
$this->assertEquals('new value', $result);
}

/**
* test normalizeObjectArray
*
Expand Down

0 comments on commit 10c3587

Please sign in to comment.