SettingsServiceTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Nextcloud - Tasks
4
 *
5
 * @author Raimund Schlüßler
6
 * @copyright 2018 Raimund Schlüßler <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\Tasks\Tests\Unit\Service;
24
25
use OCA\Tasks\Service\SettingsService;
26
use OCP\App\IAppManager;
27
use OCP\IConfig;
28
29
use PHPUnit\Framework\TestCase;
30
31
class SettingsServiceTest extends TestCase {
32
	private $settingsService;
33
	private $settings;
34
	private $userId;
35
	private $appName;
36
	private $appManager;
37
38
	/**
39
	 * Gets run before each test
40
	 */
41 View Code Duplication
	protected function setUp(): void {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
		$this->appName = 'tasks';
43
		$this->userId = 'admin';
44
		$this->config = $this->getMockBuilder(IConfig::class)
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
			->disableOriginalConstructor()
46
			->getMock();
47
		$this->appManager = $this->getMockBuilder(IAppManager::class)
48
			->disableOriginalConstructor()
49
			->getMock();
50
		$this->settingsService = new SettingsService(
51
			$this->userId,
52
			$this->config,
53
			$this->appName,
54
			$this->appManager
55
		);
56
	}
57
58
	public function testGet() {
59
		$return = [
60
			'defaultCalendarId' => 'personal',
61
			'showHidden' => false,
62
			'sortOrder' => 'default',
63
			'sortDirection' => false,
64
			'allDay' => false,
65
			'initialRoute' => '/collections/all',
66
			'calendarEnabled' => true,
67
			'showTasks' => true,
68
			'calendarView' => 'dayGridMonth'
69
		];
70
71
		$userValueMap = [
72
			[
73
				$this->userId,
74
				$this->appName,
75
				'various_defaultCalendarId',
76
				'',
77
				'personal'
78
			],
79
			[
80
				$this->userId,
81
				$this->appName,
82
				'various_showHidden',
83
				false,
84
				false
85
			],
86
			[
87
				$this->userId,
88
				$this->appName,
89
				'various_sortOrder',
90
				'default',
91
				'default'
92
			],
93
			[
94
				$this->userId,
95
				$this->appName,
96
				'various_sortDirection',
97
				false,
98
				false
99
			],
100
			[
101
				$this->userId,
102
				$this->appName,
103
				'various_allDay',
104
				false,
105
				false
106
			],
107
			[
108
				$this->userId,
109
				$this->appName,
110
				'various_initialRoute',
111
				'/collections/all',
112
				'/collections/all'
113
			],
114
			[
115
				$this->userId,
116
				'calendar',
117
				'showTasks',
118
				'yes',
119
				'yes'
120
			],
121
			[
122
				$this->userId,
123
				'calendar',
124
				'currentView',
125
				'dayGridMonth',
126
				'dayGridMonth'
127
			]
128
		];
129
130
		$appValueMap = [
131
			[
132
				'calendar',
133
				'currentView',
134
				'dayGridMonth',
135
				'dayGridMonth'
136
			],
137
			[
138
				'calendar',
139
				'showTasks',
140
				'yes',
141
				'yes'
142
			]
143
		];
144
145
		$this->config->expects($this->any())
146
			->method('getUserValue')
147
			->will(
148
				$this->returnValueMap($userValueMap)
149
			);
150
151
		$this->config->expects($this->any())
152
			->method('getAppValue')
153
			->will(
154
				$this->returnValueMap($appValueMap)
155
			);
156
157
		$this->appManager->expects($this->any())
158
			->method('isEnabledForUser')
159
			->willReturn(true);
160
161
		$result = $this->settingsService->get();
162
163
		$this->assertEquals($return, $result);
164
	}
165
166
167
	public function testSet() {
168
		$return = true;
169
		
170
		$this->config->expects($this->once())
171
			->method('setUserValue')
172
			->with(
173
				$this->equalTo($this->userId),
174
				$this->equalTo($this->appName),
175
				'various_defaultCalendarId',
176
				'personal'
177
			);
178
		
179
		$result = $this->settingsService->set('defaultCalendarId', 'personal');
180
181
		$this->assertEquals($return, $result);
182
	}
183
}
184