Moodle PHP Documentation 4.5
Moodle 4.5dev (Build: 20240606) (d3ae1391abe)
phpunit_dataset Class Reference

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

Public Member Functions

 from_array (array $structure)
 Load information from a PHP array to the dataset.
 
 from_file (string $fullpath, ?string $table=null)
 Load information from one file (XML, CSV) to the dataset.
 
 from_files (array $fullpaths)
 Load information from multiple files (XML, CSV) to the dataset.
 
 from_string (string $content, string $type, ?string $table=null)
 Load information from a string (XML, CSV) to the dataset.
 
 get_rows (array $filter=[])
 Returns the rows, for a given table, that the dataset holds.
 
 to_database (array $filter=[])
 Send all the information to the dataset to the database.
 

Protected Member Functions

 load_csv (string $content, string $tablename)
 Given a CSV content, process and load it as a table into the dataset.
 
 load_xml (string $content)
 Given a XML content, process and load it as tables into the dataset.
 

Protected Attributes

array $columns = []
 columns belonging to every table (keys) handled by the dataset
 
array $rows = []
 rows belonging to every table (keys) handled by the dataset
 
array $tables = []
 tables being handled by the dataset
 

Detailed Description

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.

Member Function Documentation

◆ from_array()

phpunit_dataset::from_array ( array $structure)

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' => ... ];
    Parameters
    array$structurephp array with a valid structure to be loaded to the dataset.

◆ from_file()

phpunit_dataset::from_file ( string $fullpath,
?string $table = null )

Load information from one file (XML, CSV) to the dataset.

Parameters
string$fullpathfull path to CSV or XML file to load.
string | null$tablename of the table which the file belongs to (only for CSV files).

◆ from_files()

phpunit_dataset::from_files ( array $fullpaths)

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', ];

Parameters
array$fullpathsfull paths to CSV or XML files to load.

◆ from_string()

phpunit_dataset::from_string ( string $content,
string $type,
?string $table = null )

Load information from a string (XML, CSV) to the dataset.

Parameters
string$contentcontents (CSV or XML) to load.
string$typeformat of the content to be loaded (csv or xml).
string | null$tablename of the table which the file belongs to (only for CSV files).

◆ get_rows()

phpunit_dataset::get_rows ( array $filter = [])

Returns the rows, for a given table, that the dataset holds.

Parameters
string[]$filterTables to return rows. If not specified, all tables are processed.
Return values
arraytables as keys with rows on each as sub array.

◆ load_csv()

phpunit_dataset::load_csv ( string $content,
string $tablename )
protected

Given a CSV content, process and load it as a table into the dataset.

Parameters
string$contentCSV content to be loaded (only one table).
string$tablenameName of the table the content belongs to.

◆ load_xml()

phpunit_dataset::load_xml ( string $content)
protected

Given a XML content, process and load it as tables into the dataset.

Parameters
string$contentXML content to be loaded (can be multi-table).

◆ to_database()

phpunit_dataset::to_database ( array $filter = [])

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.

Parameters
string[]$filterTables to be sent to database. If not specified, all tables are processed.

The documentation for this class was generated from the following file: