2017-10-16 05:43:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Core\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Environment;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
|
|
|
class EnvironmentTest extends SapphireTest
|
|
|
|
{
|
|
|
|
public function providerTestPutEnv()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['_ENVTEST_BOOL=true', '_ENVTEST_BOOL', true],
|
|
|
|
['_ENVTEST_BOOL_QUOTED="true"', '_ENVTEST_BOOL_QUOTED', 'true'],
|
|
|
|
['_ENVTEST_NUMBER=1', '_ENVTEST_NUMBER', 1],
|
|
|
|
['_ENVTEST_NUMBER_QUOTED="1"', '_ENVTEST_NUMBER_QUOTED', '1'],
|
|
|
|
['_ENVTEST_NUMBER_SPECIAL="value=4"', '_ENVTEST_NUMBER_SPECIAL', 'value=4'],
|
|
|
|
['_ENVTEST_BLANK', '_ENVTEST_BLANK', false],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestPutenv
|
|
|
|
*/
|
|
|
|
public function testPutEnv($put, $var, $value)
|
|
|
|
{
|
|
|
|
Environment::putEnv($put);
|
|
|
|
$this->assertEquals($value, Environment::getEnv($var));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestPutEnv
|
|
|
|
*/
|
|
|
|
public function testSetEnv($put, $var, $value)
|
|
|
|
{
|
|
|
|
Environment::setEnv($var, $value);
|
|
|
|
$this->assertEquals($value, Environment::getEnv($var));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRestoreEnv()
|
|
|
|
{
|
|
|
|
// Set and backup original vars
|
|
|
|
Environment::putEnv('_ENVTEST_RESTORED=initial');
|
|
|
|
$vars = Environment::getVariables();
|
|
|
|
$this->assertEquals('initial', Environment::getEnv('_ENVTEST_RESTORED'));
|
|
|
|
|
|
|
|
// Modify enironment
|
|
|
|
Environment::putEnv('_ENVTEST_RESTORED=new');
|
|
|
|
$this->assertEquals('initial', $vars['env']['_ENVTEST_RESTORED']);
|
|
|
|
$this->assertEquals('new', Environment::getEnv('_ENVTEST_RESTORED'));
|
|
|
|
|
|
|
|
// Restore
|
|
|
|
Environment::setVariables($vars);
|
|
|
|
$this->assertEquals('initial', Environment::getEnv('_ENVTEST_RESTORED'));
|
|
|
|
}
|
2018-02-23 04:25:41 +01:00
|
|
|
|
|
|
|
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']);
|
|
|
|
}
|
2017-10-16 05:43:12 +02:00
|
|
|
}
|