From 94a77b3032b8936b6f14fb3a0954f3211a49d4ef Mon Sep 17 00:00:00 2001 From: scruwi Date: Tue, 3 Sep 2024 15:37:24 +0300 Subject: [PATCH 1/3] [Dotenv][DebugCommang]: Ensure console commands respect `dotenv_path` setting from composer.json --- src/Symfony/Component/Dotenv/Command/DebugCommand.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Dotenv/Command/DebugCommand.php b/src/Symfony/Component/Dotenv/Command/DebugCommand.php index 237d7b7cfd228..59ca13ef011b9 100644 --- a/src/Symfony/Component/Dotenv/Command/DebugCommand.php +++ b/src/Symfony/Component/Dotenv/Command/DebugCommand.php @@ -50,7 +50,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } - $filePath = $this->projectDirectory.\DIRECTORY_SEPARATOR.'.env'; + $composerFile = $this->projectDirectory . '/composer.json'; + $config = (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? []; + $filePath = $this->projectDirectory.\DIRECTORY_SEPARATOR.($config['dotenv_path'] ?? '.env'); $envFiles = $this->getEnvFiles($filePath); $availableFiles = array_filter($envFiles, 'is_file'); From b66a4de16d5d7167b415267cf225e6bb69042b26 Mon Sep 17 00:00:00 2001 From: toxxxa Date: Wed, 4 Sep 2024 12:35:29 +0300 Subject: [PATCH 2/3] [Dotenv] Create tests to ensure that command will respect `dotenv_path` setting --- .../Component/Dotenv/Command/DebugCommand.php | 2 +- .../Dotenv/Tests/Command/DebugCommandTest.php | 16 ++++++++++++++-- .../Command/Fixtures/Scenario4/composer.json | 1 + .../Tests/Command/Fixtures/Scenario4/config/.env | 2 ++ .../Command/Fixtures/Scenario4/config/.env.local | 1 + 5 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json create mode 100644 src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env create mode 100644 src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env.local diff --git a/src/Symfony/Component/Dotenv/Command/DebugCommand.php b/src/Symfony/Component/Dotenv/Command/DebugCommand.php index 59ca13ef011b9..55585e8a722e3 100644 --- a/src/Symfony/Component/Dotenv/Command/DebugCommand.php +++ b/src/Symfony/Component/Dotenv/Command/DebugCommand.php @@ -52,7 +52,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $composerFile = $this->projectDirectory . '/composer.json'; $config = (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? []; - $filePath = $this->projectDirectory.\DIRECTORY_SEPARATOR.($config['dotenv_path'] ?? '.env'); + $filePath = $this->projectDirectory.'/'.($config['dotenv_path'] ?? '.env'); $envFiles = $this->getEnvFiles($filePath); $availableFiles = array_filter($envFiles, 'is_file'); diff --git a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php index 001baec0c2539..28b09e1b4bc16 100644 --- a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php @@ -167,10 +167,22 @@ public function testWarningOnPhpEnvFile() $this->assertStringContainsString('[WARNING] Due to existing dump file (.env.local.php)', $output); } - private function executeCommand(string $projectDirectory, string $env): string + public function testLoadEnvFilesFromSubdirectory() + { + $output = $this->executeCommand(__DIR__.'/Fixtures/Scenario4', 'dev', 'config/.env'); + + // Scanned Files + $this->assertStringContainsString('⨯ config/.env.local.php', $output); + $this->assertStringContainsString('⨯ config/.env.dev.local', $output); + $this->assertStringContainsString('⨯ config/.env.dev', $output); + $this->assertStringContainsString('✓ config/.env.local', $output); + $this->assertStringContainsString('✓ config/.env'.\PHP_EOL, $output); + } + + private function executeCommand(string $projectDirectory, string $env, string $envPath = '.env'): string { $_SERVER['TEST_ENV_KEY'] = $env; - (new Dotenv('TEST_ENV_KEY'))->bootEnv($projectDirectory.'/.env'); + (new Dotenv('TEST_ENV_KEY'))->bootEnv($projectDirectory.'/'.$envPath); $command = new DebugCommand($env, $projectDirectory); $command->setHelperSet(new HelperSet([new FormatterHelper()])); diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json new file mode 100644 index 0000000000000..9d9c424cb155a --- /dev/null +++ b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json @@ -0,0 +1 @@ +{"extra":{"runtime":{"dotenv_path": "config/.env"}}} \ No newline at end of file diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env new file mode 100644 index 0000000000000..dbc9b1ecab0ea --- /dev/null +++ b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env @@ -0,0 +1,2 @@ +TEST123=true +FOO=bar diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env.local b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env.local new file mode 100644 index 0000000000000..c6a735ee12086 --- /dev/null +++ b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/config/.env.local @@ -0,0 +1 @@ +FOO=baz From 6255fb65539fa712e849a9a08ae69a4edcd2c3c8 Mon Sep 17 00:00:00 2001 From: toxxxa Date: Wed, 4 Sep 2024 13:58:27 +0300 Subject: [PATCH 3/3] [Dotenv] fix: remove real composer.json from fixtures --- .../Component/Dotenv/Tests/Command/DebugCommandTest.php | 7 ++++++- .../Dotenv/Tests/Command/Fixtures/Scenario4/composer.json | 1 - 2 files changed, 6 insertions(+), 2 deletions(-) delete mode 100644 src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json diff --git a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php index 28b09e1b4bc16..64ad618dfffec 100644 --- a/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php @@ -169,7 +169,12 @@ public function testWarningOnPhpEnvFile() public function testLoadEnvFilesFromSubdirectory() { - $output = $this->executeCommand(__DIR__.'/Fixtures/Scenario4', 'dev', 'config/.env'); + $projectDirectory = __DIR__.'/Fixtures/Scenario4'; + file_put_contents($projectDirectory.'/composer.json', '{"extra":{"runtime":{"dotenv_path": "config/.env"}}}'); + + $output = $this->executeCommand($projectDirectory, 'dev', 'config/.env'); + + @unlink($projectDirectory.'/composer.json'); // Scanned Files $this->assertStringContainsString('⨯ config/.env.local.php', $output); diff --git a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json b/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json deleted file mode 100644 index 9d9c424cb155a..0000000000000 --- a/src/Symfony/Component/Dotenv/Tests/Command/Fixtures/Scenario4/composer.json +++ /dev/null @@ -1 +0,0 @@ -{"extra":{"runtime":{"dotenv_path": "config/.env"}}} \ No newline at end of file