diff options
author | Joas Schilling <coding@schilljs.com> | 2025-04-25 11:26:32 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2025-04-25 15:11:34 +0200 |
commit | 016f6c854b52f9d36234522d4e241513f9e4b4db (patch) | |
tree | 2ffa90a8d53e85166d64d8e4aae3c716aab1468a | |
parent | a461ce4e185f8a3dae908ab1ad1338584415be0c (diff) | |
download | nextcloud-server-backport/52429/stable29.tar.gz nextcloud-server-backport/52429/stable29.zip |
fix(occ): Fix autocompletion of config:app:* commandsbackport/52429/stable29
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | core/Command/Config/App/Base.php | 14 | ||||
-rw-r--r-- | core/Command/Config/App/DeleteConfig.php | 13 | ||||
-rw-r--r-- | core/Command/Config/App/GetConfig.php | 9 | ||||
-rw-r--r-- | core/Command/Config/App/SetConfig.php | 14 | ||||
-rw-r--r-- | tests/Core/Command/Config/App/DeleteConfigTest.php | 57 | ||||
-rw-r--r-- | tests/Core/Command/Config/App/GetConfigTest.php | 66 | ||||
-rw-r--r-- | tests/Core/Command/Config/App/SetConfigTest.php | 71 |
7 files changed, 98 insertions, 146 deletions
diff --git a/core/Command/Config/App/Base.php b/core/Command/Config/App/Base.php index b40f7c9e48d..c176908d85f 100644 --- a/core/Command/Config/App/Base.php +++ b/core/Command/Config/App/Base.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> * @@ -22,11 +24,15 @@ */ namespace OC\Core\Command\Config\App; -use OCP\IConfig; +use OCP\IAppConfig; use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; abstract class Base extends \OC\Core\Command\Base { - protected IConfig $config; + public function __construct( + protected IAppConfig $appConfig, + ) { + parent::__construct(); + } /** * @param string $argumentName @@ -35,12 +41,12 @@ abstract class Base extends \OC\Core\Command\Base { */ public function completeArgumentValues($argumentName, CompletionContext $context) { if ($argumentName === 'app') { - return \OC_App::getAllApps(); + return $this->appConfig->getApps(); } if ($argumentName === 'name') { $appName = $context->getWordAtIndex($context->getWordIndex() - 1); - return $this->config->getAppKeys($appName); + return $this->appConfig->getKeys($appName); } return []; } diff --git a/core/Command/Config/App/DeleteConfig.php b/core/Command/Config/App/DeleteConfig.php index b77f27ccd07..165b03dadbe 100644 --- a/core/Command/Config/App/DeleteConfig.php +++ b/core/Command/Config/App/DeleteConfig.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * @copyright Copyright (c) 2016, ownCloud, Inc. * @@ -21,19 +23,12 @@ */ namespace OC\Core\Command\Config\App; -use OCP\IConfig; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class DeleteConfig extends Base { - public function __construct( - protected IConfig $config, - ) { - parent::__construct(); - } - protected function configure() { parent::configure(); @@ -63,12 +58,12 @@ class DeleteConfig extends Base { $appName = $input->getArgument('app'); $configName = $input->getArgument('name'); - if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->config->getAppKeys($appName))) { + if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->appConfig->getKeys($appName), true)) { $output->writeln('<error>Config ' . $configName . ' of app ' . $appName . ' could not be deleted because it did not exist</error>'); return 1; } - $this->config->deleteAppValue($appName, $configName); + $this->appConfig->deleteKey($appName, $configName); $output->writeln('<info>Config value ' . $configName . ' of app ' . $appName . ' deleted</info>'); return 0; } diff --git a/core/Command/Config/App/GetConfig.php b/core/Command/Config/App/GetConfig.php index f85f978cc61..c4566b57af2 100644 --- a/core/Command/Config/App/GetConfig.php +++ b/core/Command/Config/App/GetConfig.php @@ -25,19 +25,12 @@ declare(strict_types=1); namespace OC\Core\Command\Config\App; use OCP\Exceptions\AppConfigUnknownKeyException; -use OCP\IAppConfig; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class GetConfig extends Base { - public function __construct( - protected IAppConfig $appConfig, - ) { - parent::__construct(); - } - protected function configure() { parent::configure(); @@ -72,7 +65,7 @@ class GetConfig extends Base { /** * Executes the current command. * - * @param InputInterface $input An InputInterface instance + * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * @return int 0 if everything went fine, or an error code */ diff --git a/core/Command/Config/App/SetConfig.php b/core/Command/Config/App/SetConfig.php index 3adba4af697..14ab4c034bb 100644 --- a/core/Command/Config/App/SetConfig.php +++ b/core/Command/Config/App/SetConfig.php @@ -28,6 +28,7 @@ use OC\AppConfig; use OCP\Exceptions\AppConfigIncorrectTypeException; use OCP\Exceptions\AppConfigUnknownKeyException; use OCP\IAppConfig; +use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -35,12 +36,6 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\Question; class SetConfig extends Base { - public function __construct( - protected IAppConfig $appConfig, - ) { - parent::__construct(); - } - protected function configure() { parent::configure(); @@ -193,14 +188,14 @@ class SetConfig extends Base { break; case IAppConfig::VALUE_INT: - if ($value !== ((string) ((int) $value))) { + if ($value !== ((string)((int)$value))) { throw new AppConfigIncorrectTypeException('Value is not an integer'); } $updated = $this->appConfig->setValueInt($appName, $configName, (int)$value, $lazy, $sensitive); break; case IAppConfig::VALUE_FLOAT: - if ($value !== ((string) ((float) $value))) { + if ($value !== ((string)((float)$value))) { throw new AppConfigIncorrectTypeException('Value is not a float'); } $updated = $this->appConfig->setValueFloat($appName, $configName, (float)$value, $lazy, $sensitive); @@ -232,7 +227,7 @@ class SetConfig extends Base { "<info>Config value '%s' for app '%s' is now set to '%s', stored as %s in %s</info>", $configName, $appName, - $current['value'], + $current['sensitive'] ? '<sensitive>' : $current['value'], $current['typeString'], $current['lazy'] ? 'lazy cache' : 'fast cache' ) @@ -245,6 +240,7 @@ class SetConfig extends Base { } private function ask(InputInterface $input, OutputInterface $output, string $request): bool { + /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); if ($input->getOption('no-interaction')) { return true; diff --git a/tests/Core/Command/Config/App/DeleteConfigTest.php b/tests/Core/Command/Config/App/DeleteConfigTest.php index 2c9482b57f6..8dc0f7dcdd3 100644 --- a/tests/Core/Command/Config/App/DeleteConfigTest.php +++ b/tests/Core/Command/Config/App/DeleteConfigTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * @author Joas Schilling <nickvergessen@owncloud.com> * @@ -22,37 +24,34 @@ namespace Tests\Core\Command\Config\App; use OC\Core\Command\Config\App\DeleteConfig; -use OCP\IConfig; +use OCP\IAppConfig; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; class DeleteConfigTest extends TestCase { - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - protected $config; - - /** @var \PHPUnit\Framework\MockObject\MockObject */ + /** @var IAppConfig|MockObject */ + protected $appConfig; + /** @var InputInterface|MockObject */ protected $consoleInput; - /** @var \PHPUnit\Framework\MockObject\MockObject */ + /** @var OutputInterface|MockObject */ protected $consoleOutput; - - /** @var \Symfony\Component\Console\Command\Command */ - protected $command; + protected Command $command; protected function setUp(): void { parent::setUp(); - $this->config = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); - $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + $this->appConfig = $this->createMock(IAppConfig::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); - $this->command = new DeleteConfig($this->config); + $this->command = new DeleteConfig($this->appConfig); } - public function deleteData() { + public static function dataDelete(): array { return [ [ 'name', @@ -86,22 +85,16 @@ class DeleteConfigTest extends TestCase { } /** - * @dataProvider deleteData - * - * @param string $configName - * @param bool $configExists - * @param bool $checkIfExists - * @param int $expectedReturn - * @param string $expectedMessage + * @dataProvider dataDelete */ - public function testDelete($configName, $configExists, $checkIfExists, $expectedReturn, $expectedMessage) { - $this->config->expects(($checkIfExists) ? $this->once() : $this->never()) - ->method('getAppKeys') + public function testDelete(string $configName, bool $configExists, bool $checkIfExists, int $expectedReturn, string $expectedMessage): void { + $this->appConfig->expects(($checkIfExists) ? $this->once() : $this->never()) + ->method('getKeys') ->with('app-name') ->willReturn($configExists ? [$configName] : []); - $this->config->expects(($expectedReturn === 0) ? $this->once() : $this->never()) - ->method('deleteAppValue') + $this->appConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never()) + ->method('deleteKey') ->with('app-name', $configName); $this->consoleInput->expects($this->exactly(2)) @@ -110,15 +103,13 @@ class DeleteConfigTest extends TestCase { ['app', 'app-name'], ['name', $configName], ]); - $this->consoleInput->expects($this->any()) - ->method('hasParameterOption') + $this->consoleInput->method('hasParameterOption') ->with('--error-if-not-exists') ->willReturn($checkIfExists); - $this->consoleOutput->expects($this->any()) - ->method('writeln') + $this->consoleOutput->method('writeln') ->with($this->stringContains($expectedMessage)); - $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); + $this->assertSame($expectedReturn, self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); } } diff --git a/tests/Core/Command/Config/App/GetConfigTest.php b/tests/Core/Command/Config/App/GetConfigTest.php index 5988d8d867f..4e6ea4881c8 100644 --- a/tests/Core/Command/Config/App/GetConfigTest.php +++ b/tests/Core/Command/Config/App/GetConfigTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * @author Joas Schilling <nickvergessen@owncloud.com> * @@ -21,40 +23,36 @@ namespace Tests\Core\Command\Config\App; -use OC\AppConfig; use OC\Core\Command\Config\App\GetConfig; use OCP\Exceptions\AppConfigUnknownKeyException; +use OCP\IAppConfig; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; class GetConfigTest extends TestCase { - /** @var \PHPUnit\Framework\MockObject\MockObject */ - protected $config; - - /** @var \PHPUnit\Framework\MockObject\MockObject */ + /** @var IAppConfig|MockObject */ + protected $appConfig; + /** @var InputInterface|MockObject */ protected $consoleInput; - /** @var \PHPUnit\Framework\MockObject\MockObject */ + /** @var OutputInterface|MockObject */ protected $consoleOutput; - - /** @var \Symfony\Component\Console\Command\Command */ - protected $command; + protected Command $command; protected function setUp(): void { parent::setUp(); - $config = $this->config = $this->getMockBuilder(AppConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); - $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + $this->appConfig = $this->createMock(IAppConfig::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); - /** @var \OCP\IAppConfig $config */ - $this->command = new GetConfig($config); + $this->command = new GetConfig($this->appConfig); } - public function getData() { + public static function dataGet(): array { return [ // String output as json ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')], @@ -97,21 +95,12 @@ class GetConfigTest extends TestCase { } /** - * @dataProvider getData - * - * @param string $configName - * @param mixed $value - * @param bool $configExists - * @param mixed $defaultValue - * @param bool $hasDefault - * @param string $outputFormat - * @param int $expectedReturn - * @param string $expectedMessage + * @dataProvider dataGet */ - public function testGet($configName, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage) { + public function testGet(string $configName, mixed $value, bool $configExists, mixed $defaultValue, bool $hasDefault, string $outputFormat, int $expectedReturn, ?string $expectedMessage): void { if (!$expectedReturn) { if ($configExists) { - $this->config->expects($this->once()) + $this->appConfig->expects($this->once()) ->method('getDetails') ->with('app-name', $configName) ->willReturn(['value' => $value]); @@ -119,10 +108,10 @@ class GetConfigTest extends TestCase { } if (!$configExists) { - $this->config->expects($this->once()) - ->method('getDetails') - ->with('app-name', $configName) - ->willThrowException(new AppConfigUnknownKeyException()); + $this->appConfig->expects($this->once()) + ->method('getDetails') + ->with('app-name', $configName) + ->willThrowException(new AppConfigUnknownKeyException()); } $this->consoleInput->expects($this->exactly(2)) @@ -131,14 +120,12 @@ class GetConfigTest extends TestCase { ['app', 'app-name'], ['name', $configName], ]); - $this->consoleInput->expects($this->any()) - ->method('getOption') + $this->consoleInput->method('getOption') ->willReturnMap([ ['default-value', $defaultValue], ['output', $outputFormat], ]); - $this->consoleInput->expects($this->any()) - ->method('hasParameterOption') + $this->consoleInput->method('hasParameterOption') ->willReturnMap([ ['--output', false, true], ['--default-value', false, $hasDefault], @@ -148,8 +135,7 @@ class GetConfigTest extends TestCase { global $output; $output = ''; - $this->consoleOutput->expects($this->any()) - ->method('writeln') + $this->consoleOutput->method('writeln') ->willReturnCallback(function ($value) { global $output; $output .= $value . "\n"; @@ -157,7 +143,7 @@ class GetConfigTest extends TestCase { }); } - $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); + $this->assertSame($expectedReturn, self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput])); if ($expectedMessage !== null) { global $output; diff --git a/tests/Core/Command/Config/App/SetConfigTest.php b/tests/Core/Command/Config/App/SetConfigTest.php index 4918053048a..22b18de9a5a 100644 --- a/tests/Core/Command/Config/App/SetConfigTest.php +++ b/tests/Core/Command/Config/App/SetConfigTest.php @@ -1,4 +1,6 @@ <?php + +declare(strict_types=1); /** * @author Joas Schilling <nickvergessen@owncloud.com> * @@ -25,37 +27,33 @@ use OC\AppConfig; use OC\Core\Command\Config\App\SetConfig; use OCP\Exceptions\AppConfigUnknownKeyException; use OCP\IAppConfig; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; class SetConfigTest extends TestCase { - /** @var \PHPUnit\Framework\MockObject\MockObject */ - protected $config; - - /** @var \PHPUnit\Framework\MockObject\MockObject */ + /** @var IAppConfig|MockObject */ + protected $appConfig; + /** @var InputInterface|MockObject */ protected $consoleInput; - /** @var \PHPUnit\Framework\MockObject\MockObject */ + /** @var OutputInterface|MockObject */ protected $consoleOutput; - - /** @var \Symfony\Component\Console\Command\Command */ - protected $command; + protected Command $command; protected function setUp(): void { parent::setUp(); - $config = $this->config = $this->getMockBuilder(AppConfig::class) - ->disableOriginalConstructor() - ->getMock(); - $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); - $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); + $this->appConfig = $this->createMock(AppConfig::class); + $this->consoleInput = $this->createMock(InputInterface::class); + $this->consoleOutput = $this->createMock(OutputInterface::class); - /** @var \OCP\IAppConfig $config */ - $this->command = new SetConfig($config); + $this->command = new SetConfig($this->appConfig); } - public function setData() { + public static function dataSet(): array { return [ [ 'name', @@ -77,33 +75,23 @@ class SetConfigTest extends TestCase { } /** - * @dataProvider setData - * - * @param string $configName - * @param mixed $newValue - * @param bool $configExists - * @param bool $updateOnly - * @param bool $updated - * @param string $expectedMessage + * @dataProvider dataSet */ - public function testSet($configName, $newValue, $configExists, $updateOnly, $updated, $expectedMessage) { - $this->config->expects($this->any()) - ->method('hasKey') - ->with('app-name', $configName) - ->willReturn($configExists); + public function testSet(string $configName, mixed $newValue, bool $configExists, bool $updateOnly, bool $updated, string $expectedMessage): void { + $this->appConfig->method('hasKey') + ->with('app-name', $configName) + ->willReturn($configExists); if (!$configExists) { - $this->config->expects($this->any()) - ->method('getValueType') - ->willThrowException(new AppConfigUnknownKeyException()); + $this->appConfig->method('getValueType') + ->willThrowException(new AppConfigUnknownKeyException()); } else { - $this->config->expects($this->any()) - ->method('getValueType') - ->willReturn(IAppConfig::VALUE_MIXED); + $this->appConfig->method('getValueType') + ->willReturn(IAppConfig::VALUE_MIXED); } if ($updated) { - $this->config->expects($this->once()) + $this->appConfig->expects($this->once()) ->method('setValueMixed') ->with('app-name', $configName, $newValue); } @@ -114,25 +102,22 @@ class SetConfigTest extends TestCase { ['app', 'app-name'], ['name', $configName], ]); - $this->consoleInput->expects($this->any()) - ->method('getOption') + $this->consoleInput->method('getOption') ->willReturnMap([ ['value', $newValue], ['lazy', null], ['sensitive', null], ['no-interaction', true], ]); - $this->consoleInput->expects($this->any()) - ->method('hasParameterOption') + $this->consoleInput->method('hasParameterOption') ->willReturnMap([ ['--type', false, false], ['--value', false, true], ['--update-only', false, $updateOnly] ]); - $this->consoleOutput->expects($this->any()) - ->method('writeln') + $this->consoleOutput->method('writeln') ->with($this->stringContains($expectedMessage)); - $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); + self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } } |