1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 07:14:55 +00:00

Complete tests for database driver

- Fixes #62
This commit is contained in:
J. King 2017-07-22 15:29:12 -04:00
parent d1382cec63
commit da80a8ffa6
15 changed files with 309 additions and 41 deletions

View file

@ -6,7 +6,5 @@ const BASE = __DIR__.DIRECTORY_SEPARATOR;
const NS_BASE = __NAMESPACE__."\\"; const NS_BASE = __NAMESPACE__."\\";
const VERSION = "0.0.0"; const VERSION = "0.0.0";
if(!defined(NS_BASE."INSTALL")) define(NS_BASE."INSTALL", false);
require_once BASE."vendor".DIRECTORY_SEPARATOR."autoload.php"; require_once BASE."vendor".DIRECTORY_SEPARATOR."autoload.php";
ignore_user_abort(true); ignore_user_abort(true);

View file

@ -12,11 +12,11 @@ class Database {
/** @var Db\Driver */ /** @var Db\Driver */
public $db; public $db;
public function __construct() { public function __construct($initialize = true) {
$driver = Arsse::$conf->dbDriver; $driver = Arsse::$conf->dbDriver;
$this->db = new $driver(INSTALL); $this->db = new $driver();
$ver = $this->db->schemaVersion(); $ver = $this->db->schemaVersion();
if(!INSTALL && $ver < self::SCHEMA_VERSION) { if($initialize && $ver < self::SCHEMA_VERSION) {
$this->db->schemaUpdate(self::SCHEMA_VERSION); $this->db->schemaUpdate(self::SCHEMA_VERSION);
} }
} }

View file

@ -62,10 +62,10 @@ abstract class AbstractDriver implements Driver {
$out = false; $out = false;
break; break;
case self::TR_COMMIT: case self::TR_COMMIT:
case self::TR_ROLLBACK: case self::TR_ROLLBACK: //@codeCoverageIgnore
throw new ExceptionSavepoint("stale", ['action' => "commit", 'index' => $index]); throw new ExceptionSavepoint("stale", ['action' => "commit", 'index' => $index]);
default: default:
throw new Exception("unknownSavepointStatus", $this->transStatus[$index]); throw new Exception("unknownSavepointStatus", $this->transStatus[$index]); //@codeCoverageIgnore
} }
if($index==$this->transDepth) { if($index==$this->transDepth) {
while($this->transDepth > 0 && $this->transStatus[$this->transDepth] > self::TR_PEND) { while($this->transDepth > 0 && $this->transStatus[$this->transDepth] > self::TR_PEND) {
@ -110,10 +110,10 @@ abstract class AbstractDriver implements Driver {
$out = true; $out = true;
break; break;
case self::TR_COMMIT: case self::TR_COMMIT:
case self::TR_ROLLBACK: case self::TR_ROLLBACK: //@codeCoverageIgnore
throw new ExceptionSavepoint("stale", ['action' => "rollback", 'index' => $index]); throw new ExceptionSavepoint("stale", ['action' => "rollback", 'index' => $index]);
default: default:
throw new Exception("unknownSavepointStatus", $this->transStatus[$index]); throw new Exception("unknownSavepointStatus", $this->transStatus[$index]); //@codeCoverageIgnore
} }
if($index==$this->transDepth) { if($index==$this->transDepth) {
while($this->transDepth > 0 && $this->transStatus[$this->transDepth] > self::TR_PEND) { while($this->transDepth > 0 && $this->transStatus[$this->transDepth] > self::TR_PEND) {

View file

@ -37,7 +37,7 @@ abstract class AbstractStatement implements Statement {
$this->isNullable[] = true; $this->isNullable[] = true;
} }
if(!array_key_exists($binding, self::TYPES)) { if(!array_key_exists($binding, self::TYPES)) {
throw new Exception("paramTypeInvalid", $binding); throw new Exception("paramTypeInvalid", $binding); // @codeCoverageIgnore
} }
$this->types[] = self::TYPES[$binding]; $this->types[] = self::TYPES[$binding];
} }
@ -83,7 +83,7 @@ abstract class AbstractStatement implements Statement {
} }
return $v; return $v;
default: default:
throw new Exception("paramTypeUnknown", $type); throw new Exception("paramTypeUnknown", $type); // @codeCoverageIgnore
} }
} }
} }

View file

@ -9,7 +9,7 @@ interface Driver {
const TR_PEND_COMMIT = -1; const TR_PEND_COMMIT = -1;
const TR_PEND_ROLLBACK = -2; 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) // returns a human-friendly name for the driver (for display in installer, for example)
static function driverName(): string; static function driverName(): string;
// returns the version of the scheme of the opened database; if uninitialized should return 0 // returns the version of the scheme of the opened database; if uninitialized should return 0

View file

@ -16,36 +16,39 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
protected $db; protected $db;
public function __construct(bool $install = false) { public function __construct() {
// check to make sure required extension is loaded // check to make sure required extension is loaded
if(!class_exists("SQLite3")) { if(!class_exists("SQLite3")) {
throw new Exception("extMissing", self::driverName()); throw new Exception("extMissing", self::driverName()); // @codeCoverageIgnore
} }
$file = Arsse::$conf->dbSQLite3File; $dbFile = Arsse::$conf->dbSQLite3File;
// if the file exists (or we're initializing the database), try to open it $mode = \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE;
try { 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 // set initial options
$this->db->enableExceptions(true); $this->db->enableExceptions(true);
$this->exec("PRAGMA journal_mode = wal"); $this->exec("PRAGMA journal_mode = wal");
$this->exec("PRAGMA foreign_keys = yes"); $this->exec("PRAGMA foreign_keys = yes");
} catch(\Throwable $e) { } catch(\Throwable $e) {
// if opening the database doesn't work, check various pre-conditions to find out what the problem might be // if opening the database doesn't work, check various pre-conditions to find out what the problem might be
if(!file_exists($file)) { $files = [
if($install && !is_writable(dirname($file))) { $dbFile, // main database file
throw new Exception("fileUncreatable", dirname($file)); $dbFile."-wal", // write-ahead log journal
} $dbFile."-shm", // shared memory index
throw new Exception("fileMissing", $file); ];
} foreach($files as $file) {
if(!is_readable($file) && !is_writable($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); throw new Exception("fileUnusable", $file);
} else if(!is_readable($file)) { } else if(!is_readable($file)) {
throw new Exception("fileUnreadable", $file); throw new Exception("fileUnreadable", $file);
} else if(!is_writable($file)) { } else if(!is_writable($file)) {
throw new Exception("fileUnwritable", $file); throw new Exception("fileUnwritable", $file);
} }
}
// otherwise the database is probably corrupt // 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); $sql = @file_get_contents($file);
if($sql===false) { 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 { try {
$this->exec($sql); $this->exec($sql);

View file

@ -26,7 +26,9 @@ class Driver implements \JKingWeb\Arsse\Service\Driver {
$pp = []; $pp = [];
while($this->queue) { while($this->queue) {
$id = (int) array_shift($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) { while($pp) {
$p = array_pop($pp); $p = array_pop($pp);

View file

@ -0,0 +1,189 @@
<?php
declare(strict_types=1);
namespace JKingWeb\Arsse;
use JKingWeb\Arsse\Arsse;
use org\bovigo\vfs\vfsStream;
use Phake;
/**
* @covers \JKingWeb\Arsse\Db\SQLite3\Driver<extended>
* @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;
}
}

View file

@ -16,9 +16,9 @@ class TestDbDriverSQLite3 extends Test\AbstractTest {
} }
$this->clearData(); $this->clearData();
$conf = new Conf(); $conf = new Conf();
Arsse::$conf = $conf;
$conf->dbDriver = Db\SQLite3\Driver::class; $conf->dbDriver = Db\SQLite3\Driver::class;
$conf->dbSQLite3File = tempnam(sys_get_temp_dir(), 'ook'); $conf->dbSQLite3File = tempnam(sys_get_temp_dir(), 'ook');
Arsse::$conf = $conf;
$this->drv = new Db\SQLite3\Driver(true); $this->drv = new Db\SQLite3\Driver(true);
$this->ch = new \SQLite3(Arsse::$conf->dbSQLite3File); $this->ch = new \SQLite3(Arsse::$conf->dbSQLite3File);
$this->ch->enableExceptions(true); $this->ch->enableExceptions(true);
@ -27,7 +27,9 @@ class TestDbDriverSQLite3 extends Test\AbstractTest {
function tearDown() { function tearDown() {
unset($this->drv); unset($this->drv);
unset($this->ch); unset($this->ch);
if(isset(Arsse::$conf)) {
unlink(Arsse::$conf->dbSQLite3File); unlink(Arsse::$conf->dbSQLite3File);
}
$this->clearData(); $this->clearData();
} }
@ -131,17 +133,28 @@ class TestDbDriverSQLite3 extends Test\AbstractTest {
$this->assertEquals(3, $this->drv->savepointCreate()); $this->assertEquals(3, $this->drv->savepointCreate());
$this->assertEquals(4, $this->drv->savepointCreate()); $this->assertEquals(4, $this->drv->savepointCreate());
$this->assertEquals(5, $this->drv->savepointCreate()); $this->assertEquals(5, $this->drv->savepointCreate());
$this->assertEquals(true, $this->drv->savepointUndo(3)); $this->assertTrue($this->drv->savepointUndo(3));
$this->assertEquals(false, $this->drv->savepointRelease(4)); $this->assertFalse($this->drv->savepointRelease(4));
$this->assertEquals(6, $this->drv->savepointCreate()); $this->assertEquals(6, $this->drv->savepointCreate());
$this->assertEquals(false, $this->drv->savepointRelease(5)); $this->assertFalse($this->drv->savepointRelease(5));
$this->assertEquals(true, $this->drv->savepointRelease(6)); $this->assertTrue($this->drv->savepointRelease(6));
$this->assertEquals(3, $this->drv->savepointCreate()); $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->assertException("stale", "Db", "ExceptionSavepoint");
$this->drv->savepointRelease(2); $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() { function testBeginATransaction() {
$select = "SELECT count(*) FROM test"; $select = "SELECT count(*) FROM test";
$insert = "INSERT INTO test(id) values(null)"; $insert = "INSERT INTO test(id) values(null)";
@ -307,6 +320,8 @@ class TestDbDriverSQLite3 extends Test\AbstractTest {
function testUnlockTheDatabase() { function testUnlockTheDatabase() {
$this->drv->savepointCreate(true); $this->drv->savepointCreate(true);
$this->drv->savepointRelease(); $this->drv->savepointRelease();
$this->drv->savepointCreate(true);
$this->drv->savepointUndo();
$this->assertSame(true, $this->ch->exec("CREATE TABLE test(id integer primary key)")); $this->assertSame(true, $this->ch->exec("CREATE TABLE test(id integer primary key)"));
} }
} }

View file

@ -36,6 +36,9 @@ class TestDbStatementSQLite3 extends Test\AbstractTest {
$s->rebindArray([$strict ? "strict $type" : $type]); $s->rebindArray([$strict ? "strict $type" : $type]);
$val = $s->runArray([$input])->getRow()['value']; $val = $s->runArray([$input])->getRow()['value'];
$this->assertSame($expectations[$type], $val, "Binding from type $type failed comparison."); $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.");
} }
} }

View file

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace JKingWeb\Arsse;
use JKingWeb\Arsse\Db\Transaction;
use Phake;
/**
* @covers \JKingWeb\Arsse\Db\Transaction */
class TestTransaction extends Test\AbstractTest {
protected $drv;
function setUp() {
$this->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);
}
}

View file

@ -54,7 +54,7 @@ class TestAuthorization extends Test\AbstractTest {
if($db !== null) { if($db !== null) {
Arsse::$db = new $db(); Arsse::$db = new $db();
} }
Arsse::$user = Phake::PartialMock(User::class); Arsse::$user = Phake::partialMock(User::class);
Phake::when(Arsse::$user)->authorize->thenReturn(true); Phake::when(Arsse::$user)->authorize->thenReturn(true);
foreach(self::USERS as $user => $level) { foreach(self::USERS as $user => $level) {
Arsse::$user->add($user, ""); Arsse::$user->add($user, "");

View file

@ -114,7 +114,7 @@ trait SeriesSubscription {
[3,"http://example.com/feed3", "Ack", "", "",strtotime("now + 1 hour")], [3,"http://example.com/feed3", "Ack", "", "",strtotime("now + 1 hour")],
]; ];
// initialize a partial mock of the Database object to later manipulate the feedUpdate method // 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"; $this->user = "john.doe@example.com";
} }

View file

@ -16,7 +16,7 @@ trait CommonTests {
$conf->userPreAuth = false; $conf->userPreAuth = false;
Arsse::$conf = $conf; Arsse::$conf = $conf;
Arsse::$db = new Database(); Arsse::$db = new Database();
Arsse::$user = Phake::PartialMock(User::class); Arsse::$user = Phake::partialMock(User::class);
Phake::when(Arsse::$user)->authorize->thenReturn(true); Phake::when(Arsse::$user)->authorize->thenReturn(true);
$_SERVER['PHP_AUTH_USER'] = self::USER1; $_SERVER['PHP_AUTH_USER'] = self::USER1;
$_SERVER['PHP_AUTH_PW'] = "secret"; $_SERVER['PHP_AUTH_PW'] = "secret";

View file

@ -45,8 +45,10 @@
<file>Misc/TestContext.php</file> <file>Misc/TestContext.php</file>
</testsuite> </testsuite>
<testsuite name="Database drivers"> <testsuite name="Database drivers">
<file>Db/TestTransaction.php</file>
<file>Db/SQLite3/TestDbResultSQLite3.php</file> <file>Db/SQLite3/TestDbResultSQLite3.php</file>
<file>Db/SQLite3/TestDbStatementSQLite3.php</file> <file>Db/SQLite3/TestDbStatementSQLite3.php</file>
<file>Db/SQLite3/TestDbDriverCreationSQLite3.php</file>
<file>Db/SQLite3/TestDbDriverSQLite3.php</file> <file>Db/SQLite3/TestDbDriverSQLite3.php</file>
<file>Db/SQLite3/TestDbUpdateSQLite3.php</file> <file>Db/SQLite3/TestDbUpdateSQLite3.php</file>
</testsuite> </testsuite>