Skip to content

Commit

Permalink
Merge branch '5.3' into 5.4
Browse files Browse the repository at this point in the history
* 5.3:
  [Dotenv] Fix testLoadEnv() .env.dist isolation
  • Loading branch information
nicolas-grekas committed Oct 25, 2021
2 parents 87bc683 + d86eb40 commit a188c79
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions Tests/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,12 @@ public function testLoadEnv()
$resetContext = static function (): void {
unset($_ENV['SYMFONY_DOTENV_VARS']);
unset($_ENV['FOO']);
unset($_ENV['BAR']);
unset($_ENV['TEST_APP_ENV']);
unset($_SERVER['SYMFONY_DOTENV_VARS']);
unset($_SERVER['FOO']);
unset($_SERVER['BAR']);
unset($_SERVER['TEST_APP_ENV']);
putenv('SYMFONY_DOTENV_VARS');
putenv('FOO');
putenv('BAR');
putenv('TEST_APP_ENV');
};

Expand All @@ -246,55 +243,54 @@ public function testLoadEnv()
$path = tempnam($tmpdir, 'sf-');

// .env
file_put_contents($path, 'FOO=BAR');

$resetContext();
file_put_contents($path, 'FOO=BAR');
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('BAR', getenv('FOO'));
$this->assertSame('dev', getenv('TEST_APP_ENV'));

// .env.local
file_put_contents("$path.local", 'FOO=localBAR');

$resetContext();
$_SERVER['TEST_APP_ENV'] = 'local';
file_put_contents("$path.local", 'FOO=localBAR');
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('localBAR', getenv('FOO'));

// special case for test

$resetContext();
$_SERVER['TEST_APP_ENV'] = 'test';
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('BAR', getenv('FOO'));

// .env.dev
file_put_contents("$path.dev", 'FOO=devBAR');

$resetContext();
file_put_contents("$path.dev", 'FOO=devBAR');
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('devBAR', getenv('FOO'));

// .env.dev.local
file_put_contents("$path.dev.local", 'FOO=devlocalBAR');

$resetContext();
file_put_contents("$path.dev.local", 'FOO=devlocalBAR');
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('devlocalBAR', getenv('FOO'));
unlink("$path.local");
unlink("$path.dev");
unlink("$path.dev.local");

// .env.dist
file_put_contents("$path.dist", 'FOO=distBAR');

$resetContext();
unlink($path);
file_put_contents("$path.dist", 'BAR=distBAR');
(new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
$this->assertSame('distBAR', getenv('BAR'));
$this->assertSame('distBAR', getenv('FOO'));
unlink("$path.dist");

$resetContext();
unlink("$path.dist");
unlink("$path.local");
unlink("$path.dev");
unlink("$path.dev.local");
rmdir($tmpdir);
}

Expand Down

0 comments on commit a188c79

Please sign in to comment.