DeleteConfigTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\App;
  8. use OC\Core\Command\Config\App\DeleteConfig;
  9. use OCP\IConfig;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Test\TestCase;
  13. class DeleteConfigTest extends TestCase {
  14. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  15. protected $config;
  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. $this->config = $this->getMockBuilder(IConfig::class)
  25. ->disableOriginalConstructor()
  26. ->getMock();
  27. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  28. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  29. $this->command = new DeleteConfig($this->config);
  30. }
  31. public function deleteData() {
  32. return [
  33. [
  34. 'name',
  35. true,
  36. true,
  37. 0,
  38. 'info',
  39. ],
  40. [
  41. 'name',
  42. true,
  43. false,
  44. 0,
  45. 'info',
  46. ],
  47. [
  48. 'name',
  49. false,
  50. false,
  51. 0,
  52. 'info',
  53. ],
  54. [
  55. 'name',
  56. false,
  57. true,
  58. 1,
  59. 'error',
  60. ],
  61. ];
  62. }
  63. /**
  64. * @dataProvider deleteData
  65. *
  66. * @param string $configName
  67. * @param bool $configExists
  68. * @param bool $checkIfExists
  69. * @param int $expectedReturn
  70. * @param string $expectedMessage
  71. */
  72. public function testDelete($configName, $configExists, $checkIfExists, $expectedReturn, $expectedMessage) {
  73. $this->config->expects(($checkIfExists) ? $this->once() : $this->never())
  74. ->method('getAppKeys')
  75. ->with('app-name')
  76. ->willReturn($configExists ? [$configName] : []);
  77. $this->config->expects(($expectedReturn === 0) ? $this->once() : $this->never())
  78. ->method('deleteAppValue')
  79. ->with('app-name', $configName);
  80. $this->consoleInput->expects($this->exactly(2))
  81. ->method('getArgument')
  82. ->willReturnMap([
  83. ['app', 'app-name'],
  84. ['name', $configName],
  85. ]);
  86. $this->consoleInput->expects($this->any())
  87. ->method('hasParameterOption')
  88. ->with('--error-if-not-exists')
  89. ->willReturn($checkIfExists);
  90. $this->consoleOutput->expects($this->any())
  91. ->method('writeln')
  92. ->with($this->stringContains($expectedMessage));
  93. $this->assertSame($expectedReturn, $this->invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]));
  94. }
  95. }