From dfe7e1b022b9ce91e198b9811acbd2c8cfdfdb3a Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 7 Dec 2023 13:28:35 +0100 Subject: [PATCH] properly handle SYMFONY_DOTENV_VARS being the empty string --- .../Component/Dotenv/Command/DebugCommand.php | 8 +++++- .../Dotenv/Tests/Command/DebugCommandTest.php | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Dotenv/Command/DebugCommand.php b/src/Symfony/Component/Dotenv/Command/DebugCommand.php index 8ceb1fd484845..0585043cd9463 100644 --- a/src/Symfony/Component/Dotenv/Command/DebugCommand.php +++ b/src/Symfony/Component/Dotenv/Command/DebugCommand.php @@ -82,7 +82,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int private function getVariables(array $envFiles): array { - $vars = explode(',', $_SERVER['SYMFONY_DOTENV_VARS'] ?? ''); + $dotenvVars = $_SERVER['SYMFONY_DOTENV_VARS'] ?? ''; + + if ('' === $dotenvVars) { + return []; + } + + $vars = explode(',', $dotenvVars); sort($vars); $output = []; diff --git a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php index b3b089e4559c9..b8b7e39008607 100644 --- a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php @@ -25,6 +25,8 @@ class DebugCommandTest extends TestCase */ public function testErrorOnUninitializedDotenv() { + unset($_SERVER['SYMFONY_DOTENV_VARS']); + $command = new DebugCommand('dev', __DIR__.'/Fixtures/Scenario1'); $command->setHelperSet(new HelperSet([new FormatterHelper()])); $tester = new CommandTester($command); @@ -34,6 +36,30 @@ public function testErrorOnUninitializedDotenv() $this->assertStringContainsString('[ERROR] Dotenv component is not initialized', $output); } + /** + * @runInSeparateProcess + */ + public function testEmptyDotEnvVarsList() + { + $_SERVER['SYMFONY_DOTENV_VARS'] = ''; + + $command = new DebugCommand('dev', __DIR__.'/Fixtures/Scenario1'); + $command->setHelperSet(new HelperSet([new FormatterHelper()])); + $tester = new CommandTester($command); + $tester->execute([]); + $expectedFormat = <<<'OUTPUT' +%a + ---------- ------- ------------ ------%S + Variable Value .env.local .env%S + ---------- ------- ------------ ------%S + + // Note real values might be different between web and CLI.%S +%a +OUTPUT; + + $this->assertStringMatchesFormat($expectedFormat, $tester->getDisplay()); + } + public function testScenario1InDevEnv() { $output = $this->executeCommand(__DIR__.'/Fixtures/Scenario1', 'dev');