SendChatMsgTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
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 24 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\SendChatMsg;
16
use OCA\Chat\App\Chat;
17
use OCA\Chat\Db\DBException;
18
use OCA\Chat\OCH\Exceptions\RequestDataInvalid;
19
use OCA\Chat\OCH\Db\UserOnline;
20
use OCA\Chat\OCH\Db\User;
21
use OCA\Chat\OCH\Db\PushMessage;
22
23
// DONE
24
class SendChatMsgTest extends \PHPUnit_Framework_TestCase {
25
26
	public static $command;
27
	public static $pushMessageCount;
28
	
29
	public function setUp(){
30
		$app =  new Chat();
0 ignored issues
show
Bug introduced by
The call to Chat::__construct() misses some required arguments starting with $backendManager.
Loading history...
31
		$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...
32
		$this->container['API'] = $this->getMockBuilder('\OCA\Chat\Core\API')
33
			->disableOriginalConstructor()
34
			->getMock();
35
		$this->container['API']->expects($this->any())
36
			->method('log')
37
			->will($this->returnValue(null));
38
	}
39
40
	public function testOmmittedConvId(){
41
		$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'CONV-ID-MUST-BE-PROVIDED');
42
43
		$this->container['API']->expects($this->any())
44
			->method('prepareQuery')
45
			->will($this->returnValue(true));
46
47
		// logic
48
		$sendChatMsg = new SendChatMsg($this->container);
0 ignored issues
show
Bug introduced by
The call to SendChatMsg::__construct() misses a required argument $messageMapper.

This check looks for function calls that miss required arguments.

Loading history...
49
		$sendChatMsg->setRequestData(array(
50
			'user' => array (
51
				'id' => 'admin',
52
				'online' => false,
53
				'displayname' => 'admin',
54
				'backends' => array (
55
					'och' => array (
56
						'id' => NULL,
57
						'displayname' => 'ownCloud Handle',
58
						'protocol' => 'x-owncloud-handle',
59
						'namespace' => 'och',
60
						'value' => 'admin',
61
					),
62
				),
63
				'address_book_id' => 'admin',
64
				'address_book_backend' => 'localusers',
65
			),
66
			'session_id' => 'c08809598b01894c468873fab54291aa',
67
			'timestamp' => 1397328934.658,
68
			'chat_msg' => "TestMsg",
69
			//'conv_id' => 'addeimnpr',	
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
		));
71
	}
72
	
73
	public function testOmmittedChatMsg(){
74
		$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'CHAT-MSG-MUST-BE-PROVIDED');
75
	
76
		$this->container['API']->expects($this->any())
77
			->method('prepareQuery')
78
			->will($this->returnValue(true));
79
	
80
		// logic
81
		$sendChatMsg = new SendChatMsg($this->container);
0 ignored issues
show
Bug introduced by
The call to SendChatMsg::__construct() misses a required argument $messageMapper.

This check looks for function calls that miss required arguments.

Loading history...
82
		$sendChatMsg->setRequestData(array(
83
			'user' => array (
84
				'id' => 'admin',
85
				'online' => false,
86
				'displayname' => 'admin',
87
				'backends' => array (
88
					'och' => array (
89
						'id' => NULL,
90
						'displayname' => 'ownCloud Handle',
91
						'protocol' => 'x-owncloud-handle',
92
						'namespace' => 'och',
93
						'value' => 'admin',
94
					),
95
				),
96
				'address_book_id' => 'admin',
97
				'address_book_backend' => 'localusers',
98
			),
99
			'session_id' => 'c08809598b01894c468873fab54291aa',
100
			'timestamp' => 1397328934.658,
101
			'conv_id' => 'addeimnpr',
102
			//'chat_msg' => ''
103
		));
104
	}
105
	
106
	public function testOmmittedTimestamp(){
107
		$this->setExpectedException('\OCA\Chat\OCH\Exceptions\RequestDataInvalid', 'TIMESTAMP-MUST-BE-PROVIDED');
108
	
109
		$this->container['API']->expects($this->any())
110
			->method('prepareQuery')
111
			->will($this->returnValue(true));
112
	
113
		// logic
114
		$sendChatMsg = new SendChatMsg($this->container);
0 ignored issues
show
Bug introduced by
The call to SendChatMsg::__construct() misses a required argument $messageMapper.

This check looks for function calls that miss required arguments.

Loading history...
115
		$sendChatMsg->setRequestData(array(
116
			'user' => array (
117
				'id' => 'admin',
118
				'online' => false,
119
				'displayname' => 'admin',
120
				'backends' => array (
121
					'och' => array (
122
						'id' => NULL,
123
						'displayname' => 'ownCloud Handle',
124
						'protocol' => 'x-owncloud-handle',
125
						'namespace' => 'och',
126
						'value' => 'admin',
127
					),
128
				),
129
				'address_book_id' => 'admin',
130
				'address_book_backend' => 'localusers',
131
			),
132
			'session_id' => 'c08809598b01894c468873fab54291aa',
133
			//'timestamp' => 1397328934.658,
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
134
			'conv_id' => 'addeimnpr',
135
			'chat_msg' => 'test'
136
		));
137
		
138
	}
139
	
140
	// Test if the created command in sendchatmsg.php is okay and is only created for recevier !== sender
141
	public function testCreatedCommand(){
142
		
143
		$this->container['PushMessageMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\PushMessageMapper')
144
			->disableOriginalConstructor()
145
			->getMock();
146
		
147
		$this->container['PushMessageMapper']->expects($this->any())
148
			->method('insert')
149
			->will($this->returnCallback(function($pushMessage){
150
				SendChatMsgTest::$command = $pushMessage->getCommand();
151
				SendChatMsgTest::$pushMessageCount++;
152
			}));
153
			
154
			
155
		$user1 = new User(); // This is a receiver
156
		$user1->setConversationId('addeimnpr');
157
		$user1->setUser('derp');
158
		$user1->setSessionId('c08809598b01894c4asdfasdf68873fab54291aa');
159
		
160
		$user2 = new User(); // This is a sender but is also stored in the DB -> no need to create a pushmessage
161
		$user2->setConversationId('addeimnpr');
162
		$user2->setUser('admin');
163
		$user2->setSessionId('c08809598b01894c468873fab54291aa');
164
		
165
		$this->container['UserMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\UserMapper')
166
			->disableOriginalConstructor()
167
			->getMock();
168
		
169
		$this->container['UserMapper']->expects($this->any())
170
			->method('findSessionsByConversation')
171
			->will($this->returnValue(array($user1, $user2)));
172
173
	
174
		$this->container['messageMapper'] = $this->getMockBuilder('\OCA\Chat\OCH\Db\MessageMapper')
175
			->disableOriginalConstructor()
176
			->getMock();
177
		
178
		$sendChatMsg = new SendChatMsg($this->container);
0 ignored issues
show
Bug introduced by
The call to SendChatMsg::__construct() misses a required argument $messageMapper.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
$this->container is of type array<string,?,{"messageMapper":"?"}>, 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...
179
		$sendChatMsg->setRequestData(array(
180
			'user' => array (
181
				'id' => 'admin',
182
				'online' => false,
183
				'displayname' => 'admin',
184
				'backends' => array (
185
					'och' => array (
186
						'id' => NULL,
187
						'displayname' => 'ownCloud Handle',
188
						'protocol' => 'x-owncloud-handle',
189
						'namespace' => 'och',
190
						'value' => 'admin',
191
					),
192
				),
193
				'address_book_id' => 'admin',
194
				'address_book_backend' => 'localusers',
195
			),
196
			'session_id' => 'c08809598b01894c468873fab54291aa',
197
			'timestamp' => 1397328934.658,
198
			'conv_id' => 'addeimnpr',
199
			'chat_msg' => 'test'
200
		));
201
		$sendChatMsg->execute();
202
203
		$expectedCommand = json_encode(array(
204
            'type' => 'send_chat_msg',
205
            'data' => array(
206
                'user' => array (
207
					'id' => 'admin',
208
					'online' => false,
209
					'displayname' => 'admin',
210
					'backends' => array (
211
						'och' => array (
212
							'id' => NULL,
213
							'displayname' => 'ownCloud Handle',
214
							'protocol' => 'x-owncloud-handle',
215
							'namespace' => 'och',
216
							'value' => 'admin',
217
						),
218
					),
219
					'address_book_id' => 'admin',
220
					'address_book_backend' => 'localusers',
221
				),
222
                'conv_id' => 'addeimnpr',
223
                'timestamp' => 1397328934.658, 
224
                'chat_msg' => 'test'
225
            )
226
        ));	
227
		
228
		$this->assertEquals($expectedCommand, SendChatMsgTest::$command);
229
		$this->assertEquals(1, SendChatMsgTest::$pushMessageCount);
230
		
231
		
232
	}
233
	
234
}