Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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  
  34      public function test_no_data() {
  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  
  50      public function test_simple_callback() {
  51          global $DB;
  52  
  53          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  54          $courses = array();
  55          for ($i = 0; $i < 10; $i++) {
  56              $courses[$i] = $generator->create_instance(array('course' => SITEID));
  57          }
  58  
  59          // Simple iteration.
  60          $recordset = $DB->get_recordset('assign');
  61          $walker = new \core\dml\recordset_walk($recordset, array($this, 'simple_callback'));
  62  
  63          $count = 0;
  64          foreach ($walker as $data) {
  65              // Checking that the callback is being executed on each iteration.
  66              $this->assertEquals($data->id . ' potatoes', $data->newfield);
  67              $count++;
  68          }
  69          $this->assertEquals(10, $count);
  70          // No exception if we double-close.
  71          $walker->close();
  72      }
  73  
  74      public function test_extra_params_callback() {
  75          global $DB;
  76  
  77          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  78          $courses = array();
  79          for ($i = 0; $i < 10; $i++) {
  80              $courses[$i] = $generator->create_instance(array('course' => SITEID));
  81          }
  82  
  83          // Iteration with extra callback arguments.
  84          $recordset = $DB->get_recordset('assign');
  85  
  86          $walker = new \core\dml\recordset_walk(
  87              $recordset,
  88              array($this, 'extra_callback'),
  89              array('brown' => 'onions')
  90          );
  91  
  92          $count = 0;
  93          foreach ($walker as $data) {
  94              // Checking that the callback is being executed on each
  95              // iteration and the param is being passed.
  96              $this->assertEquals('onions', $data->brown);
  97              $count++;
  98          }
  99          $this->assertEquals(10, $count);
 100  
 101          $walker->close();
 102      }
 103  
 104      /**
 105       * Simple callback requiring 1 row fields.
 106       *
 107       * @param \stdClass $data
 108       * @return \Traversable
 109       */
 110      public function simple_callback($data, $nothing = 'notpassed') {
 111          // Confirm nothing was passed.
 112          $this->assertEquals('notpassed', $nothing);
 113          $data->newfield = $data->id . ' potatoes';
 114          return $data;
 115      }
 116  
 117      /**
 118       * Callback requiring 1 row fields + other params.
 119       *
 120       * @param \stdClass $data
 121       * @param mixed $extra
 122       * @return \Traversable
 123       */
 124      public function extra_callback($data, $extra) {
 125          $data->brown = $extra['brown'];
 126          return $data;
 127      }
 128  }