From cc875be57e9ac6dec89e6ca92b1379920109a059 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Thu, 19 Oct 2017 15:32:18 -0400 Subject: [PATCH] Backport testing improvements from ttrss branch --- lib/Db/SQLite3/Driver.php | 4 ++-- tests/Db/SQLite3/TestDbDriverSQLite3.php | 3 ++- tests/Db/SQLite3/TestDbUpdateSQLite3.php | 2 +- tests/lib/Database/DriverSQLite3.php | 2 +- tests/lib/Database/Setup.php | 30 +++++++++++++++++------- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/Db/SQLite3/Driver.php b/lib/Db/SQLite3/Driver.php index d65ff2e9..46b11105 100644 --- a/lib/Db/SQLite3/Driver.php +++ b/lib/Db/SQLite3/Driver.php @@ -16,13 +16,13 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { protected $db; - public function __construct() { + public function __construct(string $dbFile = null) { // check to make sure required extension is loaded if (!class_exists("SQLite3")) { throw new Exception("extMissing", self::driverName()); // @codeCoverageIgnore } // if no database file is specified in the configuration, use a suitable default - $dbFile = Arsse::$conf->dbSQLite3File ?? \JKingWeb\Arsse\BASE."arsse.db"; + $dbFile = $dbFile ?? Arsse::$conf->dbSQLite3File ?? \JKingWeb\Arsse\BASE."arsse.db"; $mode = \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE; $timeout = Arsse::$conf->dbSQLite3Timeout * 1000; try { diff --git a/tests/Db/SQLite3/TestDbDriverSQLite3.php b/tests/Db/SQLite3/TestDbDriverSQLite3.php index 4a2058dc..0c80b5b3 100644 --- a/tests/Db/SQLite3/TestDbDriverSQLite3.php +++ b/tests/Db/SQLite3/TestDbDriverSQLite3.php @@ -18,8 +18,9 @@ class TestDbDriverSQLite3 extends Test\AbstractTest { $conf = new Conf(); Arsse::$conf = $conf; $conf->dbDriver = Db\SQLite3\Driver::class; + $conf->dbSQLite3Timeout = 0; $conf->dbSQLite3File = tempnam(sys_get_temp_dir(), 'ook'); - $this->drv = new Db\SQLite3\Driver(true); + $this->drv = new Db\SQLite3\Driver(); $this->ch = new \SQLite3(Arsse::$conf->dbSQLite3File); $this->ch->enableExceptions(true); } diff --git a/tests/Db/SQLite3/TestDbUpdateSQLite3.php b/tests/Db/SQLite3/TestDbUpdateSQLite3.php index a71c6050..34412638 100644 --- a/tests/Db/SQLite3/TestDbUpdateSQLite3.php +++ b/tests/Db/SQLite3/TestDbUpdateSQLite3.php @@ -30,7 +30,7 @@ class TestDbUpdateSQLite3 extends Test\AbstractTest { Arsse::$conf = $conf; $this->base = $this->vfs->url(); $this->path = $this->base."/SQLite3/"; - $this->drv = new Db\SQLite3\Driver(true); + $this->drv = new Db\SQLite3\Driver(); } public function tearDown() { diff --git a/tests/lib/Database/DriverSQLite3.php b/tests/lib/Database/DriverSQLite3.php index a898a084..ca096e4a 100644 --- a/tests/lib/Database/DriverSQLite3.php +++ b/tests/lib/Database/DriverSQLite3.php @@ -11,7 +11,7 @@ trait DriverSQLite3 { $this->markTestSkipped("SQLite extension not loaded"); } Arsse::$conf->dbSQLite3File = ":memory:"; - $this->drv = new Driver(true); + $this->drv = new Driver(); } public function nextID(string $table): int { diff --git a/tests/lib/Database/Setup.php b/tests/lib/Database/Setup.php index 3dfba36b..8267b0cd 100644 --- a/tests/lib/Database/Setup.php +++ b/tests/lib/Database/Setup.php @@ -48,15 +48,16 @@ trait Setup { $this->clearData(); } - public function primeDatabase(array $data): bool { - $tr = $this->drv->begin(); + public function primeDatabase(array $data, \JKingWeb\Arsse\Db\Driver $drv = null): bool { + $drv = $drv ?? $this->drv; + $tr = $drv->begin(); foreach ($data as $table => $info) { $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); + $s = $drv->prepareArray("INSERT INTO $table($cols) values($params)", $bindings); foreach ($info['rows'] as $row) { - $this->assertEquals(1, $s->runArray($row)->changes()); + $s->runArray($row); } } $tr->commit(); @@ -64,6 +65,16 @@ trait Setup { return true; } + public function primeFile(string $file, array $data = null): bool { + $data = $data ?? $this->data; + $primed = $this->primed; + $drv = new \JKingWeb\Arsse\Db\SQLite3\Driver($file); + $drv->schemaUpdate(\JKingWeb\Arsse\Database::SCHEMA_VERSION); + $this->primeDatabase($data, $drv); + $this->primed = $primed; + return true; + } + public function compareExpectations(array $expected): bool { foreach ($expected as $table => $info) { $cols = implode(",", array_keys($info['columns'])); @@ -126,11 +137,12 @@ trait Setup { $rows[] = array_intersect_key($row, $keys); } // compare the result set to the expectations - foreach ($expected as $index => $exp) { - $this->assertContains($exp, $rows, "Result set does not contain record at array index $index."); - $found = array_search($exp, $rows, true); - unset($rows[$found]); + foreach ($rows as $row) { + $this->assertContains($row, $expected, "Result set contains unexpected record."); + $found = array_search($row, $expected); + unset($expected[$found]); } + $this->assertArraySubset($expected, [], "Expectations not in result set."); } } -} +} \ No newline at end of file