From 9bd48e7c0df3ad0e404f524fa0100697e2bdb84e Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Sun, 1 Jul 2018 20:56:27 +0200 Subject: [PATCH] Adds an info if the maintenance mode is already enabled/disabled. Signed-off-by: Michael Weimann --- core/Command/Maintenance/Mode.php | 19 ++- tests/Core/Command/Maintenance/ModeTest.php | 147 ++++++++++++++++++++ 2 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 tests/Core/Command/Maintenance/ModeTest.php diff --git a/core/Command/Maintenance/Mode.php b/core/Command/Maintenance/Mode.php index 30d72da3583..db4c9dc8c0b 100644 --- a/core/Command/Maintenance/Mode.php +++ b/core/Command/Maintenance/Mode.php @@ -59,14 +59,23 @@ class Mode extends Command { } protected function execute(InputInterface $input, OutputInterface $output) { + $maintenanceMode = $this->config->getSystemValue('maintenance', false); if ($input->getOption('on')) { - $this->config->setSystemValue('maintenance', true); - $output->writeln('Maintenance mode enabled'); + if ($maintenanceMode === false) { + $this->config->setSystemValue('maintenance', true); + $output->writeln('Maintenance mode enabled'); + } else { + $output->writeln('Maintenance mode already enabled'); + } } elseif ($input->getOption('off')) { - $this->config->setSystemValue('maintenance', false); - $output->writeln('Maintenance mode disabled'); + if ($maintenanceMode === true) { + $this->config->setSystemValue('maintenance', false); + $output->writeln('Maintenance mode disabled'); + } else { + $output->writeln('Maintenance mode already disabled'); + } } else { - if ($this->config->getSystemValue('maintenance', false)) { + if ($maintenanceMode) { $output->writeln('Maintenance mode is currently enabled'); } else { $output->writeln('Maintenance mode is currently disabled'); diff --git a/tests/Core/Command/Maintenance/ModeTest.php b/tests/Core/Command/Maintenance/ModeTest.php new file mode 100644 index 00000000000..da5e95998e9 --- /dev/null +++ b/tests/Core/Command/Maintenance/ModeTest.php @@ -0,0 +1,147 @@ +config = $this->getMockBuilder(IConfig::class) + ->getMock(); + $this->mode = new Mode($this->config); + $this->input = $this->getMockBuilder(InputInterface::class) + ->getMock(); + $this->output = $this->getMockBuilder(OutputInterface::class) + ->getMock(); + } + + /** + * Provides test data for the execute test. + * + * @return array + */ + public function getExecuteTestData(): array { + return [ + 'off -> on' => [ + 'on', // command option + false, // current maintenance mode state + true, // expected maintenance mode state, null for no change + 'Maintenance mode enabled', // expected output + ], + 'on -> off' => [ + 'off', + true, + false, + 'Maintenance mode disabled', + ], + 'on -> on' => [ + 'on', + true, + null, + 'Maintenance mode already enabled', + ], + 'off -> off' => [ + 'off', + false, + null, + 'Maintenance mode already disabled', + ], + 'no option, maintenance enabled' => [ + '', + true, + null, + 'Maintenance mode is currently enabled', + ], + 'no option, maintenance disabled' => [ + '', + false, + null, + 'Maintenance mode is currently disabled', + ], + ]; + } + + /** + * Asserts that execute works as expected. + * + * @dataProvider getExecuteTestData + * @param string $option The command option. + * @param bool $currentMaintenanceState The current maintenance state. + * @param null|bool $expectedMaintenanceState + * The expected maintenance state. Null for no change. + * @param string $expectedOutput The expected command output. + * @throws \Exception + */ + public function testExecute( + string $option, + bool $currentMaintenanceState, + $expectedMaintenanceState, + string $expectedOutput + ) { + $this->config->expects($this->any()) + ->method('getSystemValue') + ->willReturn($currentMaintenanceState); + + if ($expectedMaintenanceState !== null) { + $this->config->expects($this->once()) + ->method('setSystemValue') + ->with('maintenance', $expectedMaintenanceState); + } + + $this->input->expects($this->any()) + ->method('getOption') + ->willReturnCallback(function ($callOption) use ($option) { + return $callOption === $option; + }); + + $this->output->expects($this->once()) + ->method('writeln') + ->with($expectedOutput); + + $this->mode->run($this->input, $this->output); + } +}