2017-03-28 03:19:05 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\Test\Db;
|
2017-03-28 03:19:05 +00:00
|
|
|
|
|
|
|
trait Tools {
|
2017-03-28 22:50:00 +00:00
|
|
|
protected $drv;
|
|
|
|
|
|
|
|
|
2017-03-30 03:41:05 +00:00
|
|
|
function primeDatabase(array $data): bool {
|
|
|
|
$this->drv->begin();
|
2017-03-28 03:19:05 +00:00
|
|
|
foreach($data as $table => $info) {
|
|
|
|
$cols = implode(",", array_keys($info['columns']));
|
|
|
|
$bindings = array_values($info['columns']);
|
|
|
|
$params = implode(",", array_fill(0, sizeof($info['columns']), "?"));
|
2017-03-28 22:50:00 +00:00
|
|
|
$s = $this->drv->prepareArray("INSERT INTO $table($cols) values($params)", $bindings);
|
2017-03-28 03:19:05 +00:00
|
|
|
foreach($info['rows'] as $row) {
|
|
|
|
$this->assertEquals(1, $s->runArray($row)->changes());
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 03:41:05 +00:00
|
|
|
$this->drv->commit();
|
2017-03-28 03:19:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-30 03:41:05 +00:00
|
|
|
function compareExpectations(array $expected): bool {
|
2017-03-28 03:19:05 +00:00
|
|
|
foreach($expected as $table => $info) {
|
|
|
|
$cols = implode(",", array_keys($info['columns']));
|
2017-03-28 22:50:00 +00:00
|
|
|
foreach($this->drv->prepare("SELECT $cols from $table")->run() as $num => $row) {
|
2017-03-28 03:19:05 +00:00
|
|
|
$row = array_values($row);
|
2017-03-30 03:41:05 +00:00
|
|
|
$this->assertSame($expected[$table]['rows'][$num], $row, "Row ".($num+1)." of table $table does not match expectations at array index $num.");
|
2017-03-28 03:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-30 03:41:05 +00:00
|
|
|
return true;
|
2017-03-28 03:19:05 +00:00
|
|
|
}
|
2017-03-28 22:50:00 +00:00
|
|
|
|
2017-03-30 03:41:05 +00:00
|
|
|
function primeExpectations(array $source, array $tableSpecs = null): array {
|
|
|
|
$out = [];
|
|
|
|
foreach($tableSpecs as $table => $columns) {
|
|
|
|
if(!isset($source[$table])) {
|
|
|
|
$this->assertTrue(false, "Source for expectations does not contain requested table $table.");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$out[$table] = [
|
|
|
|
'columns' => [],
|
|
|
|
'rows' => [],
|
|
|
|
];
|
|
|
|
$transformations = [];
|
|
|
|
foreach($columns as $target => $col) {
|
|
|
|
if(!isset($source[$table]['columns'][$col])) {
|
|
|
|
$this->assertTrue(false, "Source for expectations does not contain requested column $col of table $table.");
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$found = array_search($col, array_keys($source[$table]['columns']));
|
|
|
|
$transformations[$found] = $target;
|
|
|
|
$out[$table]['columns'][$col] = $source[$table]['columns'][$col];
|
|
|
|
}
|
|
|
|
foreach($source[$table]['rows'] as $sourceRow) {
|
|
|
|
$newRow = [];
|
|
|
|
foreach($transformations as $from => $to) {
|
|
|
|
$newRow[$to] = $sourceRow[$from];
|
|
|
|
}
|
|
|
|
$out[$table]['rows'][] = $newRow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
2017-03-28 22:50:00 +00:00
|
|
|
}
|
2017-03-28 03:19:05 +00:00
|
|
|
}
|