Completed
Push — master ( af1ef9...5e0cf4 )
by Ankit
03:41
created

TestAll::testReply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 48
rs 9.125
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 17 and the first side effect is on line 13.

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
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
12
use Dotenv\Dotenv;
13
$dotenv = new Dotenv(dirname(__DIR__));
14
$dotenv->load();
15
session_start();
16
17
class TestAll
18
extends
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
19
    PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "PHPUnit_Framework_TestCase"; 4 found
Loading history...
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
    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
    }
49
50
    /**
51
    * @depends testAuthRegister
52
    *  Register User2
53
    */
54 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...
55
    {
56
        $output = $this->obRegister->authRegister(
57
            [
58
                "name" => 'Test2',
59
                "email" => '[email protected]',
60
                "username" => 'test2',
61
                "mob" => '1234567890',
62
                "passRegister" => 'testing'
63
            ]
64
        );
65
66
        $output = (array)json_decode($output);
67
        $this->assertEquals([
68
            'location' => 'http://127.0.0.1/openchat/views/account.php'
69
            ], $output);
70
71
        $userId = Session::get('start');
72
        return $userId;
73
    }
74
75
    /**
76
    * @depends testAuthRegister2
77
    *  Testing for the register with empty username
78
    */
79
    public function testCompose()
80
    {
81
        $expectedOutput = ['location' => 'http://127.0.0.1/openchat/views/account.php'];
82
        $outputEmail = $this->obLogin->authLogin(
83
            [
84
                "login" => '[email protected]',
85
                "passLogin" => 'testing'
86
            ]
87
        );
88
        $outputEmail = (array)json_decode($outputEmail);
89
        $this->assertEquals($expectedOutput, $outputEmail);
90
91
92
        $expectedOutput = [
93
            "Compose" => [
94
                "0" => (object)[
95
                    "login_id" => "2",
96
                    "name" => "Test2",
97
                    "email" => "[email protected]",
98
                    "username" => "test2",
99
                    "mobile" => "1234567890",
100
                    "login_status" => "0"
101
                ]
102
            ]
103
        ];
104
105
106
        $sessionId = session_id();
107
        $compose = new Compose($sessionId);
108
109
        // Matched not found
110
        $output = $compose->selectUser((object)["value" => "ank"]);
111
        $output = (array)json_decode($output);
112
        $this->assertEquals(["Compose" => "Not Found"], $output);
113
114
        // For suggestion matched
115
        $output = $compose->selectUser((object)["value" => "t"]);
116
        $output = (array)json_decode($output);
117
        $this->assertEquals($expectedOutput, $output);
118
119
        // Not Found
120
        $output = $compose->selectUser((object)["value" => ""]);
121
        $output = (array)json_decode($output);
122
        $this->assertEquals(["Compose" => "Not Found"], $output);
123
124
        // Query Failed
125
        $output = $compose->selectUser((object)["value" => "'"]);
126
        $output = (array)json_decode($output);
127
        $this->assertEquals(["Compose" => "Query Failed"], $output);
128
129
    }
130
131
    /**
132
    * @depends testAuthRegister2
133
    *  Testing for Search Class
134
    */
135
    public function testSearch($userId)
0 ignored issues
show
Unused Code introduced by
The parameter $userId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
    {
137
        $expectedOutput = ['location' => 'http://127.0.0.1/openchat/views/account.php'];
138
        $outputEmail = $this->obLogin->authLogin(
139
            [
140
                "login" => '[email protected]',
141
                "passLogin" => 'testing'
142
            ]
143
        );
144
        $outputEmail = (array)json_decode($outputEmail);
145
        $this->assertEquals($expectedOutput, $outputEmail);
146
147
        $sessionId = session_id();
148
        $search = new Search($sessionId);
149
150
        // Matched not found
151
        $output = $search->searchItem((object)["value" => "ank"]);
152
        $output = (array)json_decode($output);
153
        $this->assertEquals(["Search" => "Not Found"], $output);
154
155
        // For suggestion matched but not in total messages
156
        $output = $search->searchItem((object)["value" => "T"]);
157
        $output = (array)json_decode($output);
158
        $this->assertEquals(["Search" => "Not Found"], $output);
159
160
        // Not Found
161
        $output = $search->searchItem((object)["value" => ""]);
162
        $output = (array)json_decode($output);
163
        $this->assertEquals(["Search" => "Not Found"], $output);
164
165
        // Query Failed
166
        $output = $search->searchItem((object)["value" => "'"]);
167
        $output = (array)json_decode($output);
168
        $this->assertEquals(["Search" => "Not Found"], $output);
169
170
    }
171
172
    /**
173
    * @depends testAuthRegister2
174
    *  Testing for Reply Class
175
    */
176
    public function testReply($userId)
177
    {
178
        $msg = [
179
            "name" => $userId,
180
            "reply" => [
181
                0 => "Hello World"
182
            ]
183
        ];
184
185
        $msg = json_encode($msg);
186
187
        $expectedOutput = ['location' => 'http://127.0.0.1/openchat/views/account.php'];
188
        $outputEmail = $this->obLogin->authLogin(
189
            [
190
                "login" => '[email protected]',
191
                "passLogin" => 'testing'
192
            ]
193
        );
194
        $outputEmail = (array)json_decode($outputEmail);
195
        $this->assertEquals($expectedOutput, $outputEmail);
196
197
198
        $sessionId = session_id();
199
        $obReply = new Reply($sessionId);
200
201
        $output = $obReply->replyTo($msg);
202
        $this->assertEquals("Messages is sent", $output);
203
204
        $output = $obReply->replyTo(json_encode([]));
205
        $this->assertEquals("Failed", $output);
206
207
        $output = $obReply->replyTo(json_encode([
208
            "name" => -1,
209
            "reply" => [
210
                0 => "Hello World"
211
            ]
212
        ]));
213
        $this->assertEquals("Invalid Authentication", $output);
214
215
        $output = $obReply->replyTo(json_encode([
216
            "name" => $userId,
217
            "reply" => [
218
                0 => "Hello"
219
            ]
220
        ]));
221
        $this->assertEquals("Messages is sent", $output);
222
223
    }
224
225
226
    /**
227
    *   @depends testReply
228
    *  Empty the DB
229
    */
230
    public function test_EmptyDB()
231
    {
232
        $connect = mysqli_connect(
233
            getenv('DB_HOST'),
234
            getenv('DB_USER'),
235
            getenv('DB_PASSWORD'),
236
            getenv('DB_NAME')
237
        );
238
        $query = "TRUNCATE `login`";
239
        $this->assertTrue($connect->query($query));
240
        $query = "TRUNCATE `profile`";
241
        $this->assertTrue($connect->query($query));
242
        $query = "TRUNCATE `messages`";
243
        $this->assertTrue($connect->query($query));
244
        $query = "TRUNCATE `total_message`";
245
        $this->assertTrue($connect->query($query));
246
        $query = "TRUNCATE `register`";
247
        $this->assertTrue($connect->query($query));
248
    }
249
250
}
251