aboutsummaryrefslogtreecommitdiffstats
path: root/tests/Core/Command/Maintenance/ModeTest.php
blob: 1e8af354d50ded885b96db37cdbf6995a00c6dce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

namespace Tests\Core\Command\Maintenance;

use OC\Core\Command\Maintenance\Mode;
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Test\TestCase;

/**
 * This class provides tests methods for the Mode command.
 *
 * @package Tests\Core\Command\Maintenance
 */
class ModeTest extends TestCase {
	/**
	 * A config mock passed to the command.
	 *
	 * @var IConfig|MockObject
	 */
	private $config;

	/**
	 * Holds a Mode command instance with a config mock.
	 *
	 * @var Mode
	 */
	private $mode;

	/**
	 * An input mock for tests.
	 *
	 * @var InputInterface|MockObject
	 */
	private $input;

	/**
	 * An output mock for tests.
	 *
	 * @var OutputInterface|MockObject
	 */
	private $output;

	/**
	 * Setups the test environment.
	 *
	 * @return void
	 */
	protected function setUp(): void {
		parent::setUp();
		$this->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('getSystemValueBool')
			->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);
	}
}