phpunit file upload: size 0 (zero)

Published 3 months ago by Zini

My test makes a post request:

// FileUploadTest.php
$this->post(
    'uri',
    ['file' => \Illuminate\Http\UploadedFile::fake()->image('file.jpg')->size(200)]
);

The controller detects the filesize:

// FileUploadController.php
if ($request->hasFile('file')) {
    \Log::debug('bytes: ' . \File::size($request->file));
}

When I upload a file manually, the correct size of the file would be logged. When I run a phpunit test, the size is always 0.

Any idea what is going on?

rin4ik
rin4ik
3 months ago (200,520 XP)
Zini

@rin4ik ok, I tried using the call() method as explained the stack answer, but it does not impact the result: the file's size remains 0.

Edit:

Also, using a file from the folder instead of mocking it, results in an upload failure.

Please sign in or create an account to participate in this conversation.