SetConfigTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Core\Command\Config\System;
  22. use OC\Core\Command\Config\System\SetConfig;
  23. use OC\SystemConfig;
  24. use Symfony\Component\Console\Input\InputInterface;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. use Test\TestCase;
  27. class SetConfigTest extends TestCase {
  28. /** @var \PHPUnit\Framework\MockObject\MockObject */
  29. protected $systemConfig;
  30. /** @var \PHPUnit\Framework\MockObject\MockObject */
  31. protected $consoleInput;
  32. /** @var \PHPUnit\Framework\MockObject\MockObject */
  33. protected $consoleOutput;
  34. /** @var \Symfony\Component\Console\Command\Command */
  35. protected $command;
  36. protected function setUp(): void {
  37. parent::setUp();
  38. $systemConfig = $this->systemConfig = $this->getMockBuilder(SystemConfig::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  42. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  43. /** @var \OC\SystemConfig $systemConfig */
  44. $this->command = new SetConfig($systemConfig);
  45. }
  46. public function setData() {
  47. return [
  48. [['name'], 'newvalue', null, 'newvalue'],
  49. [['a', 'b', 'c'], 'foobar', null, ['b' => ['c' => 'foobar']]],
  50. [['a', 'b', 'c'], 'foobar', ['b' => ['d' => 'barfoo']], ['b' => ['d' => 'barfoo', 'c' => 'foobar']]],
  51. ];
  52. }
  53. /**
  54. * @dataProvider setData
  55. *
  56. * @param array $configNames
  57. * @param string $newValue
  58. * @param mixed $existingData
  59. * @param mixed $expectedValue
  60. */
  61. public function testSet($configNames, $newValue, $existingData, $expectedValue) {
  62. $this->systemConfig->expects($this->once())
  63. ->method('setValue')
  64. ->with($configNames[0], $expectedValue);
  65. $this->systemConfig->method('getValue')
  66. ->with($configNames[0])
  67. ->willReturn($existingData);
  68. $this->consoleInput->expects($this->once())
  69. ->method('getArgument')
  70. ->with('name')
  71. ->willReturn($configNames);
  72. $this->consoleInput->method('getOption')
  73. ->willReturnMap([
  74. ['value', $newValue],
  75. ['type', 'string'],
  76. ]);
  77. $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  78. }
  79. public function setUpdateOnlyProvider() {
  80. return [
  81. [['name'], null],
  82. [['a', 'b', 'c'], null],
  83. [['a', 'b', 'c'], ['b' => 'foobar']],
  84. [['a', 'b', 'c'], ['b' => ['d' => 'foobar']]],
  85. ];
  86. }
  87. /**
  88. * @dataProvider setUpdateOnlyProvider
  89. */
  90. public function testSetUpdateOnly($configNames, $existingData) {
  91. $this->expectException(\UnexpectedValueException::class);
  92. $this->systemConfig->expects($this->never())
  93. ->method('setValue');
  94. $this->systemConfig->method('getValue')
  95. ->with($configNames[0])
  96. ->willReturn($existingData);
  97. $this->systemConfig->method('getKeys')
  98. ->willReturn($existingData ? $configNames[0] : []);
  99. $this->consoleInput->expects($this->once())
  100. ->method('getArgument')
  101. ->with('name')
  102. ->willReturn($configNames);
  103. $this->consoleInput->method('getOption')
  104. ->willReturnMap([
  105. ['value', 'foobar'],
  106. ['type', 'string'],
  107. ['update-only', true],
  108. ]);
  109. $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  110. }
  111. public function castValueProvider() {
  112. return [
  113. [null, 'string', ['value' => '', 'readable-value' => 'empty string']],
  114. ['abc', 'string', ['value' => 'abc', 'readable-value' => 'string abc']],
  115. ['123', 'integer', ['value' => 123, 'readable-value' => 'integer 123']],
  116. ['456', 'int', ['value' => 456, 'readable-value' => 'integer 456']],
  117. ['2.25', 'double', ['value' => 2.25, 'readable-value' => 'double 2.25']],
  118. ['0.5', 'float', ['value' => 0.5, 'readable-value' => 'double 0.5']],
  119. ['', 'null', ['value' => null, 'readable-value' => 'null']],
  120. ['true', 'boolean', ['value' => true, 'readable-value' => 'boolean true']],
  121. ['false', 'bool', ['value' => false, 'readable-value' => 'boolean false']],
  122. ];
  123. }
  124. /**
  125. * @dataProvider castValueProvider
  126. */
  127. public function testCastValue($value, $type, $expectedValue) {
  128. $this->assertSame($expectedValue,
  129. $this->invokePrivate($this->command, 'castValue', [$value, $type])
  130. );
  131. }
  132. public function castValueInvalidProvider() {
  133. return [
  134. ['123', 'foobar'],
  135. [null, 'integer'],
  136. ['abc', 'integer'],
  137. ['76ggg', 'double'],
  138. ['true', 'float'],
  139. ['foobar', 'boolean'],
  140. ];
  141. }
  142. /**
  143. * @dataProvider castValueInvalidProvider
  144. */
  145. public function testCastValueInvalid($value, $type) {
  146. $this->expectException(\InvalidArgumentException::class);
  147. $this->invokePrivate($this->command, 'castValue', [$value, $type]);
  148. }
  149. }