3
3
namespace Tests \Feature \Api \V1 ;
4
4
5
5
use App \User ;
6
+ use Illuminate \Http \UploadedFile ;
7
+ use Illuminate \Support \Facades \Storage ;
8
+ use Illuminate \Foundation \Testing \TestResponse ;
6
9
7
10
class UsersTest extends BaseTest
8
11
{
9
12
/** @test */
10
13
public function a_user_can_list_users ()
11
14
{
15
+ // The payload that should be sent alongside the request.
12
16
$ payload = array_merge ($ this ->getDefaultPayload (), []);
13
17
14
18
$ this ->get (route ('api.v1.users.index ' ), $ payload )->assertStatus (200 );
@@ -32,6 +36,7 @@ public function a_user_can_create_a_user()
32
36
'username ' => $ this ->faker ->userName ,
33
37
];
34
38
39
+ // The payload that should be sent alongside the request.
35
40
$ payload = array_merge ($ this ->getDefaultPayload (), []);
36
41
37
42
// Assuming that the user is created through the test data,
@@ -51,6 +56,7 @@ public function a_user_can_create_a_user()
51
56
/** @test */
52
57
public function a_user_can_view_a_user ()
53
58
{
59
+ // The payload that should be sent alongside the request.
54
60
$ payload = array_merge ($ this ->getDefaultPayload (), []);
55
61
56
62
// The user to be shown.
@@ -67,6 +73,7 @@ public function a_user_can_view_a_user()
67
73
/** @test */
68
74
public function a_user_can_update_a_user ()
69
75
{
76
+ // The payload that should be sent alongside the request.
70
77
$ payload = array_merge ($ this ->getDefaultPayload (), []);
71
78
72
79
// The user to be updated.
@@ -85,6 +92,7 @@ public function a_user_can_update_a_user()
85
92
/** @test */
86
93
public function a_user_can_delete_a_user ()
87
94
{
95
+ // The payload that should be sent alongside the request.
88
96
$ payload = array_merge ($ this ->getDefaultPayload (), []);
89
97
90
98
// The user to be deleted.
@@ -127,4 +135,72 @@ public function a_user_can_restore_a_user()
127
135
'total ' => $ incremented
128
136
]);
129
137
}
138
+
139
+ /** @test */
140
+ public function a_user_can_store_an_avatar ()
141
+ {
142
+ // The user to upload the file for.
143
+ $ user = User::first ();
144
+
145
+ // Store a fake avatar.
146
+ $ response = $ this ->storeAvatar ($ user );
147
+
148
+ $ data = $ response ->decodeResponseJson ();
149
+
150
+ // The original & thumbnail file should exist in the disk.
151
+ Storage::disk (config ('filesystems.default ' ))
152
+ ->assertExists ("{$ data ['directory ' ]}/ {$ data ['filename ' ]}" )
153
+ ->assertExists ("{$ data ['directory ' ]}/thumbnails/ {$ data ['filename ' ]}" );
154
+ }
155
+
156
+ /** @test */
157
+ public function a_user_can_destroy_an_avatar ()
158
+ {
159
+ // The user to upload the file for.
160
+ $ user = User::first ();
161
+
162
+ // Fake an upload so that we could destroy it.
163
+ $ response = $ this ->storeAvatar ($ user );
164
+
165
+ // The payload that should be sent alongside the request.
166
+ $ payload = array_merge ($ this ->getDefaultPayload ());
167
+
168
+ // Assuming that the user's avatar is removed,
169
+ // It must return a 200 response status and then,
170
+ // It must return a response with a user containing
171
+ // null upload attributes to indicate that it was completely destroyed.
172
+ $ response = $ this ->delete (route ('api.v1.users.avatar.destroy ' , $ user ), $ payload )
173
+ ->assertStatus (200 )
174
+ ->assertJsonFragment (
175
+ array_fill_keys ($ user ->getUploadAttributes (), null )
176
+ );
177
+
178
+ // The original & thumbnail file should not exist in the disk.
179
+ Storage::disk (config ('filesystems.default ' ))
180
+ ->assertMissing ("{$ user ->directory }/ {$ user ->filename }" )
181
+ ->assertMissing ("{$ user ->directory }/thumbnails/ {$ user ->filename }" );
182
+ }
183
+
184
+ /**
185
+ * Store a fake avatar.
186
+ *
187
+ * @param App\User $user
188
+ *
189
+ * @return Illuminate\Foundation\Testing\TestResponse
190
+ */
191
+ protected function storeAvatar (User $ user ) : TestResponse
192
+ {
193
+ // The payload that should be sent alongside the request.
194
+ $ payload = array_merge ($ this ->getDefaultPayload (), [
195
+ 'avatar ' => UploadedFile::fake ()->image ('avatar.jpg ' )
196
+ ]);
197
+
198
+ return $ this ->post (
199
+ route ('api.v1.users.avatar.store ' , $ user ), $ payload
200
+ )
201
+ ->assertStatus (200 )
202
+ ->assertJsonMissing (
203
+ array_fill_keys ($ user ->getUploadAttributes (), null )
204
+ );
205
+ }
130
206
}
0 commit comments