1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\User\UI\API\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use App\Containers\User\Tests\ApiTestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class UpdateUserTest. |
9
|
|
|
* |
10
|
|
|
* @group user |
11
|
|
|
* @group api |
12
|
|
|
* |
13
|
|
|
* @author Mahmoud Zalt <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class UpdateUserTest extends ApiTestCase |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
protected $endpoint = '[email protected]/users/{id}'; |
19
|
|
|
|
20
|
|
|
protected $access = [ |
21
|
|
|
'roles' => '', |
22
|
|
|
'permissions' => 'update-users', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @test |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public function testUpdateExistingUser_() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$user = $this->getTestingUser(); |
31
|
|
|
|
32
|
|
|
$data = [ |
33
|
|
|
'name' => 'Updated Name', |
34
|
|
|
'password' => 'updated#Password', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
// send the HTTP request |
38
|
|
|
$response = $this->injectId($user->id)->makeCall($data); |
39
|
|
|
|
40
|
|
|
// assert response status is correct |
41
|
|
|
$response->assertStatus(200); |
42
|
|
|
|
43
|
|
|
// assert returned user is the updated one |
44
|
|
|
$this->assertResponseContainKeyValue([ |
45
|
|
|
'object' => 'User', |
46
|
|
|
'email' => $user->email, |
47
|
|
|
'name' => $data['name'], |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
// assert data was updated in the database |
51
|
|
|
$this->assertDatabaseHas('users', ['name' => $data['name']]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @test |
56
|
|
|
*/ |
57
|
|
|
public function testUpdateNonExistingUser_() |
58
|
|
|
{ |
59
|
|
|
$data = [ |
60
|
|
|
'name' => 'Updated Name', |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
$fakeUserId = 7777; |
64
|
|
|
|
65
|
|
|
// send the HTTP request |
66
|
|
|
$response = $this->injectId($fakeUserId)->makeCall($data); |
67
|
|
|
|
68
|
|
|
// assert response status is correct |
69
|
|
|
$response->assertStatus(422); |
70
|
|
|
|
71
|
|
|
$this->assertResponseContainKeyValue([ |
72
|
|
|
'message' => 'The given data was invalid.' |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
*/ |
79
|
|
|
public function testUpdateExistingUserWithoutData_() |
80
|
|
|
{ |
81
|
|
|
// send the HTTP request |
82
|
|
|
$response = $this->makeCall(); |
83
|
|
|
|
84
|
|
|
// assert response status is correct |
85
|
|
|
$response->assertStatus(422); |
86
|
|
|
|
87
|
|
|
$this->assertResponseContainKeyValue([ |
88
|
|
|
'message' => 'The given data was invalid.' |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @test |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function testUpdateExistingUserWithEmptyValues() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$data = [ |
98
|
|
|
'name' => '', |
99
|
|
|
'password' => '', |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
// send the HTTP request |
103
|
|
|
$response = $this->makeCall($data); |
104
|
|
|
|
105
|
|
|
// assert response status is correct |
106
|
|
|
$response->assertStatus(422); |
107
|
|
|
|
108
|
|
|
$this->assertValidationErrorContain([ |
109
|
|
|
// messages should be updated after modifying the validation rules, to pass this test |
110
|
|
|
'password' => 'The password must be at least 6 characters.', |
111
|
|
|
'name' => 'The name must be at least 2 characters.', |
112
|
|
|
]); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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.