|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Containers\User\UI\API\Tests\Functional; |
|
4
|
|
|
|
|
5
|
|
|
use App\Port\Tests\PHPUnit\Abstracts\TestCase; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class UpdateUserTest. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Mahmoud Zalt <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class UpdateUserTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
private $endpoint = '/users'; |
|
16
|
|
|
|
|
17
|
|
|
public function testUpdateExistingUser_() |
|
18
|
|
|
{ |
|
19
|
|
|
$user = $this->getLoggedInTestingUser(); |
|
20
|
|
|
|
|
21
|
|
|
$data = [ |
|
22
|
|
|
'name' => 'Updated Name', |
|
23
|
|
|
'password' => 'updated#Password', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
$endpoint = $this->endpoint . '/' . $user->id; |
|
27
|
|
|
|
|
28
|
|
|
// send the HTTP request |
|
29
|
|
|
$response = $this->apiCall($endpoint, 'put', $data); |
|
30
|
|
|
|
|
31
|
|
|
// assert response status is correct |
|
32
|
|
|
$this->assertEquals($response->getStatusCode(), '200'); |
|
33
|
|
|
|
|
34
|
|
|
// assert returned user is the updated one |
|
35
|
|
|
$this->assertResponseContainKeyValue([ |
|
36
|
|
|
'email' => $user->email, |
|
37
|
|
|
'name' => $data['name'], |
|
38
|
|
|
], $response); |
|
39
|
|
|
|
|
40
|
|
|
// assert data was updated in the database |
|
41
|
|
|
$this->seeInDatabase('users', ['name' => $data['name']]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
View Code Duplication |
public function testUpdateExistingUserWithEmptyValues() |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$user = $this->getLoggedInTestingUser(); |
|
47
|
|
|
|
|
48
|
|
|
$data = []; // empty data |
|
49
|
|
|
|
|
50
|
|
|
$endpoint = $this->endpoint . '/' . $user->id; |
|
51
|
|
|
|
|
52
|
|
|
// send the HTTP request |
|
53
|
|
|
$response = $this->apiCall($endpoint, 'put', $data); |
|
54
|
|
|
|
|
55
|
|
|
// assert response status is correct |
|
56
|
|
|
$this->assertEquals($response->getStatusCode(), '417'); |
|
57
|
|
|
|
|
58
|
|
|
// assert message is correct |
|
59
|
|
|
$this->assertResponseContainKeyValue([ |
|
60
|
|
|
'message' => 'All inputs are empty.', |
|
61
|
|
|
], $response); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// TODO: after upgrading to Laravel 5.2 this function started returning 500 instead of 403 |
|
65
|
|
|
// it could be due to something in `app/Port/Exception/Handler/ExceptionsHandler.php` and the don't report thingy |
|
66
|
|
|
// same problem as testDeleteDifferentUser |
|
67
|
|
|
|
|
68
|
|
|
// public function testUpdateDifferentUser_() |
|
|
|
|
|
|
69
|
|
|
// { |
|
70
|
|
|
// $data = [ |
|
71
|
|
|
// 'name' => 'Updated Name', |
|
72
|
|
|
// 'password' => 'updated#Password', |
|
73
|
|
|
// ]; |
|
74
|
|
|
// |
|
75
|
|
|
// $endpoint = $this->endpoint . '/' . 100; // amy ID |
|
76
|
|
|
// |
|
77
|
|
|
// // send the HTTP request |
|
78
|
|
|
// $response = $this->apiCall($endpoint, 'put', $data); |
|
79
|
|
|
// |
|
80
|
|
|
// // assert response status is correct |
|
81
|
|
|
// $this->assertEquals($response->getStatusCode(), '403'); |
|
82
|
|
|
// |
|
83
|
|
|
// // assert the message means (not allowed to proceed with the request) |
|
84
|
|
|
// $this->assertEquals($response->getContent(), 'Forbidden'); |
|
85
|
|
|
// } |
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
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.