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

Pixelairport's avatar

Testing Filament FileUpload with SpatieMediaLibraryFileUpload

I have the following code to test a SpatieMediaLibraryFileUpload in Filament, which has the name avatar.

$file = UploadedFile::fake()->create('screenshot.txt');

$d = livewire(Customers::class)
  ->set('data.avatar', $file)
  ->call('create');

  dd($d->errors());

When I show the error I have the error in the message bag:

"data.avatar" => array:1 [
      0 => "The avatar field must be an image."
    ]

But when I now add the line to test

$file = UploadedFile::fake()->create('screenshot.txt');

livewire(Customers::class)
  ->set('data.avatar', $file)
  ->call('create');
  ->assertHasFormErrors([
    'avatar' => 'image',
  ])

I get the error:

Component has no matching failed rule or error message [image] for [data.avatar] attribute.
Failed asserting that an array contains 'image'.

Hope somebody understands my problem and can help. thx

0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're encountering is related to the validation rule for the avatar field. The error message indicates that the avatar field must be an image, but you're trying to upload a .txt file, which is not considered an image. This is why you're seeing the validation error.

To resolve this, you need to ensure that the file you're uploading in your test is an image. You can use Laravel's UploadedFile::fake() method to create a fake image file instead of a text file. Here's how you can modify your test:

use Illuminate\Http\UploadedFile;
use Livewire\Livewire;
use Tests\TestCase;

class YourTest extends TestCase
{
    public function test_file_upload()
    {
        // Create a fake image file
        $file = UploadedFile::fake()->image('avatar.jpg');

        // Run the Livewire component test
        $d = Livewire::test(Customers::class)
            ->set('data.avatar', $file)
            ->call('create');

        // Assert that there are no validation errors
        $d->assertHasNoErrors();

        // If you want to test for specific validation errors, you can use:
        // $d->assertHasFormErrors(['avatar' => 'image']);
    }
}

Key Points:

  • Use an Image File: Ensure that the file you're testing with is an image. You can use UploadedFile::fake()->image('avatar.jpg') to create a fake image file.
  • Validation Assertions: If you want to assert that a specific validation error occurs, make sure the conditions for that error are met. In this case, if you want to test for the 'image' validation error, you would need to upload a non-image file intentionally.

By using a fake image file, your test should pass without triggering the "The avatar field must be an image" validation error. If you want to test the validation error, you can revert to using a non-image file and assert for the error as you initially intended.

Pixelairport's avatar

I know I make a txt file. But I want to have a successful test. But the tests stops. It should be successful, because I test, that a textfile throws error for avatar, because it should be an image.

Please or to participate in this conversation.