From 58c20532011fd4db5b5d6e62dd1d668b89ed2daa Mon Sep 17 00:00:00 2001 From: Will Rowe Date: Thu, 12 Sep 2024 10:51:40 -0400 Subject: [PATCH] Fix arguments passed to artisan commands that start with 'env' (#52748) * Add failing tests * Ensure that there is an equal sign so that argument names starting with 'env' are not matched --- .../Foundation/EnvironmentDetector.php | 2 +- .../FoundationEnvironmentDetectorTest.php | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/EnvironmentDetector.php b/src/Illuminate/Foundation/EnvironmentDetector.php index b2747bc6800e..8fa61bd2e983 100644 --- a/src/Illuminate/Foundation/EnvironmentDetector.php +++ b/src/Illuminate/Foundation/EnvironmentDetector.php @@ -65,7 +65,7 @@ protected function getEnvironmentArgument(array $args) return $args[$i + 1] ?? null; } - if (str_starts_with($value, '--env')) { + if (str_starts_with($value, '--env=')) { return head(array_slice(explode('=', $value), 1)); } } diff --git a/tests/Foundation/FoundationEnvironmentDetectorTest.php b/tests/Foundation/FoundationEnvironmentDetectorTest.php index d302c375bf50..c06a8ac386dd 100644 --- a/tests/Foundation/FoundationEnvironmentDetectorTest.php +++ b/tests/Foundation/FoundationEnvironmentDetectorTest.php @@ -46,4 +46,34 @@ public function testConsoleEnvironmentDetectionWithNoValue() }, ['--env']); $this->assertSame('foobar', $result); } + + public function testConsoleEnvironmentDetectionDoesNotUseArgumentThatStartsWithEnv() + { + $env = new EnvironmentDetector; + + $result = $env->detect(function () { + return 'foobar'; + }, ['--envelope=mail']); + $this->assertSame('foobar', $result); + } + + public function testConsoleEnvironmentDetectionDoesNotUseArgumentThatStartsWithEnvSeparatedWithSpace() + { + $env = new EnvironmentDetector; + + $result = $env->detect(function () { + return 'foobar'; + }, ['--envelope', 'mail']); + $this->assertSame('foobar', $result); + } + + public function testConsoleEnvironmentDetectionDoesNotUseArgumentThatStartsWithEnvWithNoValue() + { + $env = new EnvironmentDetector; + + $result = $env->detect(function () { + return 'foobar'; + }, ['--envelope']); + $this->assertSame('foobar', $result); + } }