Skip to content

Commit e027985

Browse files
feature #810 Use the extra.runtime.test_envs composer variabels in the symfony:dump-env command (cmodijk)
This PR was merged into the 1.16-dev branch. Discussion ---------- Use the extra.runtime.test_envs composer variabels in the symfony:dump-env command Added the option to read the `extra.runtime.test_envs` to load the test envs during the environment dump. This is my attempt to fix #747. Commits ------- 6c3655b Added the option to read the `extra.runtime.test_envs` to load the test envs during the environment dump
2 parents 658f638 + 6c3655b commit e027985

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/Command/DumpEnvCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Composer\Command\BaseCommand;
1515
use Composer\Config;
16+
use Composer\Factory;
1617
use Symfony\Component\Console\Input\InputArgument;
1718
use Symfony\Component\Console\Input\InputInterface;
1819
use Symfony\Component\Console\Input\InputOption;
@@ -111,13 +112,16 @@ private function loadEnv(string $path, ?string $env): array
111112
throw new \RuntimeException('Please provide the name of the environment either by passing it as command line argument or by defining the "APP_ENV" variable in the ".env.local" file.');
112113
}
113114

115+
$testEnvs = $config['extra']['runtime']['test_envs'] ?? ['test'];
116+
114117
if (method_exists($dotenv, 'loadEnv')) {
115-
$dotenv->loadEnv($path);
118+
$config = @json_decode(file_get_contents(Factory::getComposerFile()), true);
119+
$dotenv->loadEnv($path, 'APP_ENV', 'dev', $testEnvs);
116120
} else {
117121
// fallback code in case your Dotenv component is not 4.2 or higher (when loadEnv() was added)
118122
$dotenv->load(file_exists($path) || !file_exists($p = "$path.dist") ? $path : $p);
119123

120-
if ('test' !== $env && file_exists($p = "$path.local")) {
124+
if (!\in_array($env, $testEnvs, true) && file_exists($p = "$path.local")) {
121125
$dotenv->load($p);
122126
}
123127

tests/Command/DumpEnvCommandTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,46 @@ public function testDoesNotRequireToSpecifyEnvArgumentWhenLocalFileIsPresent()
164164
unlink($envLocalPhp);
165165
}
166166

167+
public function testLoadLocalEnvWhenTestEnvIsNotEqual()
168+
{
169+
@mkdir(FLEX_TEST_DIR);
170+
$env = FLEX_TEST_DIR.'/.env';
171+
$envLocal = FLEX_TEST_DIR.'/.env.local';
172+
$envLocalPhp = FLEX_TEST_DIR.'/.env.local.php';
173+
$composer = __DIR__.'/../../composer.json';
174+
@unlink($envLocalPhp);
175+
176+
file_put_contents($env, 'APP_ENV=dev');
177+
$envContent = <<<EOF
178+
APP_ENV=test
179+
APP_SECRET=abcdefgh123456789
180+
EOF;
181+
file_put_contents($envLocal, $envContent);
182+
183+
copy($composer, FLEX_TEST_DIR.'/composer-backup.json');
184+
$composerContent = @json_decode(file_get_contents($composer), true);
185+
$composerContent['extra']['runtime']['test_envs'] = [];
186+
file_put_contents($composer, json_encode($composerContent));
187+
188+
$command = $this->createCommandDumpEnv();
189+
$command->execute([
190+
'env' => 'test',
191+
]);
192+
193+
$this->assertFileExists($envLocalPhp);
194+
195+
$vars = require $envLocalPhp;
196+
$this->assertSame([
197+
'APP_ENV' => 'test',
198+
'APP_SECRET' => 'abcdefgh123456789',
199+
], $vars);
200+
201+
unlink($env);
202+
unlink($envLocal);
203+
unlink($envLocalPhp);
204+
copy(FLEX_TEST_DIR.'/composer-backup.json', $composer);
205+
}
206+
167207
private function createCommandDumpEnv()
168208
{
169209
$command = new DumpEnvCommand(

0 commit comments

Comments
 (0)