2017-03-10 02:39:42 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-03-10 02:39:42 +00:00
|
|
|
declare(strict_types=1);
|
2017-12-22 16:41:54 +00:00
|
|
|
namespace JKingWeb\Arsse\TestCase\Db\SQLite3;
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-12-22 16:41:54 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\Conf;
|
|
|
|
use JKingWeb\Arsse\Database;
|
|
|
|
use JKingWeb\Arsse\Db\Exception;
|
|
|
|
use JKingWeb\Arsse\Db\SQLite3\Driver;
|
2017-08-29 14:50:31 +00:00
|
|
|
use org\bovigo\vfs\vfsStream;
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
/**
|
2017-07-20 22:36:03 +00:00
|
|
|
* @covers \JKingWeb\Arsse\Db\SQLite3\Driver<extended>
|
|
|
|
* @covers \JKingWeb\Arsse\Db\SQLite3\ExceptionBuilder */
|
2017-12-22 16:41:54 +00:00
|
|
|
class TestUpdate extends \JKingWeb\Arsse\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
|
|
|
|
2018-11-17 02:32:27 +00:00
|
|
|
public function setUp(array $conf = []) {
|
2017-12-22 16:45:24 +00:00
|
|
|
if (!Driver::requirementsMet()) {
|
2017-05-22 14:02:36 +00:00
|
|
|
$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' => []]);
|
2018-11-17 02:32:27 +00:00
|
|
|
$this->setConf($conf);
|
2017-08-20 03:56:32 +00:00
|
|
|
$this->base = $this->vfs->url();
|
|
|
|
$this->path = $this->base."/SQLite3/";
|
2017-12-22 16:41:54 +00:00
|
|
|
$this->drv = new Driver();
|
2017-03-10 02:39:42 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function tearDown() {
|
2017-03-10 02:39:42 +00:00
|
|
|
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-08-29 14:50:31 +00:00
|
|
|
public function testLoadMissingFile() {
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertException("updateFileMissing", "Db");
|
2017-08-20 03:56:32 +00:00
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testLoadUnreadableFile() {
|
2017-08-20 03:56:32 +00:00
|
|
|
touch($this->path."0.sql");
|
|
|
|
chmod($this->path."0.sql", 0000);
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertException("updateFileUnreadable", "Db");
|
2017-08-20 03:56:32 +00:00
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testLoadCorruptFile() {
|
2017-08-20 03:56:32 +00:00
|
|
|
file_put_contents($this->path."0.sql", "This is a corrupt file");
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertException("updateFileError", "Db");
|
2017-08-20 03:56:32 +00:00
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testLoadIncompleteFile() {
|
2017-08-20 03:56:32 +00:00
|
|
|
file_put_contents($this->path."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");
|
2017-08-20 03:56:32 +00:00
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
2017-04-07 01:41:21 +00:00
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-12-19 17:11:49 +00:00
|
|
|
public function testLoadEmptyFile() {
|
|
|
|
file_put_contents($this->path."0.sql", "");
|
|
|
|
$this->assertException("updateFileIncomplete", "Db");
|
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testLoadCorrectFile() {
|
2017-08-20 03:56:32 +00:00
|
|
|
file_put_contents($this->path."0.sql", self::MINIMAL1);
|
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertEquals(1, $this->drv->schemaVersion());
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testPerformPartialUpdate() {
|
2017-08-20 03:56:32 +00:00
|
|
|
file_put_contents($this->path."0.sql", self::MINIMAL1);
|
2017-12-19 17:11:49 +00:00
|
|
|
file_put_contents($this->path."1.sql", " ");
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertException("updateFileIncomplete", "Db");
|
|
|
|
try {
|
2017-08-20 03:56:32 +00:00
|
|
|
$this->drv->schemaUpdate(2, $this->base);
|
2017-08-29 14:50:31 +00:00
|
|
|
} catch (Exception $e) {
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertEquals(1, $this->drv->schemaVersion());
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testPerformSequentialUpdate() {
|
2017-08-20 03:56:32 +00:00
|
|
|
file_put_contents($this->path."0.sql", self::MINIMAL1);
|
|
|
|
file_put_contents($this->path."1.sql", self::MINIMAL2);
|
|
|
|
$this->drv->schemaUpdate(2, $this->base);
|
2017-04-07 01:41:21 +00:00
|
|
|
$this->assertEquals(2, $this->drv->schemaVersion());
|
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testPerformActualUpdate() {
|
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
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testDeclineManualUpdate() {
|
2017-07-21 15:13:04 +00:00
|
|
|
// turn auto-updating off
|
2018-11-17 02:32:27 +00:00
|
|
|
$this->setUp(['dbAutoUpdate' => false]);
|
2017-07-21 15:13:04 +00:00
|
|
|
$this->assertException("updateManual", "Db");
|
|
|
|
$this->drv->schemaUpdate(Database::SCHEMA_VERSION);
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function testDeclineDowngrade() {
|
2017-07-21 15:13:04 +00:00
|
|
|
$this->assertException("updateTooNew", "Db");
|
2017-08-20 03:56:32 +00:00
|
|
|
$this->drv->schemaUpdate(-1, $this->base);
|
2017-07-21 15:13:04 +00:00
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|