ModeTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Tests\Core\Command\Maintenance;
  7. use OC\Core\Command\Maintenance\Mode;
  8. use OCP\IConfig;
  9. use PHPUnit\Framework\MockObject\MockObject;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. use Test\TestCase;
  13. /**
  14. * This class provides tests methods for the Mode command.
  15. *
  16. * @package Tests\Core\Command\Maintenance
  17. */
  18. class ModeTest extends TestCase {
  19. /**
  20. * A config mock passed to the command.
  21. *
  22. * @var IConfig|MockObject
  23. */
  24. private $config;
  25. /**
  26. * Holds a Mode command instance with a config mock.
  27. *
  28. * @var Mode
  29. */
  30. private $mode;
  31. /**
  32. * An input mock for tests.
  33. *
  34. * @var InputInterface|MockObject
  35. */
  36. private $input;
  37. /**
  38. * An output mock for tests.
  39. *
  40. * @var OutputInterface|MockObject
  41. */
  42. private $output;
  43. /**
  44. * Setups the test environment.
  45. *
  46. * @return void
  47. */
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->config = $this->getMockBuilder(IConfig::class)
  51. ->getMock();
  52. $this->mode = new Mode($this->config);
  53. $this->input = $this->getMockBuilder(InputInterface::class)
  54. ->getMock();
  55. $this->output = $this->getMockBuilder(OutputInterface::class)
  56. ->getMock();
  57. }
  58. /**
  59. * Provides test data for the execute test.
  60. *
  61. * @return array
  62. */
  63. public function getExecuteTestData(): array {
  64. return [
  65. 'off -> on' => [
  66. 'on', // command option
  67. false, // current maintenance mode state
  68. true, // expected maintenance mode state, null for no change
  69. 'Maintenance mode enabled', // expected output
  70. ],
  71. 'on -> off' => [
  72. 'off',
  73. true,
  74. false,
  75. 'Maintenance mode disabled',
  76. ],
  77. 'on -> on' => [
  78. 'on',
  79. true,
  80. null,
  81. 'Maintenance mode already enabled',
  82. ],
  83. 'off -> off' => [
  84. 'off',
  85. false,
  86. null,
  87. 'Maintenance mode already disabled',
  88. ],
  89. 'no option, maintenance enabled' => [
  90. '',
  91. true,
  92. null,
  93. 'Maintenance mode is currently enabled',
  94. ],
  95. 'no option, maintenance disabled' => [
  96. '',
  97. false,
  98. null,
  99. 'Maintenance mode is currently disabled',
  100. ],
  101. ];
  102. }
  103. /**
  104. * Asserts that execute works as expected.
  105. *
  106. * @dataProvider getExecuteTestData
  107. * @param string $option The command option.
  108. * @param bool $currentMaintenanceState The current maintenance state.
  109. * @param null|bool $expectedMaintenanceState
  110. * The expected maintenance state. Null for no change.
  111. * @param string $expectedOutput The expected command output.
  112. * @throws \Exception
  113. */
  114. public function testExecute(
  115. string $option,
  116. bool $currentMaintenanceState,
  117. $expectedMaintenanceState,
  118. string $expectedOutput
  119. ) {
  120. $this->config->expects($this->any())
  121. ->method('getSystemValueBool')
  122. ->willReturn($currentMaintenanceState);
  123. if ($expectedMaintenanceState !== null) {
  124. $this->config->expects($this->once())
  125. ->method('setSystemValue')
  126. ->with('maintenance', $expectedMaintenanceState);
  127. }
  128. $this->input->expects($this->any())
  129. ->method('getOption')
  130. ->willReturnCallback(function ($callOption) use ($option) {
  131. return $callOption === $option;
  132. });
  133. $this->output->expects($this->once())
  134. ->method('writeln')
  135. ->with($expectedOutput);
  136. $this->mode->run($this->input, $this->output);
  137. }
  138. }