Skip to content

Commit

Permalink
Merge branch 'MDL-53430-30' of git://github.com/merrill-oakland/moodl…
Browse files Browse the repository at this point in the history
…e into MOODLE_30_STABLE
  • Loading branch information
stronk7 committed Mar 15, 2016
2 parents ff1cbbe + 17bf9bb commit 9e16802
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
12 changes: 6 additions & 6 deletions lib/classes/dml/recordset_walk.php
Expand Up @@ -57,7 +57,7 @@ class recordset_walk implements \Iterator {
protected $callback;

/**
* @var array|false Extra params for the callback.
* @var mixed|null Extra param for the callback.
*/
protected $callbackextra;

Expand All @@ -66,9 +66,9 @@ class recordset_walk implements \Iterator {
*
* @param \moodle_recordset $recordset Recordset to iterate.
* @param callable $callback Apply this function to each record. If using a method, it should be public.
* @param array $callbackextra Array of arguments to pass to the callback.
* @param mixed $callbackextra An extra single parameter to pass to the callback. Use a container to pass multiple values.
*/
public function __construct(\moodle_recordset $recordset, callable $callback, $callbackextra = false) {
public function __construct(\moodle_recordset $recordset, callable $callback, $callbackextra = null) {
$this->recordset = $recordset;
$this->callback = $callback;
$this->callbackextra = $callbackextra;
Expand Down Expand Up @@ -99,10 +99,10 @@ public function current() {
}

// Apply callback and return.
if ($this->callbackextra) {
return call_user_func($this->callback, $record);
} else {
if (!is_null($this->callbackextra)) {
return call_user_func($this->callback, $record, $this->callbackextra);
} else {
return call_user_func($this->callback, $record);
}
}

Expand Down
21 changes: 17 additions & 4 deletions lib/dml/tests/recordset_walk_test.php
Expand Up @@ -45,11 +45,14 @@ public function test_no_data() {

$recordset = $DB->get_recordset('assign');
$walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
$this->assertEquals(0, iterator_count($walker));
$this->assertFalse($walker->valid());

$count = 0;
foreach ($walker as $data) {
// No error here.
$count++;
}
$this->assertEquals(0, $count);
$walker->close();
}

Expand All @@ -65,11 +68,14 @@ public function test_simple_callback() {
// Simple iteration.
$recordset = $DB->get_recordset('assign');
$walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
$this->assertEquals(10, iterator_count($walker));

$count = 0;
foreach ($walker as $data) {
// Checking that the callback is being executed on each iteration.
$this->assertEquals($data->id . ' potatoes', $data->newfield);
$count++;
}
$this->assertEquals(10, $count);
// No exception if we double-close.
$walker->close();
}
Expand All @@ -85,17 +91,22 @@ public function test_extra_params_callback() {

// Iteration with extra callback arguments.
$recordset = $DB->get_recordset('assign');

$walker = new \core\dml\recordset_walk(
$recordset,
array($this, 'extra_callback'),
array('brown' => 'onions')
);
$this->assertEquals(10, iterator_count($walker));

$count = 0;
foreach ($walker as $data) {
// Checking that the callback is being executed on each
// iteration and the param is being passed.
$this->assertEquals('onions', $data->brown);
$count++;
}
$this->assertEquals(10, $count);

$walker->close();
}

Expand All @@ -105,7 +116,9 @@ public function test_extra_params_callback() {
* @param stdClass $data
* @return \Traversable
*/
public function simple_callback($data) {
public function simple_callback($data, $nothing = 'notpassed') {
// Confirm nothing was passed.
$this->assertEquals('notpassed', $nothing);
$data->newfield = $data->id . ' potatoes';
return $data;
}
Expand Down

0 comments on commit 9e16802

Please sign in to comment.