From 0863bac29acaa49a650b85c195f8c4ec03c373e0 Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Fri, 23 Feb 2018 16:25:41 +1300 Subject: [PATCH] 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']); + } }