You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.8 KiB
PHP
140 lines
4.8 KiB
PHP
<?php
|
|
namespace BSR\Utils\Configuration;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ConfigurationTest extends TestCase
|
|
{
|
|
protected $testConfig = array(
|
|
'session' => 'override',
|
|
'a' => array(
|
|
'b' => array(
|
|
'c' => 'C',
|
|
'c2' => 'D'
|
|
)
|
|
),
|
|
);
|
|
|
|
protected $defaultConfig;
|
|
protected $dummyConfigFilePath1;
|
|
protected $dummyConfigFilePath2;
|
|
|
|
public function setUp()
|
|
{
|
|
$this->defaultConfig = array('session' => array('save_path' => session_save_path()));
|
|
$this->dummyConfigFilePath1 = realpath(dirname(__FILE__) . '/../../../config/configuration.local.php');
|
|
$this->dummyConfigFilePath2 = realpath(dirname(__FILE__) . '/../../../config/configuration.logger.quiet.php');
|
|
}
|
|
|
|
public function testConfigurationInstanceIsTheSame()
|
|
{
|
|
$this->assertSame(Configuration::getInstance(), Configuration::getInstance());
|
|
}
|
|
|
|
public function testInitialDefaultConfigContainsOnlyDefaultSessionSavePath()
|
|
{
|
|
$this->assertSame(Configuration::getInstance()->getConfig(), $this->defaultConfig);
|
|
}
|
|
|
|
public function testConfigChangesAfterSettingCustomConfig()
|
|
{
|
|
$preConfig = Configuration::getInstance()->getConfig();
|
|
Configuration::getInstance()->setCustomConfig($this->testConfig);
|
|
$postConfig = Configuration::getInstance()->getConfig();
|
|
$this->assertNotSame($preConfig, $postConfig);
|
|
}
|
|
|
|
public function testCustomConfigGetsMergedProperly()
|
|
{
|
|
Configuration::getInstance()->setCustomConfig($this->testConfig);
|
|
$this->assertSame(Configuration::getInstance()->getConfig(), $this->testConfig);
|
|
}
|
|
|
|
public function testReturnsFalseIfHasKeyButNotMostNestedKey()
|
|
{
|
|
Configuration::getInstance()->setCustomConfig($this->defaultConfig);
|
|
Configuration::getInstance()->setCustomConfig($this->testConfig);
|
|
$this->assertEquals(Configuration::has(array('session', 'save_path')), false);
|
|
}
|
|
|
|
public function testReturnsFalseIfHasNotInitialKeyButHasNestedKey()
|
|
{
|
|
Configuration::getInstance()->setCustomConfig($this->defaultConfig);
|
|
$this->assertEquals(Configuration::has(array('z', 'save_path')), false);
|
|
}
|
|
|
|
public function testReturnsFalseIfHasNoneOfKeyPath()
|
|
{
|
|
Configuration::getInstance()->setCustomConfig($this->defaultConfig);
|
|
$this->assertEquals(Configuration::has(array('z', 'w')), false);
|
|
}
|
|
|
|
public function testReturnsTrueIfHasKeyPath()
|
|
{
|
|
Configuration::getInstance()->setCustomConfig($this->defaultConfig);
|
|
$this->assertEquals(Configuration::has(array('session', 'save_path')), true);
|
|
}
|
|
|
|
public function testCanGetNestedKeyValueFromKeysList()
|
|
{
|
|
Configuration::getInstance()->setCustomConfig($this->defaultConfig);
|
|
$this->assertSame(
|
|
Configuration::get(array('session', 'save_path')),
|
|
$this->defaultConfig['session']['save_path']
|
|
);
|
|
}
|
|
|
|
public function testReturnsDefaultParamIfKeyNotSet()
|
|
{
|
|
Configuration::getInstance()->setCustomConfig($this->defaultConfig);
|
|
$default = "some_random_thing";
|
|
$this->assertSame(
|
|
Configuration::get(array('x', 'y'), $default),
|
|
$default
|
|
);
|
|
}
|
|
|
|
public function testKeysInDotNotationGetConvertedToKeysArray()
|
|
{
|
|
$this->assertSame(
|
|
Configuration::parseKeys('a.b.c'),
|
|
array('a', 'b', 'c')
|
|
);
|
|
}
|
|
|
|
public function testKeysInDotNotationGetsSameResult()
|
|
{
|
|
Configuration::getInstance()->setCustomConfig($this->defaultConfig);
|
|
Configuration::getInstance()->setCustomConfig($this->testConfig);
|
|
$this->assertSame(
|
|
Configuration::get('a.b.c'),
|
|
Configuration::get(array('a', 'b', 'c'))
|
|
);
|
|
}
|
|
|
|
public function testGetWithoutParamReturnsFullConfigArray()
|
|
{
|
|
Configuration::getInstance()->setCustomConfig($this->testConfig);
|
|
$this->assertSame(Configuration::get(), $this->testConfig);
|
|
}
|
|
|
|
public function testDummyConfigFileExists()
|
|
{
|
|
$this->assertEquals(file_exists($this->dummyConfigFilePath1), true);
|
|
}
|
|
|
|
public function testFileCustomConfigGetsLoadedIfFileExists()
|
|
{
|
|
$configPriorToFileLoading = Configuration::get();
|
|
Configuration::setConfigFilePath($this->dummyConfigFilePath1);
|
|
$this->assertSame(Configuration::get(), array_replace_recursive($this->testConfig, include $this->dummyConfigFilePath1));
|
|
}
|
|
|
|
public function testSetConfigFilePathWithSecondParamTrueClearsPreviousConfig()
|
|
{
|
|
$configPriorToFileLoading = Configuration::get();
|
|
Configuration::setConfigFilePath($this->dummyConfigFilePath2, true);
|
|
$this->assertSame(Configuration::get(), include $this->dummyConfigFilePath2);
|
|
}
|
|
}
|