coder81's avatar

Test a POST request with files

I want to test the store method with a call, because I can't use attach method becouse I use javascript to submit the form and I don't have any button. My problem is that the controller doesn't recognizes the files that I have passed.

Test

        $img = Image::make('public/img/test.jpg')->save('/tmp/test.jpg');
        $file = new \Symfony\Component\HttpFoundation\File\UploadedFile('/tmp/test.jpg', 'test.jpg', 'image/jpeg', $img->filesize());

        $response = $this->actingAs(App\Models\User::where('profile_type', 'Admin')->first())
             ->call(
                'POST',
                'myurl',
                [],
                [],
                ['photos' => [$file]]
                );

        dd($response->content());

Controller

        dd($request->files); // It's empty
        //dd($request->all()); // I see the files under the parameter value
0 likes
4 replies
zachleigh's avatar

An acceptance test is where you run tests in a real browser running your actual javascript. They basically go through your code just as a user would. Its usually easier and better to do this than to try to force something to be tested in a way it will never be used. Check out Codeception: http://codeception.com/

1 like
coder81's avatar

I'm going to read something about.

Please or to participate in this conversation.