Skip to content

Commit

Permalink
Fix arguments passed to artisan commands that start with 'env' (#52748)
Browse files Browse the repository at this point in the history
* Add failing tests

* Ensure that there is an equal sign so that argument names starting with 'env' are not matched
  • Loading branch information
willrowe authored Sep 12, 2024
1 parent 80cdd87 commit 58c2053
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/EnvironmentDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Foundation/FoundationEnvironmentDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 58c2053

Please sign in to comment.