From 4da00c540672d687b8efa7b5f0c2b50107845379 Mon Sep 17 00:00:00 2001 From: Agustin Gomes Date: Mon, 9 Jan 2023 22:08:19 +0100 Subject: [PATCH] Apply workaround to maintain expected behavior This test case addition simulates what happens when the `--all-or-nothing` option is indicated, but no explicit value is passed. We'll set a default option, which is retrieved in case the option has not been provided, allowing us to override the value in case the option is different than the default value. --- .../Tools/Console/Command/MigrateCommand.php | 3 ++- ...onsoleInputMigratorConfigurationFactory.php | 18 ++++++++++++++++-- .../Console/Command/MigrateCommandTest.php | 13 +++++++++++-- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php b/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php index 13394258c..24a48df83 100644 --- a/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php +++ b/lib/Doctrine/Migrations/Tools/Console/Command/MigrateCommand.php @@ -75,7 +75,8 @@ protected function configure(): void 'all-or-nothing', null, InputOption::VALUE_OPTIONAL, - 'Wrap the entire migration in a transaction.' + 'Wrap the entire migration in a transaction.', + 'notprovided' ) ->setHelp(<<%command.name% command executes a migration to a specified version or the latest available version: diff --git a/lib/Doctrine/Migrations/Tools/Console/ConsoleInputMigratorConfigurationFactory.php b/lib/Doctrine/Migrations/Tools/Console/ConsoleInputMigratorConfigurationFactory.php index 04ee777b3..853b28913 100644 --- a/lib/Doctrine/Migrations/Tools/Console/ConsoleInputMigratorConfigurationFactory.php +++ b/lib/Doctrine/Migrations/Tools/Console/ConsoleInputMigratorConfigurationFactory.php @@ -21,12 +21,26 @@ public function getMigratorConfiguration(InputInterface $input): MigratorConfigu { $timeAllQueries = $input->hasOption('query-time') ? (bool) $input->getOption('query-time') : false; $dryRun = $input->hasOption('dry-run') ? (bool) $input->getOption('dry-run') : false; - $allOrNothing = $input->hasOption('all-or-nothing') ? $input->getOption('all-or-nothing') : null; - $allOrNothing = (bool) ($allOrNothing ?? $this->configuration->isAllOrNothing()); + $allOrNothing = $this->determineAllOrNothingValueFrom($input) ?? $this->configuration->isAllOrNothing(); return (new MigratorConfiguration()) ->setDryRun($dryRun) ->setTimeAllQueries($timeAllQueries) ->setAllOrNothing($allOrNothing); } + + private function determineAllOrNothingValueFrom(InputInterface $input): ?bool + { + $allOrNothingOption = null; + + if ($input->hasOption('all-or-nothing')) { + $allOrNothingOption = $input->getOption('all-or-nothing'); + } + + if ($allOrNothingOption === 'notprovided') { + return null; + } + + return (bool) ($allOrNothingOption ?? true); + } } diff --git a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php index 71aa4344d..3838137ae 100644 --- a/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php +++ b/tests/Doctrine/Migrations/Tests/Tools/Console/Command/MigrateCommandTest.php @@ -362,7 +362,7 @@ public function testExecuteMigrateDown(): void } /** - * @psalm-param array $input + * @psalm-param array $input * * @dataProvider allOrNothing */ @@ -390,13 +390,22 @@ public function testExecuteMigrateAllOrNothing(bool $default, array $input, bool } /** - * @psalm-return Generator, bool}> + * @psalm-return Generator, bool}> */ public function allOrNothing(): Generator { yield [false, ['--all-or-nothing' => false], false]; + yield [false, ['--all-or-nothing' => 0], false]; + yield [false, ['--all-or-nothing' => '0'], false]; + yield [false, ['--all-or-nothing' => true], true]; + yield [false, ['--all-or-nothing' => 1], true]; + yield [false, ['--all-or-nothing' => '1'], true]; + yield [false, ['--all-or-nothing' => null], true]; + yield [true, ['--all-or-nothing' => false], false]; + yield [true, ['--all-or-nothing' => 0], false]; + yield [true, ['--all-or-nothing' => '0'], false]; yield [true, [], true]; yield [false, [], false];