TestAll   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 321
Duplicated Lines 12.46 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 40
loc 321
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 7

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testAuthRegister() 18 18 1
A testAuthRegister2() 22 22 1
A testCompose() 0 52 1
A testSearch() 0 37 1
A testReply() 0 51 1
A testSearchWithTotalMessages() 0 23 1
A testSidebar() 0 43 1
A test_EmptyDB() 0 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
33
    {
34
35
        $output = $this->obRegister->authRegister(
36
            [
0 ignored issues
show
Documentation introduced by
array('name' => 'Test', ...Register' => 'testing') is of type array<string,string,{"na...assRegister":"string"}>, but the function expects a object.

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...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
56
    {
57
        $output = $this->obRegister->authRegister(
58
            [
0 ignored issues
show
Documentation introduced by
array('name' => 'Test2',...Register' => 'testing') is of type array<string,string,{"na...assRegister":"string"}>, but the function expects a object.

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...
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');
0 ignored issues
show
Unused Code introduced by
\ChatApp\Session::forget('start'); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
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([]);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a object.

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...
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