Skip to content

Commit 2877a1c

Browse files
author
Jovert Lota Palonpon
committed
Tests for User uploads, Resolve #31
1 parent 1dc4465 commit 2877a1c

File tree

5 files changed

+97
-5
lines changed

5 files changed

+97
-5
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ PUSHER_APP_CLUSTER=mt1
4040
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
4141
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
4242

43-
FILESYSTEM_DRIVER=local
43+
FILESYSTEM_DRIVER=public
4444

4545
TELESCOPE_ENABLED=true
4646

.env.testing

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ SESSION_DRIVER=array
77
DB_CONNECTION=sqlite
88
DB_DATABASE=:memory:
99

10+
FILESYSTEM_DRIVER=public
11+
1012
TELESCOPE_ENABLED=false
1113

1214
JWT_SECRET=tuHlDtSr7DO4JxxmjfuddNHOdgZLhgVC

app/Http/Controllers/Api/V1/UsersController.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ public function restore(Request $request, $id)
160160
*/
161161
public function storeAvatar(Request $request, User $user) : JsonResponse
162162
{
163-
if (! $user->upload($request->files->get('avatar'))) {
164-
return response()->json('Unable to process the upload', 422);
163+
if ($user->upload($request->files->get('avatar'))) {
164+
return response()->json($user);
165165
}
166166

167-
return response()->json('Uploaded successfully!');
167+
return response()->json('Unable to process the upload', 422);
168168
}
169169

170170
/**
@@ -177,7 +177,11 @@ public function storeAvatar(Request $request, User $user) : JsonResponse
177177
*/
178178
public function destroyAvatar(Request $request, User $user) : JsonResponse
179179
{
180-
return response()->json($user->destroyUpload());
180+
if ($user->destroyUpload()) {
181+
return response()->json($user);
182+
}
183+
184+
return response()->json('Uploaded file not removed', 422);
181185
}
182186

183187
/**

app/User.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,14 @@ public function getDirectory() : string
5555
{
5656
return 'users/'.$this->getKey();
5757
}
58+
59+
/**
60+
* Get the upload attributes
61+
*
62+
* @return array
63+
*/
64+
public function getUploadAttributes() : array
65+
{
66+
return $this->uploadAttributes;
67+
}
5868
}

tests/Feature/Api/V1/UsersTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
namespace Tests\Feature\Api\V1;
44

55
use App\User;
6+
use Illuminate\Http\UploadedFile;
7+
use Illuminate\Support\Facades\Storage;
8+
use Illuminate\Foundation\Testing\TestResponse;
69

710
class UsersTest extends BaseTest
811
{
912
/** @test */
1013
public function a_user_can_list_users()
1114
{
15+
// The payload that should be sent alongside the request.
1216
$payload = array_merge($this->getDefaultPayload(), []);
1317

1418
$this->get(route('api.v1.users.index'), $payload)->assertStatus(200);
@@ -32,6 +36,7 @@ public function a_user_can_create_a_user()
3236
'username' => $this->faker->userName,
3337
];
3438

39+
// The payload that should be sent alongside the request.
3540
$payload = array_merge($this->getDefaultPayload(), []);
3641

3742
// Assuming that the user is created through the test data,
@@ -51,6 +56,7 @@ public function a_user_can_create_a_user()
5156
/** @test */
5257
public function a_user_can_view_a_user()
5358
{
59+
// The payload that should be sent alongside the request.
5460
$payload = array_merge($this->getDefaultPayload(), []);
5561

5662
// The user to be shown.
@@ -67,6 +73,7 @@ public function a_user_can_view_a_user()
6773
/** @test */
6874
public function a_user_can_update_a_user()
6975
{
76+
// The payload that should be sent alongside the request.
7077
$payload = array_merge($this->getDefaultPayload(), []);
7178

7279
// The user to be updated.
@@ -85,6 +92,7 @@ public function a_user_can_update_a_user()
8592
/** @test */
8693
public function a_user_can_delete_a_user()
8794
{
95+
// The payload that should be sent alongside the request.
8896
$payload = array_merge($this->getDefaultPayload(), []);
8997

9098
// The user to be deleted.
@@ -127,4 +135,72 @@ public function a_user_can_restore_a_user()
127135
'total' => $incremented
128136
]);
129137
}
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+
}
130206
}

0 commit comments

Comments
 (0)