Skip to content

[DependencyInjection] Fix support for false boolean env vars #50530

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

Merged
merged 1 commit into from
Jun 9, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
if (false !== $i || 'string' !== $prefix) {
$env = $getEnv($name);
} elseif ('' === ($env = $_ENV[$name] ?? (str_starts_with($name, 'HTTP_') ? null : ($_SERVER[$name] ?? null)))
|| false === ($env = $env ?? getenv($name) ?? false) // null is a possible value because of thread safety issues
|| (false !== $env && false === ($env = $env ?? getenv($name) ?? false)) // null is a possible value because of thread safety issues
) {
foreach ($this->loadedVars as $vars) {
if (false !== ($env = ($vars[$name] ?? $env)) && '' !== $env) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,67 @@ public static function validStrings()
];
}

/**
* @dataProvider validRealEnvValues
*/
public function testGetEnvRealEnv($value, $processed)
{
$_ENV['FOO'] = $value;

$processor = new EnvVarProcessor(new Container());

$result = $processor->getEnv('string', 'FOO', function () {
$this->fail('Should not be called');
});

$this->assertSame($processed, $result);

unset($_ENV['FOO']);
}

public static function validRealEnvValues()
{
return [
['hello', 'hello'],
[true, '1'],
[false, ''],
[1, '1'],
[0, '0'],
[1.1, '1.1'],
[10, '10'],
];
}

public function testGetEnvRealEnvInvalid()
{
$_ENV['FOO'] = null;
$this->expectException(EnvNotFoundException::class);
$this->expectExceptionMessage('Environment variable not found: "FOO".');

$processor = new EnvVarProcessor(new Container());

$processor->getEnv('string', 'FOO', function () {
$this->fail('Should not be called');
});

unset($_ENV['FOO']);
}

public function testGetEnvRealEnvNonScalar()
{
$_ENV['FOO'] = [];
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Non-scalar env var "FOO" cannot be cast to "string".');

$processor = new EnvVarProcessor(new Container());

$processor->getEnv('string', 'FOO', function () {
$this->fail('Should not be called');
});

unset($_ENV['FOO']);
}

/**
* @dataProvider validBools
*/
Expand Down Expand Up @@ -97,6 +158,7 @@ public static function validBools()
['true', true],
['false', false],
['null', false],
['', false],
['1', true],
['0', false],
['1.1', true],
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy