Completed
Push — public-sharing ( 2cf47b...7109aa )
by Thomas
20:16
created

SettingsControllerTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 194
Duplicated Lines 50.52 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 98
loc 194
rs 10
wmc 15
lcom 1
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setViewWithAllowedViewDataProvider() 0 7 1
A testSetViewWithForbiddenView() 7 7 1
B setUp() 0 27 1
A testSetViewWithAllowedView() 11 11 1
A testSetViewWithConfigException() 12 12 1
A testGetView() 0 12 1
A testGetViewWithConfigException() 12 12 1
A testSetPopoverWithAllowedValue() 11 11 1
A setPopoverWithAllowedValueDataProvider() 0 6 1
A testSetPopoverWithForbiddenValue() 7 7 1
A testSetPopoverWithConfigException() 12 12 1
A testGetPopover() 0 12 1
A testGetPopoverWithConfigException() 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
 * ownCloud - 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 View Code Duplication
	public function testSetViewWithAllowedView($view) {
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...
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
	public function testGetView() {
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 View Code Duplication
	public function testSetPopoverWithAllowedValue($value) {
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...
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
	public function testGetPopover() {
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 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...
203
		$actual = $this->controller->getConfig('foo');
204
		
205
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
206
		$this->assertEquals([], $actual->getData());
207
		$this->assertEquals(400, $actual->getStatus());
208
	}
209
210 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...
211
		$actual = $this->controller->setConfig('foo', 'bar');
212
213
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
214
		$this->assertEquals([], $actual->getData());
215
		$this->assertEquals(400, $actual->getStatus());
216
	}
217
}
218