@@ -18,81 +18,81 @@ |
||
| 18 | 18 | use Test\TestCase; |
| 19 | 19 | |
| 20 | 20 | class DeleteConfigTest extends TestCase { |
| 21 | - protected IAppConfig&MockObject $appConfig; |
|
| 22 | - protected InputInterface&MockObject $consoleInput; |
|
| 23 | - protected OutputInterface&MockObject $consoleOutput; |
|
| 24 | - protected Command $command; |
|
| 21 | + protected IAppConfig&MockObject $appConfig; |
|
| 22 | + protected InputInterface&MockObject $consoleInput; |
|
| 23 | + protected OutputInterface&MockObject $consoleOutput; |
|
| 24 | + protected Command $command; |
|
| 25 | 25 | |
| 26 | - protected function setUp(): void { |
|
| 27 | - parent::setUp(); |
|
| 26 | + protected function setUp(): void { |
|
| 27 | + parent::setUp(); |
|
| 28 | 28 | |
| 29 | - $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 30 | - $this->consoleInput = $this->createMock(InputInterface::class); |
|
| 31 | - $this->consoleOutput = $this->createMock(OutputInterface::class); |
|
| 29 | + $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 30 | + $this->consoleInput = $this->createMock(InputInterface::class); |
|
| 31 | + $this->consoleOutput = $this->createMock(OutputInterface::class); |
|
| 32 | 32 | |
| 33 | - $this->command = new DeleteConfig($this->appConfig); |
|
| 34 | - } |
|
| 33 | + $this->command = new DeleteConfig($this->appConfig); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | - public static function dataDelete(): array { |
|
| 38 | - return [ |
|
| 39 | - [ |
|
| 40 | - 'name', |
|
| 41 | - true, |
|
| 42 | - true, |
|
| 43 | - 0, |
|
| 44 | - 'info', |
|
| 45 | - ], |
|
| 46 | - [ |
|
| 47 | - 'name', |
|
| 48 | - true, |
|
| 49 | - false, |
|
| 50 | - 0, |
|
| 51 | - 'info', |
|
| 52 | - ], |
|
| 53 | - [ |
|
| 54 | - 'name', |
|
| 55 | - false, |
|
| 56 | - false, |
|
| 57 | - 0, |
|
| 58 | - 'info', |
|
| 59 | - ], |
|
| 60 | - [ |
|
| 61 | - 'name', |
|
| 62 | - false, |
|
| 63 | - true, |
|
| 64 | - 1, |
|
| 65 | - 'error', |
|
| 66 | - ], |
|
| 67 | - ]; |
|
| 68 | - } |
|
| 37 | + public static function dataDelete(): array { |
|
| 38 | + return [ |
|
| 39 | + [ |
|
| 40 | + 'name', |
|
| 41 | + true, |
|
| 42 | + true, |
|
| 43 | + 0, |
|
| 44 | + 'info', |
|
| 45 | + ], |
|
| 46 | + [ |
|
| 47 | + 'name', |
|
| 48 | + true, |
|
| 49 | + false, |
|
| 50 | + 0, |
|
| 51 | + 'info', |
|
| 52 | + ], |
|
| 53 | + [ |
|
| 54 | + 'name', |
|
| 55 | + false, |
|
| 56 | + false, |
|
| 57 | + 0, |
|
| 58 | + 'info', |
|
| 59 | + ], |
|
| 60 | + [ |
|
| 61 | + 'name', |
|
| 62 | + false, |
|
| 63 | + true, |
|
| 64 | + 1, |
|
| 65 | + 'error', |
|
| 66 | + ], |
|
| 67 | + ]; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * @dataProvider dataDelete |
|
| 72 | - */ |
|
| 73 | - public function testDelete(string $configName, bool $configExists, bool $checkIfExists, int $expectedReturn, string $expectedMessage): void { |
|
| 74 | - $this->appConfig->expects(($checkIfExists) ? $this->once() : $this->never()) |
|
| 75 | - ->method('getKeys') |
|
| 76 | - ->with('app-name') |
|
| 77 | - ->willReturn($configExists ? [$configName] : []); |
|
| 70 | + /** |
|
| 71 | + * @dataProvider dataDelete |
|
| 72 | + */ |
|
| 73 | + public function testDelete(string $configName, bool $configExists, bool $checkIfExists, int $expectedReturn, string $expectedMessage): void { |
|
| 74 | + $this->appConfig->expects(($checkIfExists) ? $this->once() : $this->never()) |
|
| 75 | + ->method('getKeys') |
|
| 76 | + ->with('app-name') |
|
| 77 | + ->willReturn($configExists ? [$configName] : []); |
|
| 78 | 78 | |
| 79 | - $this->appConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never()) |
|
| 80 | - ->method('deleteKey') |
|
| 81 | - ->with('app-name', $configName); |
|
| 79 | + $this->appConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never()) |
|
| 80 | + ->method('deleteKey') |
|
| 81 | + ->with('app-name', $configName); |
|
| 82 | 82 | |
| 83 | - $this->consoleInput->expects($this->exactly(2)) |
|
| 84 | - ->method('getArgument') |
|
| 85 | - ->willReturnMap([ |
|
| 86 | - ['app', 'app-name'], |
|
| 87 | - ['name', $configName], |
|
| 88 | - ]); |
|
| 89 | - $this->consoleInput->method('hasParameterOption') |
|
| 90 | - ->with('--error-if-not-exists') |
|
| 91 | - ->willReturn($checkIfExists); |
|
| 83 | + $this->consoleInput->expects($this->exactly(2)) |
|
| 84 | + ->method('getArgument') |
|
| 85 | + ->willReturnMap([ |
|
| 86 | + ['app', 'app-name'], |
|
| 87 | + ['name', $configName], |
|
| 88 | + ]); |
|
| 89 | + $this->consoleInput->method('hasParameterOption') |
|
| 90 | + ->with('--error-if-not-exists') |
|
| 91 | + ->willReturn($checkIfExists); |
|
| 92 | 92 | |
| 93 | - $this->consoleOutput->method('writeln') |
|
| 94 | - ->with($this->stringContains($expectedMessage)); |
|
| 93 | + $this->consoleOutput->method('writeln') |
|
| 94 | + ->with($this->stringContains($expectedMessage)); |
|
| 95 | 95 | |
| 96 | - $this->assertSame($expectedReturn, self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); |
|
| 97 | - } |
|
| 96 | + $this->assertSame($expectedReturn, self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -20,87 +20,87 @@ |
||
| 20 | 20 | use Test\TestCase; |
| 21 | 21 | |
| 22 | 22 | class SetConfigTest extends TestCase { |
| 23 | - protected IAppConfig&MockObject $appConfig; |
|
| 24 | - protected InputInterface&MockObject $consoleInput; |
|
| 25 | - protected OutputInterface&MockObject $consoleOutput; |
|
| 26 | - protected Command $command; |
|
| 23 | + protected IAppConfig&MockObject $appConfig; |
|
| 24 | + protected InputInterface&MockObject $consoleInput; |
|
| 25 | + protected OutputInterface&MockObject $consoleOutput; |
|
| 26 | + protected Command $command; |
|
| 27 | 27 | |
| 28 | - protected function setUp(): void { |
|
| 29 | - parent::setUp(); |
|
| 28 | + protected function setUp(): void { |
|
| 29 | + parent::setUp(); |
|
| 30 | 30 | |
| 31 | - $this->appConfig = $this->createMock(AppConfig::class); |
|
| 32 | - $this->consoleInput = $this->createMock(InputInterface::class); |
|
| 33 | - $this->consoleOutput = $this->createMock(OutputInterface::class); |
|
| 31 | + $this->appConfig = $this->createMock(AppConfig::class); |
|
| 32 | + $this->consoleInput = $this->createMock(InputInterface::class); |
|
| 33 | + $this->consoleOutput = $this->createMock(OutputInterface::class); |
|
| 34 | 34 | |
| 35 | - $this->command = new SetConfig($this->appConfig); |
|
| 36 | - } |
|
| 35 | + $this->command = new SetConfig($this->appConfig); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | 38 | |
| 39 | - public static function dataSet(): array { |
|
| 40 | - return [ |
|
| 41 | - [ |
|
| 42 | - 'name', |
|
| 43 | - 'newvalue', |
|
| 44 | - true, |
|
| 45 | - true, |
|
| 46 | - true, |
|
| 47 | - 'info', |
|
| 48 | - ], |
|
| 49 | - [ |
|
| 50 | - 'name', |
|
| 51 | - 'newvalue', |
|
| 52 | - false, |
|
| 53 | - true, |
|
| 54 | - false, |
|
| 55 | - 'comment', |
|
| 56 | - ], |
|
| 57 | - ]; |
|
| 58 | - } |
|
| 39 | + public static function dataSet(): array { |
|
| 40 | + return [ |
|
| 41 | + [ |
|
| 42 | + 'name', |
|
| 43 | + 'newvalue', |
|
| 44 | + true, |
|
| 45 | + true, |
|
| 46 | + true, |
|
| 47 | + 'info', |
|
| 48 | + ], |
|
| 49 | + [ |
|
| 50 | + 'name', |
|
| 51 | + 'newvalue', |
|
| 52 | + false, |
|
| 53 | + true, |
|
| 54 | + false, |
|
| 55 | + 'comment', |
|
| 56 | + ], |
|
| 57 | + ]; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @dataProvider dataSet |
|
| 62 | - */ |
|
| 63 | - public function testSet(string $configName, mixed $newValue, bool $configExists, bool $updateOnly, bool $updated, string $expectedMessage): void { |
|
| 64 | - $this->appConfig->method('hasKey') |
|
| 65 | - ->with('app-name', $configName) |
|
| 66 | - ->willReturn($configExists); |
|
| 60 | + /** |
|
| 61 | + * @dataProvider dataSet |
|
| 62 | + */ |
|
| 63 | + public function testSet(string $configName, mixed $newValue, bool $configExists, bool $updateOnly, bool $updated, string $expectedMessage): void { |
|
| 64 | + $this->appConfig->method('hasKey') |
|
| 65 | + ->with('app-name', $configName) |
|
| 66 | + ->willReturn($configExists); |
|
| 67 | 67 | |
| 68 | - if (!$configExists) { |
|
| 69 | - $this->appConfig->method('getValueType') |
|
| 70 | - ->willThrowException(new AppConfigUnknownKeyException()); |
|
| 71 | - } else { |
|
| 72 | - $this->appConfig->method('getValueType') |
|
| 73 | - ->willReturn(IAppConfig::VALUE_MIXED); |
|
| 74 | - } |
|
| 68 | + if (!$configExists) { |
|
| 69 | + $this->appConfig->method('getValueType') |
|
| 70 | + ->willThrowException(new AppConfigUnknownKeyException()); |
|
| 71 | + } else { |
|
| 72 | + $this->appConfig->method('getValueType') |
|
| 73 | + ->willReturn(IAppConfig::VALUE_MIXED); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - if ($updated) { |
|
| 77 | - $this->appConfig->expects($this->once()) |
|
| 78 | - ->method('setValueMixed') |
|
| 79 | - ->with('app-name', $configName, $newValue); |
|
| 80 | - } |
|
| 76 | + if ($updated) { |
|
| 77 | + $this->appConfig->expects($this->once()) |
|
| 78 | + ->method('setValueMixed') |
|
| 79 | + ->with('app-name', $configName, $newValue); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - $this->consoleInput->expects($this->exactly(2)) |
|
| 83 | - ->method('getArgument') |
|
| 84 | - ->willReturnMap([ |
|
| 85 | - ['app', 'app-name'], |
|
| 86 | - ['name', $configName], |
|
| 87 | - ]); |
|
| 88 | - $this->consoleInput->method('getOption') |
|
| 89 | - ->willReturnMap([ |
|
| 90 | - ['value', $newValue], |
|
| 91 | - ['lazy', null], |
|
| 92 | - ['sensitive', null], |
|
| 93 | - ['no-interaction', true], |
|
| 94 | - ]); |
|
| 95 | - $this->consoleInput->method('hasParameterOption') |
|
| 96 | - ->willReturnMap([ |
|
| 97 | - ['--type', false, false], |
|
| 98 | - ['--value', false, true], |
|
| 99 | - ['--update-only', false, $updateOnly] |
|
| 100 | - ]); |
|
| 101 | - $this->consoleOutput->method('writeln') |
|
| 102 | - ->with($this->stringContains($expectedMessage)); |
|
| 82 | + $this->consoleInput->expects($this->exactly(2)) |
|
| 83 | + ->method('getArgument') |
|
| 84 | + ->willReturnMap([ |
|
| 85 | + ['app', 'app-name'], |
|
| 86 | + ['name', $configName], |
|
| 87 | + ]); |
|
| 88 | + $this->consoleInput->method('getOption') |
|
| 89 | + ->willReturnMap([ |
|
| 90 | + ['value', $newValue], |
|
| 91 | + ['lazy', null], |
|
| 92 | + ['sensitive', null], |
|
| 93 | + ['no-interaction', true], |
|
| 94 | + ]); |
|
| 95 | + $this->consoleInput->method('hasParameterOption') |
|
| 96 | + ->willReturnMap([ |
|
| 97 | + ['--type', false, false], |
|
| 98 | + ['--value', false, true], |
|
| 99 | + ['--update-only', false, $updateOnly] |
|
| 100 | + ]); |
|
| 101 | + $this->consoleOutput->method('writeln') |
|
| 102 | + ->with($this->stringContains($expectedMessage)); |
|
| 103 | 103 | |
| 104 | - self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); |
|
| 105 | - } |
|
| 104 | + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); |
|
| 105 | + } |
|
| 106 | 106 | } |
@@ -19,119 +19,119 @@ |
||
| 19 | 19 | use Test\TestCase; |
| 20 | 20 | |
| 21 | 21 | class GetConfigTest extends TestCase { |
| 22 | - protected IAppConfig&MockObject $appConfig; |
|
| 23 | - protected InputInterface&MockObject $consoleInput; |
|
| 24 | - protected OutputInterface&MockObject $consoleOutput; |
|
| 25 | - protected Command $command; |
|
| 26 | - |
|
| 27 | - protected function setUp(): void { |
|
| 28 | - parent::setUp(); |
|
| 29 | - |
|
| 30 | - $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 31 | - $this->consoleInput = $this->createMock(InputInterface::class); |
|
| 32 | - $this->consoleOutput = $this->createMock(OutputInterface::class); |
|
| 33 | - |
|
| 34 | - $this->command = new GetConfig($this->appConfig); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - |
|
| 38 | - public static function dataGet(): array { |
|
| 39 | - return [ |
|
| 40 | - // String output as json |
|
| 41 | - ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')], |
|
| 42 | - // String output as plain text |
|
| 43 | - ['name', 'newvalue', true, null, false, 'plain', 0, 'newvalue'], |
|
| 44 | - // String falling back to default output as json |
|
| 45 | - ['name', null, false, 'newvalue', true, 'json', 0, json_encode('newvalue')], |
|
| 46 | - // String falling back without default: error |
|
| 47 | - ['name', null, false, null, false, 'json', 1, null], |
|
| 48 | - |
|
| 49 | - // Int "0" output as json/plain |
|
| 50 | - ['name', 0, true, null, false, 'json', 0, json_encode(0)], |
|
| 51 | - ['name', 0, true, null, false, 'plain', 0, '0'], |
|
| 52 | - // Int "1" output as json/plain |
|
| 53 | - ['name', 1, true, null, false, 'json', 0, json_encode(1)], |
|
| 54 | - ['name', 1, true, null, false, 'plain', 0, '1'], |
|
| 55 | - |
|
| 56 | - // Bool "true" output as json/plain |
|
| 57 | - ['name', true, true, null, false, 'json', 0, json_encode(true)], |
|
| 58 | - ['name', true, true, null, false, 'plain', 0, 'true'], |
|
| 59 | - // Bool "false" output as json/plain |
|
| 60 | - ['name', false, true, null, false, 'json', 0, json_encode(false)], |
|
| 61 | - ['name', false, true, null, false, 'plain', 0, 'false'], |
|
| 62 | - |
|
| 63 | - // Null output as json/plain |
|
| 64 | - ['name', null, true, null, false, 'json', 0, json_encode(null)], |
|
| 65 | - ['name', null, true, null, false, 'plain', 0, 'null'], |
|
| 66 | - |
|
| 67 | - // Array output as json/plain |
|
| 68 | - ['name', ['a', 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])], |
|
| 69 | - ['name', ['a', 'b'], true, null, false, 'plain', 0, "a\nb"], |
|
| 70 | - // Key array output as json/plain |
|
| 71 | - ['name', [0 => 'a', 1 => 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])], |
|
| 72 | - ['name', [0 => 'a', 1 => 'b'], true, null, false, 'plain', 0, "a\nb"], |
|
| 73 | - // Associative array output as json/plain |
|
| 74 | - ['name', ['a' => 1, 'b' => 2], true, null, false, 'json', 0, json_encode(['a' => 1, 'b' => 2])], |
|
| 75 | - ['name', ['a' => 1, 'b' => 2], true, null, false, 'plain', 0, "a: 1\nb: 2"], |
|
| 76 | - |
|
| 77 | - ]; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @dataProvider dataGet |
|
| 82 | - */ |
|
| 83 | - public function testGet(string $configName, mixed $value, bool $configExists, mixed $defaultValue, bool $hasDefault, string $outputFormat, int $expectedReturn, ?string $expectedMessage): void { |
|
| 84 | - if (!$expectedReturn) { |
|
| 85 | - if ($configExists) { |
|
| 86 | - $this->appConfig->expects($this->once()) |
|
| 87 | - ->method('getDetails') |
|
| 88 | - ->with('app-name', $configName) |
|
| 89 | - ->willReturn(['value' => $value]); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - if (!$configExists) { |
|
| 94 | - $this->appConfig->expects($this->once()) |
|
| 95 | - ->method('getDetails') |
|
| 96 | - ->with('app-name', $configName) |
|
| 97 | - ->willThrowException(new AppConfigUnknownKeyException()); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $this->consoleInput->expects($this->exactly(2)) |
|
| 101 | - ->method('getArgument') |
|
| 102 | - ->willReturnMap([ |
|
| 103 | - ['app', 'app-name'], |
|
| 104 | - ['name', $configName], |
|
| 105 | - ]); |
|
| 106 | - $this->consoleInput->method('getOption') |
|
| 107 | - ->willReturnMap([ |
|
| 108 | - ['default-value', $defaultValue], |
|
| 109 | - ['output', $outputFormat], |
|
| 110 | - ]); |
|
| 111 | - $this->consoleInput->method('hasParameterOption') |
|
| 112 | - ->willReturnMap([ |
|
| 113 | - ['--output', false, true], |
|
| 114 | - ['--default-value', false, $hasDefault], |
|
| 115 | - ]); |
|
| 116 | - |
|
| 117 | - if ($expectedMessage !== null) { |
|
| 118 | - global $output; |
|
| 119 | - |
|
| 120 | - $output = ''; |
|
| 121 | - $this->consoleOutput->method('writeln') |
|
| 122 | - ->willReturnCallback(function ($value) { |
|
| 123 | - global $output; |
|
| 124 | - $output .= $value . "\n"; |
|
| 125 | - return $output; |
|
| 126 | - }); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $this->assertSame($expectedReturn, self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); |
|
| 130 | - |
|
| 131 | - if ($expectedMessage !== null) { |
|
| 132 | - global $output; |
|
| 133 | - // Remove the trailing newline |
|
| 134 | - $this->assertSame($expectedMessage, substr($output, 0, -1)); |
|
| 135 | - } |
|
| 136 | - } |
|
| 22 | + protected IAppConfig&MockObject $appConfig; |
|
| 23 | + protected InputInterface&MockObject $consoleInput; |
|
| 24 | + protected OutputInterface&MockObject $consoleOutput; |
|
| 25 | + protected Command $command; |
|
| 26 | + |
|
| 27 | + protected function setUp(): void { |
|
| 28 | + parent::setUp(); |
|
| 29 | + |
|
| 30 | + $this->appConfig = $this->createMock(IAppConfig::class); |
|
| 31 | + $this->consoleInput = $this->createMock(InputInterface::class); |
|
| 32 | + $this->consoleOutput = $this->createMock(OutputInterface::class); |
|
| 33 | + |
|
| 34 | + $this->command = new GetConfig($this->appConfig); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + |
|
| 38 | + public static function dataGet(): array { |
|
| 39 | + return [ |
|
| 40 | + // String output as json |
|
| 41 | + ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')], |
|
| 42 | + // String output as plain text |
|
| 43 | + ['name', 'newvalue', true, null, false, 'plain', 0, 'newvalue'], |
|
| 44 | + // String falling back to default output as json |
|
| 45 | + ['name', null, false, 'newvalue', true, 'json', 0, json_encode('newvalue')], |
|
| 46 | + // String falling back without default: error |
|
| 47 | + ['name', null, false, null, false, 'json', 1, null], |
|
| 48 | + |
|
| 49 | + // Int "0" output as json/plain |
|
| 50 | + ['name', 0, true, null, false, 'json', 0, json_encode(0)], |
|
| 51 | + ['name', 0, true, null, false, 'plain', 0, '0'], |
|
| 52 | + // Int "1" output as json/plain |
|
| 53 | + ['name', 1, true, null, false, 'json', 0, json_encode(1)], |
|
| 54 | + ['name', 1, true, null, false, 'plain', 0, '1'], |
|
| 55 | + |
|
| 56 | + // Bool "true" output as json/plain |
|
| 57 | + ['name', true, true, null, false, 'json', 0, json_encode(true)], |
|
| 58 | + ['name', true, true, null, false, 'plain', 0, 'true'], |
|
| 59 | + // Bool "false" output as json/plain |
|
| 60 | + ['name', false, true, null, false, 'json', 0, json_encode(false)], |
|
| 61 | + ['name', false, true, null, false, 'plain', 0, 'false'], |
|
| 62 | + |
|
| 63 | + // Null output as json/plain |
|
| 64 | + ['name', null, true, null, false, 'json', 0, json_encode(null)], |
|
| 65 | + ['name', null, true, null, false, 'plain', 0, 'null'], |
|
| 66 | + |
|
| 67 | + // Array output as json/plain |
|
| 68 | + ['name', ['a', 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])], |
|
| 69 | + ['name', ['a', 'b'], true, null, false, 'plain', 0, "a\nb"], |
|
| 70 | + // Key array output as json/plain |
|
| 71 | + ['name', [0 => 'a', 1 => 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])], |
|
| 72 | + ['name', [0 => 'a', 1 => 'b'], true, null, false, 'plain', 0, "a\nb"], |
|
| 73 | + // Associative array output as json/plain |
|
| 74 | + ['name', ['a' => 1, 'b' => 2], true, null, false, 'json', 0, json_encode(['a' => 1, 'b' => 2])], |
|
| 75 | + ['name', ['a' => 1, 'b' => 2], true, null, false, 'plain', 0, "a: 1\nb: 2"], |
|
| 76 | + |
|
| 77 | + ]; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @dataProvider dataGet |
|
| 82 | + */ |
|
| 83 | + public function testGet(string $configName, mixed $value, bool $configExists, mixed $defaultValue, bool $hasDefault, string $outputFormat, int $expectedReturn, ?string $expectedMessage): void { |
|
| 84 | + if (!$expectedReturn) { |
|
| 85 | + if ($configExists) { |
|
| 86 | + $this->appConfig->expects($this->once()) |
|
| 87 | + ->method('getDetails') |
|
| 88 | + ->with('app-name', $configName) |
|
| 89 | + ->willReturn(['value' => $value]); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + if (!$configExists) { |
|
| 94 | + $this->appConfig->expects($this->once()) |
|
| 95 | + ->method('getDetails') |
|
| 96 | + ->with('app-name', $configName) |
|
| 97 | + ->willThrowException(new AppConfigUnknownKeyException()); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $this->consoleInput->expects($this->exactly(2)) |
|
| 101 | + ->method('getArgument') |
|
| 102 | + ->willReturnMap([ |
|
| 103 | + ['app', 'app-name'], |
|
| 104 | + ['name', $configName], |
|
| 105 | + ]); |
|
| 106 | + $this->consoleInput->method('getOption') |
|
| 107 | + ->willReturnMap([ |
|
| 108 | + ['default-value', $defaultValue], |
|
| 109 | + ['output', $outputFormat], |
|
| 110 | + ]); |
|
| 111 | + $this->consoleInput->method('hasParameterOption') |
|
| 112 | + ->willReturnMap([ |
|
| 113 | + ['--output', false, true], |
|
| 114 | + ['--default-value', false, $hasDefault], |
|
| 115 | + ]); |
|
| 116 | + |
|
| 117 | + if ($expectedMessage !== null) { |
|
| 118 | + global $output; |
|
| 119 | + |
|
| 120 | + $output = ''; |
|
| 121 | + $this->consoleOutput->method('writeln') |
|
| 122 | + ->willReturnCallback(function ($value) { |
|
| 123 | + global $output; |
|
| 124 | + $output .= $value . "\n"; |
|
| 125 | + return $output; |
|
| 126 | + }); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $this->assertSame($expectedReturn, self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); |
|
| 130 | + |
|
| 131 | + if ($expectedMessage !== null) { |
|
| 132 | + global $output; |
|
| 133 | + // Remove the trailing newline |
|
| 134 | + $this->assertSame($expectedMessage, substr($output, 0, -1)); |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | 137 | } |
@@ -119,9 +119,9 @@ |
||
| 119 | 119 | |
| 120 | 120 | $output = ''; |
| 121 | 121 | $this->consoleOutput->method('writeln') |
| 122 | - ->willReturnCallback(function ($value) { |
|
| 122 | + ->willReturnCallback(function($value) { |
|
| 123 | 123 | global $output; |
| 124 | - $output .= $value . "\n"; |
|
| 124 | + $output .= $value."\n"; |
|
| 125 | 125 | return $output; |
| 126 | 126 | }); |
| 127 | 127 | } |
@@ -11,26 +11,26 @@ |
||
| 11 | 11 | use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; |
| 12 | 12 | |
| 13 | 13 | abstract class Base extends \OC\Core\Command\Base { |
| 14 | - public function __construct( |
|
| 15 | - protected IAppConfig $appConfig, |
|
| 16 | - ) { |
|
| 17 | - parent::__construct(); |
|
| 18 | - } |
|
| 14 | + public function __construct( |
|
| 15 | + protected IAppConfig $appConfig, |
|
| 16 | + ) { |
|
| 17 | + parent::__construct(); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @param string $argumentName |
|
| 22 | - * @param CompletionContext $context |
|
| 23 | - * @return string[] |
|
| 24 | - */ |
|
| 25 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 26 | - if ($argumentName === 'app') { |
|
| 27 | - return $this->appConfig->getApps(); |
|
| 28 | - } |
|
| 20 | + /** |
|
| 21 | + * @param string $argumentName |
|
| 22 | + * @param CompletionContext $context |
|
| 23 | + * @return string[] |
|
| 24 | + */ |
|
| 25 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 26 | + if ($argumentName === 'app') { |
|
| 27 | + return $this->appConfig->getApps(); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - if ($argumentName === 'name') { |
|
| 31 | - $appName = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
| 32 | - return $this->appConfig->getKeys($appName); |
|
| 33 | - } |
|
| 34 | - return []; |
|
| 35 | - } |
|
| 30 | + if ($argumentName === 'name') { |
|
| 31 | + $appName = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
| 32 | + return $this->appConfig->getKeys($appName); |
|
| 33 | + } |
|
| 34 | + return []; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -20,231 +20,231 @@ |
||
| 20 | 20 | use Symfony\Component\Console\Question\Question; |
| 21 | 21 | |
| 22 | 22 | class SetConfig extends Base { |
| 23 | - protected function configure() { |
|
| 24 | - parent::configure(); |
|
| 25 | - |
|
| 26 | - $this |
|
| 27 | - ->setName('config:app:set') |
|
| 28 | - ->setDescription('Set an app config value') |
|
| 29 | - ->addArgument( |
|
| 30 | - 'app', |
|
| 31 | - InputArgument::REQUIRED, |
|
| 32 | - 'Name of the app' |
|
| 33 | - ) |
|
| 34 | - ->addArgument( |
|
| 35 | - 'name', |
|
| 36 | - InputArgument::REQUIRED, |
|
| 37 | - 'Name of the config to set' |
|
| 38 | - ) |
|
| 39 | - ->addOption( |
|
| 40 | - 'value', |
|
| 41 | - null, |
|
| 42 | - InputOption::VALUE_REQUIRED, |
|
| 43 | - 'The new value of the config' |
|
| 44 | - ) |
|
| 45 | - ->addOption( |
|
| 46 | - 'type', |
|
| 47 | - null, |
|
| 48 | - InputOption::VALUE_REQUIRED, |
|
| 49 | - 'Value type [string, integer, float, boolean, array]', |
|
| 50 | - 'string' |
|
| 51 | - ) |
|
| 52 | - ->addOption( |
|
| 53 | - 'lazy', |
|
| 54 | - null, |
|
| 55 | - InputOption::VALUE_NEGATABLE, |
|
| 56 | - 'Set value as lazy loaded', |
|
| 57 | - ) |
|
| 58 | - ->addOption( |
|
| 59 | - 'sensitive', |
|
| 60 | - null, |
|
| 61 | - InputOption::VALUE_NEGATABLE, |
|
| 62 | - 'Set value as sensitive', |
|
| 63 | - ) |
|
| 64 | - ->addOption( |
|
| 65 | - 'update-only', |
|
| 66 | - null, |
|
| 67 | - InputOption::VALUE_NONE, |
|
| 68 | - 'Only updates the value, if it is not set before, it is not being added' |
|
| 69 | - ) |
|
| 70 | - ; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 74 | - $appName = $input->getArgument('app'); |
|
| 75 | - $configName = $input->getArgument('name'); |
|
| 76 | - |
|
| 77 | - if (!($this->appConfig instanceof AppConfig)) { |
|
| 78 | - throw new \Exception('Only compatible with OC\AppConfig as it uses internal methods'); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - if ($input->hasParameterOption('--update-only') && !$this->appConfig->hasKey($appName, $configName)) { |
|
| 82 | - $output->writeln( |
|
| 83 | - '<comment>Config value ' . $configName . ' for app ' . $appName |
|
| 84 | - . ' not updated, as it has not been set before.</comment>' |
|
| 85 | - ); |
|
| 86 | - |
|
| 87 | - return 1; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - $type = $typeString = null; |
|
| 91 | - if ($input->hasParameterOption('--type')) { |
|
| 92 | - $typeString = $input->getOption('type'); |
|
| 93 | - $type = $this->appConfig->convertTypeToInt($typeString); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * If --Value is not specified, returns an exception if no value exists in database |
|
| 98 | - * compare with current status in database and displays a reminder that this can break things. |
|
| 99 | - * confirmation is required by admin, unless --no-interaction |
|
| 100 | - */ |
|
| 101 | - $updated = false; |
|
| 102 | - if (!$input->hasParameterOption('--value')) { |
|
| 103 | - if (!$input->getOption('lazy') && $this->appConfig->isLazy($appName, $configName) && $this->ask($input, $output, 'NOT LAZY')) { |
|
| 104 | - $updated = $this->appConfig->updateLazy($appName, $configName, false); |
|
| 105 | - } |
|
| 106 | - if ($input->getOption('lazy') && !$this->appConfig->isLazy($appName, $configName) && $this->ask($input, $output, 'LAZY')) { |
|
| 107 | - $updated = $this->appConfig->updateLazy($appName, $configName, true) || $updated; |
|
| 108 | - } |
|
| 109 | - if (!$input->getOption('sensitive') && $this->appConfig->isSensitive($appName, $configName) && $this->ask($input, $output, 'NOT SENSITIVE')) { |
|
| 110 | - $updated = $this->appConfig->updateSensitive($appName, $configName, false) || $updated; |
|
| 111 | - } |
|
| 112 | - if ($input->getOption('sensitive') && !$this->appConfig->isSensitive($appName, $configName) && $this->ask($input, $output, 'SENSITIVE')) { |
|
| 113 | - $updated = $this->appConfig->updateSensitive($appName, $configName, true) || $updated; |
|
| 114 | - } |
|
| 115 | - if ($type !== null && $type !== $this->appConfig->getValueType($appName, $configName) && $typeString !== null && $this->ask($input, $output, $typeString)) { |
|
| 116 | - $updated = $this->appConfig->updateType($appName, $configName, $type) || $updated; |
|
| 117 | - } |
|
| 118 | - } else { |
|
| 119 | - /** |
|
| 120 | - * If --type is specified in the command line, we upgrade the type in database |
|
| 121 | - * after a confirmation from admin. |
|
| 122 | - * If not we get the type from current stored value or VALUE_MIXED as default. |
|
| 123 | - */ |
|
| 124 | - try { |
|
| 125 | - $currType = $this->appConfig->getValueType($appName, $configName); |
|
| 126 | - if ($type === null || $typeString === null || $type === $currType || !$this->ask($input, $output, $typeString)) { |
|
| 127 | - $type = $currType; |
|
| 128 | - } else { |
|
| 129 | - $updated = $this->appConfig->updateType($appName, $configName, $type); |
|
| 130 | - } |
|
| 131 | - } catch (AppConfigUnknownKeyException) { |
|
| 132 | - $type = $type ?? IAppConfig::VALUE_MIXED; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * if --lazy/--no-lazy option are set, compare with data stored in database. |
|
| 137 | - * If no data in database, or identical, continue. |
|
| 138 | - * If different, ask admin for confirmation. |
|
| 139 | - */ |
|
| 140 | - $lazy = $input->getOption('lazy'); |
|
| 141 | - try { |
|
| 142 | - $currLazy = $this->appConfig->isLazy($appName, $configName); |
|
| 143 | - if ($lazy === null || $lazy === $currLazy || !$this->ask($input, $output, ($lazy) ? 'LAZY' : 'NOT LAZY')) { |
|
| 144 | - $lazy = $currLazy; |
|
| 145 | - } |
|
| 146 | - } catch (AppConfigUnknownKeyException) { |
|
| 147 | - $lazy = $lazy ?? false; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * same with sensitive status |
|
| 152 | - */ |
|
| 153 | - $sensitive = $input->getOption('sensitive'); |
|
| 154 | - try { |
|
| 155 | - $currSensitive = $this->appConfig->isSensitive($appName, $configName, null); |
|
| 156 | - if ($sensitive === null || $sensitive === $currSensitive || !$this->ask($input, $output, ($sensitive) ? 'SENSITIVE' : 'NOT SENSITIVE')) { |
|
| 157 | - $sensitive = $currSensitive; |
|
| 158 | - } |
|
| 159 | - } catch (AppConfigUnknownKeyException) { |
|
| 160 | - $sensitive = $sensitive ?? false; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $value = (string)$input->getOption('value'); |
|
| 164 | - |
|
| 165 | - switch ($type) { |
|
| 166 | - case IAppConfig::VALUE_MIXED: |
|
| 167 | - $updated = $this->appConfig->setValueMixed($appName, $configName, $value, $lazy, $sensitive); |
|
| 168 | - break; |
|
| 169 | - |
|
| 170 | - case IAppConfig::VALUE_STRING: |
|
| 171 | - $updated = $this->appConfig->setValueString($appName, $configName, $value, $lazy, $sensitive); |
|
| 172 | - break; |
|
| 173 | - |
|
| 174 | - case IAppConfig::VALUE_INT: |
|
| 175 | - if ($value !== ((string)((int)$value))) { |
|
| 176 | - throw new AppConfigIncorrectTypeException('Value is not an integer'); |
|
| 177 | - } |
|
| 178 | - $updated = $this->appConfig->setValueInt($appName, $configName, (int)$value, $lazy, $sensitive); |
|
| 179 | - break; |
|
| 180 | - |
|
| 181 | - case IAppConfig::VALUE_FLOAT: |
|
| 182 | - if ($value !== ((string)((float)$value))) { |
|
| 183 | - throw new AppConfigIncorrectTypeException('Value is not a float'); |
|
| 184 | - } |
|
| 185 | - $updated = $this->appConfig->setValueFloat($appName, $configName, (float)$value, $lazy, $sensitive); |
|
| 186 | - break; |
|
| 187 | - |
|
| 188 | - case IAppConfig::VALUE_BOOL: |
|
| 189 | - if (in_array(strtolower($value), ['true', '1', 'on', 'yes'])) { |
|
| 190 | - $valueBool = true; |
|
| 191 | - } elseif (in_array(strtolower($value), ['false', '0', 'off', 'no'])) { |
|
| 192 | - $valueBool = false; |
|
| 193 | - } else { |
|
| 194 | - throw new AppConfigIncorrectTypeException('Value is not a boolean, please use \'true\' or \'false\''); |
|
| 195 | - } |
|
| 196 | - $updated = $this->appConfig->setValueBool($appName, $configName, $valueBool, $lazy); |
|
| 197 | - break; |
|
| 198 | - |
|
| 199 | - case IAppConfig::VALUE_ARRAY: |
|
| 200 | - $valueArray = json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 201 | - $valueArray = (is_array($valueArray)) ? $valueArray : throw new AppConfigIncorrectTypeException('Value is not an array'); |
|
| 202 | - $updated = $this->appConfig->setValueArray($appName, $configName, $valueArray, $lazy, $sensitive); |
|
| 203 | - break; |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - if ($updated) { |
|
| 208 | - $current = $this->appConfig->getDetails($appName, $configName); |
|
| 209 | - $output->writeln( |
|
| 210 | - sprintf( |
|
| 211 | - "<info>Config value '%s' for app '%s' is now set to '%s', stored as %s in %s</info>", |
|
| 212 | - $configName, |
|
| 213 | - $appName, |
|
| 214 | - $current['sensitive'] ? '<sensitive>' : $current['value'], |
|
| 215 | - $current['typeString'], |
|
| 216 | - $current['lazy'] ? 'lazy cache' : 'fast cache' |
|
| 217 | - ) |
|
| 218 | - ); |
|
| 219 | - } else { |
|
| 220 | - $output->writeln('<info>Config value were not updated</info>'); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return 0; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - private function ask(InputInterface $input, OutputInterface $output, string $request): bool { |
|
| 227 | - /** @var QuestionHelper $helper */ |
|
| 228 | - $helper = $this->getHelper('question'); |
|
| 229 | - if ($input->getOption('no-interaction')) { |
|
| 230 | - return true; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - $output->writeln(sprintf('You are about to set config value %s as <info>%s</info>', |
|
| 234 | - '<info>' . $input->getArgument('app') . '</info>/<info>' . $input->getArgument('name') . '</info>', |
|
| 235 | - strtoupper($request) |
|
| 236 | - )); |
|
| 237 | - $output->writeln(''); |
|
| 238 | - $output->writeln('<comment>This might break thing, affect performance on your instance or its security!</comment>'); |
|
| 239 | - |
|
| 240 | - $result = (strtolower((string)$helper->ask( |
|
| 241 | - $input, |
|
| 242 | - $output, |
|
| 243 | - new Question('<comment>Confirm this action by typing \'yes\'</comment>: '))) === 'yes'); |
|
| 244 | - |
|
| 245 | - $output->writeln(($result) ? 'done' : 'cancelled'); |
|
| 246 | - $output->writeln(''); |
|
| 247 | - |
|
| 248 | - return $result; |
|
| 249 | - } |
|
| 23 | + protected function configure() { |
|
| 24 | + parent::configure(); |
|
| 25 | + |
|
| 26 | + $this |
|
| 27 | + ->setName('config:app:set') |
|
| 28 | + ->setDescription('Set an app config value') |
|
| 29 | + ->addArgument( |
|
| 30 | + 'app', |
|
| 31 | + InputArgument::REQUIRED, |
|
| 32 | + 'Name of the app' |
|
| 33 | + ) |
|
| 34 | + ->addArgument( |
|
| 35 | + 'name', |
|
| 36 | + InputArgument::REQUIRED, |
|
| 37 | + 'Name of the config to set' |
|
| 38 | + ) |
|
| 39 | + ->addOption( |
|
| 40 | + 'value', |
|
| 41 | + null, |
|
| 42 | + InputOption::VALUE_REQUIRED, |
|
| 43 | + 'The new value of the config' |
|
| 44 | + ) |
|
| 45 | + ->addOption( |
|
| 46 | + 'type', |
|
| 47 | + null, |
|
| 48 | + InputOption::VALUE_REQUIRED, |
|
| 49 | + 'Value type [string, integer, float, boolean, array]', |
|
| 50 | + 'string' |
|
| 51 | + ) |
|
| 52 | + ->addOption( |
|
| 53 | + 'lazy', |
|
| 54 | + null, |
|
| 55 | + InputOption::VALUE_NEGATABLE, |
|
| 56 | + 'Set value as lazy loaded', |
|
| 57 | + ) |
|
| 58 | + ->addOption( |
|
| 59 | + 'sensitive', |
|
| 60 | + null, |
|
| 61 | + InputOption::VALUE_NEGATABLE, |
|
| 62 | + 'Set value as sensitive', |
|
| 63 | + ) |
|
| 64 | + ->addOption( |
|
| 65 | + 'update-only', |
|
| 66 | + null, |
|
| 67 | + InputOption::VALUE_NONE, |
|
| 68 | + 'Only updates the value, if it is not set before, it is not being added' |
|
| 69 | + ) |
|
| 70 | + ; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 74 | + $appName = $input->getArgument('app'); |
|
| 75 | + $configName = $input->getArgument('name'); |
|
| 76 | + |
|
| 77 | + if (!($this->appConfig instanceof AppConfig)) { |
|
| 78 | + throw new \Exception('Only compatible with OC\AppConfig as it uses internal methods'); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + if ($input->hasParameterOption('--update-only') && !$this->appConfig->hasKey($appName, $configName)) { |
|
| 82 | + $output->writeln( |
|
| 83 | + '<comment>Config value ' . $configName . ' for app ' . $appName |
|
| 84 | + . ' not updated, as it has not been set before.</comment>' |
|
| 85 | + ); |
|
| 86 | + |
|
| 87 | + return 1; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + $type = $typeString = null; |
|
| 91 | + if ($input->hasParameterOption('--type')) { |
|
| 92 | + $typeString = $input->getOption('type'); |
|
| 93 | + $type = $this->appConfig->convertTypeToInt($typeString); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * If --Value is not specified, returns an exception if no value exists in database |
|
| 98 | + * compare with current status in database and displays a reminder that this can break things. |
|
| 99 | + * confirmation is required by admin, unless --no-interaction |
|
| 100 | + */ |
|
| 101 | + $updated = false; |
|
| 102 | + if (!$input->hasParameterOption('--value')) { |
|
| 103 | + if (!$input->getOption('lazy') && $this->appConfig->isLazy($appName, $configName) && $this->ask($input, $output, 'NOT LAZY')) { |
|
| 104 | + $updated = $this->appConfig->updateLazy($appName, $configName, false); |
|
| 105 | + } |
|
| 106 | + if ($input->getOption('lazy') && !$this->appConfig->isLazy($appName, $configName) && $this->ask($input, $output, 'LAZY')) { |
|
| 107 | + $updated = $this->appConfig->updateLazy($appName, $configName, true) || $updated; |
|
| 108 | + } |
|
| 109 | + if (!$input->getOption('sensitive') && $this->appConfig->isSensitive($appName, $configName) && $this->ask($input, $output, 'NOT SENSITIVE')) { |
|
| 110 | + $updated = $this->appConfig->updateSensitive($appName, $configName, false) || $updated; |
|
| 111 | + } |
|
| 112 | + if ($input->getOption('sensitive') && !$this->appConfig->isSensitive($appName, $configName) && $this->ask($input, $output, 'SENSITIVE')) { |
|
| 113 | + $updated = $this->appConfig->updateSensitive($appName, $configName, true) || $updated; |
|
| 114 | + } |
|
| 115 | + if ($type !== null && $type !== $this->appConfig->getValueType($appName, $configName) && $typeString !== null && $this->ask($input, $output, $typeString)) { |
|
| 116 | + $updated = $this->appConfig->updateType($appName, $configName, $type) || $updated; |
|
| 117 | + } |
|
| 118 | + } else { |
|
| 119 | + /** |
|
| 120 | + * If --type is specified in the command line, we upgrade the type in database |
|
| 121 | + * after a confirmation from admin. |
|
| 122 | + * If not we get the type from current stored value or VALUE_MIXED as default. |
|
| 123 | + */ |
|
| 124 | + try { |
|
| 125 | + $currType = $this->appConfig->getValueType($appName, $configName); |
|
| 126 | + if ($type === null || $typeString === null || $type === $currType || !$this->ask($input, $output, $typeString)) { |
|
| 127 | + $type = $currType; |
|
| 128 | + } else { |
|
| 129 | + $updated = $this->appConfig->updateType($appName, $configName, $type); |
|
| 130 | + } |
|
| 131 | + } catch (AppConfigUnknownKeyException) { |
|
| 132 | + $type = $type ?? IAppConfig::VALUE_MIXED; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * if --lazy/--no-lazy option are set, compare with data stored in database. |
|
| 137 | + * If no data in database, or identical, continue. |
|
| 138 | + * If different, ask admin for confirmation. |
|
| 139 | + */ |
|
| 140 | + $lazy = $input->getOption('lazy'); |
|
| 141 | + try { |
|
| 142 | + $currLazy = $this->appConfig->isLazy($appName, $configName); |
|
| 143 | + if ($lazy === null || $lazy === $currLazy || !$this->ask($input, $output, ($lazy) ? 'LAZY' : 'NOT LAZY')) { |
|
| 144 | + $lazy = $currLazy; |
|
| 145 | + } |
|
| 146 | + } catch (AppConfigUnknownKeyException) { |
|
| 147 | + $lazy = $lazy ?? false; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * same with sensitive status |
|
| 152 | + */ |
|
| 153 | + $sensitive = $input->getOption('sensitive'); |
|
| 154 | + try { |
|
| 155 | + $currSensitive = $this->appConfig->isSensitive($appName, $configName, null); |
|
| 156 | + if ($sensitive === null || $sensitive === $currSensitive || !$this->ask($input, $output, ($sensitive) ? 'SENSITIVE' : 'NOT SENSITIVE')) { |
|
| 157 | + $sensitive = $currSensitive; |
|
| 158 | + } |
|
| 159 | + } catch (AppConfigUnknownKeyException) { |
|
| 160 | + $sensitive = $sensitive ?? false; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $value = (string)$input->getOption('value'); |
|
| 164 | + |
|
| 165 | + switch ($type) { |
|
| 166 | + case IAppConfig::VALUE_MIXED: |
|
| 167 | + $updated = $this->appConfig->setValueMixed($appName, $configName, $value, $lazy, $sensitive); |
|
| 168 | + break; |
|
| 169 | + |
|
| 170 | + case IAppConfig::VALUE_STRING: |
|
| 171 | + $updated = $this->appConfig->setValueString($appName, $configName, $value, $lazy, $sensitive); |
|
| 172 | + break; |
|
| 173 | + |
|
| 174 | + case IAppConfig::VALUE_INT: |
|
| 175 | + if ($value !== ((string)((int)$value))) { |
|
| 176 | + throw new AppConfigIncorrectTypeException('Value is not an integer'); |
|
| 177 | + } |
|
| 178 | + $updated = $this->appConfig->setValueInt($appName, $configName, (int)$value, $lazy, $sensitive); |
|
| 179 | + break; |
|
| 180 | + |
|
| 181 | + case IAppConfig::VALUE_FLOAT: |
|
| 182 | + if ($value !== ((string)((float)$value))) { |
|
| 183 | + throw new AppConfigIncorrectTypeException('Value is not a float'); |
|
| 184 | + } |
|
| 185 | + $updated = $this->appConfig->setValueFloat($appName, $configName, (float)$value, $lazy, $sensitive); |
|
| 186 | + break; |
|
| 187 | + |
|
| 188 | + case IAppConfig::VALUE_BOOL: |
|
| 189 | + if (in_array(strtolower($value), ['true', '1', 'on', 'yes'])) { |
|
| 190 | + $valueBool = true; |
|
| 191 | + } elseif (in_array(strtolower($value), ['false', '0', 'off', 'no'])) { |
|
| 192 | + $valueBool = false; |
|
| 193 | + } else { |
|
| 194 | + throw new AppConfigIncorrectTypeException('Value is not a boolean, please use \'true\' or \'false\''); |
|
| 195 | + } |
|
| 196 | + $updated = $this->appConfig->setValueBool($appName, $configName, $valueBool, $lazy); |
|
| 197 | + break; |
|
| 198 | + |
|
| 199 | + case IAppConfig::VALUE_ARRAY: |
|
| 200 | + $valueArray = json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 201 | + $valueArray = (is_array($valueArray)) ? $valueArray : throw new AppConfigIncorrectTypeException('Value is not an array'); |
|
| 202 | + $updated = $this->appConfig->setValueArray($appName, $configName, $valueArray, $lazy, $sensitive); |
|
| 203 | + break; |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + if ($updated) { |
|
| 208 | + $current = $this->appConfig->getDetails($appName, $configName); |
|
| 209 | + $output->writeln( |
|
| 210 | + sprintf( |
|
| 211 | + "<info>Config value '%s' for app '%s' is now set to '%s', stored as %s in %s</info>", |
|
| 212 | + $configName, |
|
| 213 | + $appName, |
|
| 214 | + $current['sensitive'] ? '<sensitive>' : $current['value'], |
|
| 215 | + $current['typeString'], |
|
| 216 | + $current['lazy'] ? 'lazy cache' : 'fast cache' |
|
| 217 | + ) |
|
| 218 | + ); |
|
| 219 | + } else { |
|
| 220 | + $output->writeln('<info>Config value were not updated</info>'); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return 0; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + private function ask(InputInterface $input, OutputInterface $output, string $request): bool { |
|
| 227 | + /** @var QuestionHelper $helper */ |
|
| 228 | + $helper = $this->getHelper('question'); |
|
| 229 | + if ($input->getOption('no-interaction')) { |
|
| 230 | + return true; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + $output->writeln(sprintf('You are about to set config value %s as <info>%s</info>', |
|
| 234 | + '<info>' . $input->getArgument('app') . '</info>/<info>' . $input->getArgument('name') . '</info>', |
|
| 235 | + strtoupper($request) |
|
| 236 | + )); |
|
| 237 | + $output->writeln(''); |
|
| 238 | + $output->writeln('<comment>This might break thing, affect performance on your instance or its security!</comment>'); |
|
| 239 | + |
|
| 240 | + $result = (strtolower((string)$helper->ask( |
|
| 241 | + $input, |
|
| 242 | + $output, |
|
| 243 | + new Question('<comment>Confirm this action by typing \'yes\'</comment>: '))) === 'yes'); |
|
| 244 | + |
|
| 245 | + $output->writeln(($result) ? 'done' : 'cancelled'); |
|
| 246 | + $output->writeln(''); |
|
| 247 | + |
|
| 248 | + return $result; |
|
| 249 | + } |
|
| 250 | 250 | } |
@@ -14,42 +14,42 @@ |
||
| 14 | 14 | use Symfony\Component\Console\Output\OutputInterface; |
| 15 | 15 | |
| 16 | 16 | class DeleteConfig extends Base { |
| 17 | - protected function configure() { |
|
| 18 | - parent::configure(); |
|
| 17 | + protected function configure() { |
|
| 18 | + parent::configure(); |
|
| 19 | 19 | |
| 20 | - $this |
|
| 21 | - ->setName('config:app:delete') |
|
| 22 | - ->setDescription('Delete an app config value') |
|
| 23 | - ->addArgument( |
|
| 24 | - 'app', |
|
| 25 | - InputArgument::REQUIRED, |
|
| 26 | - 'Name of the app' |
|
| 27 | - ) |
|
| 28 | - ->addArgument( |
|
| 29 | - 'name', |
|
| 30 | - InputArgument::REQUIRED, |
|
| 31 | - 'Name of the config to delete' |
|
| 32 | - ) |
|
| 33 | - ->addOption( |
|
| 34 | - 'error-if-not-exists', |
|
| 35 | - null, |
|
| 36 | - InputOption::VALUE_NONE, |
|
| 37 | - 'Checks whether the config exists before deleting it' |
|
| 38 | - ) |
|
| 39 | - ; |
|
| 40 | - } |
|
| 20 | + $this |
|
| 21 | + ->setName('config:app:delete') |
|
| 22 | + ->setDescription('Delete an app config value') |
|
| 23 | + ->addArgument( |
|
| 24 | + 'app', |
|
| 25 | + InputArgument::REQUIRED, |
|
| 26 | + 'Name of the app' |
|
| 27 | + ) |
|
| 28 | + ->addArgument( |
|
| 29 | + 'name', |
|
| 30 | + InputArgument::REQUIRED, |
|
| 31 | + 'Name of the config to delete' |
|
| 32 | + ) |
|
| 33 | + ->addOption( |
|
| 34 | + 'error-if-not-exists', |
|
| 35 | + null, |
|
| 36 | + InputOption::VALUE_NONE, |
|
| 37 | + 'Checks whether the config exists before deleting it' |
|
| 38 | + ) |
|
| 39 | + ; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 43 | - $appName = $input->getArgument('app'); |
|
| 44 | - $configName = $input->getArgument('name'); |
|
| 42 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 43 | + $appName = $input->getArgument('app'); |
|
| 44 | + $configName = $input->getArgument('name'); |
|
| 45 | 45 | |
| 46 | - if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->appConfig->getKeys($appName), true)) { |
|
| 47 | - $output->writeln('<error>Config ' . $configName . ' of app ' . $appName . ' could not be deleted because it did not exist</error>'); |
|
| 48 | - return 1; |
|
| 49 | - } |
|
| 46 | + if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->appConfig->getKeys($appName), true)) { |
|
| 47 | + $output->writeln('<error>Config ' . $configName . ' of app ' . $appName . ' could not be deleted because it did not exist</error>'); |
|
| 48 | + return 1; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - $this->appConfig->deleteKey($appName, $configName); |
|
| 52 | - $output->writeln('<info>Config value ' . $configName . ' of app ' . $appName . ' deleted</info>'); |
|
| 53 | - return 0; |
|
| 54 | - } |
|
| 51 | + $this->appConfig->deleteKey($appName, $configName); |
|
| 52 | + $output->writeln('<info>Config value ' . $configName . ' of app ' . $appName . ' deleted</info>'); |
|
| 53 | + return 0; |
|
| 54 | + } |
|
| 55 | 55 | } |
@@ -44,12 +44,12 @@ |
||
| 44 | 44 | $configName = $input->getArgument('name'); |
| 45 | 45 | |
| 46 | 46 | if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->appConfig->getKeys($appName), true)) { |
| 47 | - $output->writeln('<error>Config ' . $configName . ' of app ' . $appName . ' could not be deleted because it did not exist</error>'); |
|
| 47 | + $output->writeln('<error>Config '.$configName.' of app '.$appName.' could not be deleted because it did not exist</error>'); |
|
| 48 | 48 | return 1; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | $this->appConfig->deleteKey($appName, $configName); |
| 52 | - $output->writeln('<info>Config value ' . $configName . ' of app ' . $appName . ' deleted</info>'); |
|
| 52 | + $output->writeln('<info>Config value '.$configName.' of app '.$appName.' deleted</info>'); |
|
| 53 | 53 | return 0; |
| 54 | 54 | } |
| 55 | 55 | } |
@@ -15,67 +15,67 @@ |
||
| 15 | 15 | use Symfony\Component\Console\Output\OutputInterface; |
| 16 | 16 | |
| 17 | 17 | class GetConfig extends Base { |
| 18 | - protected function configure() { |
|
| 19 | - parent::configure(); |
|
| 18 | + protected function configure() { |
|
| 19 | + parent::configure(); |
|
| 20 | 20 | |
| 21 | - $this |
|
| 22 | - ->setName('config:app:get') |
|
| 23 | - ->setDescription('Get an app config value') |
|
| 24 | - ->addArgument( |
|
| 25 | - 'app', |
|
| 26 | - InputArgument::REQUIRED, |
|
| 27 | - 'Name of the app' |
|
| 28 | - ) |
|
| 29 | - ->addArgument( |
|
| 30 | - 'name', |
|
| 31 | - InputArgument::REQUIRED, |
|
| 32 | - 'Name of the config to get' |
|
| 33 | - ) |
|
| 34 | - ->addOption( |
|
| 35 | - 'details', |
|
| 36 | - null, |
|
| 37 | - InputOption::VALUE_NONE, |
|
| 38 | - 'returns complete details about the app config value' |
|
| 39 | - ) |
|
| 40 | - ->addOption( |
|
| 41 | - 'default-value', |
|
| 42 | - null, |
|
| 43 | - InputOption::VALUE_OPTIONAL, |
|
| 44 | - 'If no default value is set and the config does not exist, the command will exit with 1' |
|
| 45 | - ) |
|
| 46 | - ; |
|
| 47 | - } |
|
| 21 | + $this |
|
| 22 | + ->setName('config:app:get') |
|
| 23 | + ->setDescription('Get an app config value') |
|
| 24 | + ->addArgument( |
|
| 25 | + 'app', |
|
| 26 | + InputArgument::REQUIRED, |
|
| 27 | + 'Name of the app' |
|
| 28 | + ) |
|
| 29 | + ->addArgument( |
|
| 30 | + 'name', |
|
| 31 | + InputArgument::REQUIRED, |
|
| 32 | + 'Name of the config to get' |
|
| 33 | + ) |
|
| 34 | + ->addOption( |
|
| 35 | + 'details', |
|
| 36 | + null, |
|
| 37 | + InputOption::VALUE_NONE, |
|
| 38 | + 'returns complete details about the app config value' |
|
| 39 | + ) |
|
| 40 | + ->addOption( |
|
| 41 | + 'default-value', |
|
| 42 | + null, |
|
| 43 | + InputOption::VALUE_OPTIONAL, |
|
| 44 | + 'If no default value is set and the config does not exist, the command will exit with 1' |
|
| 45 | + ) |
|
| 46 | + ; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Executes the current command. |
|
| 51 | - * |
|
| 52 | - * @param InputInterface $input An InputInterface instance |
|
| 53 | - * @param OutputInterface $output An OutputInterface instance |
|
| 54 | - * @return int 0 if everything went fine, or an error code |
|
| 55 | - */ |
|
| 56 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 57 | - $appName = $input->getArgument('app'); |
|
| 58 | - $configName = $input->getArgument('name'); |
|
| 59 | - $defaultValue = $input->getOption('default-value'); |
|
| 49 | + /** |
|
| 50 | + * Executes the current command. |
|
| 51 | + * |
|
| 52 | + * @param InputInterface $input An InputInterface instance |
|
| 53 | + * @param OutputInterface $output An OutputInterface instance |
|
| 54 | + * @return int 0 if everything went fine, or an error code |
|
| 55 | + */ |
|
| 56 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 57 | + $appName = $input->getArgument('app'); |
|
| 58 | + $configName = $input->getArgument('name'); |
|
| 59 | + $defaultValue = $input->getOption('default-value'); |
|
| 60 | 60 | |
| 61 | - if ($input->getOption('details')) { |
|
| 62 | - $details = $this->appConfig->getDetails($appName, $configName); |
|
| 63 | - $details['type'] = $details['typeString']; |
|
| 64 | - unset($details['typeString']); |
|
| 65 | - $this->writeArrayInOutputFormat($input, $output, $details); |
|
| 66 | - return 0; |
|
| 67 | - } |
|
| 61 | + if ($input->getOption('details')) { |
|
| 62 | + $details = $this->appConfig->getDetails($appName, $configName); |
|
| 63 | + $details['type'] = $details['typeString']; |
|
| 64 | + unset($details['typeString']); |
|
| 65 | + $this->writeArrayInOutputFormat($input, $output, $details); |
|
| 66 | + return 0; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - try { |
|
| 70 | - $configValue = $this->appConfig->getDetails($appName, $configName)['value']; |
|
| 71 | - } catch (AppConfigUnknownKeyException $e) { |
|
| 72 | - if (!$input->hasParameterOption('--default-value')) { |
|
| 73 | - return 1; |
|
| 74 | - } |
|
| 75 | - $configValue = $defaultValue; |
|
| 76 | - } |
|
| 69 | + try { |
|
| 70 | + $configValue = $this->appConfig->getDetails($appName, $configName)['value']; |
|
| 71 | + } catch (AppConfigUnknownKeyException $e) { |
|
| 72 | + if (!$input->hasParameterOption('--default-value')) { |
|
| 73 | + return 1; |
|
| 74 | + } |
|
| 75 | + $configValue = $defaultValue; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - $this->writeMixedInOutputFormat($input, $output, $configValue); |
|
| 79 | - return 0; |
|
| 80 | - } |
|
| 78 | + $this->writeMixedInOutputFormat($input, $output, $configValue); |
|
| 79 | + return 0; |
|
| 80 | + } |
|
| 81 | 81 | } |