deleteconfigtest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\DeleteConfig;
  23. use Test\TestCase;
  24. class DeleteConfigTest 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 DeleteConfig($systemConfig);
  42. }
  43. public function deleteData() {
  44. return [
  45. [
  46. 'name1',
  47. true,
  48. true,
  49. 0,
  50. 'info',
  51. ],
  52. [
  53. 'name2',
  54. true,
  55. false,
  56. 0,
  57. 'info',
  58. ],
  59. [
  60. 'name3',
  61. false,
  62. false,
  63. 0,
  64. 'info',
  65. ],
  66. [
  67. 'name4',
  68. false,
  69. true,
  70. 1,
  71. 'error',
  72. ],
  73. ];
  74. }
  75. /**
  76. * @dataProvider deleteData
  77. *
  78. * @param string $configName
  79. * @param bool $configExists
  80. * @param bool $checkIfExists
  81. * @param int $expectedReturn
  82. * @param string $expectedMessage
  83. */
  84. public function testDelete($configName, $configExists, $checkIfExists, $expectedReturn, $expectedMessage) {
  85. $this->systemConfig->expects(($checkIfExists) ? $this->once() : $this->never())
  86. ->method('getKeys')
  87. ->willReturn($configExists ? [$configName] : []);
  88. $this->systemConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never())
  89. ->method('deleteValue')
  90. ->with($configName);
  91. $this->consoleInput->expects($this->once())
  92. ->method('getArgument')
  93. ->with('name')
  94. ->willReturn([$configName]);
  95. $this->consoleInput->expects($this->any())
  96. ->method('hasParameterOption')
  97. ->with('--error-if-not-exists')
  98. ->willReturn($checkIfExists);
  99. $this->consoleOutput->expects($this->any())
  100. ->method('writeln')
  101. ->with($this->stringContains($expectedMessage));
  102. $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  103. }
  104. public function deleteArrayData() {
  105. return [
  106. [
  107. ['name', 'sub'],
  108. true,
  109. false,
  110. true,
  111. true,
  112. 0,
  113. 'info',
  114. ],
  115. [
  116. ['name', 'sub', '2sub'],
  117. true,
  118. false,
  119. ['sub' => ['2sub' => 1], 'sub2' => false],
  120. ['sub' => [], 'sub2' => false],
  121. 0,
  122. 'info',
  123. ],
  124. [
  125. ['name', 'sub3'],
  126. true,
  127. false,
  128. ['sub' => ['2sub' => 1], 'sub2' => false],
  129. ['sub' => ['2sub' => 1], 'sub2' => false],
  130. 0,
  131. 'info',
  132. ],
  133. [
  134. ['name', 'sub'],
  135. false,
  136. true,
  137. true,
  138. true,
  139. 1,
  140. 'error',
  141. ],
  142. [
  143. ['name', 'sub'],
  144. true,
  145. true,
  146. true,
  147. true,
  148. 1,
  149. 'error',
  150. ],
  151. [
  152. ['name', 'sub3'],
  153. true,
  154. true,
  155. ['sub' => ['2sub' => 1], 'sub2' => false],
  156. ['sub' => ['2sub' => 1], 'sub2' => false],
  157. 1,
  158. 'error',
  159. ],
  160. ];
  161. }
  162. /**
  163. * @dataProvider deleteArrayData
  164. *
  165. * @param string[] $configNames
  166. * @param bool $configKeyExists
  167. * @param bool $checkIfKeyExists
  168. * @param mixed $configValue
  169. * @param mixed $updateValue
  170. * @param int $expectedReturn
  171. * @param string $expectedMessage
  172. */
  173. public function testArrayDelete(array $configNames, $configKeyExists, $checkIfKeyExists, $configValue, $updateValue, $expectedReturn, $expectedMessage) {
  174. $this->systemConfig->expects(($checkIfKeyExists) ? $this->once() : $this->never())
  175. ->method('getKeys')
  176. ->willReturn($configKeyExists ? [$configNames[0]] : []);
  177. $this->systemConfig->expects(($configKeyExists) ? $this->once() : $this->never())
  178. ->method('getValue')
  179. ->willReturn($configValue);
  180. $this->systemConfig->expects(($expectedReturn === 0) ? $this->once() : $this->never())
  181. ->method('setValue')
  182. ->with($configNames[0], $updateValue);
  183. $this->consoleInput->expects($this->once())
  184. ->method('getArgument')
  185. ->with('name')
  186. ->willReturn($configNames);
  187. $this->consoleInput->expects($this->any())
  188. ->method('hasParameterOption')
  189. ->with('--error-if-not-exists')
  190. ->willReturn($checkIfKeyExists);
  191. $this->consoleOutput->expects($this->any())
  192. ->method('writeln')
  193. ->with($this->stringContains($expectedMessage));
  194. $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  195. }
  196. }