1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChatApp\Tests; |
4
|
|
|
use PHPUnit_Framework_TestCase; |
5
|
|
|
use ChatApp\Register; |
6
|
|
|
use ChatApp\Login; |
7
|
|
|
use ChatApp\Search; |
8
|
|
|
use ChatApp\Compose; |
9
|
|
|
use ChatApp\Reply; |
10
|
|
|
use ChatApp\Session; |
11
|
|
|
use ChatApp\SideBar; |
12
|
|
|
use Dotenv\Dotenv; |
13
|
|
|
$dotenv = new Dotenv(dirname(__DIR__)); |
14
|
|
|
$dotenv->load(); |
15
|
|
|
session_start(); |
16
|
|
|
|
17
|
|
|
class TestAll |
18
|
|
|
extends |
19
|
|
|
PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
protected $obRegister; |
23
|
|
|
protected $obLogin; |
24
|
|
|
|
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->obRegister = new Register(); |
28
|
|
|
$this->obLogin = new Login(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// Register User 1 |
32
|
|
View Code Duplication |
public function testAuthRegister() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
$output = $this->obRegister->authRegister( |
36
|
|
|
[ |
|
|
|
|
37
|
|
|
"name" => 'Test', |
38
|
|
|
"email" => '[email protected]', |
39
|
|
|
"username" => 'test', |
40
|
|
|
"mob" => '1234567890', |
41
|
|
|
"passRegister" => 'testing' |
42
|
|
|
] |
43
|
|
|
); |
44
|
|
|
$output = (array)json_decode($output); |
45
|
|
|
$this->assertEquals([ |
46
|
|
|
'location' => 'http://127.0.0.1/openchat/views/account.php' |
47
|
|
|
], $output); |
48
|
|
|
Session::forget('start'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @depends testAuthRegister |
53
|
|
|
* Register User2 |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function testAuthRegister2() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$output = $this->obRegister->authRegister( |
58
|
|
|
[ |
|
|
|
|
59
|
|
|
"name" => 'Test2', |
60
|
|
|
"email" => '[email protected]', |
61
|
|
|
"username" => 'test2', |
62
|
|
|
"mob" => '1234567890', |
63
|
|
|
"passRegister" => 'testing' |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$output = (array)json_decode($output); |
68
|
|
|
$this->assertEquals([ |
69
|
|
|
'location' => 'http://127.0.0.1/openchat/views/account.php' |
70
|
|
|
], $output); |
71
|
|
|
|
72
|
|
|
$userId = Session::get('start'); |
73
|
|
|
return $userId; |
74
|
|
|
Session::forget('start'); |
|
|
|
|
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @depends testAuthRegister2 |
80
|
|
|
* Testing for the register with empty username |
81
|
|
|
*/ |
82
|
|
|
public function testCompose() |
83
|
|
|
{ |
84
|
|
|
$expectedOutput = ['location' => 'http://127.0.0.1/openchat/views/account.php']; |
85
|
|
|
$outputEmail = $this->obLogin->authLogin( |
86
|
|
|
[ |
87
|
|
|
"login" => '[email protected]', |
88
|
|
|
"passLogin" => 'testing' |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
$outputEmail = (array)json_decode($outputEmail); |
92
|
|
|
$this->assertEquals($expectedOutput, $outputEmail); |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
$expectedOutput = [ |
96
|
|
|
"Compose" => [ |
97
|
|
|
"0" => (object)[ |
98
|
|
|
"login_id" => bin2hex(convert_uuencode(2)), |
99
|
|
|
"name" => "Test2", |
100
|
|
|
"email" => "[email protected]", |
101
|
|
|
"username" => "test2", |
102
|
|
|
"mobile" => "1234567890", |
103
|
|
|
"login_status" => "0" |
104
|
|
|
] |
105
|
|
|
] |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
$compose = new Compose(); |
110
|
|
|
$userId = Session::get('start'); |
111
|
|
|
|
112
|
|
|
// Matched not found |
113
|
|
|
$output = $compose->selectUser((object)["value" => "ank", "userId" => $userId]); |
114
|
|
|
$output = (array)json_decode($output); |
115
|
|
|
$this->assertEquals(["Compose" => "Not Found"], $output); |
116
|
|
|
|
117
|
|
|
// For suggestion matched |
118
|
|
|
$output = $compose->selectUser((object)["value" => "t", "userId" => $userId]); |
119
|
|
|
$output = (array)json_decode($output); |
120
|
|
|
$this->assertEquals($expectedOutput, $output); |
121
|
|
|
|
122
|
|
|
// Not Found |
123
|
|
|
$output = $compose->selectUser((object)["value" => "", "userId" => $userId]); |
124
|
|
|
$output = (array)json_decode($output); |
125
|
|
|
$this->assertEquals(["Compose" => "Not Found"], $output); |
126
|
|
|
|
127
|
|
|
// Query Failed |
128
|
|
|
$output = $compose->selectUser((object)["value" => "'", "userId" => $userId]); |
129
|
|
|
$output = (array)json_decode($output); |
130
|
|
|
$this->assertEquals(["Compose" => "Query Failed"], $output); |
131
|
|
|
Session::forget('start'); |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @depends testAuthRegister2 |
137
|
|
|
* Testing for Search Class |
138
|
|
|
*/ |
139
|
|
|
public function testSearch($userId) |
140
|
|
|
{ |
141
|
|
|
$expectedOutput = ['location' => 'http://127.0.0.1/openchat/views/account.php']; |
142
|
|
|
$outputEmail = $this->obLogin->authLogin( |
143
|
|
|
[ |
144
|
|
|
"login" => '[email protected]', |
145
|
|
|
"passLogin" => 'testing' |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
$outputEmail = (array)json_decode($outputEmail); |
149
|
|
|
$this->assertEquals($expectedOutput, $outputEmail); |
150
|
|
|
|
151
|
|
|
$userId = Session::get('start'); |
152
|
|
|
$search = new Search(); |
153
|
|
|
|
154
|
|
|
// Matched not found |
155
|
|
|
$output = $search->searchItem((object)["value" => "ank", "userId" => $userId]); |
156
|
|
|
$output = (array)json_decode($output); |
157
|
|
|
$this->assertEquals(["Search" => "Not Found"], $output); |
158
|
|
|
|
159
|
|
|
// For suggestion matched but not in total messages |
160
|
|
|
$output = $search->searchItem((object)["value" => "T", "userId" => $userId]); |
161
|
|
|
$output = (array)json_decode($output); |
162
|
|
|
$this->assertEquals(["Search" => "Not Found"], $output); |
163
|
|
|
|
164
|
|
|
// Not Found |
165
|
|
|
$output = $search->searchItem((object)["value" => "", "userId" => $userId]); |
166
|
|
|
$output = (array)json_decode($output); |
167
|
|
|
$this->assertEquals(["Search" => "Not Found"], $output); |
168
|
|
|
|
169
|
|
|
// Query Failed |
170
|
|
|
$output = $search->searchItem((object)["value" => "'", "userId" => $userId]); |
171
|
|
|
$output = (array)json_decode($output); |
172
|
|
|
$this->assertEquals(["Search" => "Not Found"], $output); |
173
|
|
|
Session::forget('start'); |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @depends testAuthRegister2 |
181
|
|
|
* Testing for Reply Class |
182
|
|
|
*/ |
183
|
|
|
public function testReply($userId) |
184
|
|
|
{ |
185
|
|
|
$expectedOutput = ['location' => 'http://127.0.0.1/openchat/views/account.php']; |
186
|
|
|
$outputEmail = $this->obLogin->authLogin( |
187
|
|
|
[ |
188
|
|
|
"login" => 'test', |
189
|
|
|
"passLogin" => 'testing' |
190
|
|
|
] |
191
|
|
|
); |
192
|
|
|
$outputEmail = (array)json_decode($outputEmail); |
193
|
|
|
$this->assertEquals($expectedOutput, $outputEmail); |
194
|
|
|
$currentId = Session::get('start'); |
195
|
|
|
Session::forget('start'); |
196
|
|
|
|
197
|
|
|
$msg = (object)[ |
198
|
|
|
"name" => $userId, |
199
|
|
|
"reply" => "Hello World", |
200
|
|
|
"userId" => $currentId |
201
|
|
|
]; |
202
|
|
|
|
203
|
|
|
$obReply = new Reply(); |
204
|
|
|
$output = $obReply->replyTo($msg); |
205
|
|
|
$this->assertEquals("Messages is sent\n", $output); |
206
|
|
|
|
207
|
|
|
$msg = (object)[ |
208
|
|
|
"name" => $currentId, |
209
|
|
|
"reply" => "Hello World", |
210
|
|
|
"userId" => $userId |
211
|
|
|
]; |
212
|
|
|
|
213
|
|
|
$obReply = new Reply(); |
214
|
|
|
$output = $obReply->replyTo($msg); |
215
|
|
|
$this->assertEquals("Messages is sent\n", $output); |
216
|
|
|
|
217
|
|
|
$output = $obReply->replyTo([]); |
|
|
|
|
218
|
|
|
$this->assertEquals("Failed", $output); |
219
|
|
|
|
220
|
|
|
$output = $obReply->replyTo((object)[ |
221
|
|
|
"name" => -1, |
222
|
|
|
"reply" => "Hello World", |
223
|
|
|
"userId" => $currentId |
224
|
|
|
]); |
225
|
|
|
$this->assertEquals("Invalid Authentication", $output); |
226
|
|
|
|
227
|
|
|
$output = $obReply->replyTo((object)[ |
228
|
|
|
"name" => $userId, |
229
|
|
|
"reply" => "Hello", |
230
|
|
|
"userId" => $currentId |
231
|
|
|
]); |
232
|
|
|
$this->assertEquals("Messages is sent\n", $output); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @depends testReply |
239
|
|
|
* Testing for Search Class |
240
|
|
|
*/ |
241
|
|
|
public function testSearchWithTotalMessages() |
242
|
|
|
{ |
243
|
|
|
$expectedOutput = ['location' => 'http://127.0.0.1/openchat/views/account.php']; |
244
|
|
|
$outputEmail = $this->obLogin->authLogin( |
245
|
|
|
[ |
246
|
|
|
"login" => 'test', |
247
|
|
|
"passLogin" => 'testing' |
248
|
|
|
] |
249
|
|
|
); |
250
|
|
|
$outputEmail = (array)json_decode($outputEmail); |
251
|
|
|
$this->assertEquals($expectedOutput, $outputEmail); |
252
|
|
|
|
253
|
|
|
$userId = Session::get('start'); |
254
|
|
|
$search = new Search(); |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
// For suggestion matched but not in total messages |
258
|
|
|
$output = $search->searchItem((object)["value" => "T", "userId" => $userId]); |
259
|
|
|
$output = json_decode($output); |
260
|
|
|
$this->assertEquals("test2", $output->Search[0]->username); |
261
|
|
|
Session::forget('start'); |
262
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Testing for Search Class |
267
|
|
|
*/ |
268
|
|
|
public function testSidebar() |
269
|
|
|
{ |
270
|
|
|
$expectedOutput = ['location' => 'http://127.0.0.1/openchat/views/account.php']; |
271
|
|
|
$outputEmail = $this->obLogin->authLogin( |
272
|
|
|
[ |
273
|
|
|
"login" => 'test', |
274
|
|
|
"passLogin" => 'testing' |
275
|
|
|
] |
276
|
|
|
); |
277
|
|
|
$outputEmail = (array)json_decode($outputEmail); |
278
|
|
|
$this->assertEquals($expectedOutput, $outputEmail); |
279
|
|
|
|
280
|
|
|
$userId = Session::get('start'); |
281
|
|
|
$sidebar = new SideBar(); |
282
|
|
|
|
283
|
|
|
|
284
|
|
|
// For suggestion matched but not in total messages |
285
|
|
|
$output = $sidebar->loadSideBar($userId); |
286
|
|
|
$output = json_decode($output)[0]; |
287
|
|
|
|
288
|
|
|
$this->assertEquals("test2", $output->username); |
289
|
|
|
$this->assertEquals("Test2", $output->name); |
290
|
|
|
$this->assertEquals(bin2hex(convert_uuencode(2)), $output->login_id); |
291
|
|
|
|
292
|
|
|
$sidebar = new SideBar(); |
293
|
|
|
$output = $sidebar->loadSideBar(''); |
294
|
|
|
$this->assertEquals("Invalid Authentication", $output); |
295
|
|
|
|
296
|
|
|
$output = $sidebar->loadSideBar('\\'); |
297
|
|
|
$this->assertEquals("Query Failed", $output); |
298
|
|
|
|
299
|
|
|
$output = $sidebar->loadSideBar("100"); |
300
|
|
|
$this->assertEquals("null", $output); |
301
|
|
|
|
302
|
|
|
$sidebar = new SideBar(); |
303
|
|
|
$output = $sidebar->loadSideBar(2); |
304
|
|
|
$output = json_decode($output)[0]; |
305
|
|
|
|
306
|
|
|
$this->assertEquals("test", $output->username); |
307
|
|
|
$this->assertEquals("Test", $output->name); |
308
|
|
|
$this->assertEquals(bin2hex(convert_uuencode(1)), $output->login_id); |
309
|
|
|
Session::forget('start'); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @depends testSidebar |
315
|
|
|
* Empty the DB |
316
|
|
|
*/ |
317
|
|
|
public function test_EmptyDB() |
318
|
|
|
{ |
319
|
|
|
$connect = mysqli_connect( |
320
|
|
|
getenv('DB_HOST'), |
321
|
|
|
getenv('DB_USER'), |
322
|
|
|
getenv('DB_PASSWORD'), |
323
|
|
|
getenv('DB_NAME') |
324
|
|
|
); |
325
|
|
|
$query = "TRUNCATE `login`"; |
326
|
|
|
$this->assertTrue($connect->query($query)); |
327
|
|
|
$query = "TRUNCATE `profile`"; |
328
|
|
|
$this->assertTrue($connect->query($query)); |
329
|
|
|
$query = "TRUNCATE `messages`"; |
330
|
|
|
$this->assertTrue($connect->query($query)); |
331
|
|
|
$query = "TRUNCATE `total_message`"; |
332
|
|
|
$this->assertTrue($connect->query($query)); |
333
|
|
|
$query = "TRUNCATE `register`"; |
334
|
|
|
$this->assertTrue($connect->query($query)); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
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.