Unit tests for Settings getValue/setValue

- fixed typo in Shortcodes
- changed for -> foreach
This commit is contained in:
Jonathan Labreuille
2016-01-15 15:50:23 +01:00
parent 3689545589
commit bb77134224
3 changed files with 35 additions and 13 deletions

View File

@@ -51,7 +51,6 @@ class Setting extends Model {
$keys = explode('.', $key); $keys = explode('.', $key);
if(count($keys) === 1) { if(count($keys) === 1) {
if(is_array($value)) { if(is_array($value)) {
$value = serialize($value); $value = serialize($value);
} }
@@ -67,17 +66,15 @@ class Setting extends Model {
$current_value = &$setting_value; $current_value = &$setting_value;
$last_key = array_pop($keys); $last_key = array_pop($keys);
for($i = 0, $count = count($keys); $i < $count; $i++) { foreach($keys as $key) {
if($i < $count) {
if(!is_array($current_value)) { if(!is_array($current_value)) {
$current_value = array(); $current_value = array();
} }
if(!array_key_exists($keys[$i], $current_value)) { if(!array_key_exists($key, $current_value)) {
$current_value = array($keys[$i] => array()); $current_value = array($key => array());
}
$current_value =& $current_value[$keys[$i]];
} }
$current_value =& $current_value[$key];
} }
if(is_scalar($current_value)) { if(is_scalar($current_value)) {
$current_value = array(); $current_value = array();

View File

@@ -79,6 +79,31 @@ class SettingCest {
expect($record->value)->equals('new data'); expect($record->value)->equals('new data');
} }
function itCanGetAndSetValue() {
expect(Setting::setValue('test', '123'))->true();
expect(Setting::getValue('test'))->equals('123');
}
function itCanGetAndSetNestedValue() {
expect(Setting::setValue('test.key', '123'))->true();
expect(Setting::getValue('test.key'))->equals('123');
expect(Setting::setValue('test.key.subkey', '123'))->true();
expect(Setting::setValue('test.key.subkey2', '456'))->true();
expect(Setting::getValue('test.key'))->notEmpty();
expect(Setting::getValue('test.key.subkey'))->equals('123');
expect(Setting::getValue('test.key.subkey2'))->equals('456');
}
function itCanSetValueToNull() {
expect(Setting::setValue('test.key', true))->true();
expect(Setting::getValue('test.key'))->equals(true);
expect(Setting::setValue('test.key', null))->true();
expect(Setting::getValue('test.key'))->null();
}
function _after() { function _after() {
ORM::forTable(Setting::$_table) ORM::forTable(Setting::$_table)
->deleteMany(); ->deleteMany();