Skip to content

Commit

Permalink
[#11] Changed syntax for override property.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbankowski committed Oct 1, 2013
1 parent 02ac6be commit 0c70026
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
25 changes: 23 additions & 2 deletions lib/Ouzo/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ public static function registerConfig($customConfig)
return self::$_configInstance;
}

public static function overrideProperty($keys, $value)
public static function overrideProperty()
{
return new ConfigOverrideProperty(func_get_args());
}

public static function overridePropertyArray($keys, $value)
{
self::load()->_overrideProperty($keys, $value);
}
Expand All @@ -115,6 +120,22 @@ private function _overrideProperty($keys, $value)

public static function clearProperty()
{
self::overrideProperty(func_get_args(), null);
self::overridePropertyArray(func_get_args(), null);
}
}

class ConfigOverrideProperty
{

private $keys;

function __construct($keys)
{
$this->keys = $keys;
}

function with($value)
{
Config::overridePropertyArray($this->keys, $value);
}
}
24 changes: 20 additions & 4 deletions test/lib/Ouzo/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private function _deleteSampleConfigFile()
public function shouldOverrideProperty()
{
// given
Config::overrideProperty('key', 'value');
Config::overrideProperty('key')->with('value');

// when
$value = Config::getValue('key');
Expand All @@ -149,19 +149,35 @@ public function shouldOverrideProperty()
Config::clearProperty('key'); // cleanup
}

/**
* @test
*/
public function shouldOverrideMultidimensionalProperty()
{
// given
Config::overrideProperty('key1', 'key2')->with('value');

// when
$value = Config::getValue('key1', 'key2');

// then
$this->assertEquals('value', $value);
Config::clearProperty('key1', 'key2'); // cleanup
}

/**
* @test
*/
public function shouldClearProperty()
{
// given
Config::overrideProperty('key', 'value');
Config::clearProperty('key');
Config::overrideProperty('key_to_clear')->with('value');

// when
$value = Config::getValue('key');
Config::clearProperty('key_to_clear');

// then
$value = Config::getValue('key_to_clear');
$this->assertEmpty($value);
}
}

0 comments on commit 0c70026

Please sign in to comment.