2018-11-28 15:46:23 +00:00
|
|
|
<?php
|
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse\TestCase\Db;
|
|
|
|
|
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
use JKingWeb\Arsse\Database;
|
|
|
|
use JKingWeb\Arsse\Db\Exception;
|
|
|
|
use org\bovigo\vfs\vfsStream;
|
|
|
|
|
|
|
|
class BaseUpdate extends \JKingWeb\Arsse\Test\AbstractTest {
|
|
|
|
protected static $interface;
|
|
|
|
protected $drv;
|
|
|
|
protected $vfs;
|
|
|
|
protected $base;
|
|
|
|
protected $path;
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public static function setUpBeforeClass(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
// establish a clean baseline
|
|
|
|
static::clearData();
|
|
|
|
static::setConf();
|
2019-01-12 17:43:06 +00:00
|
|
|
static::$interface = static::dbInterface();
|
2018-11-28 15:46:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function setUp(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
if (!static::$interface) {
|
|
|
|
$this->markTestSkipped(static::$implementation." database driver not available");
|
|
|
|
}
|
|
|
|
self::clearData();
|
|
|
|
self::setConf();
|
|
|
|
// construct a fresh driver for each test
|
2019-01-12 17:43:06 +00:00
|
|
|
$this->drv = new static::$dbDriverClass;
|
2018-11-28 15:46:23 +00:00
|
|
|
$schemaId = (get_class($this->drv))::schemaID();
|
|
|
|
// set up a virtual filesystem for schema files
|
|
|
|
$this->vfs = vfsStream::setup("schemata", null, [$schemaId => []]);
|
|
|
|
$this->base = $this->vfs->url();
|
|
|
|
$this->path = $this->base."/$schemaId/";
|
|
|
|
// completely clear the database
|
2019-01-12 17:43:06 +00:00
|
|
|
static::dbRaze(static::$interface);
|
2018-11-28 15:46:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public function tearDown(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
// deconstruct the driver
|
|
|
|
unset($this->drv);
|
|
|
|
unset($this->path, $this->base, $this->vfs);
|
|
|
|
self::clearData();
|
|
|
|
}
|
|
|
|
|
2019-10-16 18:42:43 +00:00
|
|
|
public static function tearDownAfterClass(): void {
|
2018-12-10 18:17:04 +00:00
|
|
|
if (static::$interface) {
|
|
|
|
// completely clear the database
|
2019-01-12 17:43:06 +00:00
|
|
|
static::dbRaze(static::$interface);
|
2018-12-10 18:17:04 +00:00
|
|
|
}
|
|
|
|
static::$interface = null;
|
2018-11-28 15:46:23 +00:00
|
|
|
self::clearData();
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testLoadMissingFile(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
$this->assertException("updateFileMissing", "Db");
|
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testLoadUnreadableFile(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
touch($this->path."0.sql");
|
|
|
|
chmod($this->path."0.sql", 0000);
|
|
|
|
$this->assertException("updateFileUnreadable", "Db");
|
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testLoadCorruptFile(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
file_put_contents($this->path."0.sql", "This is a corrupt file");
|
|
|
|
$this->assertException("updateFileError", "Db");
|
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testLoadIncompleteFile(): void {
|
2018-12-20 23:06:28 +00:00
|
|
|
file_put_contents($this->path."0.sql", "create table arsse_meta(\"key\" varchar(255) primary key not null, value text);");
|
2018-11-28 15:46:23 +00:00
|
|
|
$this->assertException("updateFileIncomplete", "Db");
|
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testLoadEmptyFile(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
file_put_contents($this->path."0.sql", "");
|
|
|
|
$this->assertException("updateFileIncomplete", "Db");
|
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testLoadCorrectFile(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
file_put_contents($this->path."0.sql", static::$minimal1);
|
|
|
|
$this->drv->schemaUpdate(1, $this->base);
|
|
|
|
$this->assertEquals(1, $this->drv->schemaVersion());
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testPerformPartialUpdate(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
file_put_contents($this->path."0.sql", static::$minimal1);
|
2018-12-20 23:06:28 +00:00
|
|
|
file_put_contents($this->path."1.sql", "UPDATE arsse_meta set value = '1' where \"key\" = 'schema_version'");
|
2018-11-28 15:46:23 +00:00
|
|
|
$this->assertException("updateFileIncomplete", "Db");
|
|
|
|
try {
|
|
|
|
$this->drv->schemaUpdate(2, $this->base);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->assertEquals(1, $this->drv->schemaVersion());
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testPerformSequentialUpdate(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
file_put_contents($this->path."0.sql", static::$minimal1);
|
|
|
|
file_put_contents($this->path."1.sql", static::$minimal2);
|
|
|
|
$this->drv->schemaUpdate(2, $this->base);
|
|
|
|
$this->assertEquals(2, $this->drv->schemaVersion());
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testPerformActualUpdate(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
$this->drv->schemaUpdate(Database::SCHEMA_VERSION);
|
|
|
|
$this->assertEquals(Database::SCHEMA_VERSION, $this->drv->schemaVersion());
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testDeclineManualUpdate(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
// turn auto-updating off
|
|
|
|
Arsse::$conf->dbAutoUpdate = false;
|
|
|
|
$this->assertException("updateManual", "Db");
|
|
|
|
$this->drv->schemaUpdate(Database::SCHEMA_VERSION);
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testDeclineDowngrade(): void {
|
2018-11-28 15:46:23 +00:00
|
|
|
$this->assertException("updateTooNew", "Db");
|
|
|
|
$this->drv->schemaUpdate(-1, $this->base);
|
|
|
|
}
|
2019-07-26 13:37:51 +00:00
|
|
|
|
2020-01-20 18:52:48 +00:00
|
|
|
public function testPerformMaintenance(): void {
|
2019-07-26 13:37:51 +00:00
|
|
|
$this->drv->schemaUpdate(Database::SCHEMA_VERSION);
|
|
|
|
$this->assertTrue($this->drv->maintenance());
|
|
|
|
}
|
2018-11-28 15:46:23 +00:00
|
|
|
}
|