From 68b532c2a9d611b5fca804f7cdc190b7da37fef3 Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Thu, 30 Aug 2018 12:39:17 +0545 Subject: [PATCH] Support json for system:config:set --- core/Command/Config/System/SetConfig.php | 12 +++++- .../Command/Config/System/SetConfigTest.php | 43 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/core/Command/Config/System/SetConfig.php b/core/Command/Config/System/SetConfig.php index 65133ce54c8d..bcebf5db04d8 100644 --- a/core/Command/Config/System/SetConfig.php +++ b/core/Command/Config/System/SetConfig.php @@ -56,7 +56,7 @@ protected function configure() { 'type', null, InputOption::VALUE_REQUIRED, - 'Value type [string, integer, double, boolean].', + 'Value type [string, integer, double, boolean, json].', 'string' ) ->addOption( @@ -162,6 +162,16 @@ protected function castValue($value, $type) { 'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value, ]; + case 'json': + $decodedJson = \json_decode($value, true); + if ($decodedJson === null) { + throw new \InvalidArgumentException('Unable to parse value as json'); + } + return [ + 'value' => $decodedJson, + 'readable-value' => 'json ' . $value, + ]; + default: throw new \InvalidArgumentException('Invalid type'); } diff --git a/tests/Core/Command/Config/System/SetConfigTest.php b/tests/Core/Command/Config/System/SetConfigTest.php index 4d5b413ac7d9..fb1ff392b7b4 100644 --- a/tests/Core/Command/Config/System/SetConfigTest.php +++ b/tests/Core/Command/Config/System/SetConfigTest.php @@ -122,6 +122,44 @@ public function testSetUpdateOnly($configNames, $existingData) { $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } + public function setJsonData() { + return [ + [['name'], '{"sub-key":"value"}', null, ['sub-key' => 'value']], + [['name'], '{"sub-key":"value"}', 'something', ['sub-key' => 'value']], + [['name'], '{"sub-key":"value"}', ['sub-key' => 'old-value', 'other-key' => 'will disappear'], ['sub-key' => 'value']], + [['name'], '[{"key1":"value1","key2":"value2"}]', null, [['key1' => 'value1', 'key2' => 'value2']]], + ]; + } + + /** + * @dataProvider setJsonData + * + * @param array $configNames + * @param string $newValue + * @param mixed $existingData + * @param mixed $expectedValue + */ + public function testSetJson($configNames, $newValue, $existingData, $expectedValue) { + $this->systemConfig->expects($this->once()) + ->method('setValue') + ->with($configNames[0], $expectedValue); + $this->systemConfig->method('getValue') + ->with($configNames[0]) + ->willReturn($existingData); + + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('name') + ->willReturn($configNames); + $this->consoleInput->method('getOption') + ->will($this->returnValueMap([ + ['value', $newValue], + ['type', 'json'], + ])); + + $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + } + public function castValueProvider() { return [ [null, 'string', ['value' => '', 'readable-value' => 'empty string']], @@ -138,6 +176,9 @@ public function castValueProvider() { ['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']], ['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']], + + ['{"config_key":"the-value"}', 'json', ['value' => ['config_key' => 'the-value'], 'readable-value' => 'json {"config_key":"the-value"}']], + ['[{"key1":"value1","key2":"value2"}]', 'json', ['value' => [['key1' => 'value1', 'key2' => 'value2']], 'readable-value' => 'json [{"key1":"value1","key2":"value2"}]']], ]; } @@ -159,6 +200,8 @@ public function castValueInvalidProvider() { ['76ggg', 'double'], ['true', 'float'], ['foobar', 'boolean'], + ['invalid-json', 'json'], + ['', 'json'], ]; }