Skip to content
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

[11.x] Fixes config:cache command detects incorrect custom environment file #52793

Draft
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Draft
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
23 changes: 22 additions & 1 deletion src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

class LoadEnvironmentVariables
{
/**
* The externally provided APP_ENV environment variable.
*/
protected static ?string $externallyProvidedAppEnv = null;

/**
* Bootstrap the given application.
*
Expand Down Expand Up @@ -46,7 +51,7 @@ protected function checkForSpecificEnvironmentFile($app)
return;
}

$environment = Env::get('APP_ENV');
$environment = self::getExternallyProvidedAppEnv();

if (! $environment) {
return;
Expand Down Expand Up @@ -107,4 +112,20 @@ protected function writeErrorAndDie(InvalidFileException $e)

exit(1);
}

/**
* Get the APP_ENV environment variable that is provided externally.
*
* Purely externally provided variables can only be retrieved
* before Dotenv is loaded, so the second and subsequent bootstrap processes
* reuse the values retrieved the first time.
*/
protected static function getExternallyProvidedAppEnv(): string
{
if (self::$externallyProvidedAppEnv === null) {
self::$externallyProvidedAppEnv = Env::get('APP_ENV', '');
}

return self::$externallyProvidedAppEnv;
}
}
53 changes: 46 additions & 7 deletions tests/Foundation/Bootstrap/LoadEnvironmentVariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
use Mockery as m;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class LoadEnvironmentVariablesTest extends TestCase
Expand All @@ -16,18 +17,18 @@ protected function tearDown(): void
m::close();
}

protected function getAppMock($file)
protected function getAppMock($file, $n = 1)
{
$app = m::mock(Application::class);
$app = m::mock(Application::class)->makePartial();

$app->shouldReceive('configurationIsCached')
->once()->with()->andReturn(false);
->times($n)->with()->andReturn(false);
$app->shouldReceive('runningInConsole')
->once()->with()->andReturn(false);
->times($n)->with()->andReturn(false);
$app->shouldReceive('environmentPath')
->once()->with()->andReturn(__DIR__.'/../fixtures');
$app->shouldReceive('environmentFile')
->once()->with()->andReturn($file);
->atLeast($n)->with()->andReturn(__DIR__.'/../fixtures');

$app->loadEnvironmentFrom($file);

return $app;
}
Expand All @@ -50,4 +51,42 @@ public function testCanFailSilent()

(new LoadEnvironmentVariables)->bootstrap($this->getAppMock('BAD_FILE'));
}

#[DataProvider('appEnvProvider')]
public function testIdempotence(?string $appEnv, string $expected): void
{
if (isset($appEnv)) {
$_ENV['APP_ENV'] = $appEnv;
}

$app = $this->getAppMock('.env.idempotence', 2);

(new LoadEnvironmentVariables)->bootstrap($app);

$this->assertSame($expected, env('FOO'));
$this->assertSame($expected, getenv('FOO'));
$this->assertSame($expected, $_ENV['FOO']);
$this->assertSame($expected, $_SERVER['FOO']);

(new LoadEnvironmentVariables)->bootstrap($app);

$this->assertSame($expected, env('FOO'));
$this->assertSame($expected, getenv('FOO'));
$this->assertSame($expected, $_ENV['FOO']);
$this->assertSame($expected, $_SERVER['FOO']);

$reflection = new \ReflectionClass(LoadEnvironmentVariables::class);
$reflection->setStaticPropertyValue('externallyProvidedAppEnv', null);

unset($_ENV['APP_ENV'], $_SERVER['APP_ENV']);
putenv('APP_ENV');
}

public static function appEnvProvider(): array
{
return [
'Use default .env file.' => ['appEnv' => null, 'expected' => 'BAR'],
'APP_ENV has been externally provided.' => ['appEnv' => 'baz', 'expected' => 'BAZ'],
];
}
}
2 changes: 2 additions & 0 deletions tests/Foundation/fixtures/.env.idempotence
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
APP_ENV=baz
FOO=BAR
2 changes: 2 additions & 0 deletions tests/Foundation/fixtures/.env.idempotence.baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
APP_ENV=baz
FOO=BAZ