Completed
Push — testing ( 814547...b0cac2 )
by Ankit
03:05
created

TestUser::test_validateEmailInDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
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 15 and the first side effect is on line 12.

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
namespace ChatApp\Tests;
3
4
use PHPUnit_Framework_TestCase;
5
use ChatApp\Login;
6
use ChatApp\Register;
7
use ChatApp\Profile;
8
use ChatApp\Validate;
9
use ChatApp\Online;
10
use ChatApp\User;
11
use Dotenv\Dotenv;
12
$dotenv = new Dotenv(dirname(__DIR__));
13
$dotenv->load();
14
15
class TestUser
16
    extends
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
17
        PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "PHPUnit_Framework_TestCase"; 8 found
Loading history...
18
{
19
    protected $obRegister;
20
    protected $obLogin;
21
    protected $obUser;
22
    protected $obValidate;
23
24
25
    public function setUp()
26
    {
27
        $this->obRegister = new Register();
28
        $this->obLogin = new Login();
29
        $this->obUser = new User();
30
        $this->obValidate = new Validate();
31
    }
32
33
34
    public function test_authRegister()
35
    {
36
37
        $output = $this->obRegister->authRegister(
38
            [
39
                "name" => 'Test',
40
                "email" => '[email protected]',
41
                "username" => 'test',
42
                "mob" => '1234567890',
43
                "passRegister" => 'testing'
44
            ]
45
        );
46
        $output = (array)json_decode($output);
47
        $this->assertEquals([
48
            'location' => 'http://127.0.0.1/openchat/views/account.php'
49
            ], $output);
50
51
    }
52
53
    /**
54
    * @depends test_authRegister
55
    *  Testing for the login with correct credentials
56
    */
57
58
    public function test_authLogin()
59
    {
60
        $expectedOutput = ['location' => 'http://127.0.0.1/openchat/views/account.php'];
61
        $outputEmail = $this->obLogin->authLogin(
62
            [
63
                "login" => '[email protected]',
64
                "passLogin" => 'testing'
65
            ]
66
        );
67
        $outputEmail = (array)json_decode($outputEmail);
68
        $outputUsername = $this->obLogin->authLogin(
69
            [
70
                "login" => 'test',
71
                "passLogin" => 'testing'
72
            ]
73
        );
74
        $outputUsername = (array)json_decode($outputUsername);
75
        $this->assertEquals($expectedOutput, $outputEmail);
76
        $this->assertEquals($expectedOutput, $outputUsername);
77
    }
78
79
    /**
80
    * @depends test_authRegister
81
    *  Testing for the login with empty credentials
82
    */
83
84
    public function test_authLoginEmptyValues()
85
    {
86
        $output = $this->obLogin->authLogin(
87
            [
88
                "login" => '',
89
                "passLogin" => ''
90
            ]
91
        );
92
        $output = (array)json_decode($output, True);
93
        $expectedOutput = [
94
            [
95
                "key" => "login",
96
                "value" => " *Enter the login field"
97
            ],
98
            [
99
                "key" => "passLogin",
100
                "value" => " *Enter the password"
101
            ]
102
        ];
103
104
        $this->assertEquals($expectedOutput, $output);
105
    }
106
107
    /**
108
    * @depends test_authRegister
109
    *  Testing for the login with invalid or wrong email
110
    */
111
112 View Code Duplication
    public function test_authLoginWrongEmail()
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...
113
    {
114
        $output = $this->obLogin->authLogin(
115
            [
116
                "login" => '[email protected]',
117
                "passLogin" => 'egfb'
118
            ]
119
        );
120
        $output = (array)json_decode($output, True);
121
        $expectedOutput = [
122
            [
123
                "key" => "login",
124
                "value" => " *Enter correct Email address"
125
            ]
126
        ];
127
128
        $this->assertEquals($expectedOutput, $output);
129
    }
130
131
    /**
132
    * @depends test_authRegister
133
    *  Testing for the login with invalid email credentials
134
    */
135 View Code Duplication
    public function test_authLoginInvalidUsernameEmail()
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...
136
    {
137
        $output = $this->obLogin->authLogin(
138
            [
139
                "login" => 'invalid',
140
                "passLogin" => 'invalid'
141
            ]
142
        );
143
        $output = (array)json_decode($output, True);
144
        $expectedOutput = [
145
            [
146
                "key" => "login",
147
                "value" => " *Invalid username or email"
148
            ]
149
        ];
150
151
        $this->assertEquals($expectedOutput, $output);
152
    }
153
154
    /**
155
    * @depends test_authRegister
156
    *  Testing for the login with invalid password credentials
157
    */
158 View Code Duplication
    public function test_authLoginInvalidPassword()
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...
159
    {
160
        $output = $this->obLogin->authLogin(
161
            [
162
                "login" => 'test',
163
                "passLogin" => 'invalid'
164
            ]
165
        );
166
        $output = (array)json_decode($output, True);
167
        $expectedOutput = [
168
            [
169
                "key" => "passLogin",
170
                "value" => " *Invalid password"
171
            ]
172
        ];
173
        $this->assertEquals($expectedOutput, $output);
174
    }
175
176
    /**
177
    * @depends test_authRegister
178
    *  Testing for the Profile::class with valid login_id
179
    */
180
    public function test_getProfile()
181
    {
182
        $output = Profile::getProfile(1);
183
        $this->assertEquals([
184
            'login_id' => '1',
185
            'status' => 'Joined OpenChat',
186
            'education' => 'Joined OpenChat',
187
            'gender' => ''
188
        ], $output);
189
    }
190
191
    /**
192
    * @depends test_authRegister
193
    *  Testing for the Profile::class with invalid login_id
194
    */
195
    public function test_getProfileInvalidID()
196
    {
197
        $output = Profile::getProfile(0);
198
        $this->assertEquals(NULL, $output);
199
    }
200
201
    /**
202
    * @depends test_authRegister
203
    *  Testing for the User::class with valid login_id
204
    */
205
    public function test_userDetails()
206
    {
207
        $expectedOutput = [
208
            "login_id" => "1",
209
            "name" => "Test",
210
            "email" => "[email protected]",
211
            "username"=> "test",
212
            "mobile"=> "1234567890",
213
            "login_status"=> "0"
214
        ];
215
216
        $outputLoginId = $this->obUser->userDetails(1, True);
217
        $outputUsername = $this->obUser->userDetails('test', False);
218
        $this->assertEquals($expectedOutput, $outputLoginId);
219
        $this->assertEquals($expectedOutput, $outputUsername);
220
    }
221
222
    /**
223
    * @depends test_authRegister
224
    *  Testing for the User::class with invalid data
225
    */
226
    public function test_userDetailsInvalidID()
227
    {
228
        $output = $this->obUser->userDetails(0, True);
229
        $this->assertEquals(NULL, $output);
230
    }
231
232
    /**
233
    * @depends test_authRegister
234
    *  Testing for the Validate::class for email
235
    */
236
    public function test_validateEmailInDb()
237
    {
238
        $output = $this->obValidate->validateEmailInDb('[email protected]');
239
        $this->assertEquals(1, $output);
240
    }
241
242
    /**
243
    * @depends test_authRegister
244
    *  Testing for the Validate::class for username
245
    */
246
    public function test_validateUsernameInDb()
247
    {
248
        $output = $this->obValidate->validateUsernameInDb('test');
249
        $this->assertEquals(1, $output);
250
    }
251
252
    /**
253
    * @depends test_authRegister
254
    *  Testing for the Validate::class for non-existing username
255
    */
256
    public function test_validateUsernameInDbNot()
257
    {
258
        $output = $this->obValidate->validateUsernameInDb('abc');
259
        $this->assertEquals(0, $output);
260
    }
261
262
    /**
263
    * @depends test_authRegister
264
    *  Testing for the Validate::class for non-existing email
265
    */
266
    public function test_validateEmailInDbNot()
267
    {
268
        $output = $this->obValidate->validateEmailInDb('[email protected]');
269
        $this->assertEquals(0, $output);
270
    }
271
272
    /**
273
    * @depends test_authRegister
274
    *  Testing for the Online::class
275
    */
276
    public function test_Online()
277
    {
278
        Online::setOnlineStatus(1);
279
        $output = $this->obUser->userDetails(1, True);
280
        $output = $output['login_status'];
281
        $this->assertEquals("1", $output);
282
        Online::removeOnlineStatus(1);
283
        $output = $this->obUser->userDetails(1, True);
284
        $output = $output['login_status'];
285
        $this->assertEquals("0", $output);
286
    }
287
288
289
    /**
290
    *   @depends test_Online
291
    *  Empty the DB
292
    */
293
    public function test_EmptyDB()
294
    {
295
        $connect = mysqli_connect(
296
            getenv('DB_HOST'),
297
            getenv('DB_USER'),
298
            getenv('DB_PASSWORD'),
299
            getenv('DB_NAME')
300
        );
301
        $query = "TRUNCATE `login`";
302
        $this->assertTrue($connect->query($query));
303
        $query = "TRUNCATE `profile`";
304
        $this->assertTrue($connect->query($query));
305
        $query = "TRUNCATE `register`";
306
        $this->assertTrue($connect->query($query));
307
    }
308
}
309
310