ModeTest.php 3.2 KB

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