added test for configuration
parent
ca96f06876
commit
40d96a8911
@ -0,0 +1,116 @@
|
|||||||
|
<?php
|
||||||
|
namespace BSR\Config;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class ConfigurationTest extends TestCase
|
||||||
|
{
|
||||||
|
protected $testConfig = array(
|
||||||
|
'session' => 'override',
|
||||||
|
'a' => array(
|
||||||
|
'b' => array(
|
||||||
|
'c' => 'C',
|
||||||
|
'c2' => 'D'
|
||||||
|
)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
protected $defaultConfig;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->defaultConfig = array('session' => array('save_path' => session_save_path()));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue