From a048c3e696d084d34386007be9c091b2dc96bc4a Mon Sep 17 00:00:00 2001 From: butschster Date: Mon, 8 Jan 2024 15:07:21 +0400 Subject: [PATCH] Automatically run console commands without asking for confirmation in non-interactive mode. --- .../Command/Migrate/AbstractCommand.php | 2 +- .../Command/CycleOrm/MigrateCommandTest.php | 19 ++++++++++++++++--- .../Command/CycleOrm/SyncCommandTest.php | 8 +++++--- .../Command/Migrate/MigrateCommandTest.php | 5 +++-- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Console/Command/Migrate/AbstractCommand.php b/src/Console/Command/Migrate/AbstractCommand.php index 853305d..763c780 100644 --- a/src/Console/Command/Migrate/AbstractCommand.php +++ b/src/Console/Command/Migrate/AbstractCommand.php @@ -45,7 +45,7 @@ protected function verifyEnvironment(string $message = 'Confirmation is required $this->error($message); - if (!$this->askConfirmation()) { + if (!$this->askConfirmation() || !$this->isInteractive()) { $this->comment('Cancelling operation...'); return false; diff --git a/tests/src/Console/Command/CycleOrm/MigrateCommandTest.php b/tests/src/Console/Command/CycleOrm/MigrateCommandTest.php index e43456d..d913d86 100644 --- a/tests/src/Console/Command/CycleOrm/MigrateCommandTest.php +++ b/tests/src/Console/Command/CycleOrm/MigrateCommandTest.php @@ -37,12 +37,25 @@ protected function setUp(): void public function testMigrate(): void { // Create migration - $this->assertConsoleCommandOutputContainsStrings('cycle:migrate', [], self::USER_MIGRATION); + $this->assertConsoleCommandOutputContainsStrings('cycle:migrate', [ + '--no-interaction' => true, + ], self::USER_MIGRATION); - $this->assertConsoleCommandOutputContainsStrings('cycle:migrate', [], [ + $this->assertConsoleCommandOutputContainsStrings('cycle:migrate', [ + '--no-interaction' => true, + ], [ 'Outstanding migrations found', - 'Detecting schema changes...', + 'Please run `migrate` first.', + ]); + + $this->assertConsoleCommandOutputContainsStrings('migrate', [ 'was successfully executed.', + ]); + + $this->assertConsoleCommandOutputContainsStrings('cycle:migrate', [ + '--no-interaction' => true, + ], [ + 'Detecting schema changes...', 'no database changes has been detected', ]); } diff --git a/tests/src/Console/Command/CycleOrm/SyncCommandTest.php b/tests/src/Console/Command/CycleOrm/SyncCommandTest.php index dc674aa..2ee137c 100644 --- a/tests/src/Console/Command/CycleOrm/SyncCommandTest.php +++ b/tests/src/Console/Command/CycleOrm/SyncCommandTest.php @@ -20,7 +20,10 @@ final class SyncCommandTest extends ConsoleTest #[Env('SAFE_MIGRATIONS', 'false')] public function testUnsafeSync(): void { - $output = $this->runCommand('cycle:sync'); + $output = $this->runCommand('cycle:sync', [ + '--no-interaction' => true, + ]); + $this->assertStringContainsString('This operation is not recommended for production environment.', $output); $this->assertStringContainsString('Cancelling operation...', $output); } @@ -58,8 +61,7 @@ public function testSchemaDefaultsShouldBePassedToCompiler(): void { $config['schema']['defaults'][SchemaInterface::TYPECAST_HANDLER][] = 'foo'; - $memory = new class implements MemoryInterface - { + $memory = new class implements MemoryInterface { private mixed $data; public function loadData(string $section): mixed diff --git a/tests/src/Console/Command/Migrate/MigrateCommandTest.php b/tests/src/Console/Command/Migrate/MigrateCommandTest.php index df97c67..5c02f4a 100644 --- a/tests/src/Console/Command/Migrate/MigrateCommandTest.php +++ b/tests/src/Console/Command/Migrate/MigrateCommandTest.php @@ -34,14 +34,15 @@ public function tesForceMigrate(): void public function testUnsafeMigrate(): void { $db = $this->initMigrations(); - $output = $this->runCommand('migrate'); + $output = $this->runCommand('migrate', [ + '--no-interaction' => true, + ]); $this->assertStringContainsString('Confirmation is required to run migrations!', $output); $this->assertStringContainsString('Cancelling operation...', $output); $this->assertCount(1, $db->getTables()); } /** - * @return void * @throws \Throwable */ public function initMigrations(): DatabaseInterface