Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  /**
  18   * Test \core\dml\recordset_walk.
  19   *
  20   * @package    core
  21   * @category   dml
  22   * @copyright  2015 David Monllao
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Test case for recordset_walk.
  30   *
  31   * @package    core
  32   * @category   dml
  33   * @copyright  2015 David Monllao
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class core_recordset_walk_testcase extends advanced_testcase {
  37  
  38      public function setUp(): void {
  39          parent::setUp();
  40          $this->resetAfterTest();
  41      }
  42  
  43      public function test_no_data() {
  44          global $DB;
  45  
  46          $recordset = $DB->get_recordset('assign');
  47          $walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
  48          $this->assertFalse($walker->valid());
  49  
  50          $count = 0;
  51          foreach ($walker as $data) {
  52              // No error here.
  53              $count++;
  54          }
  55          $this->assertEquals(0, $count);
  56          $walker->close();
  57      }
  58  
  59      public function test_simple_callback() {
  60          global $DB;
  61  
  62          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  63          $courses = array();
  64          for ($i = 0; $i < 10; $i++) {
  65              $courses[$i] = $generator->create_instance(array('course' => SITEID));
  66          }
  67  
  68          // Simple iteration.
  69          $recordset = $DB->get_recordset('assign');
  70          $walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
  71  
  72          $count = 0;
  73          foreach ($walker as $data) {
  74              // Checking that the callback is being executed on each iteration.
  75              $this->assertEquals($data->id . ' potatoes', $data->newfield);
  76              $count++;
  77          }
  78          $this->assertEquals(10, $count);
  79          // No exception if we double-close.
  80          $walker->close();
  81      }
  82  
  83      public function test_extra_params_callback() {
  84          global $DB;
  85  
  86          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  87          $courses = array();
  88          for ($i = 0; $i < 10; $i++) {
  89              $courses[$i] = $generator->create_instance(array('course' => SITEID));
  90          }
  91  
  92          // Iteration with extra callback arguments.
  93          $recordset = $DB->get_recordset('assign');
  94  
  95          $walker = new \core\dml\recordset_walk(
  96              $recordset,
  97              array($this, 'extra_callback'),
  98              array('brown' => 'onions')
  99          );
 100  
 101          $count = 0;
 102          foreach ($walker as $data) {
 103              // Checking that the callback is being executed on each
 104              // iteration and the param is being passed.
 105              $this->assertEquals('onions', $data->brown);
 106              $count++;
 107          }
 108          $this->assertEquals(10, $count);
 109  
 110          $walker->close();
 111      }
 112  
 113      /**
 114       * Simple callback requiring 1 row fields.
 115       *
 116       * @param stdClass $data
 117       * @return \Traversable
 118       */
 119      public function simple_callback($data, $nothing = 'notpassed') {
 120          // Confirm nothing was passed.
 121          $this->assertEquals('notpassed', $nothing);
 122          $data->newfield = $data->id . ' potatoes';
 123          return $data;
 124      }
 125  
 126      /**
 127       * Callback requiring 1 row fields + other params.
 128       *
 129       * @param stdClass $data
 130       * @param mixed $extra
 131       * @return \Traversable
 132       */
 133      public function extra_callback($data, $extra) {
 134          $data->brown = $extra['brown'];
 135          return $data;
 136      }
 137  }