ChatTest::testGetUserasContact()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
nc 1
nop 3
1
<?php
2
/**
3
 * Copyright (c) 2014, Tobia De Koninck hey--at--ledfan.be
4
 * This file is licensed under the AGPL version 3 or later.
5
 * See the COPYING file.
6
 */
7
8
namespace OCA\Chat\App;
9
10
class ChatTest extends \PHPUnit_Framework_TestCase {
11
12
	public function setUp(){
13
		$this->userOnlineMapper = $this->getMockBuilder('\OCA\Chat\OCH\Db\UserOnlineMapper')
0 ignored issues
show
Bug introduced by
The property userOnlineMapper does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
			->disableOriginalConstructor()
15
			->getMock();
16
17
		$this->och = $this->getMockBuilder('\OCA\Chat\OCH\OCH')
0 ignored issues
show
Bug introduced by
The property och does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
			->disableOriginalConstructor()
19
			->getMock();
20
21
		$this->syncOnline = $this->getMockBuilder('\OCA\Chat\OCH\Commands\SyncOnline')
0 ignored issues
show
Bug introduced by
The property syncOnline does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
			->disableOriginalConstructor()
23
			->getMock();
24
25
		$this->contactsManager = $this->getMockBuilder('\OCP\Contacts\IManager')
0 ignored issues
show
Bug introduced by
The property contactsManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
			->disableOriginalConstructor()
27
			->getMock();
28
29
		$this->backendManager = $this->getMockBuilder('\OCA\Chat\IBackendManager')
0 ignored issues
show
Bug introduced by
The property backendManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
			->disableOriginalConstructor()
31
			->getMock();
32
33
		$this->user = $this->getMockBuilder('\OCP\IUser')
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
			->disableOriginalConstructor()
35
			->getMock();
36
37
		$this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder')
0 ignored issues
show
Bug introduced by
The property rootFolder does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
			->disableOriginalConstructor()
39
			->getMock();
40
41
		$this->chat =  new Chat(
0 ignored issues
show
Bug introduced by
The property chat does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
			$this->backendManager,
43
			$this->userOnlineMapper,
44
			$this->syncOnline,
45
			$this->user,
46
			$this->contactsManager,
47
			$this->rootFolder
48
		);
49
50
51
	}
52
53
	public function contactsProvider() {
54
		return array(
55
			array(
56
				array(
57
					'foo'
58
				),
59
				array(
60
					0 => array(
61
						'id' => 'foo',
62
						'FN' => 'foo',
63
						'EMAIL' => array(),
64
						'IMPP' => array(
65
							0 => 'x-owncloud-handle:foo',
66
						),
67
						'addressbook-key' => 'local',
68
					),
69
					1 => array(
70
						'id' => 'bar',
71
						'FN' => 'bar',
72
						'EMAIL' => array(),
73
						'IMPP' => array(
74
							0 => 'x-owncloud-handle:bar',
75
						),
76
						'addressbook-key' => 'local',
77
					),
78
					2 => array(
79
						'id' => '1',
80
						'N' => array(
81
							0 => '',
82
							1 => 'TestContact',
83
							2 => '',
84
							3 => '',
85
							4 => '',
86
						),
87
						'UID' => '1a2a30d7-4907-4d5c-8e4a-3e51cf89e55a@localhost',
88
						'FN' => 'TestContact',
89
						'addressbook-key' => 'local:1',
90
					)
91
				),
92
				array(
93
					'contacts' =>
94
						array(
95
							0 =>
96
								array(
97
									'id' => 'foo',
98
									'online' => true,
99
									'displayname' => 'foo',
100
									'order' => 1,
101
									'saved' => true,
102
									'backends' =>
103
										array(
104
											0 =>
105
												array(
106
													'id' => 'email',
107
													'displayname' => 'E-mail',
108
													'protocol' => 'email',
109
													'namespace' => ' email',
110
													'value' =>
111
														array(
112
															0 =>
113
																array(),
114
														),
115
												),
116
											1 =>
117
												array(
118
													'id' => 'och',
119
													'displayname' => 'ownCloud Chat',
120
													'protocol' => 'x-owncloud-handle',
121
													'namespace' => 'och',
122
													'value' => 'foo',
123
												),
124
										),
125
									'address_book_id' => '',
126
									'address_book_backend' => 'local',
127
								),
128
							1 =>
129
								array(
130
									'id' => 'bar',
131
									'online' => false,
132
									'displayname' => 'bar',
133
									'order' => 2,
134
									'saved' => true,
135
									'backends' =>
136
										array(
137
											0 =>
138
												array(
139
													'id' => 'email',
140
													'displayname' => 'E-mail',
141
													'protocol' => 'email',
142
													'namespace' => ' email',
143
													'value' =>
144
														array(
145
															0 =>
146
																array(),
147
														),
148
												),
149
											1 =>
150
												array(
151
													'id' => 'och',
152
													'displayname' => 'ownCloud Chat',
153
													'protocol' => 'x-owncloud-handle',
154
													'namespace' => 'och',
155
													'value' => 'bar',
156
												),
157
										),
158
									'address_book_id' => '',
159
									'address_book_backend' => 'local',
160
								),
161
							2 =>
162
								array(
163
									'id' => '1',
164
									'online' => false,
165
									'displayname' => 'TestContact',
166
									'order' => 3,
167
									'saved' => true,
168
									'backends' =>
169
										array(
170
											0 =>
171
												array(
172
													'id' => 'email',
173
													'displayname' => 'E-mail',
174
													'protocol' => 'email',
175
													'namespace' => ' email',
176
													'value' =>
177
														array(
178
															0 =>
179
																array(),
180
														),
181
												),
182
										),
183
									'address_book_id' => '1',
184
									'address_book_backend' => 'local',
185
								),
186
						),
187
					'contactsList' =>
188
						array(
189
							0 => 'foo',
190
							1 => 'bar',
191
							2 => '1',
192
						),
193
					'contactsObj' =>
194
						array(
195
							'foo' =>
196
								array(
197
									'id' => 'foo',
198
									'online' => true,
199
									'displayname' => 'foo',
200
									'order' => 1,
201
									'saved' => true,
202
									'backends' =>
203
										array(
204
											0 =>
205
												array(
206
													'id' => 'email',
207
													'displayname' => 'E-mail',
208
													'protocol' => 'email',
209
													'namespace' => ' email',
210
													'value' =>
211
														array(
212
															0 =>
213
																array(),
214
														),
215
												),
216
											1 =>
217
												array(
218
													'id' => 'och',
219
													'displayname' => 'ownCloud Chat',
220
													'protocol' => 'x-owncloud-handle',
221
													'namespace' => 'och',
222
													'value' => 'foo',
223
												),
224
										),
225
									'address_book_id' => '',
226
									'address_book_backend' => 'local',
227
								),
228
							'bar' =>
229
								array(
230
									'id' => 'bar',
231
									'online' => false,
232
									'displayname' => 'bar',
233
									'order' => 2,
234
									'saved' => true,
235
									'backends' =>
236
										array(
237
											0 =>
238
												array(
239
													'id' => 'email',
240
													'displayname' => 'E-mail',
241
													'protocol' => 'email',
242
													'namespace' => ' email',
243
													'value' =>
244
														array(
245
															0 =>
246
																array(),
247
														),
248
												),
249
											1 =>
250
												array(
251
													'id' => 'och',
252
													'displayname' => 'ownCloud Chat',
253
													'protocol' => 'x-owncloud-handle',
254
													'namespace' => 'och',
255
													'value' => 'bar',
256
												),
257
										),
258
									'address_book_id' => '',
259
									'address_book_backend' => 'local',
260
								),
261
							1 =>
262
								array(
263
									'id' => '1',
264
									'online' => false,
265
									'displayname' => 'TestContact',
266
									'order' => 3,
267
									'saved' => true,
268
									'backends' =>
269
										array(
270
											0 =>
271
												array(
272
													'id' => 'email',
273
													'displayname' => 'E-mail',
274
													'protocol' => 'email',
275
													'namespace' => ' email',
276
													'value' =>
277
														array(
278
															0 =>
279
																array(),
280
														),
281
												),
282
										),
283
									'address_book_id' => '1',
284
									'address_book_backend' => 'local',
285
								),
286
						),
287
				)
288
			)
289
		);
290
	}
291
292
	/**
293
	 * @dataProvider contactsProvider
294
	 */
295
	public function testGetContacts($onlineUsers, $rawContacts, $parsedContacts) {
296
		$this->userOnlineMapper->expects($this->any())
297
			->method('getOnlineUsers')
298
			->will($this->returnValue($onlineUsers));
299
300
		$this->och->expects($this->any())
301
			->method('getId')
302
			->will($this->returnValue('och'));
303
304
		$this->och->expects($this->any())
305
			->method('getDisplayName')
306
			->will($this->returnValue('ownCloud Chat'));
307
308
		$this->backendManager->expects($this->any())
309
			->method('getBackendByProtocol')
310
			->will($this->returnValue($this->och));
311
312
		$this->contactsManager->expects($this->any())
313
			->method('search')
314
			->will($this->returnValue($rawContacts));
315
316
		$result = $this->chat->getContacts();
317
		$this->assertEquals($parsedContacts, $result);
318
	}
319
320
321
	/**
322
	 * This test will run after the testGetContacts method
323
	 * This test will test if the cache is used instead of re-fetching the contacts information
324
	 *
325
	 * @depends      testGetContacts
326
	 * @dataProvider contactsProvider
327
//	 */
328
	public function testGetContactsCache($onlineUsers, $rawContacts, $parsedContacts) {
0 ignored issues
show
Unused Code introduced by
The parameter $onlineUsers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $rawContacts is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
329
		$this->contactsManager->expects($this->never())
330
			->method('search');
331
332
		$result = $this->chat->getContacts();
333
		$this->assertEquals($parsedContacts, $result);
334
	}
335
336
337
	public function testGetBackends() {
338
		$this->backendManager->expects($this->once())
339
			->method('getEnabledBackends')
340
			->will($this->returnValue(array($this->och)));
341
342
		$expectedResult = array();
343
		$expectedResult[] = $this->och;
344
345
		$result = $this->chat->getBackends();
346
		$this->assertEquals($expectedResult, $result);
347
	}
348
349
350
	public function userContactProvider() {
351
		return array(
352
			array(
353
				array(
354
					0 => array(
355
						'id' => 'foo',
356
						'FN' => 'foo',
357
						'EMAIL' => array(),
358
						'IMPP' => array(
359
							0 => 'x-owncloud-handle:foo',
360
						),
361
						'addressbook-key' => 'local',
362
					),
363
					1 => array(
364
						'id' => 'bar',
365
						'FN' => 'bar',
366
						'EMAIL' => array(),
367
						'IMPP' => array(
368
							0 => 'x-owncloud-handle:bar',
369
						),
370
						'addressbook-key' => 'local',
371
					),
372
					2 => array(
373
						'id' => '1',
374
						'N' => array(
375
							0 => '',
376
							1 => 'TestContact',
377
							2 => '',
378
							3 => '',
379
							4 => '',
380
						),
381
						'UID' => '1a2a30d7-4907-4d5c-8e4a-3e51cf89e55a@localhost',
382
						'FN' => 'TestContact',
383
						'addressbook-key' => 'local:1',
384
					)
385
				),
386
				array(
387
					'online' => 1,
388
					'order' => 1,
389
    				'saved' => 1,
390
					'id' => 'foo',
391
					'displayname' => 'foo',
392
					'backends' =>
393
						array(
394
							0 =>
395
								array(
396
									'id' => 'email',
397
									'displayname' => 'E-mail',
398
									'protocol' => 'email',
399
									'namespace' => ' email',
400
									'value' =>
401
										array(
402
											0 =>
403
												array(),
404
										),
405
								),
406
							1 =>
407
								array(
408
									'id' => 'och',
409
									'displayname' => 'ownCloud Chat',
410
									'protocol' => 'x-owncloud-handle',
411
									'namespace' => 'och',
412
									'value' => 'foo',
413
								),
414
						),
415
					'address_book_id' => '',
416
					'address_book_backend' => 'local',
417
				),
418
				'foo'
419
			)
420
		);
421
	}
422
423
	/**
424
	 * @dataProvider userContactProvider
425
	 */
426
	public function testGetUserasContact($rawContacts, $expectedResult, $UID) {
427
		$this->backendManager->expects($this->any())
428
			->method('getBackendByProtocol')
429
			->will($this->returnValue($this->och));
430
431
		$this->contactsManager->expects($this->any())
432
			->method('search')
433
			->will($this->returnValue($rawContacts));
434
435
		$result = $this->chat->getUserasContact($UID);
436
		$this->assertEquals($expectedResult, $result);
437
	}
438
439
}