Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Dotenv] [DebugCommang] Ensure console commands respect dotenv_path #58159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Symfony/Component/Dotenv/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'/'.($config['dotenv_path'] ?? '.env');
$envFiles = $this->getEnvFiles($filePath);
$availableFiles = array_filter($envFiles, 'is_file');

Expand Down
21 changes: 19 additions & 2 deletions src/Symfony/Component/Dotenv/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,27 @@ 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()
{
$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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? Why don't we store the composer.json file in the fixtures directory in the repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it before, but some tests failed for some reason. Please look at the previous commmit.


// 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()]));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TEST123=true
FOO=bar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FOO=baz