UserTest   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 394
Duplicated Lines 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
eloc 197
c 10
b 0
f 0
dl 0
loc 394
rs 10
wmc 21

21 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetUserNotFound() 0 13 1
A testGetUsers() 0 13 1
A testGetUsersByPage() 0 14 1
A testGetUser() 0 13 1
A testCreateUser() 0 19 1
A testDeleteUser() 0 10 1
A testUpdateUser() 0 17 1
A testLoginUserFailed() 0 14 1
A testUpdateUserWithOutSendData() 0 12 1
A testCreateUserWithInvalidEmail() 0 14 1
A testUpdateUserPermissionsFailed() 0 15 1
A testLoginUser() 0 16 1
A testCreateUserWithEmailAlreadyExists() 0 14 1
A testCreateUserWithoutName() 0 12 1
A testDeleteUserPermissionsFailed() 0 11 1
A testGetUserCreated() 0 13 1
A testCreateUserWithInvalidName() 0 15 1
A testCreateUserWithoutEmail() 0 11 1
A testLoginWithoutPasswordField() 0 13 1
A testLoginWithoutEmailField() 0 13 1
A testUpdateUserWithInvalidData() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tests\integration;
6
7
class UserTest extends BaseTestCase
8
{
9
    private static int $id;
10
11
    /**
12
     * Test Get All Users.
13
     */
14
    public function testGetUsers(): void
15
    {
16
        $response = $this->runApp('GET', '/api/v1/users');
17
18
        $result = (string) $response->getBody();
19
20
        $this->assertEquals(200, $response->getStatusCode());
21
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
22
        $this->assertStringContainsString('success', $result);
23
        $this->assertStringContainsString('id', $result);
24
        $this->assertStringContainsString('name', $result);
25
        $this->assertStringContainsString('email', $result);
26
        $this->assertStringNotContainsString('error', $result);
27
    }
28
29
    /**
30
     * Test Get Users By Page.
31
     */
32
    public function testGetUsersByPage(): void
33
    {
34
        $response = $this->runApp('GET', '/api/v1/users?page=1&perPage=3');
35
36
        $result = (string) $response->getBody();
37
38
        $this->assertEquals(200, $response->getStatusCode());
39
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
40
        $this->assertStringContainsString('success', $result);
41
        $this->assertStringContainsString('pagination', $result);
42
        $this->assertStringContainsString('id', $result);
43
        $this->assertStringContainsString('name', $result);
44
        $this->assertStringContainsString('email', $result);
45
        $this->assertStringNotContainsString('error', $result);
46
    }
47
48
    /**
49
     * Test Get One User.
50
     */
51
    public function testGetUser(): void
52
    {
53
        $response = $this->runApp('GET', '/api/v1/users/8');
54
55
        $result = (string) $response->getBody();
56
57
        $this->assertEquals(200, $response->getStatusCode());
58
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
59
        $this->assertStringContainsString('success', $result);
60
        $this->assertStringContainsString('id', $result);
61
        $this->assertStringContainsString('name', $result);
62
        $this->assertStringContainsString('email', $result);
63
        $this->assertStringNotContainsString('error', $result);
64
    }
65
66
    /**
67
     * Test Get User Not Found.
68
     */
69
    public function testGetUserNotFound(): void
70
    {
71
        $response = $this->runApp('GET', '/api/v1/users/123456789');
72
73
        $result = (string) $response->getBody();
74
75
        $this->assertEquals(404, $response->getStatusCode());
76
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
77
        $this->assertStringNotContainsString('success', $result);
78
        $this->assertStringNotContainsString('id', $result);
79
        $this->assertStringNotContainsString('name', $result);
80
        $this->assertStringNotContainsString('email', $result);
81
        $this->assertStringContainsString('error', $result);
82
    }
83
84
    /**
85
     * Test Create User.
86
     */
87
    public function testCreateUser(): void
88
    {
89
        $response = $this->runApp(
90
            'POST',
91
            '/api/v1/users',
92
            ['name' => 'Esteban', 'email' => '[email protected]', 'password' => 'AnyPass1000']
93
        );
94
95
        $result = (string) $response->getBody();
96
97
        self::$id = json_decode($result)->message->id;
98
99
        $this->assertEquals(201, $response->getStatusCode());
100
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
101
        $this->assertStringContainsString('success', $result);
102
        $this->assertStringContainsString('id', $result);
103
        $this->assertStringContainsString('name', $result);
104
        $this->assertStringContainsString('email', $result);
105
        $this->assertStringNotContainsString('error', $result);
106
    }
107
108
    /**
109
     * Test Get User Created.
110
     */
111
    public function testGetUserCreated(): void
112
    {
113
        $response = $this->runApp('GET', '/api/v1/users/' . self::$id);
114
115
        $result = (string) $response->getBody();
116
117
        $this->assertEquals(200, $response->getStatusCode());
118
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
119
        $this->assertStringContainsString('success', $result);
120
        $this->assertStringContainsString('id', $result);
121
        $this->assertStringContainsString('name', $result);
122
        $this->assertStringContainsString('email', $result);
123
        $this->assertStringNotContainsString('error', $result);
124
    }
125
126
    /**
127
     * Test Create User Without Name.
128
     */
129
    public function testCreateUserWithoutName(): void
130
    {
131
        $response = $this->runApp('POST', '/api/v1/users');
132
133
        $result = (string) $response->getBody();
134
135
        $this->assertEquals(400, $response->getStatusCode());
136
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
137
        $this->assertStringNotContainsString('success', $result);
138
        $this->assertStringNotContainsString('id', $result);
139
        $this->assertStringNotContainsString('email', $result);
140
        $this->assertStringContainsString('error', $result);
141
    }
142
143
    /**
144
     * Test Create User Without Email.
145
     */
146
    public function testCreateUserWithoutEmail(): void
147
    {
148
        $response = $this->runApp('POST', '/api/v1/users', ['name' => 'z']);
149
150
        $result = (string) $response->getBody();
151
152
        $this->assertEquals(400, $response->getStatusCode());
153
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
154
        $this->assertStringNotContainsString('success', $result);
155
        $this->assertStringNotContainsString('id', $result);
156
        $this->assertStringContainsString('error', $result);
157
    }
158
159
    /**
160
     * Test Create User With Invalid Name.
161
     */
162
    public function testCreateUserWithInvalidName(): void
163
    {
164
        $response = $this->runApp(
165
            'POST',
166
            '/api/v1/users',
167
            ['name' => 'z', 'email' => '[email protected]']
168
        );
169
170
        $result = (string) $response->getBody();
171
172
        $this->assertEquals(400, $response->getStatusCode());
173
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
174
        $this->assertStringNotContainsString('success', $result);
175
        $this->assertStringNotContainsString('email', $result);
176
        $this->assertStringContainsString('error', $result);
177
    }
178
179
    /**
180
     * Test Create User With Invalid Email.
181
     */
182
    public function testCreateUserWithInvalidEmail(): void
183
    {
184
        $response = $this->runApp(
185
            'POST',
186
            '/api/v1/users',
187
            ['name' => 'Esteban', 'email' => 'email.incorrecto', 'password' => 'AnyPass1000']
188
        );
189
190
        $result = (string) $response->getBody();
191
192
        $this->assertEquals(400, $response->getStatusCode());
193
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
194
        $this->assertStringNotContainsString('success', $result);
195
        $this->assertStringContainsString('error', $result);
196
    }
197
198
    /**
199
     * Test Create User With An Email That Already Exists.
200
     */
201
    public function testCreateUserWithEmailAlreadyExists(): void
202
    {
203
        $response = $this->runApp(
204
            'POST',
205
            '/api/v1/users',
206
            ['name' => 'Esteban', 'email' => '[email protected]', 'password' => 'AnyPass1000']
207
        );
208
209
        $result = (string) $response->getBody();
210
211
        $this->assertEquals(400, $response->getStatusCode());
212
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
213
        $this->assertStringNotContainsString('success', $result);
214
        $this->assertStringContainsString('error', $result);
215
    }
216
217
    /**
218
     * Test Update User.
219
     */
220
    public function testUpdateUser(): void
221
    {
222
        $response0 = $this->runApp('POST', '/login', ['email' => '[email protected]', 'password' => 'AnyPass1000']);
223
        $result0 = (string) $response0->getBody();
224
        self::$jwt = json_decode($result0)->message->Authorization;
225
226
        $response = $this->runApp('PUT', '/api/v1/users/' . self::$id, ['name' => 'Stu', 'email' => '[email protected]']);
227
228
        $result = (string) $response->getBody();
229
230
        $this->assertEquals(200, $response->getStatusCode());
231
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
232
        $this->assertStringContainsString('success', $result);
233
        $this->assertStringContainsString('id', $result);
234
        $this->assertStringContainsString('name', $result);
235
        $this->assertStringContainsString('email', $result);
236
        $this->assertStringNotContainsString('error', $result);
237
    }
238
239
    /**
240
     * Test Update User Without Send Data.
241
     */
242
    public function testUpdateUserWithOutSendData(): void
243
    {
244
        $response = $this->runApp('PUT', '/api/v1/users/' . self::$id);
245
246
        $result = (string) $response->getBody();
247
248
        $this->assertEquals(400, $response->getStatusCode());
249
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
250
        $this->assertStringNotContainsString('success', $result);
251
        $this->assertStringNotContainsString('id', $result);
252
        $this->assertStringNotContainsString('email', $result);
253
        $this->assertStringContainsString('error', $result);
254
    }
255
256
    /**
257
     * Test Update User Permissions Failed.
258
     */
259
    public function testUpdateUserPermissionsFailed(): void
260
    {
261
        $response = $this->runApp(
262
            'PUT',
263
            '/api/v1/users/1',
264
            ['name' => 'Victor']
265
        );
266
267
        $result = (string) $response->getBody();
268
269
        $this->assertEquals(400, $response->getStatusCode());
270
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
271
        $this->assertStringNotContainsString('id', $result);
272
        $this->assertStringNotContainsString('name', $result);
273
        $this->assertStringContainsString('error', $result);
274
    }
275
276
    /**
277
     * Test Update User With Invalid Data.
278
     */
279
    public function testUpdateUserWithInvalidData(): void
280
    {
281
        $response = $this->runApp(
282
            'PUT',
283
            '/api/v1/users/' . self::$id,
284
            ['name' => '', 'email' => 'email-incorrecto...']
285
        );
286
287
        $result = (string) $response->getBody();
288
289
        $this->assertEquals(400, $response->getStatusCode());
290
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
291
        $this->assertStringNotContainsString('success', $result);
292
        $this->assertStringNotContainsString('email', $result);
293
        $this->assertStringContainsString('error', $result);
294
    }
295
296
    /**
297
     * Test Delete User.
298
     */
299
    public function testDeleteUser(): void
300
    {
301
        $response = $this->runApp('DELETE', '/api/v1/users/' . self::$id);
302
303
        $result = (string) $response->getBody();
304
305
        $this->assertEquals(204, $response->getStatusCode());
306
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
307
        $this->assertStringContainsString('success', $result);
308
        $this->assertStringNotContainsString('error', $result);
309
    }
310
311
    /**
312
     * Test Delete User Permissions Failed.
313
     */
314
    public function testDeleteUserPermissionsFailed(): void
315
    {
316
        $response = $this->runApp('DELETE', '/api/v1/users/123456789');
317
318
        $result = (string) $response->getBody();
319
320
        $this->assertEquals(400, $response->getStatusCode());
321
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
322
        $this->assertStringNotContainsString('success', $result);
323
        $this->assertStringNotContainsString('id', $result);
324
        $this->assertStringContainsString('error', $result);
325
    }
326
327
    /**
328
     * Test that user login endpoint it is working fine.
329
     */
330
    public function testLoginUser(): void
331
    {
332
        $response = $this->runApp('POST', '/login', ['email' => '[email protected]', 'password' => 'AnyPass1000']);
333
334
        $result = (string) $response->getBody();
335
336
        $this->assertEquals(200, $response->getStatusCode());
337
        $this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
338
        $this->assertStringContainsString('status', $result);
339
        $this->assertStringContainsString('success', $result);
340
        $this->assertStringContainsString('message', $result);
341
        $this->assertStringContainsString('Authorization', $result);
342
        $this->assertStringContainsString('Bearer', $result);
343
        $this->assertStringContainsString('ey', $result);
344
        $this->assertStringNotContainsString('error', $result);
345
        $this->assertStringNotContainsString('Failed', $result);
346
    }
347
348
    /**
349
     * Test login endpoint with invalid credentials.
350
     */
351
    public function testLoginUserFailed(): void
352
    {
353
        $response = $this->runApp('POST', '/login', ['email' => '[email protected]', 'password' => 'p']);
354
355
        $result = (string) $response->getBody();
356
357
        $this->assertEquals(400, $response->getStatusCode());
358
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
359
        $this->assertStringContainsString('Login failed', $result);
360
        $this->assertStringContainsString('Exception', $result);
361
        $this->assertStringContainsString('error', $result);
362
        $this->assertStringNotContainsString('success', $result);
363
        $this->assertStringNotContainsString('Authorization', $result);
364
        $this->assertStringNotContainsString('Bearer', $result);
365
    }
366
367
    /**
368
     * Test login endpoint without send required field email.
369
     */
370
    public function testLoginWithoutEmailField(): void
371
    {
372
        $response = $this->runApp('POST', '/login', ['password' => 'p']);
373
374
        $result = (string) $response->getBody();
375
376
        $this->assertEquals(400, $response->getStatusCode());
377
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
378
        $this->assertStringContainsString('Exception', $result);
379
        $this->assertStringContainsString('error', $result);
380
        $this->assertStringNotContainsString('success', $result);
381
        $this->assertStringNotContainsString('Authorization', $result);
382
        $this->assertStringNotContainsString('Bearer', $result);
383
    }
384
385
    /**
386
     * Test login endpoint without send required field password.
387
     */
388
    public function testLoginWithoutPasswordField(): void
389
    {
390
        $response = $this->runApp('POST', '/login', ['email' => '[email protected]']);
391
392
        $result = (string) $response->getBody();
393
394
        $this->assertEquals(400, $response->getStatusCode());
395
        $this->assertEquals('application/problem+json', $response->getHeaderLine('Content-Type'));
396
        $this->assertStringContainsString('Exception', $result);
397
        $this->assertStringContainsString('error', $result);
398
        $this->assertStringNotContainsString('success', $result);
399
        $this->assertStringNotContainsString('Authorization', $result);
400
        $this->assertStringNotContainsString('Bearer', $result);
401
    }
402
}
403