SettingsControllerTest   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 296
Duplicated Lines 65.54 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 4
dl 194
loc 296
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 27 1
A testSetViewWithAllowedView() 0 11 1
A setViewWithAllowedViewDataProvider() 0 7 1
A testSetViewWithForbiddenView() 7 7 1
A testSetViewWithConfigException() 12 12 1
A testGetView() 12 12 1
A testGetViewWithConfigException() 12 12 1
A testSetPopoverWithAllowedValue() 0 11 1
A setPopoverWithAllowedValueDataProvider() 0 6 1
A testSetPopoverWithForbiddenValue() 7 7 1
A testSetPopoverWithConfigException() 12 12 1
A testGetPopover() 12 12 1
A testGetPopoverWithConfigException() 12 12 1
A testSetFirstRun() 11 11 1
A testSetFirstRunWithException() 12 12 1
A testGetFirstRun() 12 12 1
A testGetFirstRunWithException() 12 12 1
A testSetTimezone() 11 11 1
A testSetTimezoneWithException() 12 12 1
A testGetTimezone() 12 12 1
A testGetTimezoneWithException() 12 12 1
A testGetNotExistingConfig() 7 7 1
A testSetNotExistingConfig() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[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
namespace OCA\Calendar\Controller;
23
24
use PHPUnit\Framework\TestCase;
25
26
class SettingsControllerTest extends TestCase {
27
28
	private $appName;
29
	private $request;
30
	private $userSession;
31
	private $config;
32
33
	private $dummyUser;
34
35
	private $controller;
36
37
	public function setUp() {
38
		$this->appName = 'calendar';
39
		$this->request = $this->getMockBuilder('\OCP\IRequest')
40
			->disableOriginalConstructor()
41
			->getMock();
42
		$this->userSession = $this->getMockBuilder('OCP\IUserSession')
43
			->disableOriginalConstructor()
44
			->getMock();
45
		$this->config = $this->getMockBuilder('OCP\IConfig')
46
			->disableOriginalConstructor()
47
			->getMock();
48
49
		$this->dummyUser = $this->getMockBuilder('OCP\IUser')
50
			->disableOriginalConstructor()
51
			->getMock();
52
53
		$this->userSession->expects($this->once())
54
			->method('getUser')
55
			->will($this->returnValue($this->dummyUser));
56
57
		$this->dummyUser->expects($this->once())
58
			->method('getUID')
59
			->will($this->returnValue('user123'));
60
61
		$this->controller = new SettingsController($this->appName,
62
			$this->request, $this->userSession, $this->config);
63
	}
64
65
	/**
66
	 * @dataProvider setViewWithAllowedViewDataProvider
67
	 */
68
	public function testSetViewWithAllowedView($view) {
69
		$this->config->expects($this->once())
70
			->method('setUserValue')
71
			->with('user123', $this->appName, 'currentView', $view);
72
73
		$actual = $this->controller->setConfig('view', $view);
74
75
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
76
		$this->assertEquals([], $actual->getData());
77
		$this->assertEquals(200, $actual->getStatus());
78
	}
79
80
	public function setViewWithAllowedViewDataProvider() {
81
		return [
82
			['agendaDay'],
83
			['agendaWeek'],
84
			['month'],
85
		];
86
	}
87
88 View Code Duplication
	public function testSetViewWithForbiddenView() {
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...
89
		$actual = $this->controller->setConfig('view','someForbiddenView');
90
91
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
92
		$this->assertEquals([], $actual->getData());
93
		$this->assertEquals(422, $actual->getStatus());
94
	}
95
96 View Code Duplication
	public function testSetViewWithConfigException() {
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...
97
		$this->config->expects($this->once())
98
			->method('setUserValue')
99
			->with('user123', $this->appName, 'currentView', 'month')
100
			->will($this->throwException(new \Exception));
0 ignored issues
show
Documentation introduced by
new \Exception() is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
102
		$actual = $this->controller->setConfig('view', 'month');
103
104
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
105
		$this->assertEquals([], $actual->getData());
106
		$this->assertEquals(500, $actual->getStatus());
107
	}
108
109 View Code Duplication
	public function testGetView() {
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...
110
		$this->config->expects($this->once())
111
			->method('getUserValue')
112
			->with('user123', $this->appName, 'currentView', 'month')
113
			->will($this->returnValue('agendaWeek'));
114
115
		$actual = $this->controller->getConfig('view');
116
117
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
118
		$this->assertEquals(['value' => 'agendaWeek'], $actual->getData());
119
		$this->assertEquals(200, $actual->getStatus());
120
	}
121
122 View Code Duplication
	public function testGetViewWithConfigException() {
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...
123
		$this->config->expects($this->once())
124
			->method('getUserValue')
125
			->with('user123', $this->appName, 'currentView', 'month')
126
			->will($this->throwException(new \Exception));
0 ignored issues
show
Documentation introduced by
new \Exception() is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
128
		$actual = $this->controller->getConfig('view');
129
130
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
131
		$this->assertEquals([], $actual->getData());
132
		$this->assertEquals(500, $actual->getStatus());
133
	}
134
135
	/**
136
	 * @dataProvider setPopoverWithAllowedValueDataProvider
137
	 */
138
	public function testSetPopoverWithAllowedValue($value) {
139
		$this->config->expects($this->once())
140
			->method('setUserValue')
141
			->with('user123', $this->appName, 'skipPopover', $value);
142
143
		$actual = $this->controller->setConfig('skipPopover', $value);
144
145
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
146
		$this->assertEquals([], $actual->getData());
147
		$this->assertEquals(200, $actual->getStatus());
148
	}
149
150
	public function setPopoverWithAllowedValueDataProvider() {
151
		return [
152
			['yes'],
153
			['no']
154
		];
155
	}
156
157 View Code Duplication
	public function testSetPopoverWithForbiddenValue() {
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...
158
		$actual = $this->controller->setConfig('skipPopover','someForbiddenValue');
159
160
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
161
		$this->assertEquals([], $actual->getData());
162
		$this->assertEquals(422, $actual->getStatus());
163
	}
164
165 View Code Duplication
	public function testSetPopoverWithConfigException() {
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...
166
		$this->config->expects($this->once())
167
			->method('setUserValue')
168
			->with('user123', $this->appName, 'skipPopover', 'no')
169
			->will($this->throwException(new \Exception));
0 ignored issues
show
Documentation introduced by
new \Exception() is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
170
171
		$actual = $this->controller->setConfig('skipPopover', 'no');
172
173
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
174
		$this->assertEquals([], $actual->getData());
175
		$this->assertEquals(500, $actual->getStatus());
176
	}
177
178 View Code Duplication
	public function testGetPopover() {
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...
179
		$this->config->expects($this->once())
180
			->method('getUserValue')
181
			->with('user123', $this->appName, 'skipPopover', 'no')
182
			->will($this->returnValue('agendaWeek'));
183
184
		$actual = $this->controller->getConfig('skipPopover');
185
186
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
187
		$this->assertEquals(['value' => 'agendaWeek'], $actual->getData());
188
		$this->assertEquals(200, $actual->getStatus());
189
	}
190
191 View Code Duplication
	public function testGetPopoverWithConfigException() {
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...
192
		$this->config->expects($this->once())
193
			->method('getUserValue')
194
			->with('user123', $this->appName, 'skipPopover', 'no')
195
			->will($this->throwException(new \Exception));
0 ignored issues
show
Documentation introduced by
new \Exception() is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
196
197
		$actual = $this->controller->getConfig('skipPopover');
198
199
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
200
		$this->assertEquals([], $actual->getData());
201
		$this->assertEquals(500, $actual->getStatus());
202
	}
203
204 View Code Duplication
	public function testSetFirstRun() {
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...
205
		$this->config->expects($this->once())
206
			->method('setUserValue')
207
			->with('user123', $this->appName, 'firstRun', 'no');
208
209
		$actual = $this->controller->setConfig('firstRun', 'some_random_ignored_value');
210
211
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
212
		$this->assertEquals([], $actual->getData());
213
		$this->assertEquals(200, $actual->getStatus());
214
	}
215
216 View Code Duplication
	public function testSetFirstRunWithException() {
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...
217
		$this->config->expects($this->once())
218
			->method('setUserValue')
219
			->with('user123', $this->appName, 'firstRun', 'no')
220
			->will($this->throwException(new \Exception));
0 ignored issues
show
Documentation introduced by
new \Exception() is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
221
222
		$actual = $this->controller->setConfig('firstRun', 'some_random_ignored_value');
223
224
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
225
		$this->assertEquals([], $actual->getData());
226
		$this->assertEquals(500, $actual->getStatus());
227
	}
228
229 View Code Duplication
	public function testGetFirstRun() {
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...
230
		$this->config->expects($this->once())
231
			->method('getUserValue')
232
			->with('user123', $this->appName, 'firstRun', 'yes')
233
			->will($this->returnValue('no'));
234
235
		$actual = $this->controller->getConfig('firstRun');
236
237
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
238
		$this->assertEquals(['value' => 'no'], $actual->getData());
239
		$this->assertEquals(200, $actual->getStatus());
240
	}
241
242 View Code Duplication
	public function testGetFirstRunWithException() {
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...
243
		$this->config->expects($this->once())
244
			->method('getUserValue')
245
			->with('user123', $this->appName, 'firstRun', 'yes')
246
			->will($this->throwException(new \Exception));
0 ignored issues
show
Documentation introduced by
new \Exception() is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
247
248
		$actual = $this->controller->getConfig('firstRun');
249
250
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
251
		$this->assertEquals([], $actual->getData());
252
		$this->assertEquals(500, $actual->getStatus());
253
	}
254
255 View Code Duplication
	public function testSetTimezone() {
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...
256
		$this->config->expects($this->once())
257
			->method('setUserValue')
258
			->with('user123', $this->appName, 'timezone', 'Europe/Berlin');
259
260
		$actual = $this->controller->setConfig('timezone', 'Europe/Berlin');
261
262
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
263
		$this->assertEquals([], $actual->getData());
264
		$this->assertEquals(200, $actual->getStatus());
265
	}
266
267 View Code Duplication
	public function testSetTimezoneWithException() {
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...
268
		$this->config->expects($this->once())
269
			->method('setUserValue')
270
			->with('user123', $this->appName, 'timezone', 'Europe/Berlin')
271
			->will($this->throwException(new \Exception));
0 ignored issues
show
Documentation introduced by
new \Exception() is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
272
273
		$actual = $this->controller->setConfig('timezone', 'Europe/Berlin');
274
275
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
276
		$this->assertEquals([], $actual->getData());
277
		$this->assertEquals(500, $actual->getStatus());
278
	}
279
280 View Code Duplication
	public function testGetTimezone() {
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...
281
		$this->config->expects($this->once())
282
			->method('getUserValue')
283
			->with('user123', $this->appName, 'timezone', 'automatic')
284
			->will($this->returnValue('Europe/Berlin'));
285
286
		$actual = $this->controller->getConfig('timezone');
287
288
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
289
		$this->assertEquals(['value' => 'Europe/Berlin'], $actual->getData());
290
		$this->assertEquals(200, $actual->getStatus());
291
	}
292
293 View Code Duplication
	public function testGetTimezoneWithException() {
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...
294
		$this->config->expects($this->once())
295
			->method('getUserValue')
296
			->with('user123', $this->appName, 'timezone', 'automatic')
297
			->will($this->throwException(new \Exception));
0 ignored issues
show
Documentation introduced by
new \Exception() is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
298
299
		$actual = $this->controller->getConfig('timezone');
300
301
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
302
		$this->assertEquals([], $actual->getData());
303
		$this->assertEquals(500, $actual->getStatus());
304
	}
305
306 View Code Duplication
	public function testGetNotExistingConfig() {
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...
307
		$actual = $this->controller->getConfig('foo');
308
		
309
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
310
		$this->assertEquals([], $actual->getData());
311
		$this->assertEquals(400, $actual->getStatus());
312
	}
313
314 View Code Duplication
	public function testSetNotExistingConfig() {
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...
315
		$actual = $this->controller->setConfig('foo', 'bar');
316
317
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
318
		$this->assertEquals([], $actual->getData());
319
		$this->assertEquals(400, $actual->getStatus());
320
	}
321
}
322