Search code examples
laravelfiletestingfaker

How to upload a file( image, PDF or video etc ) with faker in Laravel testing


I am writing tests for laravel. I need to pass these validation rules. Please guide how can I upload fake image and video in my test. thanks

Here is what i tried.

`'image' => ['required', 'file', 'mimes:jpg,jpeg,bmp,png'],
'video' => ['required', 'file', 'mimes:mp4'],

Solution

  • You just provided the validation rules you need to get passed. However in order to upload a file with faker in laravel testing you can use use Illuminate\Http\UploadedFile;

    'image' => UploadedFile::fake()->image('file1.png', 600, 600),
    'video' => UploadedFile::fake()->create('sample.mp4', '1000', 'mp4'),
    'file' = UploadedFile::fake()->create($faker->word . '.pdf', 100);
    
    

    This is how you can use Faker to generate a fake image, video or pdf file with a random name and specified size.