diff options
-rw-r--r-- | core/command/config/system/setconfig.php | 107 | ||||
-rw-r--r-- | tests/core/command/config/system/setconfigtest.php | 40 |
2 files changed, 71 insertions, 76 deletions
diff --git a/core/command/config/system/setconfig.php b/core/command/config/system/setconfig.php index 83a969fd55d..f05b8d6ead3 100644 --- a/core/command/config/system/setconfig.php +++ b/core/command/config/system/setconfig.php @@ -83,7 +83,7 @@ class SetConfig extends Base { $existingValue = $this->systemConfig->getValue($configName); $newValue = $this->mergeArrayValue( - array_slice($configNames, 1), $existingValue, $configValue, $updateOnly + array_slice($configNames, 1), $existingValue, $configValue['value'], $updateOnly ); $this->systemConfig->setValue($configName, $newValue); @@ -92,10 +92,10 @@ class SetConfig extends Base { throw new \UnexpectedValueException('Config parameter does not exist'); } - $this->systemConfig->setValue($configName, $configValue); + $this->systemConfig->setValue($configName, $configValue['value']); } - $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' set to ' . $configValue . '</info>'); + $output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' set to ' . $configValue['readable-value'] . '</info>'); return 0; } @@ -106,57 +106,62 @@ class SetConfig extends Base { * @throws \InvalidArgumentException */ protected function castValue($value, $type) { - if ($value === null) { - return null; - } - - $type = strtolower($type); switch ($type) { - case 'string': - case 'str': - case 's': - return $value; - - case 'integer': - case 'int': - case 'i': - if (!is_numeric($value)) { - throw new \InvalidArgumentException('Non-numeric value specified'); - } - return (int) $value; - - case 'double': - case 'd': - case 'float': - case 'f': - if (!is_numeric($value)) { - throw new \InvalidArgumentException('Non-numeric value specified'); - } - return (double) $value; - - case 'boolean': - case 'bool': - case 'b': - $value = strtolower($value); - switch ($value) { - case 'true': - case 'yes': - case 'y': - case '1': - return true; - - case 'false': - case 'no': - case 'n': - case '0': - return false; + case 'integer': + case 'int': + if (!is_numeric($value)) { + throw new \InvalidArgumentException('Non-numeric value specified'); + } + return [ + 'value' => (int) $value, + 'readable-value' => 'integer ' . (int) $value, + ]; + + case 'double': + case 'float': + if (!is_numeric($value)) { + throw new \InvalidArgumentException('Non-numeric value specified'); + } + return [ + 'value' => (double) $value, + 'readable-value' => 'double ' . (double) $value, + ]; + + case 'boolean': + case 'bool': + $value = strtolower($value); + switch ($value) { + case 'true': + return [ + 'value' => true, + 'readable-value' => 'boolean ' . $value, + ]; + + case 'false': + return [ + 'value' => false, + 'readable-value' => 'boolean ' . $value, + ]; + + default: + throw new \InvalidArgumentException('Unable to parse value as boolean'); + } + + case 'null': + return [ + 'value' => null, + 'readable-value' => 'null', + ]; + + case 'string': + $value = (string) $value; + return [ + 'value' => $value, + 'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value, + ]; default: - throw new \InvalidArgumentException('Unable to parse value as boolean'); - } - - default: - throw new \InvalidArgumentException('Invalid type'); + throw new \InvalidArgumentException('Invalid type'); } } diff --git a/tests/core/command/config/system/setconfigtest.php b/tests/core/command/config/system/setconfigtest.php index 14a6a980938..c0b664d7522 100644 --- a/tests/core/command/config/system/setconfigtest.php +++ b/tests/core/command/config/system/setconfigtest.php @@ -126,31 +126,20 @@ class SetConfigTest extends TestCase { public function castValueProvider() { return [ - [null, 'integer', null], - [null, 'string', null], - - ['abc', 'string', 'abc'], - ['dEF', 'str', 'dEF'], - ['123', 's', '123'], - - ['123', 'integer', 123], - ['456', 'int', 456], - ['-666', 'i', -666], - - // only use powers of 2 to avoid precision errors - ['2', 'double', 2.0], - ['0.25', 'd', 0.25], - ['0.5', 'float', 0.5], - ['0.125', 'f', 0.125], - - ['true', 'boolean', true], - ['false', 'bool', false], - ['yes', 'b', true], - ['no', 'b', false], - ['y', 'b', true], - ['n', 'b', false], - ['1', 'b', true], - ['0', 'b', false], + [null, 'string', ['value' => '', 'readable-value' => 'empty string']], + + ['abc', 'string', ['value' => 'abc', 'readable-value' => 'string abc']], + + ['123', 'integer', ['value' => 123, 'readable-value' => 'integer 123']], + ['456', 'int', ['value' => 456, 'readable-value' => 'integer 456']], + + ['2.25', 'double', ['value' => 2.25, 'readable-value' => 'double 2.25']], + ['0.5', 'float', ['value' => 0.5, 'readable-value' => 'double 0.5']], + + ['', 'null', ['value' => null, 'readable-value' => 'null']], + + ['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']], + ['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']], ]; } @@ -167,6 +156,7 @@ class SetConfigTest extends TestCase { return [ ['123', 'foobar'], + [null, 'integer'], ['abc', 'integer'], ['76ggg', 'double'], ['true', 'float'], |