friendica_2021-01/tests/Util/Database/YamlDataSet.php
Philipp Holzer b08ac3c0a7
Remove Phpunit/Dbunit
- Introduce own Yaml-to-SQL class
- Introduce new way of MySQL-DB-tests (per rollback)
- Remove dependency phpunit/dbunit
- Introduce new dev-dependency for YAML-ready (Symfony YAML reader)
2019-07-28 17:40:42 +02:00

50 lines
848 B
PHP

<?php
namespace Friendica\Test\Util\Database;
use Friendica\Database\Database;
use Symfony\Component\Yaml\Yaml;
/**
* Util class to load YAML files into the database
*/
class YamlDataSet
{
/**
* @var array
*/
private $tables = [];
public function __construct(string $yamlFile)
{
$this->addYamlFile($yamlFile);
}
public function addYamlFile(string $yamlFile)
{
$data = Yaml::parse(file_get_contents($yamlFile));
foreach ($data as $tableName => $rows) {
if (!isset($rows)) {
$rows = [];
}
if (!is_array($rows)) {
continue;
}
foreach ($rows as $key => $value) {
$this->tables[$tableName][$key] = $value;
}
}
}
public function load(Database $database)
{
foreach ($this->tables as $tableName => $rows) {
foreach ($rows as $row) {
$database->insert($tableName, $row);
}
}
}
}