MDL-67673 phpunit: Move tests to use new phpunit_dataset

- Make advanced_testcase old methods to use new ones internally.
- Fix advanced_testcase, statslib, mod/quiz and mod/data tests.

Originally MDL-64600
This commit is contained in:
Eloy Lafuente (stronk7) 2020-10-05 00:37:34 +02:00
parent be396eddf0
commit 1f13dff1a7
7 changed files with 139 additions and 126 deletions

View file

@ -153,6 +153,7 @@ abstract class advanced_testcase extends base_testcase {
* @return PHPUnit\DbUnit\DataSet\FlatXmlDataSet
*/
protected function createFlatXMLDataSet($xmlFile) {
// TODO: MDL-67673 - removed
return new PHPUnit\DbUnit\DataSet\FlatXmlDataSet($xmlFile);
}
@ -163,24 +164,22 @@ abstract class advanced_testcase extends base_testcase {
* @return PHPUnit\DbUnit\DataSet\XmlDataSet
*/
protected function createXMLDataSet($xmlFile) {
return new PHPUnit\DbUnit\DataSet\XmlDataSet($xmlFile);
// TODO: MDL-67673 - deprecate this (debugging...)
return $this->dataset_from_files([$xmlFile]);
}
/**
* Creates a new CsvDataSet from the given array of csv files. (absolute paths.)
*
* @param array $files array tablename=>cvsfile
* @param string $delimiter
* @param string $enclosure
* @param string $escape
* @return PHPUnit\DbUnit\DataSet\CsvDataSet
* @param string $delimiter unused
* @param string $enclosure unused
* @param string $escape unused
* @return phpunit_dataset
*/
protected function createCsvDataSet($files, $delimiter = ',', $enclosure = '"', $escape = '"') {
$dataSet = new PHPUnit\DbUnit\DataSet\CsvDataSet($delimiter, $enclosure, $escape);
foreach($files as $table=>$file) {
$dataSet->addTable($table, $file);
}
return $dataSet;
// TODO: MDL-67673 - deprecate this (debugging...)
return $this->dataset_from_files($files);
}
/**
@ -190,7 +189,8 @@ abstract class advanced_testcase extends base_testcase {
* @return phpunit_ArrayDataSet
*/
protected function createArrayDataSet(array $data) {
return new phpunit_ArrayDataSet($data);
// TODO: MDL-67673 - deprecate this (debugging...)
return $this->dataset_from_array($data);
}
/**
@ -198,36 +198,59 @@ abstract class advanced_testcase extends base_testcase {
*
* Note: it is usually better to use data generators
*
* @param PHPUnit\DbUnit\DataSet\IDataSet $dataset
* @param phpunit_dataset $dataset
* @return void
*/
protected function loadDataSet(PHPUnit\DbUnit\DataSet\IDataSet $dataset) {
global $DB;
protected function loadDataSet(phpunit_dataset $dataset) {
// TODO: MDL-67673 - deprecate this (debugging...)
$dataset->to_database();
}
$structure = phpunit_util::get_tablestructure();
/**
* Creates a new dataset from CVS/XML files.
*
* 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 $files full paths to CSV or XML files to load.
* @return phpunit_dataset
*/
protected function dataset_from_files(array $files) {
// We ignore $delimiter, $enclosure and $escape, use the default ones in your fixtures.
$dataset = new phpunit_dataset();
$dataset->from_files($files);
return $dataset;
}
foreach($dataset->getTableNames() as $tablename) {
$table = $dataset->getTable($tablename);
$metadata = $dataset->getTableMetaData($tablename);
$columns = $metadata->getColumns();
/**
* Creates a new dataset from string (CSV or XML).
*
* @param string $content contents (CSV or XML) to load.
* @param string $type format of the content to be loaded (csv or xml).
* @param string $table name of the table which the file belongs to (only for CSV files).
*/
protected function dataset_from_string(string $content, string $type, ?string $table = null) {
$dataset = new phpunit_dataset();
$dataset->from_string($content, $type, $table);
return $dataset;
}
$doimport = false;
if (isset($structure[$tablename]['id']) and $structure[$tablename]['id']->auto_increment) {
$doimport = in_array('id', $columns);
}
for($r=0; $r<$table->getRowCount(); $r++) {
$record = $table->getRow($r);
if ($doimport) {
$DB->import_record($tablename, $record);
} else {
$DB->insert_record($tablename, $record);
}
}
if ($doimport) {
$DB->get_manager()->reset_sequence(new xmldb_table($tablename));
}
}
/**
* Creates a new dataset from PHP array.
*
* @param array $data array of tables, see {@see phpunit_dataset::from_array()} for supported formats.
* @return phpunit_dataset
*/
protected function dataset_from_array(array $data) {
$dataset = new phpunit_dataset();
$dataset->from_array($data);
return $dataset;
}
/**

View file

@ -318,26 +318,42 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
$this->assertFalse($DB->get_record('user', array('id'=>9999)));
}
public function test_load_dataset() {
public function test_load_data_dataset_xml() {
global $DB;
$this->resetAfterTest();
$this->assertFalse($DB->record_exists('user', array('id'=>5)));
$this->assertFalse($DB->record_exists('user', array('id'=>7)));
$this->assertFalse($DB->record_exists('user', array('id' => 5)));
$this->assertFalse($DB->record_exists('user', array('id' => 7)));
$dataset = $this->createXMLDataSet(__DIR__.'/fixtures/sample_dataset.xml');
$this->loadDataSet($dataset);
$this->assertTrue($DB->record_exists('user', array('id'=>5)));
$this->assertTrue($DB->record_exists('user', array('id'=>7)));
$user5 = $DB->get_record('user', array('id'=>5));
$user7 = $DB->get_record('user', array('id'=>7));
$this->assertSame('john.doe', $user5->username);
$this->assertSame('jane.doe', $user7->username);
$this->assertTrue($DB->record_exists('user', array('id' => 5)));
$this->assertTrue($DB->record_exists('user', array('id' => 7)));
$user5 = $DB->get_record('user', array('id' => 5));
$user7 = $DB->get_record('user', array('id' => 7));
$this->assertSame('bozka.novakova', $user5->username);
$this->assertSame('pepa.novak', $user7->username);
$dataset = $this->createCsvDataSet(array('user'=>__DIR__.'/fixtures/sample_dataset.csv'));
}
public function test_load_dataset_csv() {
global $DB;
$this->resetAfterTest();
$this->assertFalse($DB->record_exists('user', array('id' => 8)));
$this->assertFalse($DB->record_exists('user', array('id' => 9)));
$dataset = $this->createCsvDataSet(array('user' => __DIR__.'/fixtures/sample_dataset.csv'));
$this->loadDataSet($dataset);
$this->assertEquals(8, $DB->get_field('user', 'id', array('username'=>'pepa.novak')));
$this->assertEquals(9, $DB->get_field('user', 'id', array('username'=>'bozka.novakova')));
$this->assertEquals(5, $DB->get_field('user', 'id', array('username' => 'bozka.novakova')));
$this->assertEquals(7, $DB->get_field('user', 'id', array('username' => 'pepa.novak')));
}
public function test_load_dataset_array() {
global $DB;
$this->resetAfterTest();
$data = array(
'user' => array(
@ -346,21 +362,24 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
array('low.secret', 'low@example.com'),
),
);
$this->assertFalse($DB->record_exists('user', array('email' => 'top@example.com')));
$this->assertFalse($DB->record_exists('user', array('email' => 'low@example.com')));
$dataset = $this->createArrayDataSet($data);
$this->loadDataSet($dataset);
$this->assertTrue($DB->record_exists('user', array('email'=>'top@example.com')));
$this->assertTrue($DB->record_exists('user', array('email'=>'low@example.com')));
$this->assertTrue($DB->record_exists('user', array('email' => 'top@example.com')));
$this->assertTrue($DB->record_exists('user', array('email' => 'low@example.com')));
$data = array(
'user' => array(
array('username'=>'noidea', 'email'=>'noidea@example.com'),
array('username'=>'onemore', 'email'=>'onemore@example.com'),
array('username' => 'noidea', 'email' => 'noidea@example.com'),
array('username' => 'onemore', 'email' => 'onemore@example.com'),
),
);
$dataset = $this->createArrayDataSet($data);
$this->loadDataSet($dataset);
$this->assertTrue($DB->record_exists('user', array('username'=>'noidea')));
$this->assertTrue($DB->record_exists('user', array('username'=>'onemore')));
$this->assertTrue($DB->record_exists('user', array('username' => 'noidea')));
$this->assertTrue($DB->record_exists('user', array('username' => 'onemore')));
}
public function test_assert_time_current() {