OutlawPlz's avatar

Testing file upload

Hi guys,

I'm trying to test a file upload, but the name of the uploaded file is different from the hash name of the file in test class. Due to the different name the test case is failing, even though the code works properly. I read the docs and I think my test case is doing everything right... Hope someone could help me, thanks!

Relevant code is in uploadProfileImageFor() and a_user_may_update_his_info(), the update() method is there for completeness. Here's my code!

/* ----- Controller ----- */

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request
 * @param  \App\User  $user
 * @return \Illuminate\Http\Response
 *
 * @throws \Illuminate\Auth\Access\AuthorizationException
 */
public function update(Request $request, User $user)
{
    $this->authorize('update', $user);

    $user->update($request->validate(self::rules()) + [
        'profile_image' => $this->uploadProfileImageFor($user, 'nullable|image|max:512'),
    ]);

    if ($request->wantsJson()) return $user;

    return redirect()->route('users.show', ['user' => $user->id]);
}

/**
 * Handle upload and deletion of the image.
 *
 * @param  User  $user
 * @param string|array $rules
 * @param  string  $disk
 * @return string|null
 */
protected function uploadProfileImageFor(User $user, $rules, string $disk = 'public')
{
    // TODO: Make this function generic.
    // Using magic method __call() I could intercept the call to a function,
    // check that starts with 'upload' and ends with 'for' and then call
    // a generic function that handle the upload of a file.

    $file = preg_replace(['/^upload_/', '/_for$/'], '', Str::snake(__FUNCTION__));
    $delete_file = "delete_$file";

    if ( ! empty($rules)) request()->validate([$file => $rules]);

    if ( ! request()->hasFile($file)
        && ! request()->has($delete_file)) return $user->$file;

    Storage::disk('public')->delete($user->$file);

    if (request()->$delete_file) return null;

    return request()->$file->store(Str::plural($file), $disk);
}

/* ----- Test case ----- */

/** @test */
public function a_user_may_update_his_info()
{
    $user = factory(User::class)->create();

    $this->actingAs($user);

    Storage::fake('public');

    $file = UploadedFile::fake()->image('avatar.jpg');

    $this->get(route('users.edit', ['user' => $user->id]))
        ->assertStatus(Response::HTTP_OK)
        ->assertSeeText($user->email);

    $this->patch(route('users.update', ['user' => $user->id]), [
        'name' => 'Martino Aiazzo',
        'profile_image' => UploadedFile::fake()->image('avatar.jpg'),
    ])->assertStatus(Response::HTTP_FOUND);

    $this->assertDatabaseHas('users', [
        'id' => $user->id,
        'name' => 'Martino Aiazzo',
    ]);

    Storage::disk('public')->assertExists("profile_images/{$file->hashName()}");
}

0 likes
1 reply
OutlawPlz's avatar

Ok, I'm dumb... I found the error at the exact time I post the question... Sorry ahah

Please or to participate in this conversation.