J. King
7 years ago
15 changed files with 309 additions and 41 deletions
@ -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; |
||||
|
} |
||||
|
} |
@ -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); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue