1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Feature; |
4
|
|
|
|
5
|
|
|
use App\Models\Employee; |
6
|
|
|
use Tests\AuthenticatedTestCase; |
7
|
|
|
use Illuminate\Foundation\Testing\DatabaseMigrations; |
8
|
|
|
|
9
|
|
|
class EmployeeControllerTest extends AuthenticatedTestCase |
10
|
|
|
{ |
11
|
|
|
use DatabaseMigrations; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string $endpoint |
15
|
|
|
*/ |
16
|
|
|
private $endpoint = 'api/v1/backoffice/employee/'; |
17
|
|
|
|
18
|
|
|
public function testResourceShowMethodMustReturnAnEmployeeData() |
19
|
|
|
{ |
20
|
|
|
// We create 10 employees |
21
|
|
|
$employees = factory(Employee::class, 10)->create(); |
22
|
|
|
$first = $employees->first(); |
23
|
|
|
|
24
|
|
|
// When we request the first one |
25
|
|
|
$response = $this->json('GET', $this->endpoint.$first->id); |
26
|
|
|
|
27
|
|
|
// It should return a valid, one employee json |
28
|
|
|
$response |
29
|
|
|
->assertStatus(200) |
30
|
|
|
->assertJson($this->jsonEmployeeStructure($first)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testResourceIndexMethodMustReturnAnEmployeeList() |
34
|
|
|
{ |
35
|
|
|
// We create 10 employees |
36
|
|
|
factory(Employee::class, 10)->create(); |
37
|
|
|
|
38
|
|
|
// When we request the all of the employees |
39
|
|
|
$response = $this->json('GET', $this->endpoint); |
40
|
|
|
|
41
|
|
|
// It should return a valid, pagination employee json |
42
|
|
|
$response |
43
|
|
|
->assertStatus(200) |
44
|
|
|
->assertJsonStructure([ |
45
|
|
|
'current_page', |
46
|
|
|
'data' => [ |
47
|
|
|
'*' => [ |
48
|
|
|
'id', |
49
|
|
|
'full_name', |
50
|
|
|
'br_cpf', |
51
|
|
|
'email', |
52
|
|
|
'telephone_type', |
53
|
|
|
'telephone', |
54
|
|
|
'zip_code', |
55
|
|
|
'city', |
56
|
|
|
'state', |
57
|
|
|
'avenue', |
58
|
|
|
'number', |
59
|
|
|
'neighborhood', |
60
|
|
|
'complement', |
61
|
|
|
'active', |
62
|
|
|
'updated_at', |
63
|
|
|
'created_at', |
64
|
|
|
], |
65
|
|
|
], |
66
|
|
|
'first_page_url', |
67
|
|
|
'from', |
68
|
|
|
'last_page', |
69
|
|
|
'last_page_url', |
70
|
|
|
'next_page_url', |
71
|
|
|
'path', |
72
|
|
|
'per_page', |
73
|
|
|
'prev_page_url', |
74
|
|
|
'to', |
75
|
|
|
'total', |
76
|
|
|
]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testMustCreateANewEmployee() |
80
|
|
|
{ |
81
|
|
|
// We create one new employee and save it on the database |
82
|
|
|
$employee = factory(Employee::class)->create(); |
83
|
|
|
|
84
|
|
|
// We create one new employee on memory |
85
|
|
|
$newEmployee = factory(Employee::class)->make()->toArray(); |
86
|
|
|
|
87
|
|
|
// Find the last employee |
88
|
|
|
$findedEmployee = Employee::find($employee->id); |
89
|
|
|
$id = $findedEmployee ? $findedEmployee->id + 1 : 1; |
90
|
|
|
|
91
|
|
|
// When we save this new employee |
92
|
|
|
$newEmployee[ 'password' ] = 'teste123'; |
93
|
|
|
$newEmployee[ 'password_confirmation' ] = 'teste123'; |
94
|
|
|
$newEmployee[ 'active' ] = true; |
95
|
|
|
$response = $this->json('POST', $this->endpoint, $newEmployee); |
96
|
|
|
|
97
|
|
|
// It should return this valid json employee |
98
|
|
|
$response |
99
|
|
|
->assertStatus(201) |
100
|
|
|
->assertJson([ |
101
|
|
|
'id' => $id, |
102
|
|
|
'full_name' => $newEmployee[ 'full_name' ], |
103
|
|
|
'br_cpf' => $newEmployee[ 'br_cpf' ], |
104
|
|
|
'email' => $newEmployee[ 'email' ], |
105
|
|
|
'telephone_type' => $newEmployee[ 'telephone_type' ], |
106
|
|
|
'telephone' => $newEmployee[ 'telephone' ], |
107
|
|
|
'zip_code' => $newEmployee[ 'zip_code' ], |
108
|
|
|
'city' => $newEmployee[ 'city' ], |
109
|
|
|
'state' => $newEmployee[ 'state' ], |
110
|
|
|
'avenue' => $newEmployee[ 'avenue' ], |
111
|
|
|
'number' => $newEmployee[ 'number' ], |
112
|
|
|
'neighborhood' => $newEmployee[ 'neighborhood' ], |
113
|
|
|
'complement' => $newEmployee[ 'complement' ], |
114
|
|
|
'active' => $newEmployee[ 'active' ], |
115
|
|
|
]) |
116
|
|
|
->assertJsonStructure([ |
117
|
|
|
'updated_at', |
118
|
|
|
'created_at', |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testUpdateAnEmployee() |
123
|
|
|
{ |
124
|
|
|
// We create one new employee and save it on the database |
125
|
|
|
$employee = factory(Employee::class)->create(); |
126
|
|
|
|
127
|
|
|
// We create one new employee on memory |
128
|
|
|
$employeeModified = factory(Employee::class)->make()->toArray(); |
129
|
|
|
$employeeModified[ 'password' ] = 'teste123'; |
130
|
|
|
$employeeModified[ 'password_confirmation' ] = 'teste123'; |
131
|
|
|
|
132
|
|
|
// When we edit this employee |
133
|
|
|
$uri = $this->endpoint.$employee->id; |
134
|
|
|
$response = $this->json('PUT', $uri, $employeeModified); |
135
|
|
|
|
136
|
|
|
// And find the employee edited in the database |
137
|
|
|
$findedEmployee = Employee::find($employee->id)->toArray(); |
138
|
|
|
|
139
|
|
|
// It should return this valid json employee, with the full_name changed |
140
|
|
|
$response |
141
|
|
|
->assertStatus(200) |
142
|
|
|
->assertJsonFragment($findedEmployee); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testMustDeleteAnEmployee() |
146
|
|
|
{ |
147
|
|
|
// We create one new employee and save it on the database |
148
|
|
|
$employee = factory(Employee::class)->create(); |
149
|
|
|
|
150
|
|
|
// When we delete this employee |
151
|
|
|
$uri = $this->endpoint.$employee->id; |
152
|
|
|
$response = $this->json('DELETE', $uri); |
153
|
|
|
|
154
|
|
|
// And then try to find it |
155
|
|
|
$employeeRemove = Employee::find($employee->id); |
156
|
|
|
|
157
|
|
|
// It should return nothing (null) |
158
|
|
|
$response->assertStatus(204); |
159
|
|
|
$this->assertEquals(null, $employeeRemove); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testMustActiveAnEmployee() |
163
|
|
|
{ |
164
|
|
|
// We create one new employee and save it on the database |
165
|
|
|
$employee = factory(Employee::class)->create(); |
166
|
|
|
|
167
|
|
|
// This employee is inactive |
168
|
|
|
$employee->active = false; |
169
|
|
|
|
170
|
|
|
// When we change this employee to active |
171
|
|
|
$uri = $this->endpoint.'active/'.$employee->id; |
172
|
|
|
$response = $this->json('POST', $uri); |
173
|
|
|
|
174
|
|
|
// And then try to find it |
175
|
|
|
$employeeChanged = Employee::find($employee->id); |
176
|
|
|
|
177
|
|
|
// It should this valid json employee, with active = true |
178
|
|
|
$response |
179
|
|
|
->assertStatus(200) |
180
|
|
|
->assertJsonFragment([ |
181
|
|
|
'active' => true, |
182
|
|
|
]); |
183
|
|
|
|
184
|
|
|
$this->assertEquals(true, $employeeChanged->active); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testMustInactiveAnEmployee() |
188
|
|
|
{ |
189
|
|
|
// We create one new employee and save it on the database |
190
|
|
|
$employee = factory(Employee::class)->create(); |
191
|
|
|
|
192
|
|
|
// This employee is active |
193
|
|
|
$employee->active = true; |
194
|
|
|
|
195
|
|
|
// When we change this employee to inactive |
196
|
|
|
$uri = $this->endpoint.'inactive/'.$employee->id; |
197
|
|
|
$response = $this->json('POST', $uri); |
198
|
|
|
|
199
|
|
|
// And then try to find it |
200
|
|
|
$employeeChanged = Employee::find($employee->id); |
201
|
|
|
|
202
|
|
|
// It should this valid json employee, with active = false |
203
|
|
|
$response |
204
|
|
|
->assertStatus(200) |
205
|
|
|
->assertJsonFragment([ |
206
|
|
|
'active' => false, |
207
|
|
|
]); |
208
|
|
|
|
209
|
|
|
$this->assertEquals(false, $employeeChanged->active); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
private function jsonEmployeeStructure($employee) |
213
|
|
|
{ |
214
|
|
|
return [ |
215
|
|
|
'id' => $employee->id, |
216
|
|
|
'full_name' => $employee->full_name, |
217
|
|
|
'br_cpf' => $employee->br_cpf, |
218
|
|
|
'email' => $employee->email, |
219
|
|
|
'telephone_type' => $employee->telephone_type, |
220
|
|
|
'telephone' => $employee->telephone, |
221
|
|
|
'zip_code' => $employee->zip_code, |
222
|
|
|
'city' => $employee->city, |
223
|
|
|
'state' => $employee->state, |
224
|
|
|
'avenue' => $employee->avenue, |
225
|
|
|
'number' => $employee->number, |
226
|
|
|
'neighborhood' => $employee->neighborhood, |
227
|
|
|
'complement' => $employee->complement, |
228
|
|
|
'active' => $employee->active, |
229
|
|
|
'updated_at' => $employee->updated_at, |
230
|
|
|
'created_at' => $employee->created_at, |
231
|
|
|
]; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|