diff --git a/bootstrap.php b/bootstrap.php index 5802d7a..339fde4 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -6,7 +6,5 @@ const BASE = __DIR__.DIRECTORY_SEPARATOR; const NS_BASE = __NAMESPACE__."\\"; const VERSION = "0.0.0"; -if(!defined(NS_BASE."INSTALL")) define(NS_BASE."INSTALL", false); - require_once BASE."vendor".DIRECTORY_SEPARATOR."autoload.php"; ignore_user_abort(true); \ No newline at end of file diff --git a/lib/Database.php b/lib/Database.php index 77305ca..48bf2bd 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -12,11 +12,11 @@ class Database { /** @var Db\Driver */ public $db; - public function __construct() { + public function __construct($initialize = true) { $driver = Arsse::$conf->dbDriver; - $this->db = new $driver(INSTALL); + $this->db = new $driver(); $ver = $this->db->schemaVersion(); - if(!INSTALL && $ver < self::SCHEMA_VERSION) { + if($initialize && $ver < self::SCHEMA_VERSION) { $this->db->schemaUpdate(self::SCHEMA_VERSION); } } diff --git a/lib/Db/AbstractDriver.php b/lib/Db/AbstractDriver.php index b5885c5..96d1aa7 100644 --- a/lib/Db/AbstractDriver.php +++ b/lib/Db/AbstractDriver.php @@ -62,10 +62,10 @@ abstract class AbstractDriver implements Driver { $out = false; break; case self::TR_COMMIT: - case self::TR_ROLLBACK: + case self::TR_ROLLBACK: //@codeCoverageIgnore throw new ExceptionSavepoint("stale", ['action' => "commit", 'index' => $index]); default: - throw new Exception("unknownSavepointStatus", $this->transStatus[$index]); + throw new Exception("unknownSavepointStatus", $this->transStatus[$index]); //@codeCoverageIgnore } if($index==$this->transDepth) { while($this->transDepth > 0 && $this->transStatus[$this->transDepth] > self::TR_PEND) { @@ -110,10 +110,10 @@ abstract class AbstractDriver implements Driver { $out = true; break; case self::TR_COMMIT: - case self::TR_ROLLBACK: + case self::TR_ROLLBACK: //@codeCoverageIgnore throw new ExceptionSavepoint("stale", ['action' => "rollback", 'index' => $index]); default: - throw new Exception("unknownSavepointStatus", $this->transStatus[$index]); + throw new Exception("unknownSavepointStatus", $this->transStatus[$index]); //@codeCoverageIgnore } if($index==$this->transDepth) { while($this->transDepth > 0 && $this->transStatus[$this->transDepth] > self::TR_PEND) { diff --git a/lib/Db/AbstractStatement.php b/lib/Db/AbstractStatement.php index 3e3f52c..c21e04c 100644 --- a/lib/Db/AbstractStatement.php +++ b/lib/Db/AbstractStatement.php @@ -37,7 +37,7 @@ abstract class AbstractStatement implements Statement { $this->isNullable[] = true; } if(!array_key_exists($binding, self::TYPES)) { - throw new Exception("paramTypeInvalid", $binding); + throw new Exception("paramTypeInvalid", $binding); // @codeCoverageIgnore } $this->types[] = self::TYPES[$binding]; } @@ -83,7 +83,7 @@ abstract class AbstractStatement implements Statement { } return $v; default: - throw new Exception("paramTypeUnknown", $type); + throw new Exception("paramTypeUnknown", $type); // @codeCoverageIgnore } } } \ No newline at end of file diff --git a/lib/Db/Driver.php b/lib/Db/Driver.php index 57cf873..6ea63bc 100644 --- a/lib/Db/Driver.php +++ b/lib/Db/Driver.php @@ -9,7 +9,7 @@ interface Driver { const TR_PEND_COMMIT = -1; const TR_PEND_ROLLBACK = -2; - function __construct(bool $install = false); + function __construct(); // returns a human-friendly name for the driver (for display in installer, for example) static function driverName(): string; // returns the version of the scheme of the opened database; if uninitialized should return 0 diff --git a/lib/Db/SQLite3/Driver.php b/lib/Db/SQLite3/Driver.php index c04124d..a12f5ed 100644 --- a/lib/Db/SQLite3/Driver.php +++ b/lib/Db/SQLite3/Driver.php @@ -16,36 +16,39 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { protected $db; - public function __construct(bool $install = false) { + public function __construct() { // check to make sure required extension is loaded if(!class_exists("SQLite3")) { - throw new Exception("extMissing", self::driverName()); + throw new Exception("extMissing", self::driverName()); // @codeCoverageIgnore } - $file = Arsse::$conf->dbSQLite3File; - // if the file exists (or we're initializing the database), try to open it + $dbFile = Arsse::$conf->dbSQLite3File; + $mode = \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE; try { - $this->db = $this->makeConnection($file, \SQLITE3_OPEN_CREATE | \SQLITE3_OPEN_READWRITE, Arsse::$conf->dbSQLite3Key); + $this->db = $this->makeConnection($dbFile, $mode, Arsse::$conf->dbSQLite3Key); // set initial options $this->db->enableExceptions(true); $this->exec("PRAGMA journal_mode = wal"); $this->exec("PRAGMA foreign_keys = yes"); } catch(\Throwable $e) { // if opening the database doesn't work, check various pre-conditions to find out what the problem might be - if(!file_exists($file)) { - if($install && !is_writable(dirname($file))) { - throw new Exception("fileUncreatable", dirname($file)); + $files = [ + $dbFile, // main database file + $dbFile."-wal", // write-ahead log journal + $dbFile."-shm", // shared memory index + ]; + foreach($files as $file) { + if(!file_exists($file) && !is_writable(dirname($file))) { + throw new Exception("fileUncreatable", $file); + } else if(!is_readable($file) && !is_writable($file)) { + throw new Exception("fileUnusable", $file); + } else if(!is_readable($file)) { + throw new Exception("fileUnreadable", $file); + } else if(!is_writable($file)) { + throw new Exception("fileUnwritable", $file); } - throw new Exception("fileMissing", $file); - } - if(!is_readable($file) && !is_writable($file)) { - throw new Exception("fileUnusable", $file); - } else if(!is_readable($file)) { - throw new Exception("fileUnreadable", $file); - } else if(!is_writable($file)) { - throw new Exception("fileUnwritable", $file); } // otherwise the database is probably corrupt - throw new Exception("fileCorrupt", $mainfile); + throw new Exception("fileCorrupt", $dbFile); } } @@ -89,7 +92,7 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { } $sql = @file_get_contents($file); if($sql===false) { - throw new Exception("updateFileUnusable", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]); + throw new Exception("updateFileUnusable", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]); // @codeCoverageIgnore } try { $this->exec($sql); diff --git a/lib/Service/Forking/Driver.php b/lib/Service/Forking/Driver.php index b4d1e91..4dba4b9 100644 --- a/lib/Service/Forking/Driver.php +++ b/lib/Service/Forking/Driver.php @@ -26,7 +26,9 @@ class Driver implements \JKingWeb\Arsse\Service\Driver { $pp = []; while($this->queue) { $id = (int) array_shift($this->queue); - array_push($pp, popen('"'.\PHP_BINARY.'" "'.$_SERVER['argv'][0].'" feed refresh '.$id, "r")); + $php = '"'.\PHP_BINARY.'"'; + $arsse = '"'.$_SERVER['argv'][0].'"'; + array_push($pp, popen("$php $arsse feed refresh $id", "r")); } while($pp) { $p = array_pop($pp); diff --git a/tests/Db/SQLite3/TestDbDriverCreationSQLite3.php b/tests/Db/SQLite3/TestDbDriverCreationSQLite3.php new file mode 100644 index 0000000..1f345c5 --- /dev/null +++ b/tests/Db/SQLite3/TestDbDriverCreationSQLite3.php @@ -0,0 +1,189 @@ + + * @covers \JKingWeb\Arsse\Db\SQLite3\ExceptionBuilder */ +class TestDbDriverCreationSQLite3 extends Test\AbstractTest { + protected $data; + protected $drv; + protected $ch; + + function setUp() { + if(!extension_loaded("sqlite3")) { + $this->markTestSkipped("SQLite extension not loaded"); + } + $this->clearData(); + // test files + $this->files = [ + // cannot create files + 'Cmain' => [], + 'Cshm' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + ], + 'Cwal' => [ + 'arsse.db' => "", + ], + // cannot write to files + 'Wmain' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + 'arsse.db-shm' => "", + ], + 'Wwal' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + 'arsse.db-shm' => "", + ], + 'Wshm' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + 'arsse.db-shm' => "", + ], + // cannot read from files + 'Rmain' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + 'arsse.db-shm' => "", + ], + 'Rwal' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + 'arsse.db-shm' => "", + ], + 'Rshm' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + 'arsse.db-shm' => "", + ], + // can neither read from or write to files + 'Amain' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + 'arsse.db-shm' => "", + ], + 'Awal' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + 'arsse.db-shm' => "", + ], + 'Ashm' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + 'arsse.db-shm' => "", + ], + // non-filesystem errors + 'corrupt' => [ + 'arsse.db' => "", + 'arsse.db-wal' => "", + 'arsse.db-shm' => "", + ], + ]; + $vfs = vfsStream::setup("dbtest", 0777, $this->files); + $this->path = $path = $vfs->url()."/"; + // set up access blocks + chmod($path."Cmain", 0555); + chmod($path."Cwal", 0555); + chmod($path."Cshm", 0555); + chmod($path."Rmain/arsse.db", 0333); + chmod($path."Rwal/arsse.db-wal", 0333); + chmod($path."Rshm/arsse.db-shm", 0333); + chmod($path."Wmain/arsse.db", 0555); + chmod($path."Wwal/arsse.db-wal", 0555); + chmod($path."Wshm/arsse.db-shm", 0555); + chmod($path."Amain/arsse.db", 0111); + chmod($path."Awal/arsse.db-wal", 0111); + chmod($path."Ashm/arsse.db-shm", 0111); + // set up configuration + Arsse::$conf = new Conf(); + Arsse::$conf->dbSQLite3File = ":memory:"; + // set up database shim + } + + function tearDown() { + $this->clearData(); + } + + function testFailToCreateDatabase() { + Arsse::$conf->dbSQLite3File = $this->path."Cmain/arsse.db"; + $this->assertException("fileUncreatable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToCreateJournal() { + Arsse::$conf->dbSQLite3File = $this->path."Cwal/arsse.db"; + $this->assertException("fileUncreatable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToCreateSharedMmeory() { + Arsse::$conf->dbSQLite3File = $this->path."Cshm/arsse.db"; + $this->assertException("fileUncreatable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToReadDatabase() { + Arsse::$conf->dbSQLite3File = $this->path."Rmain/arsse.db"; + $this->assertException("fileUnreadable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToReadJournal() { + Arsse::$conf->dbSQLite3File = $this->path."Rwal/arsse.db"; + $this->assertException("fileUnreadable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToReadSharedMmeory() { + Arsse::$conf->dbSQLite3File = $this->path."Rshm/arsse.db"; + $this->assertException("fileUnreadable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToWriteToDatabase() { + Arsse::$conf->dbSQLite3File = $this->path."Wmain/arsse.db"; + $this->assertException("fileUnwritable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToWriteToJournal() { + Arsse::$conf->dbSQLite3File = $this->path."Wwal/arsse.db"; + $this->assertException("fileUnwritable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToWriteToSharedMmeory() { + Arsse::$conf->dbSQLite3File = $this->path."Wshm/arsse.db"; + $this->assertException("fileUnwritable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToAccessDatabase() { + Arsse::$conf->dbSQLite3File = $this->path."Amain/arsse.db"; + $this->assertException("fileUnusable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToAccessJournal() { + Arsse::$conf->dbSQLite3File = $this->path."Awal/arsse.db"; + $this->assertException("fileUnusable", "Db"); + new Db\SQLite3\Driver; + } + + function testFailToAccessSharedMmeory() { + Arsse::$conf->dbSQLite3File = $this->path."Ashm/arsse.db"; + $this->assertException("fileUnusable", "Db"); + new Db\SQLite3\Driver; + } + + function testAssumeDatabaseCorruption() { + Arsse::$conf->dbSQLite3File = $this->path."corrupt/arsse.db"; + $this->assertException("fileCorrupt", "Db"); + new Db\SQLite3\Driver; + } +} \ No newline at end of file diff --git a/tests/Db/SQLite3/TestDbDriverSQLite3.php b/tests/Db/SQLite3/TestDbDriverSQLite3.php index 1562b26..5a6a23c 100644 --- a/tests/Db/SQLite3/TestDbDriverSQLite3.php +++ b/tests/Db/SQLite3/TestDbDriverSQLite3.php @@ -16,9 +16,9 @@ class TestDbDriverSQLite3 extends Test\AbstractTest { } $this->clearData(); $conf = new Conf(); + Arsse::$conf = $conf; $conf->dbDriver = Db\SQLite3\Driver::class; $conf->dbSQLite3File = tempnam(sys_get_temp_dir(), 'ook'); - Arsse::$conf = $conf; $this->drv = new Db\SQLite3\Driver(true); $this->ch = new \SQLite3(Arsse::$conf->dbSQLite3File); $this->ch->enableExceptions(true); @@ -27,7 +27,9 @@ class TestDbDriverSQLite3 extends Test\AbstractTest { function tearDown() { unset($this->drv); unset($this->ch); - unlink(Arsse::$conf->dbSQLite3File); + if(isset(Arsse::$conf)) { + unlink(Arsse::$conf->dbSQLite3File); + } $this->clearData(); } @@ -131,17 +133,28 @@ class TestDbDriverSQLite3 extends Test\AbstractTest { $this->assertEquals(3, $this->drv->savepointCreate()); $this->assertEquals(4, $this->drv->savepointCreate()); $this->assertEquals(5, $this->drv->savepointCreate()); - $this->assertEquals(true, $this->drv->savepointUndo(3)); - $this->assertEquals(false, $this->drv->savepointRelease(4)); + $this->assertTrue($this->drv->savepointUndo(3)); + $this->assertFalse($this->drv->savepointRelease(4)); $this->assertEquals(6, $this->drv->savepointCreate()); - $this->assertEquals(false, $this->drv->savepointRelease(5)); - $this->assertEquals(true, $this->drv->savepointRelease(6)); + $this->assertFalse($this->drv->savepointRelease(5)); + $this->assertTrue($this->drv->savepointRelease(6)); $this->assertEquals(3, $this->drv->savepointCreate()); - $this->assertEquals(true, $this->drv->savepointRelease(2)); + $this->assertTrue($this->drv->savepointRelease(2)); $this->assertException("stale", "Db", "ExceptionSavepoint"); $this->drv->savepointRelease(2); } + function testManipulateSavepointsSomeMore() { + $this->assertEquals(1, $this->drv->savepointCreate()); + $this->assertEquals(2, $this->drv->savepointCreate()); + $this->assertEquals(3, $this->drv->savepointCreate()); + $this->assertEquals(4, $this->drv->savepointCreate()); + $this->assertTrue($this->drv->savepointRelease(2)); + $this->assertFalse($this->drv->savepointUndo(3)); + $this->assertException("stale", "Db", "ExceptionSavepoint"); + $this->drv->savepointUndo(2); + } + function testBeginATransaction() { $select = "SELECT count(*) FROM test"; $insert = "INSERT INTO test(id) values(null)"; @@ -307,6 +320,8 @@ class TestDbDriverSQLite3 extends Test\AbstractTest { function testUnlockTheDatabase() { $this->drv->savepointCreate(true); $this->drv->savepointRelease(); + $this->drv->savepointCreate(true); + $this->drv->savepointUndo(); $this->assertSame(true, $this->ch->exec("CREATE TABLE test(id integer primary key)")); } } \ No newline at end of file diff --git a/tests/Db/SQLite3/TestDbStatementSQLite3.php b/tests/Db/SQLite3/TestDbStatementSQLite3.php index 3d1cb6b..6135e8b 100644 --- a/tests/Db/SQLite3/TestDbStatementSQLite3.php +++ b/tests/Db/SQLite3/TestDbStatementSQLite3.php @@ -36,6 +36,9 @@ class TestDbStatementSQLite3 extends Test\AbstractTest { $s->rebindArray([$strict ? "strict $type" : $type]); $val = $s->runArray([$input])->getRow()['value']; $this->assertSame($expectations[$type], $val, "Binding from type $type failed comparison."); + $s->rebind(...[$strict ? "strict $type" : $type]); + $val = $s->run(...[$input])->getRow()['value']; + $this->assertSame($expectations[$type], $val, "Binding from type $type failed comparison."); } } diff --git a/tests/Db/TestTransaction.php b/tests/Db/TestTransaction.php new file mode 100644 index 0000000..97f1ef6 --- /dev/null +++ b/tests/Db/TestTransaction.php @@ -0,0 +1,56 @@ +clearData(); + $drv = Phake::mock(Db\SQLite3\Driver::class); + Phake::when($drv)->savepointRelease->thenReturn(true); + Phake::when($drv)->savepointUndo->thenReturn(true); + Phake::when($drv)->savepointCreate->thenReturn(1)->thenReturn(2); + $this->drv = $drv; + } + + function testManipulateTransactions() { + $tr1 = new Transaction($this->drv); + $tr2 = new Transaction($this->drv); + Phake::verify($this->drv, Phake::times(2))->savepointCreate; + $this->assertSame(1, $tr1->getIndex()); + $this->assertSame(2, $tr2->getIndex()); + unset($tr1); + Phake::verify($this->drv)->savepointUndo(1); + unset($tr2); + Phake::verify($this->drv)->savepointUndo(2); + } + + function testCloseTransactions() { + $tr1 = new Transaction($this->drv); + $tr2 = new Transaction($this->drv); + $this->assertTrue($tr1->isPending()); + $this->assertTrue($tr2->isPending()); + $tr1->commit(); + $this->assertFalse($tr1->isPending()); + $this->assertTrue($tr2->isPending()); + Phake::verify($this->drv)->savepointRelease(1); + $tr2->rollback(); + $this->assertFalse($tr1->isPending()); + $this->assertFalse($tr2->isPending()); + Phake::verify($this->drv)->savepointUndo(2); + } + + function testIgnoreRollbackErrors() { + Phake::when($this->drv)->savepointUndo->thenThrow(new Db\ExceptionSavepoint("stale")); + $tr1 = new Transaction($this->drv); + $tr2 = new Transaction($this->drv); + unset($tr1, $tr2); // no exception should bubble up + Phake::verify($this->drv)->savepointUndo(1); + Phake::verify($this->drv)->savepointUndo(2); + } +} \ No newline at end of file diff --git a/tests/User/TestAuthorization.php b/tests/User/TestAuthorization.php index b443cf5..a442515 100644 --- a/tests/User/TestAuthorization.php +++ b/tests/User/TestAuthorization.php @@ -54,7 +54,7 @@ class TestAuthorization extends Test\AbstractTest { if($db !== null) { Arsse::$db = new $db(); } - Arsse::$user = Phake::PartialMock(User::class); + Arsse::$user = Phake::partialMock(User::class); Phake::when(Arsse::$user)->authorize->thenReturn(true); foreach(self::USERS as $user => $level) { Arsse::$user->add($user, ""); diff --git a/tests/lib/Database/SeriesSubscription.php b/tests/lib/Database/SeriesSubscription.php index dd85fb1..92e8a5b 100644 --- a/tests/lib/Database/SeriesSubscription.php +++ b/tests/lib/Database/SeriesSubscription.php @@ -114,7 +114,7 @@ trait SeriesSubscription { [3,"http://example.com/feed3", "Ack", "", "",strtotime("now + 1 hour")], ]; // initialize a partial mock of the Database object to later manipulate the feedUpdate method - Arsse::$db = Phake::PartialMock(Database::class, $this->drv); + Arsse::$db = Phake::partialMock(Database::class, $this->drv); $this->user = "john.doe@example.com"; } diff --git a/tests/lib/User/CommonTests.php b/tests/lib/User/CommonTests.php index e643206..5a2f62c 100644 --- a/tests/lib/User/CommonTests.php +++ b/tests/lib/User/CommonTests.php @@ -16,7 +16,7 @@ trait CommonTests { $conf->userPreAuth = false; Arsse::$conf = $conf; Arsse::$db = new Database(); - Arsse::$user = Phake::PartialMock(User::class); + Arsse::$user = Phake::partialMock(User::class); Phake::when(Arsse::$user)->authorize->thenReturn(true); $_SERVER['PHP_AUTH_USER'] = self::USER1; $_SERVER['PHP_AUTH_PW'] = "secret"; diff --git a/tests/phpunit.xml b/tests/phpunit.xml index f26b1dd..872077b 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -45,8 +45,10 @@ Misc/TestContext.php + Db/TestTransaction.php Db/SQLite3/TestDbResultSQLite3.php Db/SQLite3/TestDbStatementSQLite3.php + Db/SQLite3/TestDbDriverCreationSQLite3.php Db/SQLite3/TestDbDriverSQLite3.php Db/SQLite3/TestDbUpdateSQLite3.php