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