Completed
Push — testing ( b9c750...814547 )
by Ankit
03:44
created

TestUser::test_EmptyDB()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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

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