SetConfigTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Tests\Core\Command\Config\App;
  8. use OC\AppConfig;
  9. use OC\Core\Command\Config\App\SetConfig;
  10. use OCP\Exceptions\AppConfigUnknownKeyException;
  11. use OCP\IAppConfig;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Test\TestCase;
  15. class SetConfigTest extends TestCase {
  16. /** @var \PHPUnit\Framework\MockObject\MockObject */
  17. protected $config;
  18. /** @var \PHPUnit\Framework\MockObject\MockObject */
  19. protected $consoleInput;
  20. /** @var \PHPUnit\Framework\MockObject\MockObject */
  21. protected $consoleOutput;
  22. /** @var \Symfony\Component\Console\Command\Command */
  23. protected $command;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $config = $this->config = $this->getMockBuilder(AppConfig::class)
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  30. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  31. /** @var \OCP\IAppConfig $config */
  32. $this->command = new SetConfig($config);
  33. }
  34. public function setData() {
  35. return [
  36. [
  37. 'name',
  38. 'newvalue',
  39. true,
  40. true,
  41. true,
  42. 'info',
  43. ],
  44. [
  45. 'name',
  46. 'newvalue',
  47. false,
  48. true,
  49. false,
  50. 'comment',
  51. ],
  52. ];
  53. }
  54. /**
  55. * @dataProvider setData
  56. *
  57. * @param string $configName
  58. * @param mixed $newValue
  59. * @param bool $configExists
  60. * @param bool $updateOnly
  61. * @param bool $updated
  62. * @param string $expectedMessage
  63. */
  64. public function testSet($configName, $newValue, $configExists, $updateOnly, $updated, $expectedMessage): void {
  65. $this->config->expects($this->any())
  66. ->method('hasKey')
  67. ->with('app-name', $configName)
  68. ->willReturn($configExists);
  69. if (!$configExists) {
  70. $this->config->expects($this->any())
  71. ->method('getValueType')
  72. ->willThrowException(new AppConfigUnknownKeyException());
  73. } else {
  74. $this->config->expects($this->any())
  75. ->method('getValueType')
  76. ->willReturn(IAppConfig::VALUE_MIXED);
  77. }
  78. if ($updated) {
  79. $this->config->expects($this->once())
  80. ->method('setValueMixed')
  81. ->with('app-name', $configName, $newValue);
  82. }
  83. $this->consoleInput->expects($this->exactly(2))
  84. ->method('getArgument')
  85. ->willReturnMap([
  86. ['app', 'app-name'],
  87. ['name', $configName],
  88. ]);
  89. $this->consoleInput->expects($this->any())
  90. ->method('getOption')
  91. ->willReturnMap([
  92. ['value', $newValue],
  93. ['lazy', null],
  94. ['sensitive', null],
  95. ['no-interaction', true],
  96. ]);
  97. $this->consoleInput->expects($this->any())
  98. ->method('hasParameterOption')
  99. ->willReturnMap([
  100. ['--type', false, false],
  101. ['--value', false, true],
  102. ['--update-only', false, $updateOnly]
  103. ]);
  104. $this->consoleOutput->expects($this->any())
  105. ->method('writeln')
  106. ->with($this->stringContains($expectedMessage));
  107. $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  108. }
  109. }