DeleteConfigTest.php 5.4 KB

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