From 5eab830ebabf8518f47911560ccd321c19b7da0b Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Sat, 14 Oct 2023 10:55:55 +0200 Subject: [PATCH 1/3] Add test for failing input mode --- src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php b/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php index b5289fa0185d2..ac7634b9e29d5 100644 --- a/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php +++ b/src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php @@ -232,6 +232,12 @@ public static function provideNegatableOptions() ['foo' => false], '->parse() parses long options without a value', ], + [ + ['cli.php'], + [new InputOption('foo', null, InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE, '', false)], + ['foo' => false], + '->parse() parses long options without a value', + ], ]; } From 7ec91b4ec9894633aa311a04ab8e3dcc0be852f2 Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Sat, 14 Oct 2023 11:03:28 +0200 Subject: [PATCH 2/3] Allow default values for negatable input in InputOption --- src/Symfony/Component/Console/Input/InputOption.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/Input/InputOption.php b/src/Symfony/Component/Console/Input/InputOption.php index 2bec34fe1a395..9693a5b2f0ef6 100644 --- a/src/Symfony/Component/Console/Input/InputOption.php +++ b/src/Symfony/Component/Console/Input/InputOption.php @@ -177,7 +177,7 @@ public function isNegatable(): bool */ public function setDefault($default = null) { - if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) { + if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && !$this->isNegatable() && null !== $default) { throw new LogicException('Cannot set a default value when using InputOption::VALUE_NONE mode.'); } From ba2f4a5d9cbab20945c60d57e570fe3004ca1c51 Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Sat, 14 Oct 2023 11:04:51 +0200 Subject: [PATCH 3/3] Add test to ensure default value is allowed with InputOption::VALUE_NEGATABLE --- src/Symfony/Component/Console/Tests/Input/InputOptionTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php b/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php index 943bcf628c586..b8dd0ab9c4811 100644 --- a/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php +++ b/src/Symfony/Component/Console/Tests/Input/InputOptionTest.php @@ -154,6 +154,10 @@ public function testSetDefault() $option = new InputOption('foo', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY); $option->setDefault([1, 2]); $this->assertEquals([1, 2], $option->getDefault(), '->setDefault() changes the default value'); + + $option = new InputOption('foo', null, InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE); + $option->setDefault(true); + $this->assertTrue($option->getDefault(), '->setDefault() changes the default value'); } public function testDefaultValueWithValueNoneMode()