Update getVariables to return a copy of globals rather than including the reference in an array merge

This commit is contained in:
Aaron Carlino 2018-02-23 16:25:41 +13:00 committed by Damian Mooyman
parent e23353fe99
commit 0863bac29a
2 changed files with 19 additions and 1 deletions

View File

@ -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;
}
/**

View File

@ -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']);
}
}