|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace flusio\cli; |
| 4 | + |
| 5 | +class MigrationsTest extends \PHPUnit\Framework\TestCase |
| 6 | +{ |
| 7 | + use \Minz\Tests\ApplicationHelper; |
| 8 | + use \Minz\Tests\ResponseAsserts; |
| 9 | + |
| 10 | + /** |
| 11 | + * @beforeClass |
| 12 | + */ |
| 13 | + public static function loadApplication(): void |
| 14 | + { |
| 15 | + self::$application = new \flusio\cli\Application(); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * @before |
| 20 | + */ |
| 21 | + public function uninstall(): void |
| 22 | + { |
| 23 | + $migration_file = \Minz\Configuration::$data_path . '/migrations_version.txt'; |
| 24 | + @unlink($migration_file); |
| 25 | + \Minz\Database::drop(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @afterClass |
| 30 | + */ |
| 31 | + public static function recreateDatabase(): void |
| 32 | + { |
| 33 | + \Minz\Database::reset(); |
| 34 | + $schema = @file_get_contents(\Minz\Configuration::$schema_path); |
| 35 | + assert($schema !== false); |
| 36 | + $database = \Minz\Database::get(); |
| 37 | + $database->exec($schema); |
| 38 | + } |
| 39 | + |
| 40 | + public function testAllMigrationsCanBeApplied(): void |
| 41 | + { |
| 42 | + $migrations_version_path = \Minz\Configuration::$data_path . '/migrations_version.txt'; |
| 43 | + $migrations_path = \Minz\Configuration::$app_path . '/src/migrations'; |
| 44 | + touch($migrations_version_path); |
| 45 | + \Minz\Database::create(); |
| 46 | + $migrator = new \Minz\Migration\Migrator($migrations_path); |
| 47 | + $last_migration_version = $migrator->lastVersion(); |
| 48 | + $expected_output = []; |
| 49 | + foreach ($migrator->migrations() as $version => $migration) { |
| 50 | + $expected_output[] = "{$version}: OK"; |
| 51 | + } |
| 52 | + $expected_output = implode("\n", $expected_output); |
| 53 | + |
| 54 | + $response = $this->appRun('CLI', '/migrations/setup'); |
| 55 | + |
| 56 | + $this->assertResponseCode($response, 200); |
| 57 | + $current_migration_version = @file_get_contents($migrations_version_path); |
| 58 | + $this->assertSame($last_migration_version, $current_migration_version); |
| 59 | + $this->assertResponseEquals($response, $expected_output); |
| 60 | + } |
| 61 | + |
| 62 | + public function testAllMigrationsCanRollback(): void |
| 63 | + { |
| 64 | + $migrations_path = \Minz\Configuration::$app_path . '/src/migrations'; |
| 65 | + $migrations_version_path = \Minz\Configuration::$data_path . '/migrations_version.txt'; |
| 66 | + $migrations_files = scandir($migrations_path); |
| 67 | + $this->assertNotFalse($migrations_files); |
| 68 | + $number_migrations = count($migrations_files) - 2; |
| 69 | + \Minz\Database::create(); |
| 70 | + $migrator = new \Minz\Migration\Migrator($migrations_path); |
| 71 | + $migrator->migrate(); |
| 72 | + @file_put_contents($migrations_version_path, $migrator->version()); |
| 73 | + $expected_output = []; |
| 74 | + foreach ($migrator->migrations(reverse: true) as $version => $migration) { |
| 75 | + $expected_output[] = "{$version}: OK"; |
| 76 | + } |
| 77 | + $expected_output = implode("\n", $expected_output); |
| 78 | + |
| 79 | + $response = $this->appRun('CLI', '/migrations/rollback', [ |
| 80 | + 'steps' => $number_migrations, |
| 81 | + ]); |
| 82 | + |
| 83 | + $this->assertResponseCode($response, 200); |
| 84 | + $current_migration_version = @file_get_contents($migrations_version_path); |
| 85 | + $this->assertSame('', $current_migration_version); |
| 86 | + $this->assertResponseEquals($response, $expected_output); |
| 87 | + } |
| 88 | +} |
0 commit comments