#Storage fake creates folder but not the file?

27 messages · Page 1 of 1 (latest)

full oxide
#
Storage::fake('cloud');

$fileName = 'large-image.jpg';
$uuid = Uuid::uuid4()->toString();
$newFilePath = "{$uuid}/{$fileName}";

$storage = Storage::disk('cloud');
$storage->put($newFilePath, file_get_contents($filePath));
$storage->assertExists($newFilePath);

Looking in the folders storage/framework/testing/disks/cloud the folder is created but there's no file in the folder and thus the assertExists fails. Any idea what causes this? The folder seemingly has the right permissions/owner.

versed carbon
#

You are making a file name, not an actual file. There is no contents. Now that test in itself seems pointless to me, but assume you are just learning how to assert before testing actual business logic.

#
Storage::fake('cloud');
$path = Str::orderedUuid()->toString();
UploadedFile::fake()->image('large-image.jpg')->storeAs("$path/large-image.jpg", 'large-image.jpg', [
    'disk' => 'cloud',
]);

Storage::disk('cloud')->assertExists("$path/large-image.jpg");
#

That is only testing the framework, but showing you the idea

#

It is one thing to "post" a fake file to upload, and assert it was stored. It is another to setup and store a fake file before you act upon it.

full oxide
#

Sorry I forgot to include "$filePath", it leads to an actual image that exists.

versed carbon
#

But it does not exist in the faked disk

full oxide
#

It exists outside the Storage fake, I want to "put' or copy it into the Storage faked disk.

The goal is to copy the static file into the test disk, then I create a model that references it, a listener uses spatie/laravel-medialibrary to trigger a resize on the image. The end goal is to make sure a conditional size is created or not created depending on the real provided image.

versed carbon
#

Oof, with the media lib, not sure the best way to test around that.

#

What about using the fake file as above, just not image

#

pass your contents

#
UploadedFile::fake()
    ->create('large-image.jpg', file_get_contents($filePath), 'image/jpeg')
    ->storeAs("$path/large-image.jpg", 'large-image.jpg', [
        'disk' => 'cloud',
    ]);
full oxide
#

Thanks Tippin, I have tried that and while the assertion passes, the file is never actually created in the storage/framework/testing/disks/cloud folder. It does however create the UUID named folder there, but no file, even if I use persistentFake.

versed carbon
#

You know that faked disks are emptied during test teardown right?

#

Either after the entire suite runs, or, when running an individual case, only if you perform an assertion of the file. From what I've observed.

#

Ah, the persistent fake, never tried that

#

Try to run the test without the storage assertion and see if it leaves a file behind

full oxide
#

Yeah even without the assertion the file isn't there. Hmm..

versed carbon
#

I can tell you that I've done my above with using the fake upload file to stick json or otherwise fixture data into fake storage that my business logic will actually pull from in testing.

#

Not sure what might be causing your issue (if it's an actual issue)

#

But I'm not dealing with a package in the middle

full oxide
#

Thanks, I'm not sure. I let it run after the assertion and the listener can't find the file/does not exist.
Something I noticed is if I chmod -R 775 storage/ then run the test, the file is created but only once. I have to re-run the chmod command again manually for the new file to be created.. so I wonder if this is permission related to the owner of the artisan file?

#

If I add sleep(1000) to the file, I see it being created but after 2-3 seconds it's deleted even though the test is still technically running and hasn't called tearDown yet. Very weird.

versed carbon
#

Might be time to find another angle to attack and test the behavior you're looking for.

#

Unfortunately I'm out of ideas 😦