From ef75b5e9ab845f0cb1b7fa3343654b8713738638 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Tue, 19 Dec 2017 12:11:49 -0500 Subject: [PATCH] SQLite driver tweaks --- lib/Db/AbstractDriver.php | 4 +++ lib/Db/SQLite3/Driver.php | 10 +++---- .../SQLite3/TestDbDriverCreationSQLite3.php | 30 +++++++++---------- .../cases/Db/SQLite3/TestDbDriverSQLite3.php | 2 +- .../cases/Db/SQLite3/TestDbResultSQLite3.php | 4 ++- .../cases/Db/SQLite3/TestDbUpdateSQLite3.php | 8 ++++- 6 files changed, 35 insertions(+), 23 deletions(-) diff --git a/lib/Db/AbstractDriver.php b/lib/Db/AbstractDriver.php index 1f106f9..624074c 100644 --- a/lib/Db/AbstractDriver.php +++ b/lib/Db/AbstractDriver.php @@ -13,6 +13,8 @@ abstract class AbstractDriver implements Driver { protected $transDepth = 0; protected $transStatus = []; + protected abstract function getError(): string; + /** @codeCoverageIgnore */ public function schemaVersion(): int { // FIXME: generic schemaVersion() will need to be covered for database engines other than SQLite @@ -46,6 +48,8 @@ abstract class AbstractDriver implements Driver { $sql = @file_get_contents($file); if ($sql===false) { throw new Exception("updateFileUnusable", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]); // @codeCoverageIgnore + } elseif ($sql==="") { + throw new Exception("updateFileIncomplete", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]); } try { $this->exec($sql); diff --git a/lib/Db/SQLite3/Driver.php b/lib/Db/SQLite3/Driver.php index a2eace4..7c7beea 100644 --- a/lib/Db/SQLite3/Driver.php +++ b/lib/Db/SQLite3/Driver.php @@ -27,10 +27,9 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { } // if no database file is specified in the configuration, use a suitable default $dbFile = $dbFile ?? Arsse::$conf->dbSQLite3File ?? \JKingWeb\Arsse\BASE."arsse.db"; - $mode = \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE; $timeout = Arsse::$conf->dbSQLite3Timeout * 1000; try { - $this->makeConnection($dbFile, $mode, Arsse::$conf->dbSQLite3Key); + $this->makeConnection($dbFile, Arsse::$conf->dbSQLite3Key); // set the timeout; parameters are not allowed for pragmas, but this usage should be safe $this->exec("PRAGMA busy_timeout = $timeout"); // set other initial options @@ -62,8 +61,8 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { return class_exists("SQLite3"); } - protected function makeConnection(string $file, int $opts, string $key) { - $this->db = new \SQLite3($file, $opts, $key); + protected function makeConnection(string $file, string $key) { + $this->db = new \SQLite3($file, \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE, $key); // enable exceptions $this->db->enableExceptions(true); } @@ -76,6 +75,7 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { unset($this->db); } + /** @codeCoverageIgnore */ public static function create(): \JKingWeb\Arsse\Db\Driver { if (self::requirementsMet()) { return new self; @@ -96,7 +96,7 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { } public function schemaVersion(): int { - return $this->query("PRAGMA user_version")->getValue(); + return (int) $this->query("PRAGMA user_version")->getValue(); } public function schemaUpdate(int $to, string $basePath = null): bool { diff --git a/tests/cases/Db/SQLite3/TestDbDriverCreationSQLite3.php b/tests/cases/Db/SQLite3/TestDbDriverCreationSQLite3.php index 402542a..ebd9249 100644 --- a/tests/cases/Db/SQLite3/TestDbDriverCreationSQLite3.php +++ b/tests/cases/Db/SQLite3/TestDbDriverCreationSQLite3.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace JKingWeb\Arsse; use JKingWeb\Arsse\Arsse; +use JKingWeb\Arsse\Db\SQLite3\Driver; use org\bovigo\vfs\vfsStream; use Phake; @@ -19,7 +20,7 @@ class TestDbDriverCreationSQLite3 extends Test\AbstractTest { protected $ch; public function setUp() { - if (!extension_loaded("sqlite3")) { + if (!Driver::requirementsMet()) { $this->markTestSkipped("SQLite extension not loaded"); } $this->clearData(); @@ -107,7 +108,6 @@ class TestDbDriverCreationSQLite3 extends Test\AbstractTest { // set up configuration Arsse::$conf = new Conf(); Arsse::$conf->dbSQLite3File = ":memory:"; - // set up database shim } public function tearDown() { @@ -117,78 +117,78 @@ class TestDbDriverCreationSQLite3 extends Test\AbstractTest { public function testFailToCreateDatabase() { Arsse::$conf->dbSQLite3File = $this->path."Cmain/arsse.db"; $this->assertException("fileUncreatable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToCreateJournal() { Arsse::$conf->dbSQLite3File = $this->path."Cwal/arsse.db"; $this->assertException("fileUncreatable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToCreateSharedMmeory() { Arsse::$conf->dbSQLite3File = $this->path."Cshm/arsse.db"; $this->assertException("fileUncreatable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToReadDatabase() { Arsse::$conf->dbSQLite3File = $this->path."Rmain/arsse.db"; $this->assertException("fileUnreadable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToReadJournal() { Arsse::$conf->dbSQLite3File = $this->path."Rwal/arsse.db"; $this->assertException("fileUnreadable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToReadSharedMmeory() { Arsse::$conf->dbSQLite3File = $this->path."Rshm/arsse.db"; $this->assertException("fileUnreadable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToWriteToDatabase() { Arsse::$conf->dbSQLite3File = $this->path."Wmain/arsse.db"; $this->assertException("fileUnwritable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToWriteToJournal() { Arsse::$conf->dbSQLite3File = $this->path."Wwal/arsse.db"; $this->assertException("fileUnwritable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToWriteToSharedMmeory() { Arsse::$conf->dbSQLite3File = $this->path."Wshm/arsse.db"; $this->assertException("fileUnwritable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToAccessDatabase() { Arsse::$conf->dbSQLite3File = $this->path."Amain/arsse.db"; $this->assertException("fileUnusable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToAccessJournal() { Arsse::$conf->dbSQLite3File = $this->path."Awal/arsse.db"; $this->assertException("fileUnusable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testFailToAccessSharedMmeory() { Arsse::$conf->dbSQLite3File = $this->path."Ashm/arsse.db"; $this->assertException("fileUnusable", "Db"); - new Db\SQLite3\Driver; + new Driver; } public function testAssumeDatabaseCorruption() { Arsse::$conf->dbSQLite3File = $this->path."corrupt/arsse.db"; $this->assertException("fileCorrupt", "Db"); - new Db\SQLite3\Driver; + new Driver; } } diff --git a/tests/cases/Db/SQLite3/TestDbDriverSQLite3.php b/tests/cases/Db/SQLite3/TestDbDriverSQLite3.php index 051ebae..224f2cc 100644 --- a/tests/cases/Db/SQLite3/TestDbDriverSQLite3.php +++ b/tests/cases/Db/SQLite3/TestDbDriverSQLite3.php @@ -15,7 +15,7 @@ class TestDbDriverSQLite3 extends Test\AbstractTest { protected $ch; public function setUp() { - if (!extension_loaded("sqlite3")) { + if (!Db\SQLite3\Driver::requirementsMet()) { $this->markTestSkipped("SQLite extension not loaded"); } $this->clearData(); diff --git a/tests/cases/Db/SQLite3/TestDbResultSQLite3.php b/tests/cases/Db/SQLite3/TestDbResultSQLite3.php index 19ae8db..31b5bca 100644 --- a/tests/cases/Db/SQLite3/TestDbResultSQLite3.php +++ b/tests/cases/Db/SQLite3/TestDbResultSQLite3.php @@ -11,7 +11,8 @@ class TestDbResultSQLite3 extends Test\AbstractTest { protected $c; public function setUp() { - if (!extension_loaded("sqlite3")) { + $this->clearData(); + if (!Db\SQLite3\Driver::requirementsMet()) { $this->markTestSkipped("SQLite extension not loaded"); } $c = new \SQLite3(":memory:"); @@ -22,6 +23,7 @@ class TestDbResultSQLite3 extends Test\AbstractTest { public function tearDown() { $this->c->close(); unset($this->c); + $this->clearData(); } public function testConstructResult() { diff --git a/tests/cases/Db/SQLite3/TestDbUpdateSQLite3.php b/tests/cases/Db/SQLite3/TestDbUpdateSQLite3.php index 61e5ced..7c48d2f 100644 --- a/tests/cases/Db/SQLite3/TestDbUpdateSQLite3.php +++ b/tests/cases/Db/SQLite3/TestDbUpdateSQLite3.php @@ -68,6 +68,12 @@ class TestDbUpdateSQLite3 extends Test\AbstractTest { $this->drv->schemaUpdate(1, $this->base); } + public function testLoadEmptyFile() { + file_put_contents($this->path."0.sql", ""); + $this->assertException("updateFileIncomplete", "Db"); + $this->drv->schemaUpdate(1, $this->base); + } + public function testLoadCorrectFile() { file_put_contents($this->path."0.sql", self::MINIMAL1); $this->drv->schemaUpdate(1, $this->base); @@ -76,7 +82,7 @@ class TestDbUpdateSQLite3 extends Test\AbstractTest { public function testPerformPartialUpdate() { file_put_contents($this->path."0.sql", self::MINIMAL1); - file_put_contents($this->path."1.sql", ""); + file_put_contents($this->path."1.sql", " "); $this->assertException("updateFileIncomplete", "Db"); try { $this->drv->schemaUpdate(2, $this->base);