Skip to content

Commit 84e9aff

Browse files
Merge pull request #52429 from nextcloud/bugfix/noid/fix-autocomplete-of-app-configs
2 parents f154e60 + 7962df9 commit 84e9aff

File tree

7 files changed

+80
-140
lines changed

7 files changed

+80
-140
lines changed

core/Command/Config/App/Base.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
46
* SPDX-License-Identifier: AGPL-3.0-or-later
57
*/
68
namespace OC\Core\Command\Config\App;
79

8-
use OCP\IConfig;
10+
use OCP\IAppConfig;
911
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
1012

1113
abstract class Base extends \OC\Core\Command\Base {
12-
protected IConfig $config;
14+
public function __construct(
15+
protected IAppConfig $appConfig,
16+
) {
17+
parent::__construct();
18+
}
1319

1420
/**
1521
* @param string $argumentName
@@ -18,12 +24,12 @@ abstract class Base extends \OC\Core\Command\Base {
1824
*/
1925
public function completeArgumentValues($argumentName, CompletionContext $context) {
2026
if ($argumentName === 'app') {
21-
return \OC_App::getAllApps();
27+
return $this->appConfig->getApps();
2228
}
2329

2430
if ($argumentName === 'name') {
2531
$appName = $context->getWordAtIndex($context->getWordIndex() - 1);
26-
return $this->config->getAppKeys($appName);
32+
return $this->appConfig->getKeys($appName);
2733
}
2834
return [];
2935
}

core/Command/Config/App/DeleteConfig.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
<?php
22

3+
declare(strict_types=1);
34
/**
45
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
56
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
67
* SPDX-License-Identifier: AGPL-3.0-only
78
*/
89
namespace OC\Core\Command\Config\App;
910

10-
use OCP\IConfig;
1111
use Symfony\Component\Console\Input\InputArgument;
1212
use Symfony\Component\Console\Input\InputInterface;
1313
use Symfony\Component\Console\Input\InputOption;
1414
use Symfony\Component\Console\Output\OutputInterface;
1515

1616
class DeleteConfig extends Base {
17-
public function __construct(
18-
protected IConfig $config,
19-
) {
20-
parent::__construct();
21-
}
22-
2317
protected function configure() {
2418
parent::configure();
2519

@@ -49,12 +43,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4943
$appName = $input->getArgument('app');
5044
$configName = $input->getArgument('name');
5145

52-
if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->config->getAppKeys($appName))) {
46+
if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->appConfig->getKeys($appName), true)) {
5347
$output->writeln('<error>Config ' . $configName . ' of app ' . $appName . ' could not be deleted because it did not exist</error>');
5448
return 1;
5549
}
5650

57-
$this->config->deleteAppValue($appName, $configName);
51+
$this->appConfig->deleteKey($appName, $configName);
5852
$output->writeln('<info>Config value ' . $configName . ' of app ' . $appName . ' deleted</info>');
5953
return 0;
6054
}

core/Command/Config/App/GetConfig.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,12 @@
99
namespace OC\Core\Command\Config\App;
1010

1111
use OCP\Exceptions\AppConfigUnknownKeyException;
12-
use OCP\IAppConfig;
1312
use Symfony\Component\Console\Input\InputArgument;
1413
use Symfony\Component\Console\Input\InputInterface;
1514
use Symfony\Component\Console\Input\InputOption;
1615
use Symfony\Component\Console\Output\OutputInterface;
1716

1817
class GetConfig extends Base {
19-
public function __construct(
20-
protected IAppConfig $appConfig,
21-
) {
22-
parent::__construct();
23-
}
24-
2518
protected function configure() {
2619
parent::configure();
2720

core/Command/Config/App/SetConfig.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
use Symfony\Component\Console\Question\Question;
2121

2222
class SetConfig extends Base {
23-
public function __construct(
24-
protected IAppConfig $appConfig,
25-
) {
26-
parent::__construct();
27-
}
28-
2923
protected function configure() {
3024
parent::configure();
3125

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
46
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -8,37 +10,31 @@
810
namespace Tests\Core\Command\Config\App;
911

1012
use OC\Core\Command\Config\App\DeleteConfig;
11-
use OCP\IConfig;
13+
use OCP\IAppConfig;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use Symfony\Component\Console\Command\Command;
1216
use Symfony\Component\Console\Input\InputInterface;
1317
use Symfony\Component\Console\Output\OutputInterface;
1418
use Test\TestCase;
1519

1620
class DeleteConfigTest extends TestCase {
17-
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
18-
protected $config;
19-
20-
/** @var \PHPUnit\Framework\MockObject\MockObject */
21-
protected $consoleInput;
22-
/** @var \PHPUnit\Framework\MockObject\MockObject */
23-
protected $consoleOutput;
24-
25-
/** @var \Symfony\Component\Console\Command\Command */
26-
protected $command;
21+
protected IAppConfig&MockObject $appConfig;
22+
protected InputInterface&MockObject $consoleInput;
23+
protected OutputInterface&MockObject $consoleOutput;
24+
protected Command $command;
2725

2826
protected function setUp(): void {
2927
parent::setUp();
3028

31-
$this->config = $this->getMockBuilder(IConfig::class)
32-
->disableOriginalConstructor()
33-
->getMock();
34-
$this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
35-
$this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
29+
$this->appConfig = $this->createMock(IAppConfig::class);
30+
$this->consoleInput = $this->createMock(InputInterface::class);
31+
$this->consoleOutput = $this->createMock(OutputInterface::class);
3632

37-
$this->command = new DeleteConfig($this->config);
33+
$this->command = new DeleteConfig($this->appConfig);
3834
}
3935

4036

41-
public function deleteData() {
37+
public static function dataDelete(): array {
4238
return [
4339
[
4440
'name',
@@ -72,22 +68,16 @@ public function deleteData() {
7268
}
7369

7470
/**
75-
* @dataProvider deleteData
76-
*
77-
* @param string $configName
78-
* @param bool $configExists
79-
* @param bool $checkIfExists
80-
* @param int $expectedReturn
81-
* @param string $expectedMessage
71+
* @dataProvider dataDelete
8272
*/
83-
public function testDelete($configName, $configExists, $checkIfExists, $expectedReturn, $expectedMessage): void {
84-
$this->config->expects(($checkIfExists) ? $this->once() : $this->never())
85-
->method('getAppKeys')
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')
8676
->with('app-name')
8777
->willReturn($configExists ? [$configName] : []);
8878

89-
$this->config->expects(($expectedReturn === 0) ? $this->once() : $this->never())
90-
->method('deleteAppValue')
79+
$this->appConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never())
80+
->method('deleteKey')
9181
->with('app-name', $configName);
9282

9383
$this->consoleInput->expects($this->exactly(2))
@@ -96,15 +86,13 @@ public function testDelete($configName, $configExists, $checkIfExists, $expected
9686
['app', 'app-name'],
9787
['name', $configName],
9888
]);
99-
$this->consoleInput->expects($this->any())
100-
->method('hasParameterOption')
89+
$this->consoleInput->method('hasParameterOption')
10190
->with('--error-if-not-exists')
10291
->willReturn($checkIfExists);
10392

104-
$this->consoleOutput->expects($this->any())
105-
->method('writeln')
93+
$this->consoleOutput->method('writeln')
10694
->with($this->stringContains($expectedMessage));
10795

108-
$this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
96+
$this->assertSame($expectedReturn, self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
10997
}
11098
}

tests/Core/Command/Config/App/GetConfigTest.php

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
46
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -7,40 +9,33 @@
79

810
namespace Tests\Core\Command\Config\App;
911

10-
use OC\AppConfig;
1112
use OC\Core\Command\Config\App\GetConfig;
1213
use OCP\Exceptions\AppConfigUnknownKeyException;
14+
use OCP\IAppConfig;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use Symfony\Component\Console\Command\Command;
1317
use Symfony\Component\Console\Input\InputInterface;
1418
use Symfony\Component\Console\Output\OutputInterface;
1519
use Test\TestCase;
1620

1721
class GetConfigTest extends TestCase {
18-
/** @var \PHPUnit\Framework\MockObject\MockObject */
19-
protected $config;
20-
21-
/** @var \PHPUnit\Framework\MockObject\MockObject */
22-
protected $consoleInput;
23-
/** @var \PHPUnit\Framework\MockObject\MockObject */
24-
protected $consoleOutput;
25-
26-
/** @var \Symfony\Component\Console\Command\Command */
27-
protected $command;
22+
protected IAppConfig&MockObject $appConfig;
23+
protected InputInterface&MockObject $consoleInput;
24+
protected OutputInterface&MockObject $consoleOutput;
25+
protected Command $command;
2826

2927
protected function setUp(): void {
3028
parent::setUp();
3129

32-
$config = $this->config = $this->getMockBuilder(AppConfig::class)
33-
->disableOriginalConstructor()
34-
->getMock();
35-
$this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
36-
$this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
30+
$this->appConfig = $this->createMock(IAppConfig::class);
31+
$this->consoleInput = $this->createMock(InputInterface::class);
32+
$this->consoleOutput = $this->createMock(OutputInterface::class);
3733

38-
/** @var \OCP\IAppConfig $config */
39-
$this->command = new GetConfig($config);
34+
$this->command = new GetConfig($this->appConfig);
4035
}
4136

4237

43-
public function getData() {
38+
public static function dataGet(): array {
4439
return [
4540
// String output as json
4641
['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')],
@@ -83,29 +78,20 @@ public function getData() {
8378
}
8479

8580
/**
86-
* @dataProvider getData
87-
*
88-
* @param string $configName
89-
* @param mixed $value
90-
* @param bool $configExists
91-
* @param mixed $defaultValue
92-
* @param bool $hasDefault
93-
* @param string $outputFormat
94-
* @param int $expectedReturn
95-
* @param string $expectedMessage
81+
* @dataProvider dataGet
9682
*/
97-
public function testGet($configName, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage): void {
83+
public function testGet(string $configName, mixed $value, bool $configExists, mixed $defaultValue, bool $hasDefault, string $outputFormat, int $expectedReturn, ?string $expectedMessage): void {
9884
if (!$expectedReturn) {
9985
if ($configExists) {
100-
$this->config->expects($this->once())
86+
$this->appConfig->expects($this->once())
10187
->method('getDetails')
10288
->with('app-name', $configName)
10389
->willReturn(['value' => $value]);
10490
}
10591
}
10692

10793
if (!$configExists) {
108-
$this->config->expects($this->once())
94+
$this->appConfig->expects($this->once())
10995
->method('getDetails')
11096
->with('app-name', $configName)
11197
->willThrowException(new AppConfigUnknownKeyException());
@@ -117,14 +103,12 @@ public function testGet($configName, $value, $configExists, $defaultValue, $hasD
117103
['app', 'app-name'],
118104
['name', $configName],
119105
]);
120-
$this->consoleInput->expects($this->any())
121-
->method('getOption')
106+
$this->consoleInput->method('getOption')
122107
->willReturnMap([
123108
['default-value', $defaultValue],
124109
['output', $outputFormat],
125110
]);
126-
$this->consoleInput->expects($this->any())
127-
->method('hasParameterOption')
111+
$this->consoleInput->method('hasParameterOption')
128112
->willReturnMap([
129113
['--output', false, true],
130114
['--default-value', false, $hasDefault],
@@ -134,16 +118,15 @@ public function testGet($configName, $value, $configExists, $defaultValue, $hasD
134118
global $output;
135119

136120
$output = '';
137-
$this->consoleOutput->expects($this->any())
138-
->method('writeln')
121+
$this->consoleOutput->method('writeln')
139122
->willReturnCallback(function ($value) {
140123
global $output;
141124
$output .= $value . "\n";
142125
return $output;
143126
});
144127
}
145128

146-
$this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
129+
$this->assertSame($expectedReturn, self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
147130

148131
if ($expectedMessage !== null) {
149132
global $output;

0 commit comments

Comments
 (0)