aboulayla's avatar

File Upload With Behat

I am trying to test a simple upload with Behat. When i test manually in browser, the code works fine. But when I run my test, I have this error :

The image failed to upload

In searching, I founded that method isValid() from UploadedFile return false because of test attribute is false and is_uploaded_file method return false too.

How to solve this ?

Here is my feature :

Feature: Add an advert
    In order to advert is public visible
    As a user connected
    I want to add an advert

    Scenario: Add a simple advert
        Given I am on "/adverts/create"
        Then I should see "Add an advert"
        When I fill in "name" with "House"
        And I attach the file "IMG_0119.jpg" to "image"
        And I fill in "description" with "Description"
        And I press "Submit"
        Then I should see "Advert added."

Here is my controller method :

public function store(Request $request)
    {
        $validator = $this->validate($request, [
            'image' => 'required|image',
        ]);
        return redirect('/adverts/create')->with('success', trans('advert.created.success'));
0 likes
3 replies
krzysztofl's avatar

I am using a FormRequest rules and its failing on the validation. When the main validator class is validating attributes and it find a UploadedFile is trying to call isValid on it which internally use the php function is_uploaded_file which is failing. This is due that it required super global $_FILE to be set. Which is not set while testing using behat.

Please or to participate in this conversation.