Skip to content

Commit 4910ac6

Browse files
committed
bug symfony#24686 Fix $_ENV/$_SERVER precedence in test framework (fabpot)
This PR was merged into the 3.3 branch. Discussion ---------- Fix $_ENV/$_SERVER precedence in test framework | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a When defining env vars values in `phpunit.xml.dist`, we are using `<env ...>`. PHPUnit registers those env vars in `$_ENV`, but not in `$_SERVER`. This means that those values might not be used by Symfony if env vars defined in `.env` are automatically registered (which is my case). In any case, I think it makes sense to make `$_ENV` take precedence as this is how we register them in `phpunit.xml.dist`. Commits ------- 6ed9919 fixed $_ENV/$_SERVER precedence in test framework
2 parents 6cf3d56 + 6ed9919 commit 4910ac6

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private static function getPhpUnitCliConfigArgument()
107107
protected static function getKernelClass()
108108
{
109109
if (isset($_SERVER['KERNEL_CLASS']) || isset($_ENV['KERNEL_CLASS'])) {
110-
$class = isset($_SERVER['KERNEL_CLASS']) ? $_SERVER['KERNEL_CLASS'] : $_ENV['KERNEL_CLASS'];
110+
$class = isset($_ENV['KERNEL_CLASS']) ? $_ENV['KERNEL_CLASS'] : $_SERVER['KERNEL_CLASS'];
111111
if (!class_exists($class)) {
112112
throw new \RuntimeException(sprintf('Class "%s" doesn\'t exist or cannot be autoloaded. Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel or override the %s::createKernel() method.', $class, static::class));
113113
}
@@ -116,7 +116,7 @@ protected static function getKernelClass()
116116
}
117117

118118
if (isset($_SERVER['KERNEL_DIR']) || isset($_ENV['KERNEL_DIR'])) {
119-
$dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : $_ENV['KERNEL_DIR'];
119+
$dir = isset($_ENV['KERNEL_DIR']) ? $_ENV['KERNEL_DIR'] : $_SERVER['KERNEL_DIR'];
120120

121121
if (!is_dir($dir)) {
122122
$phpUnitDir = static::getPhpUnitXmlDir();
@@ -176,20 +176,20 @@ protected static function createKernel(array $options = array())
176176

177177
if (isset($options['environment'])) {
178178
$env = $options['environment'];
179-
} elseif (isset($_SERVER['APP_ENV'])) {
180-
$env = $_SERVER['APP_ENV'];
181179
} elseif (isset($_ENV['APP_ENV'])) {
182180
$env = $_ENV['APP_ENV'];
181+
} elseif (isset($_SERVER['APP_ENV'])) {
182+
$env = $_SERVER['APP_ENV'];
183183
} else {
184184
$env = 'test';
185185
}
186186

187187
if (isset($options['debug'])) {
188188
$debug = $options['debug'];
189-
} elseif (isset($_SERVER['APP_DEBUG'])) {
190-
$debug = $_SERVER['APP_DEBUG'];
191189
} elseif (isset($_ENV['APP_DEBUG'])) {
192190
$debug = $_ENV['APP_DEBUG'];
191+
} elseif (isset($_SERVER['APP_DEBUG'])) {
192+
$debug = $_SERVER['APP_DEBUG'];
193193
} else {
194194
$debug = true;
195195
}

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,12 @@ protected function getEnv($name)
432432
if (isset($this->envCache[$name]) || array_key_exists($name, $this->envCache)) {
433433
return $this->envCache[$name];
434434
}
435-
if (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
436-
return $this->envCache[$name] = $_SERVER[$name];
437-
}
438435
if (isset($_ENV[$name])) {
439436
return $this->envCache[$name] = $_ENV[$name];
440437
}
438+
if (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
439+
return $this->envCache[$name] = $_SERVER[$name];
440+
}
441441
if (false !== ($env = getenv($name)) && null !== $env) { // null is a possible value because of thread safety issues
442442
return $this->envCache[$name] = $env;
443443
}

0 commit comments

Comments
 (0)