2017-03-10 02:39:42 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse;
|
2017-03-28 22:50:00 +00:00
|
|
|
use org\bovigo\vfs\vfsStream;
|
2017-03-10 02:39:42 +00:00
|
|
|
|
|
|
|
|
2017-07-20 22:36:03 +00:00
|
|
|
/**
|
|
|
|
* @covers \JKingWeb\Arsse\Db\SQLite3\Driver<extended>
|
|
|
|
* @covers \JKingWeb\Arsse\Db\SQLite3\ExceptionBuilder */
|
2017-07-08 01:06:38 +00:00
|
|
|
class TestDbUpdateSQLite3 extends Test\AbstractTest {
|
2017-03-10 02:39:42 +00:00
|
|
|
protected $data;
|
2017-04-07 01:41:21 +00:00
|
|
|
protected $drv;
|
|
|
|
protected $vfs;
|
|
|
|
protected $base;
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-07-07 15:49:54 +00:00
|
|
|
const MINIMAL1 = "create table arsse_meta(key text primary key not null, value text); pragma user_version=1";
|
2017-04-07 01:41:21 +00:00
|
|
|
const MINIMAL2 = "pragma user_version=2";
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-07-21 15:13:04 +00:00
|
|
|
function setUp(Conf $conf = null) {
|
2017-05-22 14:02:36 +00:00
|
|
|
if(!extension_loaded("sqlite3")) {
|
|
|
|
$this->markTestSkipped("SQLite extension not loaded");
|
|
|
|
}
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->clearData();
|
2017-03-10 02:39:42 +00:00
|
|
|
$this->vfs = vfsStream::setup("schemata", null, ['SQLite3' => []]);
|
2017-07-21 15:13:04 +00:00
|
|
|
if(!$conf) {
|
|
|
|
$conf = new Conf();
|
|
|
|
}
|
2017-04-07 01:41:21 +00:00
|
|
|
$conf->dbDriver = Db\SQLite3\Driver::class;
|
|
|
|
$conf->dbSchemaBase = $this->vfs->url();
|
|
|
|
$conf->dbSQLite3File = ":memory:";
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$conf = $conf;
|
2017-07-21 15:13:04 +00:00
|
|
|
$this->base = $this->vfs->url()."/SQLite3/";
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->drv = new Db\SQLite3\Driver(true);
|
2017-03-10 02:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown() {
|
|
|
|
unset($this->drv);
|
|
|
|
unset($this->data);
|
|
|
|
unset($this->vfs);
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->clearData();
|
2017-03-10 02:39:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testLoadMissingFile() {
|
|
|
|
$this->assertException("updateFileMissing", "Db");
|
|
|
|
$this->drv->schemaUpdate(1);
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testLoadUnreadableFile() {
|
|
|
|
touch($this->base."0.sql");
|
|
|
|
chmod($this->base."0.sql", 0000);
|
|
|
|
$this->assertException("updateFileUnreadable", "Db");
|
|
|
|
$this->drv->schemaUpdate(1);
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testLoadCorruptFile() {
|
|
|
|
file_put_contents($this->base."0.sql", "This is a corrupt file");
|
|
|
|
$this->assertException("updateFileError", "Db");
|
|
|
|
$this->drv->schemaUpdate(1);
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testLoadIncompleteFile() {
|
2017-07-07 15:49:54 +00:00
|
|
|
file_put_contents($this->base."0.sql", "create table arsse_meta(key text primary key not null, value text);");
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertException("updateFileIncomplete", "Db");
|
|
|
|
$this->drv->schemaUpdate(1);
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testLoadCorrectFile() {
|
|
|
|
file_put_contents($this->base."0.sql", self::MINIMAL1);
|
|
|
|
$this->drv->schemaUpdate(1);
|
|
|
|
$this->assertEquals(1, $this->drv->schemaVersion());
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testPerformPartialUpdate() {
|
|
|
|
file_put_contents($this->base."0.sql", self::MINIMAL1);
|
|
|
|
file_put_contents($this->base."1.sql", "");
|
|
|
|
$this->assertException("updateFileIncomplete", "Db");
|
|
|
|
try {
|
|
|
|
$this->drv->schemaUpdate(2);
|
|
|
|
} catch(Exception $e) {
|
|
|
|
$this->assertEquals(1, $this->drv->schemaVersion());
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testPerformSequentialUpdate() {
|
|
|
|
file_put_contents($this->base."0.sql", self::MINIMAL1);
|
|
|
|
file_put_contents($this->base."1.sql", self::MINIMAL2);
|
|
|
|
$this->drv->schemaUpdate(2);
|
|
|
|
$this->assertEquals(2, $this->drv->schemaVersion());
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-04-07 01:41:21 +00:00
|
|
|
function testPerformActualUpdate() {
|
2017-07-17 11:47:57 +00:00
|
|
|
Arsse::$conf->dbSchemaBase = (new Conf())->dbSchemaBase;
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->drv->schemaUpdate(Database::SCHEMA_VERSION);
|
|
|
|
$this->assertEquals(Database::SCHEMA_VERSION, $this->drv->schemaVersion());
|
|
|
|
}
|
2017-07-21 15:13:04 +00:00
|
|
|
|
|
|
|
function testDeclineManualUpdate() {
|
|
|
|
// turn auto-updating off
|
|
|
|
$conf = new Conf();
|
|
|
|
$conf->dbAutoUpdate = false;
|
|
|
|
$this->setUp($conf);
|
|
|
|
$this->assertException("updateManual", "Db");
|
|
|
|
$this->drv->schemaUpdate(Database::SCHEMA_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDeclineDowngrade() {
|
|
|
|
$this->assertException("updateTooNew", "Db");
|
|
|
|
$this->drv->schemaUpdate(-1);
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
}
|