ovidiu_dtp's avatar

How do you unit test file uploads?

I was thinking, how can you mock and test if file upload works as intended? I mean, a proper unit test. See it pass, see it fail... Can I just use a file I have on my HDD and feed the path?

0 likes
4 replies
oldtimeguitarguy's avatar

Just created a simple trait that does what Mehdi_Souihed's link does. You can pull it in via composer

composer require oldtimeguitarguy/laravel-mocks-file-uploads

deweller's avatar

Make sure you are using Illuminate\Http\UploadedFile to mock your file and not Symfony\Component\HttpFoundation\File\UploadedFile.

        $songfile = new UploadedFile($temp_filepath, 'foofile.mp3', 'audio/mpeg', 100023, null, $test=true);
        $files = ['songfile' => $songfile,];
        // define $uri, $method, $parameters, $cookies, $server, $content
    $request = Request::create($uri, $method, $parameters, $cookies, $files, $server, $content);

2 likes

Please or to participate in this conversation.