setconfigtest.php 5.1 KB

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