Completed
Push — sharing_indication ( 68bf2d...e0780d )
by Georg
22:19
created

viewcontrollerTest.php ➔ file_get_contents()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 1
dl 0
loc 18
rs 9.2
c 0
b 0
f 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
	/**
87
	 * @dataProvider indexDataProvider
88
	 */
89
	public function testIndex($isAssetPipelineEnabled, $serverVersion, $expectsSupportsClass) {
90
		$this->config->expects($this->at(0))
91
			->method('getSystemValue')
92
			->with('version')
93
			->will($this->returnValue($serverVersion));
94
95
		$this->config->expects($this->at(1))
96
			->method('getSystemValue')
97
			->with('asset-pipeline.enabled', false)
98
			->will($this->returnValue($isAssetPipelineEnabled));
99
100
		if ($isAssetPipelineEnabled) {
101
			$actual = $this->controller->index();
102
103
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
104
			$this->assertEquals([], $actual->getParams());
105
			$this->assertEquals('main-asset-pipeline-unsupported', $actual->getTemplateName());
106
		} else {
107
			$this->userSession->expects($this->once())
108
				->method('getUser')
109
				->will($this->returnValue($this->dummyUser));
110
111
			$this->dummyUser->expects($this->once())
112
				->method('getUID')
113
				->will($this->returnValue('user123'));
114
115
			$this->dummyUser->expects($this->once())
116
				->method('getEMailAddress')
117
				->will($this->returnValue('[email protected]'));
118
119
			$this->config->expects($this->once())
120
				->method('getAppValue')
121
				->with($this->appName, 'installed_version')
122
				->will($this->returnValue('42.13.37'));
123
124
			$this->config->expects($this->once())
125
				->method('getUserValue')
126
				->with('user123', $this->appName, 'currentView', 'month')
127
				->will($this->returnValue('someView'));
128
129
			$actual = $this->controller->index();
130
131
			$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $actual);
132
			$this->assertEquals([
133
				'appVersion' => '42.13.37',
134
				'defaultView' => 'someView',
135
				'emailAddress' => '[email protected]',
136
				'supportsClass' => $expectsSupportsClass
137
			], $actual->getParams());
138
			$this->assertEquals('main', $actual->getTemplateName());
139
		}
140
141
	}
142
143
	public function indexDataProvider() {
144
		return [
145
			[true, '9.0.5.2', false],
146
			[true, '9.1.0.0', true],
147
			[false, '9.0.5.2', false],
148
			[false, '9.1.0.0', true]
149
		];
150
	}
151
152
	public function testGetTimezone() {
153
		$actual = $this->controller->getTimezone('TIMEZONE1.ics');
154
155
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
156
		$this->assertEquals('TIMEZONE1-data', $actual->getData());
157
	}
158
159
	public function testGetTimezoneWithFakeTz() {
160
		$actual = $this->controller->getTimezone('TIMEZONE42.ics');
161
162
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
163
	}
164
165
	public function testGetTimezoneWithRegion() {
166
		$actual = $this->controller->getTimezoneWithRegion('REG', 'CIT.ics');
167
168
		$this->assertInstanceOf('OCP\AppFramework\Http\DataDisplayResponse', $actual);
169
		$this->assertEquals('TIMEZONE DATA WITH REGION AND CITY', $actual->getData());
170
	}
171
172
	public function testGetTimezoneWithRegionWithFakeTz() {
173
		$actual = $this->controller->getTimezoneWithRegion('EUROPE', 'BERLIN.ics');
174
175
		$this->assertInstanceOf('OCP\AppFramework\Http\NotFoundResponse', $actual);
176
	}
177
}
178