Allow deleting a nested system config value

This commit is contained in:
Joas Schilling 2015-12-09 14:26:17 +01:00
parent 00ab50defc
commit a06f0256a9
3 changed files with 156 additions and 16 deletions

View File

@ -48,8 +48,8 @@ class DeleteConfig extends Base {
->setDescription('Delete a system config value')
->addArgument(
'name',
InputArgument::REQUIRED,
'Name of the config to delete'
InputArgument::REQUIRED | InputArgument::IS_ARRAY,
'Name of the config to delete, specify multiple for array parameter'
)
->addOption(
'error-if-not-exists',
@ -61,15 +61,57 @@ class DeleteConfig extends Base {
}
protected function execute(InputInterface $input, OutputInterface $output) {
$configName = $input->getArgument('name');
$configNames = $input->getArgument('name');
$configName = $configNames[0];
if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
$output->writeln('<error>System config ' . $configName . ' could not be deleted because it did not exist</error>');
return 1;
if (sizeof($configNames) > 1) {
if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
$output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
return 1;
}
$value = $this->systemConfig->getValue($configName);
try {
$value = $this->removeSubValue(array_slice($configNames, 1), $value, $input->hasParameterOption('--error-if-not-exists'));
}
catch (\UnexpectedValueException $e) {
$output->writeln('<error>System config ' . implode(' => ', $configNames) . ' could not be deleted because it did not exist</error>');
return 1;
}
$this->systemConfig->setValue($configName, $value);
$output->writeln('<info>System config value ' . implode(' => ', $configNames) . ' deleted</info>');
return 0;
} else {
if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->systemConfig->getKeys())) {
$output->writeln('<error>System config ' . $configName . ' could not be deleted because it did not exist</error>');
return 1;
}
$this->systemConfig->deleteValue($configName);
$output->writeln('<info>System config value ' . $configName . ' deleted</info>');
return 0;
}
}
protected function removeSubValue($keys, $currentValue, $throwError) {
$nextKey = array_shift($keys);
if (is_array($currentValue)) {
if (isset($currentValue[$nextKey])) {
if (empty($keys)) {
unset($currentValue[$nextKey]);
} else {
$currentValue[$nextKey] = $this->removeSubValue($keys, $currentValue[$nextKey], $throwError);
}
} else if ($throwError) {
throw new \UnexpectedValueException('Config parameter does not exist');
}
} else if ($throwError) {
throw new \UnexpectedValueException('Config parameter does not exist');
}
$this->systemConfig->deleteValue($configName);
$output->writeln('<info>System config value ' . $configName . ' deleted</info>');
return 0;
return $currentValue;
}
}

View File

@ -79,7 +79,7 @@ class SetConfig extends Base {
$configValue = $this->castValue($input->getOption('value'), $input->getOption('type'));
$updateOnly = $input->getOption('update-only');
if (count($configNames) > 1) {
if (sizeof($configNames) > 1) {
$existingValue = $this->systemConfig->getValue($configName);
$newValue = $this->mergeArrayValue(

View File

@ -50,32 +50,31 @@ class DeleteConfigTest extends TestCase {
$this->command = new DeleteConfig($systemConfig);
}
public function deleteData() {
return [
[
'name',
'name1',
true,
true,
0,
'info',
],
[
'name',
'name2',
true,
false,
0,
'info',
],
[
'name',
'name3',
false,
false,
0,
'info',
],
[
'name',
'name4',
false,
true,
1,
@ -105,7 +104,7 @@ class DeleteConfigTest extends TestCase {
$this->consoleInput->expects($this->once())
->method('getArgument')
->with('name')
->willReturn($configName);
->willReturn([$configName]);
$this->consoleInput->expects($this->any())
->method('hasParameterOption')
->with('--error-if-not-exists')
@ -117,4 +116,103 @@ class DeleteConfigTest extends TestCase {
$this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
}
public function deleteArrayData() {
return [
[
['name', 'sub'],
true,
false,
true,
true,
0,
'info',
],
[
['name', 'sub', '2sub'],
true,
false,
['sub' => ['2sub' => 1], 'sub2' => false],
['sub' => [], 'sub2' => false],
0,
'info',
],
[
['name', 'sub3'],
true,
false,
['sub' => ['2sub' => 1], 'sub2' => false],
['sub' => ['2sub' => 1], 'sub2' => false],
0,
'info',
],
[
['name', 'sub'],
false,
true,
true,
true,
1,
'error',
],
[
['name', 'sub'],
true,
true,
true,
true,
1,
'error',
],
[
['name', 'sub3'],
true,
true,
['sub' => ['2sub' => 1], 'sub2' => false],
['sub' => ['2sub' => 1], 'sub2' => false],
1,
'error',
],
];
}
/**
* @dataProvider deleteArrayData
*
* @param string[] $configNames
* @param bool $configKeyExists
* @param bool $checkIfKeyExists
* @param mixed $configValue
* @param mixed $updateValue
* @param int $expectedReturn
* @param string $expectedMessage
*/
public function testArrayDelete(array $configNames, $configKeyExists, $checkIfKeyExists, $configValue, $updateValue, $expectedReturn, $expectedMessage) {
$this->systemConfig->expects(($checkIfKeyExists) ? $this->once() : $this->never())
->method('getKeys')
->willReturn($configKeyExists ? [$configNames[0]] : []);
$this->systemConfig->expects(($configKeyExists) ? $this->once() : $this->never())
->method('getValue')
->willReturn($configValue);
$this->systemConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never())
->method('setValue')
->with($configNames[0], $updateValue);
$this->consoleInput->expects($this->once())
->method('getArgument')
->with('name')
->willReturn($configNames);
$this->consoleInput->expects($this->any())
->method('hasParameterOption')
->with('--error-if-not-exists')
->willReturn($checkIfKeyExists);
$this->consoleOutput->expects($this->any())
->method('writeln')
->with($this->stringContains($expectedMessage));
$this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
}
}