diff --git a/mailpoet/RoboFile.php b/mailpoet/RoboFile.php index 3f33f235a0..f5a7b344be 100644 --- a/mailpoet/RoboFile.php +++ b/mailpoet/RoboFile.php @@ -448,12 +448,13 @@ class RoboFile extends \Robo\Tasks { } /** - * Creates a new migration file. Creates Db level migration. Use --level=App to create an application level migration. - * @param $opts array{level: string} + * Creates a new migration file. Use `migrations:new db` for a Db level migration or `migrations:new app` for App level migration. + * @param $level string - db or app */ - public function migrationsNew($opts = ['level' => \MailPoet\Migrator\Repository::MIGRATIONS_LEVEL_DB]) { + public function migrationsNew($level) { $generator = new \MailPoet\Migrator\Repository(); - $result = $generator->create($opts['level']); + $level = ucfirst(strtolower($level)); + $result = $generator->create($level); $path = realpath($result['path']); $this->output->writeln('MAILPOET DATABASE MIGRATIONS'); $this->output->writeln("============================\n"); diff --git a/mailpoet/lib/Migrator/MigratorException.php b/mailpoet/lib/Migrator/MigratorException.php index 619aaa206a..d724c767b3 100644 --- a/mailpoet/lib/Migrator/MigratorException.php +++ b/mailpoet/lib/Migrator/MigratorException.php @@ -12,6 +12,12 @@ class MigratorException extends InvalidStateException { ); } + public static function invalidMigrationLevel(string $level): self { + return self::create()->withMessage( + sprintf('Migration level "%s" is not supported! Use "App" of "Db".', $level) + ); + } + public static function migrationFileWriteFailed(string $path): self { return self::create()->withMessage( sprintf('Could not write migration file "%s".', $path) diff --git a/mailpoet/lib/Migrator/Repository.php b/mailpoet/lib/Migrator/Repository.php index 4e66196979..7298496725 100644 --- a/mailpoet/lib/Migrator/Repository.php +++ b/mailpoet/lib/Migrator/Repository.php @@ -27,6 +27,9 @@ class Repository { /** @return array{name: string, path: string} */ public function create(string $level): array { + if (!in_array($level, [self::MIGRATIONS_LEVEL_APP, self::MIGRATIONS_LEVEL_DB], true)) { + throw MigratorException::invalidMigrationLevel($level); + } $templateFile = str_replace('{level}', $level, $this->templateFile); $template = @file_get_contents($templateFile); if (!$template) { diff --git a/mailpoet/tests/unit/Migrator/RepositoryTest.php b/mailpoet/tests/unit/Migrator/RepositoryTest.php index 0d631d03bd..5271569b55 100644 --- a/mailpoet/tests/unit/Migrator/RepositoryTest.php +++ b/mailpoet/tests/unit/Migrator/RepositoryTest.php @@ -47,6 +47,18 @@ class RepositoryTest extends MailPoetUnitTest { $repository->create(Repository::MIGRATIONS_LEVEL_DB); } + public function testItFailsCreatingLevelIsInvalid(): void { + $migrationsDir = __DIR__ . '/TestMigrations'; + $repository = $this->make(Repository::class, [ + 'migrationsDir' => $migrationsDir, + 'templateFile' => self::TEMPLATE_FILE, + ]); + + $this->expectException(MigratorException::class); + $this->expectExceptionMessage(sprintf('Migration level "%s" is not supported! Use "App" of "Db".', 'abc')); + $repository->create('abc'); + } + public function testItLoadsMigrationFiles(): void { $repository = $this->make(Repository::class, [ 'migrationsDir' => __DIR__ . '/TestMigrations',