Skip to content

Commit bc64573

Browse files
committed
fix: copy SetConfig.php and SetConfigTest.php from master to backport branch
Signed-off-by: yemkareems <[email protected]>
1 parent 43e38a1 commit bc64573

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

core/Command/Config/System/SetConfig.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,80 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8181
return 0;
8282
}
8383

84+
/**
85+
* @param string $value
86+
* @param string $type
87+
* @return mixed
88+
* @throws \InvalidArgumentException
89+
*/
90+
protected function castValue($value, $type) {
91+
switch ($type) {
92+
case 'integer':
93+
case 'int':
94+
if (!is_numeric($value)) {
95+
throw new \InvalidArgumentException('Non-numeric value specified');
96+
}
97+
return [
98+
'value' => (int)$value,
99+
'readable-value' => 'integer ' . (int)$value,
100+
];
101+
102+
case 'double':
103+
case 'float':
104+
if (!is_numeric($value)) {
105+
throw new \InvalidArgumentException('Non-numeric value specified');
106+
}
107+
return [
108+
'value' => (float)$value,
109+
'readable-value' => 'double ' . (float)$value,
110+
];
111+
112+
case 'boolean':
113+
case 'bool':
114+
$value = strtolower($value);
115+
switch ($value) {
116+
case 'true':
117+
return [
118+
'value' => true,
119+
'readable-value' => 'boolean ' . $value,
120+
];
121+
122+
case 'false':
123+
return [
124+
'value' => false,
125+
'readable-value' => 'boolean ' . $value,
126+
];
127+
128+
default:
129+
throw new \InvalidArgumentException('Unable to parse value as boolean');
130+
}
131+
132+
// no break
133+
case 'null':
134+
return [
135+
'value' => null,
136+
'readable-value' => 'null',
137+
];
138+
139+
case 'string':
140+
$value = (string)$value;
141+
return [
142+
'value' => $value,
143+
'readable-value' => ($value === '') ? 'empty string' : 'string ' . $value,
144+
];
145+
146+
case 'json':
147+
$value = json_decode($value, true);
148+
return [
149+
'value' => $value,
150+
'readable-value' => 'json ' . json_encode($value),
151+
];
152+
153+
default:
154+
throw new \InvalidArgumentException('Invalid type');
155+
}
156+
}
157+
84158
/**
85159
* @param array $configNames
86160
* @param mixed $existingValues

tests/Core/Command/Config/System/SetConfigTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
45
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -56,7 +57,7 @@ public function setData() {
5657
* @param mixed $existingData
5758
* @param mixed $expectedValue
5859
*/
59-
public function testSet($configNames, $newValue, $existingData, $expectedValue): void {
60+
public function testSet($configNames, $newValue, $existingData, $expectedValue) {
6061
$this->systemConfig->expects($this->once())
6162
->method('setValue')
6263
->with($configNames[0], $expectedValue);
@@ -89,7 +90,7 @@ public function setUpdateOnlyProvider() {
8990
/**
9091
* @dataProvider setUpdateOnlyProvider
9192
*/
92-
public function testSetUpdateOnly($configNames, $existingData): void {
93+
public function testSetUpdateOnly($configNames, $existingData) {
9394
$this->expectException(\UnexpectedValueException::class);
9495

9596
$this->systemConfig->expects($this->never())
@@ -136,7 +137,7 @@ public function castValueProvider() {
136137
/**
137138
* @dataProvider castValueProvider
138139
*/
139-
public function testCastValue($value, $type, $expectedValue): void {
140+
public function testCastValue($value, $type, $expectedValue) {
140141
$this->assertSame($expectedValue,
141142
$this->invokePrivate($this->command, 'castValue', [$value, $type])
142143
);
@@ -157,7 +158,7 @@ public function castValueInvalidProvider() {
157158
/**
158159
* @dataProvider castValueInvalidProvider
159160
*/
160-
public function testCastValueInvalid($value, $type): void {
161+
public function testCastValueInvalid($value, $type) {
161162
$this->expectException(\InvalidArgumentException::class);
162163

163164
$this->invokePrivate($this->command, 'castValue', [$value, $type]);

0 commit comments

Comments
 (0)