Newer
Older
<?php
/**
* MigrationTest.php - unit tests for the migrations
*
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @license GPL2 or any later version
*/
class MigrationTest extends \Codeception\Test\Unit
{
protected $migrator;
protected $before = null;
public function setUp(): void
$this->before = $GLOBALS['CACHING_ENABLE'] ?? null;
$GLOBALS['CACHING_ENABLE'] = false;
require_once 'lib/classes/SimpleORMap.class.php';
require_once 'lib/classes/StudipCache.class.php';
require_once 'lib/classes/StudipMemoryCache.class.php';
require_once 'lib/classes/StudipCacheFactory.class.php';
require_once 'lib/migrations/Migration.php';
require_once 'lib/migrations/Migrator.php';
require_once 'lib/migrations/SchemaVersion.php';
}
public function tearDown(): void
{
if ($this->before !== null) {
$GLOBALS['CACHING_ENABLE'] = $this->before;
} else {
unset($GLOBALS['CACHING_ENABLE']);
}
}
private function getSchemaVersion()
{
return new class() implements SchemaVersion
{
public function getDomain()
{
return 'test';
}
}
};
}
private function getMigrator($schema_version = null)
{
return new Migrator(
$schema_version ?: $this->getSchemaVersion()
);
}
public function testRelevance()
{
$migrator = $this->getMigrator();
$relevant = $migrator->relevantMigrations(null);
$this->assertSame(4, count($relevant));
$relevant = $migrator->relevantMigrations(null);
}
public function testMigrationUp()
{
$schema_version = $this->getSchemaVersion();
$migrator = $this->getMigrator($schema_version);
$migrator->migrateTo(null);
$this->assertSame(0, $migrator->pendingMigrations());
return $schema_version;
}
/**
* @depends testMigrationUp
*/
public function testMigrationDown($schema_version)
{
$migrator = $this->getMigrator($schema_version);
$migrator->migrateTo(0);
$this->assertSame(0, $schema_version->get());
$this->assertSame(4, $migrator->pendingMigrations());
}
public function testGaps()
{
$schema_version = $this->getSchemaVersion();
$migrator = $this->getMigrator($schema_version);
$relevant = $migrator->relevantMigrations(null);
$this->assertSame(1, count($relevant));
$this->assertEquals(['2.1'], array_keys($relevant));