2017-03-28 03:19:05 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2017-03-31 17:24:00 +00:00
|
|
|
namespace JKingWeb\Arsse\Test\Database;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-03-31 17:24:00 +00:00
|
|
|
use JKingWeb\Arsse\User\Driver as UserDriver;
|
2017-07-17 11:47:57 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
2017-03-31 17:24:00 +00:00
|
|
|
use JKingWeb\Arsse\Conf;
|
|
|
|
use JKingWeb\Arsse\User;
|
2017-05-04 00:00:29 +00:00
|
|
|
use JKingWeb\Arsse\Test\Database;
|
2017-05-15 03:03:48 +00:00
|
|
|
use JKingWeb\Arsse\Db\Result;
|
2017-03-31 17:24:00 +00:00
|
|
|
use Phake;
|
2017-03-28 03:19:05 +00:00
|
|
|
|
2017-03-31 17:24:00 +00:00
|
|
|
trait Setup {
|
2017-04-07 01:41:21 +00:00
|
|
|
protected $drv;
|
2017-07-18 20:38:23 +00:00
|
|
|
protected $primed = false;
|
2017-03-31 17:24:00 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function setUp() {
|
2017-03-31 17:24:00 +00:00
|
|
|
// establish a clean baseline
|
|
|
|
$this->clearData();
|
|
|
|
// create a default configuration
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$conf = new Conf();
|
2017-03-31 17:24:00 +00:00
|
|
|
// configure and create the relevant database driver
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->setUpDriver();
|
2017-03-31 17:24:00 +00:00
|
|
|
// create the database interface with the suitable driver
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$db = new Database($this->drv);
|
2017-07-18 20:38:23 +00:00
|
|
|
Arsse::$db->driverSchemaUpdate();
|
2017-03-31 17:24:00 +00:00
|
|
|
// create a mock user manager
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$user = Phake::mock(User::class);
|
|
|
|
Phake::when(Arsse::$user)->authorize->thenReturn(true);
|
2017-03-31 17:24:00 +00:00
|
|
|
// call the additional setup method if it exists
|
2017-08-29 14:50:31 +00:00
|
|
|
if (method_exists($this, "setUpSeries")) {
|
|
|
|
$this->setUpSeries();
|
|
|
|
}
|
2017-07-18 20:38:23 +00:00
|
|
|
// prime the database with series data if it hasn't already been done
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!$this->primed && isset($this->data)) {
|
|
|
|
$this->primeDatabase($this->data);
|
|
|
|
}
|
2017-03-31 17:24:00 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function tearDown() {
|
2017-03-31 17:24:00 +00:00
|
|
|
// call the additional teardiwn method if it exists
|
2017-08-29 14:50:31 +00:00
|
|
|
if (method_exists($this, "tearDownSeries")) {
|
|
|
|
$this->tearDownSeries();
|
|
|
|
}
|
2017-03-31 17:24:00 +00:00
|
|
|
// clean up
|
2017-07-18 20:38:23 +00:00
|
|
|
$this->primed = false;
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->drv = null;
|
2017-03-31 17:24:00 +00:00
|
|
|
$this->clearData();
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-31 17:24:00 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function primeDatabase(array $data): bool {
|
2017-05-06 16:02:27 +00:00
|
|
|
$tr = $this->drv->begin();
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($data as $table => $info) {
|
2017-04-07 01:41:21 +00:00
|
|
|
$cols = implode(",", array_keys($info['columns']));
|
|
|
|
$bindings = array_values($info['columns']);
|
|
|
|
$params = implode(",", array_fill(0, sizeof($info['columns']), "?"));
|
|
|
|
$s = $this->drv->prepareArray("INSERT INTO $table($cols) values($params)", $bindings);
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($info['rows'] as $row) {
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertEquals(1, $s->runArray($row)->changes());
|
|
|
|
}
|
|
|
|
}
|
2017-05-06 16:02:27 +00:00
|
|
|
$tr->commit();
|
2017-07-18 20:38:23 +00:00
|
|
|
$this->primed = true;
|
2017-04-07 01:41:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-03-28 03:19:05 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function compareExpectations(array $expected): bool {
|
|
|
|
foreach ($expected as $table => $info) {
|
2017-04-07 01:41:21 +00:00
|
|
|
$cols = implode(",", array_keys($info['columns']));
|
|
|
|
$data = $this->drv->prepare("SELECT $cols from $table")->run()->getAll();
|
|
|
|
$cols = array_keys($info['columns']);
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($info['rows'] as $index => $row) {
|
2017-06-02 18:12:36 +00:00
|
|
|
$this->assertCount(sizeof($cols), $row, "The number of values for array index $index does not match the number of fields");
|
|
|
|
$row = array_combine($cols, $row);
|
|
|
|
$this->assertContains($row, $data, "Table $table does not contain record at array index $index.");
|
2017-05-18 17:21:17 +00:00
|
|
|
$found = array_search($row, $data, true);
|
2017-04-07 01:41:21 +00:00
|
|
|
unset($data[$found]);
|
|
|
|
}
|
|
|
|
$this->assertSame([], $data);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-28 22:50:00 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function primeExpectations(array $source, array $tableSpecs = null): array {
|
2017-04-07 01:41:21 +00:00
|
|
|
$out = [];
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($tableSpecs as $table => $columns) {
|
2017-06-02 18:12:36 +00:00
|
|
|
// make sure the source has the table we want
|
|
|
|
$this->assertArrayHasKey($table, $source, "Source for expectations does not contain requested table $table.");
|
2017-04-07 01:41:21 +00:00
|
|
|
$out[$table] = [
|
|
|
|
'columns' => [],
|
2017-08-29 14:50:31 +00:00
|
|
|
'rows' => array_fill(0, sizeof($source[$table]['rows']), []),
|
2017-04-07 01:41:21 +00:00
|
|
|
];
|
2017-06-02 18:12:36 +00:00
|
|
|
// make sure the source has all the columns we want for the table
|
|
|
|
$cols = array_flip($columns);
|
|
|
|
$cols = array_intersect_key($cols, $source[$table]['columns']);
|
|
|
|
$this->assertSame(array_keys($cols), $columns, "Source for table $table does not contain all requested columns");
|
|
|
|
// get a map of source value offsets and keys
|
|
|
|
$targets = array_flip(array_keys($source[$table]['columns']));
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($cols as $key => $order) {
|
2017-06-02 18:12:36 +00:00
|
|
|
// fill the column-spec
|
|
|
|
$out[$table]['columns'][$key] = $source[$table]['columns'][$key];
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($source[$table]['rows'] as $index => $row) {
|
2017-06-02 18:12:36 +00:00
|
|
|
// fill each row column-wise with re-ordered values
|
|
|
|
$out[$table]['rows'][$index][$order] = $row[$targets[$key]];
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $out;
|
|
|
|
}
|
2017-05-15 03:03:48 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function assertResult(array $expected, Result $data) {
|
2017-06-02 18:12:36 +00:00
|
|
|
$data = $data->getAll();
|
|
|
|
$this->assertCount(sizeof($expected), $data, "Number of result rows (".sizeof($data).") differs from number of expected rows (".sizeof($expected).")");
|
2017-08-29 14:50:31 +00:00
|
|
|
if (sizeof($expected)) {
|
2017-06-02 18:12:36 +00:00
|
|
|
// make sure the expectations are consistent
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($expected as $exp) {
|
|
|
|
if (!isset($keys)) {
|
2017-06-02 18:12:36 +00:00
|
|
|
$keys = $exp;
|
|
|
|
continue;
|
2017-05-15 03:03:48 +00:00
|
|
|
}
|
2017-06-02 18:12:36 +00:00
|
|
|
$this->assertSame(array_keys($keys), array_keys($exp), "Result set expectations are irregular");
|
|
|
|
}
|
|
|
|
// filter the result set to contain just the desired keys (we don't care if the result has extra keys)
|
|
|
|
$rows = [];
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($data as $row) {
|
2017-06-02 18:12:36 +00:00
|
|
|
$rows[] = array_intersect_key($row, $keys);
|
2017-05-15 03:03:48 +00:00
|
|
|
}
|
2017-06-02 18:12:36 +00:00
|
|
|
// compare the result set to the expectations
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($expected as $index => $exp) {
|
2017-06-02 18:12:36 +00:00
|
|
|
$this->assertContains($exp, $rows, "Result set does not contain record at array index $index.");
|
|
|
|
$found = array_search($exp, $rows, true);
|
|
|
|
unset($rows[$found]);
|
2017-05-15 03:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|