SetConfigTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\System;
  8. use OC\Core\Command\Config\System\SetConfig;
  9. use OC\SystemConfig;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Test\TestCase;
  13. class SetConfigTest extends TestCase {
  14. /** @var \PHPUnit\Framework\MockObject\MockObject */
  15. protected $systemConfig;
  16. /** @var \PHPUnit\Framework\MockObject\MockObject */
  17. protected $consoleInput;
  18. /** @var \PHPUnit\Framework\MockObject\MockObject */
  19. protected $consoleOutput;
  20. /** @var \Symfony\Component\Console\Command\Command */
  21. protected $command;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $systemConfig = $this->systemConfig = $this->getMockBuilder(SystemConfig::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  28. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  29. /** @var \OC\SystemConfig $systemConfig */
  30. $this->command = new SetConfig($systemConfig);
  31. }
  32. public function setData() {
  33. return [
  34. [['name'], 'newvalue', null, 'newvalue'],
  35. [['a', 'b', 'c'], 'foobar', null, ['b' => ['c' => 'foobar']]],
  36. [['a', 'b', 'c'], 'foobar', ['b' => ['d' => 'barfoo']], ['b' => ['d' => 'barfoo', 'c' => 'foobar']]],
  37. ];
  38. }
  39. /**
  40. * @dataProvider setData
  41. *
  42. * @param array $configNames
  43. * @param string $newValue
  44. * @param mixed $existingData
  45. * @param mixed $expectedValue
  46. */
  47. public function testSet($configNames, $newValue, $existingData, $expectedValue): void {
  48. $this->systemConfig->expects($this->once())
  49. ->method('setValue')
  50. ->with($configNames[0], $expectedValue);
  51. $this->systemConfig->method('getValue')
  52. ->with($configNames[0])
  53. ->willReturn($existingData);
  54. $this->consoleInput->expects($this->once())
  55. ->method('getArgument')
  56. ->with('name')
  57. ->willReturn($configNames);
  58. $this->consoleInput->method('getOption')
  59. ->willReturnMap([
  60. ['value', $newValue],
  61. ['type', 'string'],
  62. ]);
  63. $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  64. }
  65. public function setUpdateOnlyProvider() {
  66. return [
  67. [['name'], null],
  68. [['a', 'b', 'c'], null],
  69. [['a', 'b', 'c'], ['b' => 'foobar']],
  70. [['a', 'b', 'c'], ['b' => ['d' => 'foobar']]],
  71. ];
  72. }
  73. /**
  74. * @dataProvider setUpdateOnlyProvider
  75. */
  76. public function testSetUpdateOnly($configNames, $existingData): void {
  77. $this->expectException(\UnexpectedValueException::class);
  78. $this->systemConfig->expects($this->never())
  79. ->method('setValue');
  80. $this->systemConfig->method('getValue')
  81. ->with($configNames[0])
  82. ->willReturn($existingData);
  83. $this->systemConfig->method('getKeys')
  84. ->willReturn($existingData ? $configNames[0] : []);
  85. $this->consoleInput->expects($this->once())
  86. ->method('getArgument')
  87. ->with('name')
  88. ->willReturn($configNames);
  89. $this->consoleInput->method('getOption')
  90. ->willReturnMap([
  91. ['value', 'foobar'],
  92. ['type', 'string'],
  93. ['update-only', true],
  94. ]);
  95. $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  96. }
  97. public function castValueProvider() {
  98. return [
  99. [null, 'string', ['value' => '', 'readable-value' => 'empty string']],
  100. ['abc', 'string', ['value' => 'abc', 'readable-value' => 'string abc']],
  101. ['123', 'integer', ['value' => 123, 'readable-value' => 'integer 123']],
  102. ['456', 'int', ['value' => 456, 'readable-value' => 'integer 456']],
  103. ['2.25', 'double', ['value' => 2.25, 'readable-value' => 'double 2.25']],
  104. ['0.5', 'float', ['value' => 0.5, 'readable-value' => 'double 0.5']],
  105. ['', 'null', ['value' => null, 'readable-value' => 'null']],
  106. ['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']],
  107. ['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']],
  108. ];
  109. }
  110. /**
  111. * @dataProvider castValueProvider
  112. */
  113. public function testCastValue($value, $type, $expectedValue): void {
  114. $this->assertSame($expectedValue,
  115. $this->invokePrivate($this->command, 'castValue', [$value, $type])
  116. );
  117. }
  118. public function castValueInvalidProvider() {
  119. return [
  120. ['123', 'foobar'],
  121. [null, 'integer'],
  122. ['abc', 'integer'],
  123. ['76ggg', 'double'],
  124. ['true', 'float'],
  125. ['foobar', 'boolean'],
  126. ];
  127. }
  128. /**
  129. * @dataProvider castValueInvalidProvider
  130. */
  131. public function testCastValueInvalid($value, $type): void {
  132. $this->expectException(\InvalidArgumentException::class);
  133. $this->invokePrivate($this->command, 'castValue', [$value, $type]);
  134. }
  135. }