Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.
   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   * Array based data iterator.
  19   *
  20   * @package    core
  21   * @category   phpunit
  22   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  /**
  28   * Based on array iterator code from PHPUnit documentation by Sebastian Bergmann
  29   * with new constructor parameter for different array types.
  30   *
  31   * @package    core
  32   * @category   phpunit
  33   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class phpunit_ArrayDataSet extends PHPUnit\DbUnit\DataSet\AbstractDataSet {
  37      /**
  38       * @var array
  39       */
  40      protected $tables = array();
  41  
  42      /**
  43       * @param array $data
  44       */
  45      public function __construct(array $data) {
  46          foreach ($data AS $tableName => $rows) {
  47              $firstrow = reset($rows);
  48  
  49              if (array_key_exists(0, $firstrow)) {
  50                  // columns in first row
  51                  $columnsInFirstRow = true;
  52                  $columns = $firstrow;
  53                  $key = key($rows);
  54                  unset($rows[$key]);
  55              } else {
  56                  // column name is in each row as key
  57                  $columnsInFirstRow = false;
  58                  $columns = array_keys($firstrow);
  59              }
  60  
  61              $metaData = new PHPUnit\DbUnit\DataSet\DefaultTableMetadata($tableName, $columns);
  62              $table = new PHPUnit\DbUnit\DataSet\DefaultTable($metaData);
  63  
  64              foreach ($rows AS $row) {
  65                  if ($columnsInFirstRow) {
  66                      $row = array_combine($columns, $row);
  67                  }
  68                  $table->addRow($row);
  69              }
  70              $this->tables[$tableName] = $table;
  71          }
  72      }
  73  
  74      protected function createIterator($reverse = FALSE) {
  75          return new PHPUnit\DbUnit\DataSet\DefaultTableIterator($this->tables, $reverse);
  76      }
  77  
  78      public function getTable($tableName) {
  79          if (!isset($this->tables[$tableName])) {
  80              throw new InvalidArgumentException("$tableName is not a table in the current database.");
  81          }
  82  
  83          return $this->tables[$tableName];
  84      }
  85  }