Framework: [5.2][Testing] Can't Attach files to a multiple file field

Created on 3 Mar 2016  路  5Comments  路  Source: laravel/framework

In 5.2.22 (assuming that this issue exists in previous versions as well) during testing I'm unable to attach files to a multiple file field? The field is:

<input class="form-control" id="images" multiple="1" name="images[]" type="file">
$guide = Guide::first();

$this->visit(route('resources.guides.images', [$guide->slug]))
    ->attach(base_path('tests/assets/test.jpg'), 'images')
    ->press('Upload')
    ->see('Success!');

I receive the error after running phpunit:

InvalidArgumentException: Cannot set value on a compound field "images".

If I pass in an array I receive required validation errors for the image field:

$guide = Guide::first();

$this->visit(route('resources.guides.images', [$guide->slug]))
    ->attach([base_path('tests/assets/test.jpg')], 'images')
    ->press('Upload')
    ->see('Success!');

I'm not sure if this is a bug or there's an alternate method I'm supposed to be using?

Another example here: https://www.reddit.com/r/laravel/comments/3z21zi/laravel_testing_multiple_file_upload/

Most helpful comment

I think there still is an error in:

framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php

as the convertUploadsForTesting and getUploadedFileForTesting methods don't take into account the possibility of a having a compound file input field. Also, please take a look, at how $this->uploads property is treated in the attach method.

I believe that the issue should be reopened.

All 5 comments

In 5.2.22 Symfony\Component\HttpFoundation\File\UploadedFile has been replaced/wrapped with Illuminate\Http\UploadedFile. I suspect since it doesn't pass on the test parameter when being initialised in createFromBase() you will lose the testing flag associated with Symfony UploadedFiles.

In my case I was relying on the test parameter to test some file uploads, these tests now fail with the same errors you highlight as the validator is unable to find a valid UploadedFile instance.

Looking at the Illuminate/UploadedFile history support for test was there but removed before release. I'd be keen to understand why it was removed; it looks useful and something that was considered at least for a while.

My tests with UploadedFile now are all failing because it's not possible to set the test property anymore.

I have the same problem. The code from illuminate\http\UploadedFile doesn't consider the $test property anymore, therefore our test suite is failing. Is anyone aware of an alternative approach to test file uploads?

@jessedc @daack @mauricius This is not really the problem. You should now use:

\Illuminate\Http\UploadedFile

instead of

\Symfony\Component\HttpFoundation\File\UploadedFile

For details have a look at http://stackoverflow.com/questions/36857800/laravel-5-2-testing-uploadedfile-misses-the-test-value-after-post-bug

I think there still is an error in:

framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php

as the convertUploadsForTesting and getUploadedFileForTesting methods don't take into account the possibility of a having a compound file input field. Also, please take a look, at how $this->uploads property is treated in the attach method.

I believe that the issue should be reopened.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

digirew picture digirew  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

felixsanz picture felixsanz  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments