Completed
Push — fix_untranslated_strings ( 07522f...8348da )
by
unknown
17:53
created

SettingsControllerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 133
Duplicated Lines 59.4 %

Coupling/Cohesion

Components 1
Dependencies 1
Metric Value
wmc 7
lcom 1
cbo 1
dl 79
loc 133
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 19 19 1
A testSetViewWithAllowedView() 0 19 1
A setViewWithAllowedViewDataProvider() 0 7 1
A testSetViewWithForbiddenView() 0 7 1
A testSetViewWithConfigException() 20 20 1
A testGetView() 20 20 1
A testGetViewWithConfigException() 20 20 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 View Code Duplication
	public function setUp() {
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...
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->controller = new SettingsController($this->appName,
52
			$this->request, $this->userSession, $this->config);
53
	}
54
55
	/**
56
	 * @dataProvider setViewWithAllowedViewDataProvider
57
	 */
58
	public function testSetViewWithAllowedView($view) {
59
		$this->userSession->expects($this->once())
60
			->method('getUser')
61
			->will($this->returnValue($this->dummyUser));
62
63
		$this->dummyUser->expects($this->once())
64
			->method('getUID')
65
			->will($this->returnValue('user123'));
66
67
		$this->config->expects($this->once())
68
			->method('setUserValue')
69
			->with('user123', $this->appName, 'currentView', $view);
70
71
		$actual = $this->controller->setView($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
	public function testSetViewWithForbiddenView() {
87
		$actual = $this->controller->setView('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->userSession->expects($this->once())
96
			->method('getUser')
97
			->will($this->returnValue($this->dummyUser));
98
99
		$this->dummyUser->expects($this->once())
100
			->method('getUID')
101
			->will($this->returnValue('user123'));
102
103
		$this->config->expects($this->once())
104
			->method('setUserValue')
105
			->with('user123', $this->appName, 'currentView', 'month')
106
			->will($this->throwException(new \Exception));
107
108
		$actual = $this->controller->setView('month');
109
110
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
111
		$this->assertEquals([], $actual->getData());
112
		$this->assertEquals(500, $actual->getStatus());
113
	}
114
115 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...
116
		$this->userSession->expects($this->once())
117
			->method('getUser')
118
			->will($this->returnValue($this->dummyUser));
119
120
		$this->dummyUser->expects($this->once())
121
			->method('getUID')
122
			->will($this->returnValue('user123'));
123
124
		$this->config->expects($this->once())
125
			->method('getUserValue')
126
			->with('user123', $this->appName, 'currentView', 'month')
127
			->will($this->returnValue('agendaWeek'));
128
129
		$actual = $this->controller->getView();
130
131
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
132
		$this->assertEquals(['value' => 'agendaWeek'], $actual->getData());
133
		$this->assertEquals(200, $actual->getStatus());
134
	}
135
136 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...
137
		$this->userSession->expects($this->once())
138
			->method('getUser')
139
			->will($this->returnValue($this->dummyUser));
140
141
		$this->dummyUser->expects($this->once())
142
			->method('getUID')
143
			->will($this->returnValue('user123'));
144
145
		$this->config->expects($this->once())
146
			->method('getUserValue')
147
			->with('user123', $this->appName, 'currentView', 'month')
148
			->will($this->throwException(new \Exception));
149
150
		$actual = $this->controller->getView();
151
152
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
153
		$this->assertEquals([], $actual->getData());
154
		$this->assertEquals(500, $actual->getStatus());
155
	}
156
}
157