JoinTest::testInsertNoPushMessagesNoGroupConv()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 109
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 109
rs 8.2857
cc 1
eloc 86
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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\OCH\Commands;
9
10
include_once(__DIR__ . '/../../../autoloader.php');
11
include_once(__DIR__ . '/../../../vendor/Pimple/Pimple.php');
12
13
14
use OCA\Chat\Core\API;
15
use OCA\Chat\OCH\Commands\Join;
16
use OCA\Chat\App\Chat;
17
use OCA\Chat\OCH\Db\InitConv;
18
use OCA\Chat\OCH\Db\PushMessage;
19
use \OCA\Chat\OCH\Db\UserOnline;
20
use OCA\Chat\Db\DBException;
21
use OCA\Chat\OCH\Exceptions\RequestDataInvalid;
22
use OCA\Chat\OCH\Db\User;
23
24
// Refer to GreetTest::testDBFailure for a DBFailure test
25
// This almost the same for every unit test
26
27
// The GetUsers and Messages commands are tested in their testClassess
28
class JoinTest extends \PHPUnit_Framework_TestCase {
29
30
	public static $initConv;
31
	public static $pushMessages;
32
	public static $pushMessageNoGroup;
33
34
    public function setUp(){
35
		$app =  new Chat();
0 ignored issues
show
Bug introduced by
The call to Chat::__construct() misses some required arguments starting with $backendManager.
Loading history...
36
		$this->container = $app->getContainer();
0 ignored issues
show
Bug introduced by
The property container 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...
Bug introduced by
The method getContainer() does not seem to exist on object<OCA\Chat\App\Chat>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
		$this->container['API'] = $this->getMockBuilder('\OCA\Chat\Core\API')
38
			->disableOriginalConstructor()
39
			->getMock();
40
    	$this->container['API']->expects($this->any())
41
    		->method('log')
42
    		->will($this->returnValue(null));
43
    }
44
    
45
	public function testOmmittedConvId(){
46
		$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'CONV-ID-MUST-BE-PROVIDED');
47
		
48
   		$this->container['API']->expects($this->any())
49
   			->method('prepareQuery')
50
   			->will($this->returnValue(true));
51
   	
52
	   	// logic
53
	   	$join = new Join($this->container);
0 ignored issues
show
Bug introduced by
The call to Join::__construct() misses some required arguments starting with $getUsers.
Loading history...
54
	   	$join->setRequestData(array(
55
	   			"user" => array(
56
	   					"id" => "admin",
57
	   					"displayname"=> "admin",
58
	   					"backends" => array(
59
	   							"och" => array(
60
	   									"id" => NULL,
61
	   									"displayname" => "wnCloud Handle",
62
	   									"protocol" => "x-owncloud-handle",
63
	   									"namespace" => "och",
64
	   									"value" => "admin"
65
	   							)
66
	   					),
67
	   					"address_book_id" => "admin",
68
	   					"address_book_backend"=> "localusers",
69
	   			),
70
	   			"session_id" => "87ce2b3faeb92f0fb745645e7827f51a",
71
	   			"timestamp" => 1397193430.516,
72
	   			//"conv_id" => "dasdfwffws" => this is ommitted
73
	   	));
74
   }
75
76
	public function testInitConvInsert(){
77
		$this->container['InitConvMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\InitConvMapper')
78
			->disableOriginalConstructor()
79
			->getMock();
80
		$this->container['InitConvMapper']->expects($this->any())
81
			->method('insertUnique')
82
			->will($this->returnCallback(function($initConv){
83
				JoinTest::$initConv = $initConv;
84
			}));
85
86
		$this->container['GetUsersData'] = $this->getMockBuilder('\OCA\Chat\OCH\Data\GetUsers')
87
			->disableOriginalConstructor()
88
			->getMock();
89
		$this->container['GetUsersData']->expects($this->any())
90
			->method('execute')
91
			->will($this->returnValue(array("users" => array())));
92
93
		$this->container['MessagesData'] = $this->getMockBuilder('\OCA\Chat\OCH\Data\Messages')
94
			->disableOriginalConstructor()
95
			->getMock();
96
		$this->container['MessagesData']->expects($this->any())
97
			->method('execute')
98
			->will($this->returnValue(array("messages" => array())));
99
100
		$expectedInitConv =  new InitConv();
101
		$expectedInitConv->setConvId('dasdfwffws');
102
		$expectedInitConv->setUser('admin');
103
104
		$join = new Join($this->container);
0 ignored issues
show
Bug introduced by
The call to Join::__construct() misses some required arguments starting with $getUsers.
Loading history...
Documentation introduced by
$this->container is of type array<string,?,{"MessagesData":"?"}>, but the function expects a object<OCA\Chat\OCH\Db\PushMessageMapper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
		$join->setRequestData(array(
106
			"user" => array(
107
				"id" => "admin",
108
				"displayname"=> "admin",
109
				"backends" => array(
110
					"och" => array(
111
						"id" => NULL,
112
						"displayname" => "wnCloud Handle",
113
						"protocol" => "x-owncloud-handle",
114
						"namespace" => "och",
115
						"value" => "admin"
116
					)
117
				),
118
				"address_book_id" => "admin",
119
				"address_book_backend"=> "localusers",
120
			),
121
			"session_id" => "87ce2b3faeb92f0fb745645e7827f51a",
122
			"timestamp" => 1397193430.516,
123
			'conv_id' => 'dasdfwffws'
124
		));
125
		$join->execute();
126
127
		$this->assertEquals($expectedInitConv, JoinTest::$initConv);
128
	}
129
130
	public function testPushMessageInsertGroupConv(){
131
		$this->container['InitConvMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\InitConvMapper')
132
			->disableOriginalConstructor()
133
			->getMock();
134
		$this->container['InitConvMapper']->expects($this->any())
135
			->method('insertUnique')
136
			->will($this->returnValue(true));
137
138
		$this->container['GetUsersData'] = $this->getMockBuilder('\OCA\Chat\OCH\Data\GetUsers')
139
			->disableOriginalConstructor()
140
			->getMock();
141
		$this->container['GetUsersData']->expects($this->any())
142
			->method('execute')
143
			->will($this->returnValue(array("users" => array(
144
				0 => array (
145
					'id' => 'derp',
146
					'online' => false,
147
					'displayname' => 'derp',
148
					'backends' => array (
149
						'och' => array (
150
							'id' => NULL,
151
							'displayname' => 'ownCloud Handle',
152
							'protocol' => 'x-owncloud-handle',
153
							'namespace' => 'och',
154
							'value' => 'derp',
155
						),
156
					),
157
					'address_book_id' => 'admin',
158
					'address_book_backend' => 'localusers',
159
				),
160
				1 => array (
161
					'id' => 'admin',
162
					'online' => true,
163
					'displayname' => 'admin',
164
					'backends' => array (
165
						'och' => array (
166
							'id' => NULL,
167
							'displayname' => 'ownCloud Handle',
168
							'protocol' => 'x-owncloud-handle',
169
							'namespace' => 'och',
170
							'value' => 'admin',
171
						),
172
					),
173
					'address_book_id' => 'admin',
174
					'address_book_backend' => 'localusers',
175
				),
176
				2 => array (
177
					'id' => 'foo',
178
					'online' => true,
179
					'displayname' => 'foo',
180
					'backends' => array (
181
						'och' => array (
182
							'id' => NULL,
183
							'displayname' => 'ownCloud Handle',
184
							'protocol' => 'x-owncloud-handle',
185
							'namespace' => 'och',
186
							'value' => 'foo',
187
						),
188
					),
189
					'address_book_id' => 'admin',
190
					'address_book_backend' => 'localusers',
191
192
				),
193
			))));
194
195
		$this->container['MessagesData'] = $this->getMockBuilder('\OCA\Chat\OCH\Data\Messages')
196
			->disableOriginalConstructor()
197
			->getMock();
198
		$this->container['MessagesData']->expects($this->any())
199
			->method('execute')
200
			->will($this->returnValue(array("messages" => array())));
201
202
		$this->container['UserMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\UserMapper')
203
			->disableOriginalConstructor()
204
			->getMock();
205
206
		$derpSession = new User();
207
		$derpSession->setUser('derp');
208
		$derpSession->setSessionId('session-id-2');
209
210
		$adminSession = new User();
211
		$adminSession->setUser('admin');
212
		$adminSession->setSessionId('session-id-1');
213
214
		$this->container['UserMapper']->expects($this->any())
215
			->method('findSessionsByConversation')
216
			->will($this->returnValue(array($derpSession, $adminSession)));
217
218
		$this->container['PushMessageMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\PushMessageMapper')
219
			->disableOriginalConstructor()
220
			->getMock();
221
		$this->container['PushMessageMapper']->expects($this->any())
222
			->method('insert')
223
			->will($this->returnCallback(function($pushMessage){
224
				JoinTest::$pushMessages[] = $pushMessage;
225
			}));
226
227
		$expectedInitConv =  new InitConv();
228
		$expectedInitConv->setConvId('dasdfwffws');
229
		$expectedInitConv->setUser('admin');
230
231
		$join = new Join($this->container);
0 ignored issues
show
Bug introduced by
The call to Join::__construct() misses some required arguments starting with $getUsers.
Loading history...
Documentation introduced by
$this->container is of type array<string,?,{"PushMessageMapper":"?"}>, but the function expects a object<OCA\Chat\OCH\Db\PushMessageMapper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
232
		$join->setRequestData(array(
233
			"user" => array(
234
				"id" => "admin",
235
				"displayname"=> "admin",
236
				"backends" => array(
237
					"och" => array(
238
						"id" => NULL,
239
						"displayname" => "wnCloud Handle",
240
						"protocol" => "x-owncloud-handle",
241
						"namespace" => "och",
242
						"value" => "admin"
243
					)
244
				),
245
				"address_book_id" => "admin",
246
				"address_book_backend"=> "localusers",
247
			),
248
			"session_id" => "87ce2b3faeb92f0fb745645e7827f51a",
249
			"timestamp" => 1397193430.516,
250
			'conv_id' => 'dasdfwffws'
251
		));
252
		$join->execute();
253
254
		$expectedPushMessage1 = new PushMessage();
255
		$expectedPushMessage1->setSender('admin');
256
		$expectedPushMessage1->setReceiver('derp');
257
		$expectedPushMessage1->setReceiverSessionId('session-id-2');
258
		$expectedPushMessage1->setCommand(json_encode(array(
259
			"type" => "joined",
260
			"data" => array(
261
				"conv_id" => 'dasdfwffws',
262
				"messages" => array(),
263
				"users" => array(
264
					0 => array (
265
						'id' => 'derp',
266
						'online' => false,
267
						'displayname' => 'derp',
268
						'backends' => array (
269
							'och' => array (
270
								'id' => NULL,
271
								'displayname' => 'ownCloud Handle',
272
								'protocol' => 'x-owncloud-handle',
273
								'namespace' => 'och',
274
								'value' => 'derp',
275
							),
276
						),
277
						'address_book_id' => 'admin',
278
						'address_book_backend' => 'localusers',
279
					),
280
					1 => array (
281
						'id' => 'admin',
282
						'online' => true,
283
						'displayname' => 'admin',
284
						'backends' => array (
285
							'och' => array (
286
								'id' => NULL,
287
								'displayname' => 'ownCloud Handle',
288
								'protocol' => 'x-owncloud-handle',
289
								'namespace' => 'och',
290
								'value' => 'admin',
291
							),
292
						),
293
						'address_book_id' => 'admin',
294
						'address_book_backend' => 'localusers',
295
					),
296
					2 => array (
297
						'id' => 'foo',
298
						'online' => true,
299
						'displayname' => 'foo',
300
						'backends' => array (
301
							'och' => array (
302
								'id' => NULL,
303
								'displayname' => 'ownCloud Handle',
304
								'protocol' => 'x-owncloud-handle',
305
								'namespace' => 'och',
306
								'value' => 'foo',
307
							),
308
						),
309
						'address_book_id' => 'admin',
310
						'address_book_backend' => 'localusers',
311
					),
312
				)
313
			)
314
		)));
315
316
		$expectedPushMessage2 = new PushMessage();
317
		$expectedPushMessage2->setSender('admin');
318
		$expectedPushMessage2->setReceiver('admin');
319
		$expectedPushMessage2->setReceiverSessionId('session-id-1');
320
		$expectedPushMessage2->setCommand(json_encode(array(
321
			"type" => "joined",
322
			"data" => array(
323
				"conv_id" => 'dasdfwffws',
324
				"messages" => array(),
325
				"users" => array(
326
					0 => array (
327
						'id' => 'derp',
328
						'online' => false,
329
						'displayname' => 'derp',
330
						'backends' => array (
331
							'och' => array (
332
								'id' => NULL,
333
								'displayname' => 'ownCloud Handle',
334
								'protocol' => 'x-owncloud-handle',
335
								'namespace' => 'och',
336
								'value' => 'derp',
337
							),
338
						),
339
						'address_book_id' => 'admin',
340
						'address_book_backend' => 'localusers',
341
					),
342
					1 => array (
343
						'id' => 'admin',
344
						'online' => true,
345
						'displayname' => 'admin',
346
						'backends' => array (
347
							'och' => array (
348
								'id' => NULL,
349
								'displayname' => 'ownCloud Handle',
350
								'protocol' => 'x-owncloud-handle',
351
								'namespace' => 'och',
352
								'value' => 'admin',
353
							),
354
						),
355
						'address_book_id' => 'admin',
356
						'address_book_backend' => 'localusers',
357
					),
358
					2 => array (
359
						'id' => 'foo',
360
						'online' => true,
361
						'displayname' => 'foo',
362
						'backends' => array (
363
							'och' => array (
364
								'id' => NULL,
365
								'displayname' => 'ownCloud Handle',
366
								'protocol' => 'x-owncloud-handle',
367
								'namespace' => 'och',
368
								'value' => 'foo',
369
							),
370
						),
371
						'address_book_id' => 'admin',
372
						'address_book_backend' => 'localusers',
373
					),
374
				)
375
			)
376
		)));
377
378
		$this->assertEquals(array($expectedPushMessage1,$expectedPushMessage2), JoinTest::$pushMessages);
379
380
	}
381
382
	public function testInsertNoPushMessagesNoGroupConv(){
383
		$this->container['InitConvMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\InitConvMapper')
384
			->disableOriginalConstructor()
385
			->getMock();
386
		$this->container['InitConvMapper']->expects($this->any())
387
			->method('insertUnique')
388
			->will($this->returnValue(true));
389
390
		$this->container['GetUsersData'] = $this->getMockBuilder('\OCA\Chat\OCH\Data\GetUsers')
391
			->disableOriginalConstructor()
392
			->getMock();
393
		$this->container['GetUsersData']->expects($this->any())
394
			->method('execute')
395
			->will($this->returnValue(array("users" => array(
396
				0 => array (
397
					'id' => 'derp',
398
					'online' => false,
399
					'displayname' => 'derp',
400
					'backends' => array (
401
						'och' => array (
402
							'id' => NULL,
403
							'displayname' => 'ownCloud Handle',
404
							'protocol' => 'x-owncloud-handle',
405
							'namespace' => 'och',
406
							'value' => 'derp',
407
						),
408
					),
409
					'address_book_id' => 'admin',
410
					'address_book_backend' => 'localusers',
411
				),
412
				1 => array (
413
					'id' => 'admin',
414
					'online' => true,
415
					'displayname' => 'admin',
416
					'backends' => array (
417
						'och' => array (
418
							'id' => NULL,
419
							'displayname' => 'ownCloud Handle',
420
							'protocol' => 'x-owncloud-handle',
421
							'namespace' => 'och',
422
							'value' => 'admin',
423
						),
424
					),
425
					'address_book_id' => 'admin',
426
					'address_book_backend' => 'localusers',
427
				),
428
			))));
429
430
		$this->container['MessagesData'] = $this->getMockBuilder('\OCA\Chat\OCH\Data\Messages')
431
			->disableOriginalConstructor()
432
			->getMock();
433
		$this->container['MessagesData']->expects($this->any())
434
			->method('execute')
435
			->will($this->returnValue(array("messages" => array())));
436
437
		$this->container['UserMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\UserMapper')
438
			->disableOriginalConstructor()
439
			->getMock();
440
441
		$derpSession = new User();
442
		$derpSession->setUser('derp');
443
		$derpSession->setSessionId('session-id-2');
444
445
		$adminSession = new User();
446
		$adminSession->setUser('admin');
447
		$adminSession->setSessionId('session-id-1');
448
449
		$this->container['UserMapper']->expects($this->any())
450
			->method('findSessionsByConversation')
451
			->will($this->returnValue(array($derpSession, $adminSession)));
452
453
		$this->container['PushMessageMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\PushMessageMapper')
454
			->disableOriginalConstructor()
455
			->getMock();
456
		$this->container['PushMessageMapper']->expects($this->any())
457
			->method('insert')
458
			->will($this->returnCallback(function($pushMessage){
459
				JoinTest::$pushMessageNoGroup[] = $pushMessage;
460
			}));
461
462
		$expectedInitConv =  new InitConv();
463
		$expectedInitConv->setConvId('dasdfwffws');
464
		$expectedInitConv->setUser('admin');
465
466
		$join = new Join($this->container);
0 ignored issues
show
Bug introduced by
The call to Join::__construct() misses some required arguments starting with $getUsers.
Loading history...
Documentation introduced by
$this->container is of type array<string,?,{"PushMessageMapper":"?"}>, but the function expects a object<OCA\Chat\OCH\Db\PushMessageMapper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
467
		$join->setRequestData(array(
468
			"user" => array(
469
				"id" => "admin",
470
				"displayname"=> "admin",
471
				"backends" => array(
472
					"och" => array(
473
						"id" => NULL,
474
						"displayname" => "wnCloud Handle",
475
						"protocol" => "x-owncloud-handle",
476
						"namespace" => "och",
477
						"value" => "admin"
478
					)
479
				),
480
				"address_book_id" => "admin",
481
				"address_book_backend"=> "localusers",
482
			),
483
			"session_id" => "87ce2b3faeb92f0fb745645e7827f51a",
484
			"timestamp" => 1397193430.516,
485
			'conv_id' => 'dasdfwffws'
486
		));
487
		$join->execute();
488
489
		$this->assertEquals(null, JoinTest::$pushMessageNoGroup);
490
	}
491
}
492