From d91648dd6e3ffa2ff0bdf11f463739ac75cc0004 Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Fri, 23 Feb 2018 16:25:41 +1300 Subject: [PATCH 1/2] Update getVariables to return a copy of globals rather than including the reference in an array merge --- src/Core/Environment.php | 7 ++++++- tests/php/Core/EnvironmentTest.php | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Core/Environment.php b/src/Core/Environment.php index 6d5f1f34b..c8f6cb806 100644 --- a/src/Core/Environment.php +++ b/src/Core/Environment.php @@ -43,7 +43,12 @@ class Environment public static function getVariables() { // Suppress return by-ref - return array_merge($GLOBALS, [ 'env' => static::$env ]); + $vars = [ 'env' => static::$env ]; + foreach ($GLOBALS as $varName => $varValue) { + $vars[$varName] = $varValue; + } + + return $vars; } /** diff --git a/tests/php/Core/EnvironmentTest.php b/tests/php/Core/EnvironmentTest.php index 220f47b58..ccd31e83e 100644 --- a/tests/php/Core/EnvironmentTest.php +++ b/tests/php/Core/EnvironmentTest.php @@ -53,4 +53,17 @@ class EnvironmentTest extends SapphireTest Environment::setVariables($vars); $this->assertEquals('initial', Environment::getEnv('_ENVTEST_RESTORED')); } + + public function testGetVariables() + { + $GLOBALS['test'] = 'global'; + $vars = Environment::getVariables(); + $this->assertArrayHasKey('test', $vars); + $this->assertEquals('global', $vars['test']); + $this->assertEquals('global', $GLOBALS['test']); + + $vars['test'] = 'fail'; + $this->assertEquals('fail', $vars['test']); + $this->assertEquals('global', $GLOBALS['test']); + } } From f650c9243619c4c97eae60d33216a4c8edfc5b23 Mon Sep 17 00:00:00 2001 From: Christopher Joe Date: Mon, 26 Feb 2018 12:24:02 +1300 Subject: [PATCH 2/2] Added comment about array_merge() not working --- src/Core/Environment.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Core/Environment.php b/src/Core/Environment.php index c8f6cb806..27d28c65a 100644 --- a/src/Core/Environment.php +++ b/src/Core/Environment.php @@ -44,6 +44,7 @@ class Environment { // Suppress return by-ref $vars = [ 'env' => static::$env ]; + // needs to use a for loop, using `array_merge([], $GLOBALS);` left reference traces somehow foreach ($GLOBALS as $varName => $varValue) { $vars[$varName] = $varValue; }