Completed
Push — remove_tz_index_json ( cf88f2 )
by
unknown
18:19
created

ViewControllerTest::testTimezoneList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
eloc 8
nc 1
nop 0
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
	public function testIndex() {
87
		$this->userSession->expects($this->once())
88
			->method('getUser')
89
			->will($this->returnValue($this->dummyUser));
90
91
		$this->dummyUser->expects($this->once())
92
			->method('getUID')
93
			->will($this->returnValue('user123'));
94
95
		$this->config->expects($this->once())
96
			->method('getAppValue')
97
			->with($this->appName, 'installed_version')
98
			->will($this->returnValue('42.13.37'));
99
100
		$this->config->expects($this->at(1))
101
			->method('getUserValue')
102
			->with('user123', $this->appName, 'currentView', 'month')
103
			->will($this->returnValue('someView'));
104
105
		$this->config->expects($this->at(2))
106
			->method('getUserValue')
107
			->with('user123', 'settings', 'email')
108
			->will($this->returnValue('[email protected]'));
109
110
		$actual = $this->controller->index();
111
112
		$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
113
		$this->assertEquals([
114
			'appVersion' => '42.13.37',
115
			'defaultView' => 'someView',
116
			'emailAddress' => '[email protected]',
117
		], $actual->getParams());
118
		$this->assertEquals('main', $actual->getTemplateName());
119
	}
120
121
	public function testGetTimezone() {
122
		$actual = $this->controller->getTimezone('TIMEZONE1.ics');
123
124
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
125
		$this->assertEquals('TIMEZONE1-data', $actual->getData());
126
	}
127
128
	public function testGetTimezoneWithFakeTz() {
129
		$actual = $this->controller->getTimezone('TIMEZONE42.ics');
130
131
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
132
	}
133
134
	public function testGetTimezoneWithRegion() {
135
		$actual = $this->controller->getTimezoneWithRegion('REG', 'CIT.ics');
136
137
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
138
		$this->assertEquals('TIMEZONE DATA WITH REGION AND CITY', $actual->getData());
139
	}
140
141
	public function testGetTimezoneWithRegionWithFakeTz() {
142
		$actual = $this->controller->getTimezoneWithRegion('EUROPE', 'BERLIN.ics');
143
144
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
145
	}
146
}
147