112 lines
4.4 KiB
PHP
112 lines
4.4 KiB
PHP
<?php declare(strict_types = 1);
|
|
|
|
namespace MailPoet\Migrator;
|
|
|
|
use MailPoet\Config\Env;
|
|
use MailPoetTest;
|
|
|
|
// testing migration files
|
|
require_once __DIR__ . '/TestMigrations/Db/Migration_20221024_080348.php';
|
|
require_once __DIR__ . '/TestMigrations/App/Migration_20221023_080348.php';
|
|
require_once __DIR__ . '/TestMigrationsFail/Db/Migration_20221023_040819.php';
|
|
require_once __DIR__ . '/TestMigrationsInvalid/Db/Migration_20221022_021304.php';
|
|
|
|
class RunnerTest extends MailPoetTest {
|
|
private const DATE_TIME_FORMAT = '%d%d-%d%d-%d%d %d%d:%d%d:%d%d';
|
|
|
|
/** @var string */
|
|
private $table;
|
|
|
|
/** @var Store */
|
|
private $store;
|
|
|
|
/** @var Runner */
|
|
private $runner;
|
|
|
|
public function _before() {
|
|
parent::_before();
|
|
$this->table = Env::$dbPrefix . 'testing_migrations';
|
|
$this->connection->executeStatement("DROP TABLE IF EXISTS {$this->table}");
|
|
$this->store = $this->getServiceWithOverrides(Store::class, [
|
|
'table' => $this->table,
|
|
]);
|
|
$this->runner = $this->getServiceWithOverrides(Runner::class, [
|
|
'store' => $this->store,
|
|
]);
|
|
$this->store->ensureMigrationsTable();
|
|
}
|
|
|
|
public function testItRunsDbMigration(): void {
|
|
ob_start();
|
|
$this->runner->runMigration('Migration_20221024_080348', Repository::MIGRATIONS_LEVEL_DB);
|
|
$output = ob_get_clean();
|
|
$this->assertSame('Migration run called!', $output);
|
|
|
|
$processed = $this->store->getAll();
|
|
$this->assertCount(1, $processed);
|
|
|
|
$data = $processed[0];
|
|
$this->assertSame('Migration_20221024_080348', $data['name']);
|
|
$this->assertStringMatchesFormat(self::DATE_TIME_FORMAT, $data['started_at']);
|
|
$this->assertStringMatchesFormat(self::DATE_TIME_FORMAT, $data['completed_at']);
|
|
$this->assertNull($data['error']);
|
|
}
|
|
|
|
public function testItRunsAppMigration(): void {
|
|
ob_start();
|
|
$this->runner->runMigration('Migration_20221023_080348', Repository::MIGRATIONS_LEVEL_APP);
|
|
$output = ob_get_clean();
|
|
$this->assertSame('Migration run called!', $output);
|
|
|
|
$processed = $this->store->getAll();
|
|
$this->assertCount(1, $processed);
|
|
|
|
$data = $processed[0];
|
|
$this->assertSame('Migration_20221023_080348', $data['name']);
|
|
$this->assertStringMatchesFormat(self::DATE_TIME_FORMAT, $data['started_at']);
|
|
$this->assertStringMatchesFormat(self::DATE_TIME_FORMAT, $data['completed_at']);
|
|
$this->assertNull($data['error']);
|
|
}
|
|
|
|
public function testItFailsWithNonExistentDbMigration(): void {
|
|
$this->expectException(MigratorException::class);
|
|
$this->expectExceptionMessage('MailPoet\Migrations\Db\MigrationThatDoesntExist" not found.');
|
|
$this->runner->runMigration('MigrationThatDoesntExist', Repository::MIGRATIONS_LEVEL_DB);
|
|
$this->assertEmpty($this->store->getAll());
|
|
}
|
|
|
|
public function testItFailsWithNonExistentAppMigration(): void {
|
|
$this->expectException(MigratorException::class);
|
|
$this->expectExceptionMessage('MailPoet\Migrations\App\MigrationThatDoesntExist" not found.');
|
|
$this->runner->runMigration('MigrationThatDoesntExist', Repository::MIGRATIONS_LEVEL_APP);
|
|
$this->assertEmpty($this->store->getAll());
|
|
}
|
|
|
|
public function testItFailsWithInvalidMigrationClass(): void {
|
|
$this->expectException(MigratorException::class);
|
|
$this->expectExceptionMessage('Migration class "MailPoet\Migrations\Db\Migration_20221022_021304" is not a subclass of "MailPoet\Migrator\DbMigration".');
|
|
$this->runner->runMigration('Migration_20221022_021304', Repository::MIGRATIONS_LEVEL_DB);
|
|
$this->assertEmpty($this->store->getAll());
|
|
}
|
|
|
|
public function testItFailsWithBrokenMigration(): void {
|
|
$this->expectException(MigratorException::class);
|
|
$this->expectExceptionMessage('Migration "MailPoet\Migrations\Db\Migration_20221023_040819" failed. Details: Testing failing migration.');
|
|
$this->runner->runMigration('Migration_20221023_040819', Repository::MIGRATIONS_LEVEL_DB);
|
|
|
|
$processed = $this->store->getAll();
|
|
$this->assertCount(1, $processed);
|
|
|
|
$data = $processed[0];
|
|
$this->assertSame('Migration_20221023_040819', $data['name']);
|
|
$this->assertStringMatchesFormat(self::DATE_TIME_FORMAT, $data['started_at']);
|
|
$this->assertStringMatchesFormat(self::DATE_TIME_FORMAT, $data['completed_at']);
|
|
$this->assertSame($data['error'], 'Testing failing migration.');
|
|
}
|
|
|
|
public function _after() {
|
|
parent::_after();
|
|
$this->connection->executeStatement("DROP TABLE IF EXISTS {$this->table}");
|
|
}
|
|
}
|