Laravel 5.2 | Testing UploadedFile Misses the $test Value after Post. Bug? Laravel 5.2 | Testing UploadedFile Misses the $test Value after Post. Bug? laravel laravel

Laravel 5.2 | Testing UploadedFile Misses the $test Value after Post. Bug?


Explanation

This is really interesting thing. You've noticed already a lot of when creating this post (if anyone has the problem should read it carefully).

In this commit as you already mentioned $testing parameter was removed and code of classes was simplified removing Reflection to get testing property value of Symfony\Component\HttpFoundation\File\UploadedFile.

And now the tricky thing is that depending on what you are testing, you might event not notice the change and everything might work but in some cases it won't and you won't really know why.

For example everything might work - file will be uploaded without a problem, but if you add into your Request class for instance mimes rule like so:

'logo' => ['mimes:jpeg,png'],

it will fail telling you the file has invalid mime (this is because internally it will be also verified if file was really uploaded and in case of tests in fact it's not the same as real upload).

The solution is looking again at what was really changed into the commit and how the method look like. In this file instance of uploaded file is returned like so:

 return $file instanceof static ? $file : new static(            $file->getRealPath(), $file->getClientOriginalName(), $file->getClientMimeType(),            $file->getClientSize(), $file->getError()        );

so in case if the file is instance of this class it will return this instance unmodified, otherwise it will create now object without passing $testing argument to class constructor.

Solution

So to solve this, when testing file uploads you should not use

\Symfony\Component\HttpFoundation\File\UploadedFile

class anymore. You should now use

\Illuminate\Http\UploadedFile

to don't get any strange issues when testing file uploads (of course you should still pass to this object constructor true as $testing parameter but now it will be used later without a problem)