Completed
Push — show_asset-pipeline_error ( e34d11 )
by
unknown
22:16
created

ViewControllerTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 126
Duplicated Lines 15.08 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
c 2
b 0
f 1
lcom 1
cbo 1
dl 19
loc 126
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 19 19 1
A testIndex() 0 48 2
A indexDataProvider() 0 6 1
A testTimezoneList() 0 9 1
A testGetTimezone() 0 6 1
A testGetTimezoneWithFakeTz() 0 5 1
A testGetTimezoneWithRegion() 0 6 1
A testGetTimezoneWithRegionWithFakeTz() 0 5 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
function scandir($directory) {
25
	$dir = substr(__DIR__, 0, -strlen('tests/unit/controller')) . 'controller/../timezones/';
26
	return $dir === $directory ? [
27
		'..',
28
		'.',
29
		'TIMEZONE1.ics',
30
		'TIMEZONE2.ics',
31
		'REG-CIT.ics',
32
		'INFO.md',
33
	] : [];
34
}
35
36
function file_get_contents($file) {
37
	$file_parts = explode('/', $file);
38
	end($file_parts);
39
	$file = current($file_parts);
40
	switch($file) {
41
		case 'TIMEZONE1.ics':
42
			return 'TIMEZONE1-data';
43
44
		case 'TIMEZONE2.ics':
45
			return 'ANOTHER TIMEZONE DATA';
46
47
		case 'REG-CIT.ics':
48
			return 'TIMEZONE DATA WITH REGION AND CITY';
49
50
		default:
51
			return null;
52
	}
53
}
54
55
class ViewControllerTest extends \PHPUnit_Framework_TestCase {
56
57
	private $appName;
58
	private $request;
59
	private $config;
60
	private $userSession;
61
62
	private $dummyUser;
63
64
	private $controller;
65
66 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...
67
		$this->appName = 'calendar';
68
		$this->request = $this->getMockBuilder('\OCP\IRequest')
69
			->disableOriginalConstructor()
70
			->getMock();
71
		$this->config = $this->getMockBuilder('\OCP\IConfig')
72
			->disableOriginalConstructor()
73
			->getMock();
74
		$this->userSession = $this->getMockBuilder('\OCP\IUserSession')
75
			->disableOriginalConstructor()
76
			->getMock();
77
78
		$this->dummyUser = $this->getMockBuilder('OCP\IUser')
79
			->disableOriginalConstructor()
80
			->getMock();
81
82
		$this->controller = new ViewController($this->appName, $this->request,
83
			$this->userSession, $this->config);
84
	}
85
86
	/**
87
	 * @dataProvider indexDataProvider
88
	 */
89
	public function testIndex($isAssetPipelineEnabled) {
90
		$this->config->expects($this->once())
91
			->method('getSystemValue')
92
			->with('asset-pipeline.enabled', false)
93
			->will($this->returnValue($isAssetPipelineEnabled));
94
95
		if ($isAssetPipelineEnabled) {
96
			$actual = $this->controller->index();
97
98
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
99
			$this->assertEquals([], $actual->getParams());
100
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
101
		} else {
102
			$this->userSession->expects($this->once())
103
				->method('getUser')
104
				->will($this->returnValue($this->dummyUser));
105
106
			$this->dummyUser->expects($this->once())
107
				->method('getUID')
108
				->will($this->returnValue('user123'));
109
110
			$this->config->expects($this->once())
111
				->method('getAppValue')
112
				->with($this->appName, 'installed_version')
113
				->will($this->returnValue('42.13.37'));
114
115
			$this->config->expects($this->at(2))
116
				->method('getUserValue')
117
				->with('user123', $this->appName, 'currentView', 'month')
118
				->will($this->returnValue('someView'));
119
120
			$this->config->expects($this->at(3))
121
				->method('getUserValue')
122
				->with('user123', 'settings', 'email')
123
				->will($this->returnValue('[email protected]'));
124
125
			$actual = $this->controller->index();
126
127
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
128
			$this->assertEquals([
129
				'appVersion' => '42.13.37',
130
				'defaultView' => 'someView',
131
				'emailAddress' => '[email protected]',
132
			], $actual->getParams());
133
			$this->assertEquals('main', $actual->getTemplateName());
134
		}
135
136
	}
137
138
	public function indexDataProvider() {
139
		return [
140
			[true],
141
			[false]
142
		];
143
	}
144
145
	public function testTimezoneList() {
146
		$actual = $this->controller->timezoneList();
147
		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);
148
		$this->assertEquals([
149
			'TIMEZONE1.ics',
150
			'TIMEZONE2.ics',
151
			'REG-CIT.ics',
152
		], $actual->getData());
153
	}
154
155
	public function testGetTimezone() {
156
		$actual = $this->controller->getTimezone('TIMEZONE1.ics');
157
158
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
159
		$this->assertEquals('TIMEZONE1-data', $actual->getData());
160
	}
161
162
	public function testGetTimezoneWithFakeTz() {
163
		$actual = $this->controller->getTimezone('TIMEZONE42.ics');
164
165
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
166
	}
167
168
	public function testGetTimezoneWithRegion() {
169
		$actual = $this->controller->getTimezoneWithRegion('REG', 'CIT.ics');
170
171
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
172
		$this->assertEquals('TIMEZONE DATA WITH REGION AND CITY', $actual->getData());
173
	}
174
175
	public function testGetTimezoneWithRegionWithFakeTz() {
176
		$actual = $this->controller->getTimezoneWithRegion('EUROPE', 'BERLIN.ics');
177
178
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
179
	}
180
}
181