Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions core/Command/Config/App/Base.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
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
Expand All @@ -18,12 +24,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 [];
}
Expand Down
12 changes: 3 additions & 9 deletions core/Command/Config/App/DeleteConfig.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
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();

Expand Down Expand Up @@ -49,12 +43,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$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;
}
Expand Down
7 changes: 0 additions & 7 deletions core/Command/Config/App/GetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@
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();

Expand Down
6 changes: 0 additions & 6 deletions core/Command/Config/App/SetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
use Symfony\Component\Console\Question\Question;

class SetConfig extends Base {
public function __construct(
protected IAppConfig $appConfig,
) {
parent::__construct();
}

protected function configure() {
parent::configure();

Expand Down
58 changes: 23 additions & 35 deletions tests/Core/Command/Config/App/DeleteConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand All @@ -8,37 +10,31 @@
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 */
protected $consoleInput;
/** @var \PHPUnit\Framework\MockObject\MockObject */
protected $consoleOutput;

/** @var \Symfony\Component\Console\Command\Command */
protected $command;
protected IAppConfig&MockObject $appConfig;
protected InputInterface&MockObject $consoleInput;
protected OutputInterface&MockObject $consoleOutput;
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',
Expand Down Expand Up @@ -72,22 +68,16 @@ public function deleteData() {
}

/**
* @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))
Expand All @@ -96,15 +86,13 @@ public function testDelete($configName, $configExists, $checkIfExists, $expected
['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]));
}
}
67 changes: 25 additions & 42 deletions tests/Core/Command/Config/App/GetConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand All @@ -7,40 +9,33 @@

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 */
protected $consoleInput;
/** @var \PHPUnit\Framework\MockObject\MockObject */
protected $consoleOutput;

/** @var \Symfony\Component\Console\Command\Command */
protected $command;
protected IAppConfig&MockObject $appConfig;
protected InputInterface&MockObject $consoleInput;
protected OutputInterface&MockObject $consoleOutput;
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')],
Expand Down Expand Up @@ -83,32 +78,23 @@ public function getData() {
}

/**
* @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]);
}
}

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))
Expand All @@ -117,14 +103,12 @@ public function testGet($configName, $value, $configExists, $defaultValue, $hasD
['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],
Expand All @@ -134,16 +118,15 @@ public function testGet($configName, $value, $configExists, $defaultValue, $hasD
global $output;

$output = '';
$this->consoleOutput->expects($this->any())
->method('writeln')
$this->consoleOutput->method('writeln')
->willReturnCallback(function ($value) {
global $output;
$output .= $value . "\n";
return $output;
});
}

$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;
Expand Down
Loading
Loading