SettingsControllerTest   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 245
Duplicated Lines 60 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
lcom 1
cbo 1
dl 147
loc 245
rs 10

19 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 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
class SettingsControllerTest extends \PHPUnit_Framework_TestCase {
25
26
	private $appName;
27
	private $request;
28
	private $userSession;
29
	private $config;
30
31
	private $dummyUser;
32
33
	private $controller;
34
35
	public function setUp() {
36
		$this->appName = 'calendar';
37
		$this->request = $this->getMockBuilder('\OCP\IRequest')
38
			->disableOriginalConstructor()
39
			->getMock();
40
		$this->userSession = $this->getMockBuilder('OCP\IUserSession')
41
			->disableOriginalConstructor()
42
			->getMock();
43
		$this->config = $this->getMockBuilder('OCP\IConfig')
44
			->disableOriginalConstructor()
45
			->getMock();
46
47
		$this->dummyUser = $this->getMockBuilder('OCP\IUser')
48
			->disableOriginalConstructor()
49
			->getMock();
50
51
		$this->userSession->expects($this->once())
52
			->method('getUser')
53
			->will($this->returnValue($this->dummyUser));
54
55
		$this->dummyUser->expects($this->once())
56
			->method('getUID')
57
			->will($this->returnValue('user123'));
58
59
		$this->controller = new SettingsController($this->appName,
60
			$this->request, $this->userSession, $this->config);
61
	}
62
63
	/**
64
	 * @dataProvider setViewWithAllowedViewDataProvider
65
	 */
66
	public function testSetViewWithAllowedView($view) {
67
		$this->config->expects($this->once())
68
			->method('setUserValue')
69
			->with('user123', $this->appName, 'currentView', $view);
70
71
		$actual = $this->controller->setConfig('view', $view);
72
73
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
74
		$this->assertEquals([], $actual->getData());
75
		$this->assertEquals(200, $actual->getStatus());
76
	}
77
78
	public function setViewWithAllowedViewDataProvider() {
79
		return [
80
			['agendaDay'],
81
			['agendaWeek'],
82
			['month'],
83
		];
84
	}
85
86 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...
87
		$actual = $this->controller->setConfig('view','someForbiddenView');
88
89
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
90
		$this->assertEquals([], $actual->getData());
91
		$this->assertEquals(422, $actual->getStatus());
92
	}
93
94 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...
95
		$this->config->expects($this->once())
96
			->method('setUserValue')
97
			->with('user123', $this->appName, 'currentView', 'month')
98
			->will($this->throwException(new \Exception));
99
100
		$actual = $this->controller->setConfig('view', 'month');
101
102
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
103
		$this->assertEquals([], $actual->getData());
104
		$this->assertEquals(500, $actual->getStatus());
105
	}
106
107 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...
108
		$this->config->expects($this->once())
109
			->method('getUserValue')
110
			->with('user123', $this->appName, 'currentView', 'month')
111
			->will($this->returnValue('agendaWeek'));
112
113
		$actual = $this->controller->getConfig('view');
114
115
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
116
		$this->assertEquals(['value' => 'agendaWeek'], $actual->getData());
117
		$this->assertEquals(200, $actual->getStatus());
118
	}
119
120 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...
121
		$this->config->expects($this->once())
122
			->method('getUserValue')
123
			->with('user123', $this->appName, 'currentView', 'month')
124
			->will($this->throwException(new \Exception));
125
126
		$actual = $this->controller->getConfig('view');
127
128
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
129
		$this->assertEquals([], $actual->getData());
130
		$this->assertEquals(500, $actual->getStatus());
131
	}
132
133
	/**
134
	 * @dataProvider setPopoverWithAllowedValueDataProvider
135
	 */
136
	public function testSetPopoverWithAllowedValue($value) {
137
		$this->config->expects($this->once())
138
			->method('setUserValue')
139
			->with('user123', $this->appName, 'skipPopover', $value);
140
141
		$actual = $this->controller->setConfig('skipPopover', $value);
142
143
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
144
		$this->assertEquals([], $actual->getData());
145
		$this->assertEquals(200, $actual->getStatus());
146
	}
147
148
	public function setPopoverWithAllowedValueDataProvider() {
149
		return [
150
			['yes'],
151
			['no']
152
		];
153
	}
154
155 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...
156
		$actual = $this->controller->setConfig('skipPopover','someForbiddenValue');
157
158
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
159
		$this->assertEquals([], $actual->getData());
160
		$this->assertEquals(422, $actual->getStatus());
161
	}
162
163 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...
164
		$this->config->expects($this->once())
165
			->method('setUserValue')
166
			->with('user123', $this->appName, 'skipPopover', 'no')
167
			->will($this->throwException(new \Exception));
168
169
		$actual = $this->controller->setConfig('skipPopover', 'no');
170
171
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
172
		$this->assertEquals([], $actual->getData());
173
		$this->assertEquals(500, $actual->getStatus());
174
	}
175
176 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...
177
		$this->config->expects($this->once())
178
			->method('getUserValue')
179
			->with('user123', $this->appName, 'skipPopover', 'no')
180
			->will($this->returnValue('agendaWeek'));
181
182
		$actual = $this->controller->getConfig('skipPopover');
183
184
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
185
		$this->assertEquals(['value' => 'agendaWeek'], $actual->getData());
186
		$this->assertEquals(200, $actual->getStatus());
187
	}
188
189 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...
190
		$this->config->expects($this->once())
191
			->method('getUserValue')
192
			->with('user123', $this->appName, 'skipPopover', 'no')
193
			->will($this->throwException(new \Exception));
194
195
		$actual = $this->controller->getConfig('skipPopover');
196
197
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
198
		$this->assertEquals([], $actual->getData());
199
		$this->assertEquals(500, $actual->getStatus());
200
	}
201
202 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...
203
		$this->config->expects($this->once())
204
			->method('setUserValue')
205
			->with('user123', $this->appName, 'firstRun', 'no');
206
207
		$actual = $this->controller->setConfig('firstRun', 'some_random_ignored_value');
208
209
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
210
		$this->assertEquals([], $actual->getData());
211
		$this->assertEquals(200, $actual->getStatus());
212
	}
213
214 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...
215
		$this->config->expects($this->once())
216
			->method('setUserValue')
217
			->with('user123', $this->appName, 'firstRun', 'no')
218
			->will($this->throwException(new \Exception));
219
220
		$actual = $this->controller->setConfig('firstRun', 'some_random_ignored_value');
221
222
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
223
		$this->assertEquals([], $actual->getData());
224
		$this->assertEquals(500, $actual->getStatus());
225
	}
226
227 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...
228
		$this->config->expects($this->once())
229
			->method('getUserValue')
230
			->with('user123', $this->appName, 'firstRun', 'yes')
231
			->will($this->returnValue('no'));
232
233
		$actual = $this->controller->getConfig('firstRun');
234
235
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
236
		$this->assertEquals(['value' => 'no'], $actual->getData());
237
		$this->assertEquals(200, $actual->getStatus());
238
	}
239
240 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...
241
		$this->config->expects($this->once())
242
			->method('getUserValue')
243
			->with('user123', $this->appName, 'firstRun', 'yes')
244
			->will($this->throwException(new \Exception));
245
246
		$actual = $this->controller->getConfig('firstRun');
247
248
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
249
		$this->assertEquals([], $actual->getData());
250
		$this->assertEquals(500, $actual->getStatus());
251
	}
252
253 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...
254
		$actual = $this->controller->getConfig('foo');
255
		
256
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
257
		$this->assertEquals([], $actual->getData());
258
		$this->assertEquals(400, $actual->getStatus());
259
	}
260
261 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...
262
		$actual = $this->controller->setConfig('foo', 'bar');
263
264
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
265
		$this->assertEquals([], $actual->getData());
266
		$this->assertEquals(400, $actual->getStatus());
267
	}
268
}
269