diff --git a/core/Config.php b/core/Config.php index 65012f2a9..365875479 100644 --- a/core/Config.php +++ b/core/Config.php @@ -310,7 +310,6 @@ class Config { } public static function merge_high_into_low(&$result, $value) { - if (!$value) return; $newType = self::get_value_type($value); if (!$result) { @@ -509,7 +508,7 @@ class Config { * Update a configuration value * * Configuration is modify only. The value passed is merged into the existing configuration. If you want to - * replace the current value, you'll need to call remove first. + * replace the current array value, you'll need to call remove first. * * @param $class string - The class to update a configuration value for * @param $name string - The configuration property name to update @@ -522,10 +521,17 @@ class Config { * You will get an error if you try and override array values with non-array values or vice-versa */ public function update($class, $name, $val) { - if (!isset($this->overrides[0][$class])) $this->overrides[0][$class] = array(); + if(is_null($val)) { + $this->remove($class, $name); + } else { + if (!isset($this->overrides[0][$class])) $this->overrides[0][$class] = array(); - if (!isset($this->overrides[0][$class][$name])) $this->overrides[0][$class][$name] = $val; - else self::merge_high_into_low($this->overrides[0][$class][$name], $val); + if (!array_key_exists($name, $this->overrides[0][$class])) { + $this->overrides[0][$class][$name] = $val; + } else { + self::merge_high_into_low($this->overrides[0][$class][$name], $val); + } + } $this->cache->clean("__{$class}__{$name}"); } @@ -560,7 +566,7 @@ class Config { * * Matching is always by "==", not by "===" */ - public function remove($class, $name) { + public function remove($class, $name /*,$key = null*/ /*,$value = null*/) { $argc = func_num_args(); $key = $argc > 2 ? func_get_arg(2) : self::anything(); $value = $argc > 3 ? func_get_arg(3) : self::anything(); diff --git a/tests/core/ConfigTest.php b/tests/core/ConfigTest.php index 7ed19ed9e..6a2519d69 100644 --- a/tests/core/ConfigTest.php +++ b/tests/core/ConfigTest.php @@ -21,6 +21,16 @@ class ConfigStaticTest_First extends Config { public static $first = array('test_1'); public static $second = array('test_1'); public static $third = 'test_1'; + + public static $bool = true; + public static $int = 42; + public static $string = 'value'; + public static $nullable = 'value'; + + public static $default_false = false; + public static $default_null = null; + public static $default_zero = 0; + public static $default_emtpy_string = ''; } class ConfigStaticTest_Second extends ConfigStaticTest_First { @@ -83,6 +93,44 @@ class ConfigTest extends SapphireTest { array('test_3_2')); } + public function testUpdateWithFalsyValues() { + // Booleans + $this->assertTrue(Config::inst()->get('ConfigStaticTest_First', 'bool')); + Config::inst()->update('ConfigStaticTest_First', 'bool', false); + $this->assertFalse(Config::inst()->get('ConfigStaticTest_First', 'bool')); + Config::inst()->update('ConfigStaticTest_First', 'bool', true); + $this->assertTrue(Config::inst()->get('ConfigStaticTest_First', 'bool')); + + // Integers + $this->assertEquals(42, Config::inst()->get('ConfigStaticTest_First', 'int')); + Config::inst()->update('ConfigStaticTest_First', 'int', 0); + $this->assertEquals(0, Config::inst()->get('ConfigStaticTest_First', 'int')); + Config::inst()->update('ConfigStaticTest_First', 'int', 42); + $this->assertEquals(42, Config::inst()->get('ConfigStaticTest_First', 'int')); + + // Strings + $this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'string')); + Config::inst()->update('ConfigStaticTest_First', 'string', ''); + $this->assertEquals('', Config::inst()->get('ConfigStaticTest_First', 'string')); + Config::inst()->update('ConfigStaticTest_First', 'string', 'value'); + $this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'string')); + + // Nulls + $this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'nullable')); + Config::inst()->update('ConfigStaticTest_First', 'nullable', null); + $this->assertNull(Config::inst()->get('ConfigStaticTest_First', 'nullable')); + Config::inst()->update('ConfigStaticTest_First', 'nullable', 'value'); + $this->assertEquals('value', Config::inst()->get('ConfigStaticTest_First', 'nullable')); + } + + public function testSetsFalsyDefaults() { + $this->assertFalse(Config::inst()->get('ConfigStaticTest_First', 'default_false')); + // Technically the same as an undefined config key + $this->assertNull(Config::inst()->get('ConfigStaticTest_First', 'default_null')); + $this->assertEquals(0, Config::inst()->get('ConfigStaticTest_First', 'default_zero')); + $this->assertEquals('', Config::inst()->get('ConfigStaticTest_First', 'default_empty_string')); + } + public function testUninheritedStatic() { $this->assertEquals(Config::inst()->get('ConfigStaticTest_First', 'third', Config::UNINHERITED), 'test_1'); $this->assertEquals(Config::inst()->get('ConfigStaticTest_Fourth', 'third', Config::UNINHERITED), null);