Skip to content

Commit

Permalink
Avoiding fatal errors by chechink precense of reducer function
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 24, 2013
1 parent acc00a7 commit 65cc263
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Cake/ORM/MapReduce.php
Expand Up @@ -163,6 +163,8 @@ public function emit($value, $key = null) {
* bucket created during the Map phase call the reduce function.
*
* @return void
* @throws \LogicException if emitIntermediate was called but no reducer function
* was provided
*/
protected function _execute() {
$mapper = $this->_mapper;
Expand All @@ -171,6 +173,10 @@ protected function _execute() {
}
$this->_data = null;

if (!empty($this->_intermediate) && empty($this->_reducer)) {
throw new \LogicException(__d('cake_dev', 'No reducer function was provided'));
}

$reducer = $this->_reducer;
foreach ($this->_intermediate as $key => $list) {
$reducer($key, $list, $this);
Expand Down
17 changes: 17 additions & 0 deletions lib/Cake/Test/TestCase/ORM/MapReduceTest.php
Expand Up @@ -86,4 +86,21 @@ public function testEmitFinalInMapper() {
$this->assertEquals($expected, iterator_to_array($results));
}

/**
* Tests that a reducer is required when there are intermediate resutls
*
* @expectedException \LogicException
* @return void
*/
public function testReducerRequired() {
$data = ['a' => ['one', 'two'], 'b' => ['three', 'four']];
$mapper = function ($key, $row, $mr) {
foreach ($row as $number) {
$mr->emitIntermediate('a', $number);
}
};
$results = new MapReduce(new ArrayIterator($data), $mapper);
iterator_to_array($results);
}

}

0 comments on commit 65cc263

Please sign in to comment.