J. King
6 years ago
8 changed files with 174 additions and 222 deletions
@ -0,0 +1,136 @@ |
|||||
|
<?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 JKingWeb\Arsse\Test\DatabaseInformation; |
||||
|
use org\bovigo\vfs\vfsStream; |
||||
|
|
||||
|
class BaseUpdate extends \JKingWeb\Arsse\Test\AbstractTest { |
||||
|
protected static $dbInfo; |
||||
|
protected static $interface; |
||||
|
protected $drv; |
||||
|
protected $vfs; |
||||
|
protected $base; |
||||
|
protected $path; |
||||
|
|
||||
|
public static function setUpBeforeClass() { |
||||
|
// establish a clean baseline |
||||
|
static::clearData(); |
||||
|
static::$dbInfo = new DatabaseInformation(static::$implementation); |
||||
|
static::setConf(); |
||||
|
static::$interface = (static::$dbInfo->interfaceConstructor)(); |
||||
|
} |
||||
|
|
||||
|
public function setUp() { |
||||
|
if (!static::$interface) { |
||||
|
$this->markTestSkipped(static::$implementation." database driver not available"); |
||||
|
} |
||||
|
self::clearData(); |
||||
|
self::setConf(); |
||||
|
// construct a fresh driver for each test |
||||
|
$this->drv = new static::$dbInfo->driverClass; |
||||
|
$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 |
||||
|
(static::$dbInfo->razeFunction)(static::$interface); |
||||
|
} |
||||
|
|
||||
|
public function tearDown() { |
||||
|
// deconstruct the driver |
||||
|
unset($this->drv); |
||||
|
if (static::$interface) { |
||||
|
// completely clear the database |
||||
|
(static::$dbInfo->razeFunction)(static::$interface); |
||||
|
} |
||||
|
unset($this->path, $this->base, $this->vfs); |
||||
|
self::clearData(); |
||||
|
} |
||||
|
|
||||
|
public static function tearDownAfterClass() { |
||||
|
static::$implementation = null; |
||||
|
static::$dbInfo = null; |
||||
|
self::clearData(); |
||||
|
} |
||||
|
|
||||
|
public function testLoadMissingFile() { |
||||
|
$this->assertException("updateFileMissing", "Db"); |
||||
|
$this->drv->schemaUpdate(1, $this->base); |
||||
|
} |
||||
|
|
||||
|
public function testLoadUnreadableFile() { |
||||
|
touch($this->path."0.sql"); |
||||
|
chmod($this->path."0.sql", 0000); |
||||
|
$this->assertException("updateFileUnreadable", "Db"); |
||||
|
$this->drv->schemaUpdate(1, $this->base); |
||||
|
} |
||||
|
|
||||
|
public function testLoadCorruptFile() { |
||||
|
file_put_contents($this->path."0.sql", "This is a corrupt file"); |
||||
|
$this->assertException("updateFileError", "Db"); |
||||
|
$this->drv->schemaUpdate(1, $this->base); |
||||
|
} |
||||
|
|
||||
|
public function testLoadIncompleteFile() { |
||||
|
file_put_contents($this->path."0.sql", "create table arsse_meta(key text primary key not null, value text);"); |
||||
|
$this->assertException("updateFileIncomplete", "Db"); |
||||
|
$this->drv->schemaUpdate(1, $this->base); |
||||
|
} |
||||
|
|
||||
|
public function testLoadEmptyFile() { |
||||
|
file_put_contents($this->path."0.sql", ""); |
||||
|
$this->assertException("updateFileIncomplete", "Db"); |
||||
|
$this->drv->schemaUpdate(1, $this->base); |
||||
|
} |
||||
|
|
||||
|
public function testLoadCorrectFile() { |
||||
|
file_put_contents($this->path."0.sql", static::$minimal1); |
||||
|
$this->drv->schemaUpdate(1, $this->base); |
||||
|
$this->assertEquals(1, $this->drv->schemaVersion()); |
||||
|
} |
||||
|
|
||||
|
public function testPerformPartialUpdate() { |
||||
|
file_put_contents($this->path."0.sql", static::$minimal1); |
||||
|
file_put_contents($this->path."1.sql", "UPDATE arsse_meta set value = '1' where key = 'schema_version'"); |
||||
|
$this->assertException("updateFileIncomplete", "Db"); |
||||
|
try { |
||||
|
$this->drv->schemaUpdate(2, $this->base); |
||||
|
} catch (Exception $e) { |
||||
|
$this->assertEquals(1, $this->drv->schemaVersion()); |
||||
|
throw $e; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public function testPerformSequentialUpdate() { |
||||
|
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()); |
||||
|
} |
||||
|
|
||||
|
public function testPerformActualUpdate() { |
||||
|
$this->drv->schemaUpdate(Database::SCHEMA_VERSION); |
||||
|
$this->assertEquals(Database::SCHEMA_VERSION, $this->drv->schemaVersion()); |
||||
|
} |
||||
|
|
||||
|
public function testDeclineManualUpdate() { |
||||
|
// turn auto-updating off |
||||
|
Arsse::$conf->dbAutoUpdate = false; |
||||
|
$this->assertException("updateManual", "Db"); |
||||
|
$this->drv->schemaUpdate(Database::SCHEMA_VERSION); |
||||
|
} |
||||
|
|
||||
|
public function testDeclineDowngrade() { |
||||
|
$this->assertException("updateTooNew", "Db"); |
||||
|
$this->drv->schemaUpdate(-1, $this->base); |
||||
|
} |
||||
|
} |
@ -0,0 +1,16 @@ |
|||||
|
<?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\PostgreSQL; |
||||
|
|
||||
|
/** |
||||
|
* @covers \JKingWeb\Arsse\Db\PostgreSQL\PDODriver<extended> |
||||
|
* @covers \JKingWeb\Arsse\Db\PDOError */ |
||||
|
class TestUpdate extends \JKingWeb\Arsse\TestCase\Db\BaseUpdate { |
||||
|
protected static $implementation = "PDO PostgreSQL"; |
||||
|
protected static $minimal1 = "CREATE TABLE arsse_meta(key text primary key, value text); INSERT INTO arsse_meta(key,value) values('schema_version','1');"; |
||||
|
protected static $minimal2 = "UPDATE arsse_meta set value = '2' where key = 'schema_version';"; |
||||
|
} |
Loading…
Reference in new issue