laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.48k stars 419 forks source link

File upload fails in HTTP tests #835

Closed maglor closed 5 years ago

maglor commented 5 years ago

Description:

HTTP file upload fails in tests like https://laravel.com/docs/5.7/http-tests#testing-file-uploads $request->file('image')->isValid(); fails with Call to a member function isValid() on null but $request->file('image') is instanse of Illuminate\Http\Testing\File

Steps To Reproduce:

TestCase:

use Illuminate\Http\UploadedFile;

class ImagesTest extends TestCase
{

    public function testImageAdd() {

        $file = UploadedFile::fake()->image('product.jpg', '600', '600');

        $this->post(route('image_store'), [
            'image' => $file
        ]);

        $this->assertResponseStatus(201);
    }
}

ImageController:

class ImageController extends Controller {

    public function store(Request $request) {

        $request->file('image')->isValid();

        /** 
         * print_r ($request->file('image'));
         * 
         * Illuminate\Http\Testing\File Object
         * (        
         *    [name] => product.jpg
         *    [tempFile] => Resource id #170
         *    [sizeToReport] => 
         *    [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1
         *    [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => product.jpg
         *    [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
         *    [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
         *    [hashName:protected] => 
         *    [pathName:SplFileInfo:private] => C:\Users\6\AppData\Local\Temp\php9B4F.tmp
         *    [fileName:SplFileInfo:private] => php9B4F.tmp
         * )
         *
         * File C:\Users\6\AppData\Local\Temp\php9B4F.tmp exists on the disk in this time
         * 
         * print_r($_FILES)
         * 
         * Array
         * (
         * )
         * 
         */

        // Other code

    }

}
jensdenies commented 5 years ago

This is happening because the files parameter passed to the call method in the post call is hardcoded as an empty array.

maglor commented 5 years ago

Thanks! It's work for me

$this->call('POST', route('image_store'), [], [], ['image' => $file], []);
VusalShahbazov commented 4 years ago

Thanks! It's work for me

$this->call('POST', route('image_store'), [], [], ['image' => $file], []);

For me not working (