Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thepassenger's avatar

How to check if uploaded file exists in storage if name was hashed.

Hi, I'm testing my controller upload avatar method. Following the documentation I got to this point.

public function testUploadAvatarMethodStoresImageCorrectlyAndUpdatesDb()
    {
        Storage::fake('local');

        $response = $this->actingAs($this->user)->json('POST', 'profile/avatar', [
            'avatar' => UploadedFile::fake()->image('avatar.jpg')
        ])->assertStatus(200);
        // Assert the file was stored...
        Storage::disk('local')->assertExists('public/avatars/avatar.jpg'); //Problem is here

        // Assert a file does not exist...
        Storage::disk('local')->assertMissing('public/avatars/missing.jpg');
    }

The problem is in the controller I use

$path = request()->file('avatar')->store('public/avatars');

and the store method hashes the file name. I tracked the hashing part from the helpers function and it actually strips name and extension and then put a Str::random(40); as name.

How do I compare the file names then? I thought something like assertExists('public/avatars/*.jpg'); but it doesn't work. Btw the method works correctly if I use storeAs and put avatar.jpg as name gives no error. Any ideas?

0 likes
7 replies
thepassenger's avatar

At the moment I'm just doing this to check if storage is empty before post request, and not empty after.

public function testUploadAvatarMethodStoresImageCorrectlyAndUpdatesDb()
    {
        Storage::fake('local');

        // Assert storage is empty.        
        $this->assertEquals([], Storage::allFiles());
        
        $response = $this->actingAs($this->user)->json('POST', 'profile/avatar', [
            'avatar' => UploadedFile::fake()->image('avatar.jpg')
        ])->assertStatus(200);

        // Assert the file was stored...
         $this->assertNotEquals([], Storage::allFiles());

    }

Don't like it tho.

1 like
kyslik's avatar

I am sure you can list all files from folder, so do that.

But its waste of time; if you do tests right (and clean up after each test), just check if folder is empty.

1 like
thepassenger's avatar

@kyslik Thank you for answering. So you think what I posted in my reply would be acceptable? Checking if Storage::allFiles() is empty or not.

kyslik's avatar

@thepassenger definitely. If you do tests correct; state of application should not change after tests are done. So testing empty storage is in place and easiest... no figuring out how to hash fake file in order to compare it etc.

pactode's avatar
pactode
Best Answer
Level 9

@thepassenger You could do something like this:

public function testUploadAvatarMethodStoresImageCorrectlyAndUpdatesDb()
    {
        Storage::fake('local');
    $file = UploadedFile::fake()->image('avatar.jpg');

        $response = $this->actingAs($this->user)->json('POST', 'profile/avatar', [
            'avatar' => $file
        ])->assertStatus(200);
        // Assert the file was stored...

    Storage::disk('local')->assertExists("public/avatars/{$file->hashName()}");
    }
6 likes

Please or to participate in this conversation.