1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Test case for recordset_walk.
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2015 David Monllao
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
class recordset_walk_test extends \advanced_testcase {
|
|
|
28 |
|
|
|
29 |
public function setUp(): void {
|
|
|
30 |
parent::setUp();
|
|
|
31 |
$this->resetAfterTest();
|
|
|
32 |
}
|
|
|
33 |
|
11 |
efrain |
34 |
public function test_no_data(): void {
|
1 |
efrain |
35 |
global $DB;
|
|
|
36 |
|
|
|
37 |
$recordset = $DB->get_recordset('assign');
|
|
|
38 |
$walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
|
|
|
39 |
$this->assertFalse($walker->valid());
|
|
|
40 |
|
|
|
41 |
$count = 0;
|
|
|
42 |
foreach ($walker as $data) {
|
|
|
43 |
// No error here.
|
|
|
44 |
$count++;
|
|
|
45 |
}
|
|
|
46 |
$this->assertEquals(0, $count);
|
|
|
47 |
$walker->close();
|
|
|
48 |
}
|
|
|
49 |
|
11 |
efrain |
50 |
public function test_simple_callback(): void {
|
1 |
efrain |
51 |
global $DB;
|
|
|
52 |
|
|
|
53 |
/** @var \mod_assign_generator $generator */
|
|
|
54 |
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
|
|
55 |
$courses = array();
|
|
|
56 |
for ($i = 0; $i < 10; $i++) {
|
|
|
57 |
$courses[$i] = $generator->create_instance(array('course' => SITEID));
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
// Simple iteration.
|
|
|
61 |
$recordset = $DB->get_recordset('assign');
|
|
|
62 |
$walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
|
|
|
63 |
|
|
|
64 |
$count = 0;
|
|
|
65 |
foreach ($walker as $data) {
|
|
|
66 |
// Checking that the callback is being executed on each iteration.
|
|
|
67 |
$this->assertEquals($data->id . ' potatoes', $data->newfield);
|
|
|
68 |
$count++;
|
|
|
69 |
}
|
|
|
70 |
$this->assertEquals(10, $count);
|
|
|
71 |
// No exception if we double-close.
|
|
|
72 |
$walker->close();
|
|
|
73 |
}
|
|
|
74 |
|
11 |
efrain |
75 |
public function test_extra_params_callback(): void {
|
1 |
efrain |
76 |
global $DB;
|
|
|
77 |
|
|
|
78 |
/** @var \mod_assign_generator $generator */
|
|
|
79 |
$generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
|
|
|
80 |
$courses = array();
|
|
|
81 |
for ($i = 0; $i < 10; $i++) {
|
|
|
82 |
$courses[$i] = $generator->create_instance(array('course' => SITEID));
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
// Iteration with extra callback arguments.
|
|
|
86 |
$recordset = $DB->get_recordset('assign');
|
|
|
87 |
|
|
|
88 |
$walker = new \core\dml\recordset_walk(
|
|
|
89 |
$recordset,
|
|
|
90 |
array($this, 'extra_callback'),
|
|
|
91 |
array('brown' => 'onions')
|
|
|
92 |
);
|
|
|
93 |
|
|
|
94 |
$count = 0;
|
|
|
95 |
foreach ($walker as $data) {
|
|
|
96 |
// Checking that the callback is being executed on each
|
|
|
97 |
// iteration and the param is being passed.
|
|
|
98 |
$this->assertEquals('onions', $data->brown);
|
|
|
99 |
$count++;
|
|
|
100 |
}
|
|
|
101 |
$this->assertEquals(10, $count);
|
|
|
102 |
|
|
|
103 |
$walker->close();
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Simple callback requiring 1 row fields.
|
|
|
108 |
*
|
|
|
109 |
* @param \stdClass $data
|
|
|
110 |
* @return \Traversable
|
|
|
111 |
*/
|
|
|
112 |
public function simple_callback($data, $nothing = 'notpassed') {
|
|
|
113 |
// Confirm nothing was passed.
|
|
|
114 |
$this->assertEquals('notpassed', $nothing);
|
|
|
115 |
$data->newfield = $data->id . ' potatoes';
|
|
|
116 |
return $data;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Callback requiring 1 row fields + other params.
|
|
|
121 |
*
|
|
|
122 |
* @param \stdClass $data
|
|
|
123 |
* @param mixed $extra
|
|
|
124 |
* @return \Traversable
|
|
|
125 |
*/
|
|
|
126 |
public function extra_callback($data, $extra) {
|
|
|
127 |
$data->brown = $extra['brown'];
|
|
|
128 |
return $data;
|
|
|
129 |
}
|
|
|
130 |
}
|