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.

Handle simple PHP/CSV/XML datasets to be use with ease by unit tests. This is a very minimal class, able to load data from PHP arrays and CSV/XML files, optionally uploading them to database.

Copyright: 2020 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 371 lines (15 kb)
Included or required: 1 time
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

phpunit_dataset:: (8 methods):
  from_files()
  from_file()
  from_string()
  from_array()
  to_database()
  get_rows()
  load_csv()
  load_xml()


Class: phpunit_dataset  - X-Ref

Lightweight dataset class for phpunit, supports XML, CSV and array datasets.

This is a simple replacement class for the old old phpunit/dbunit, now
archived. It allows to load CSV, XML and array structures to database.
from_files(array $fullpaths)   X-Ref
Load information from multiple files (XML, CSV) to the dataset.

This method accepts an array of full paths to CSV or XML files to be loaded
into the dataset. For CSV files, the name of the table which the file belongs
to needs to be specified. Example:

$fullpaths = [
'/path/to/users.xml',
'course' => '/path/to/courses.csv',
];

param: array $fullpaths full paths to CSV or XML files to load.

from_file(string $fullpath, ?string $table = null)   X-Ref
Load information from one file (XML, CSV) to the dataset.

param: string $fullpath full path to CSV or XML file to load.
param: string|null $table name of the table which the file belongs to (only for CSV files).

from_string(string $content, string $type, ?string $table = null)   X-Ref
Load information from a string (XML, CSV) to the dataset.

param: string $content contents (CSV or XML) to load.
param: string $type format of the content to be loaded (csv or xml).
param: string|null $table name of the table which the file belongs to (only for CSV files).

from_array(array $structure)   X-Ref
Load information from a PHP array to the dataset.

The general structure of the PHP array must be
[table name] => [array of rows, each one being an array of values or column => values.
The format of the array must be one of the following:
- non-associative array, with column names in the first row (pretty much like CSV files are):
$structure = [
'table 1' => [
['column name 1', 'column name 2'],
['row 1 column 1 value', 'row 1 column 2 value'*,
['row 2 column 1 value', 'row 2 column 2 value'*,
],
'table 2' => ...
];
- associative array, with column names being keys in the array.
$structure = [
'table 1' => [
['column name 1' => 'row 1 column 1 value', 'column name 2' => 'row 1 column 2 value'],
['column name 1' => 'row 2 column 1 value', 'column name 2' => 'row 2 column 2 value'],
],
'table 2' => ...
];
param: array $structure php array with a valid structure to be loaded to the dataset.

to_database(array $filter = [])   X-Ref
Send all the information to the dataset to the database.

This method gets all the information loaded in the dataset, using the from_xxx() methods
and sends it to the database; table and column names must match.

Note that, if the information to be sent to database contains sequence columns (usually 'id')
then those values will be preserved (performing an import and adjusting sequences later). Else
normal inserts will happen and sequence (auto-increment) columns will be fed automatically.

param: string[] $filter Tables to be sent to database. If not specified, all tables are processed.

get_rows(array $filter = [])   X-Ref
Returns the rows, for a given table, that the dataset holds.

param: string[] $filter Tables to return rows. If not specified, all tables are processed.
return: array tables as keys with rows on each as sub array.

load_csv(string $content, string $tablename)   X-Ref
Given a CSV content, process and load it as a table into the dataset.

param: string $content CSV content to be loaded (only one table).
param: string $tablename Name of the table the content belongs to.

load_xml(string $content)   X-Ref
Given a XML content, process and load it as tables into the dataset.

param: string $content XML content to be loaded (can be multi-table).