Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File upload fails in HTTP tests #835

Closed
maglor opened this issue Oct 26, 2018 · 3 comments
Closed

File upload fails in HTTP tests #835

maglor opened this issue Oct 26, 2018 · 3 comments

Comments

@maglor
Copy link

maglor commented Oct 26, 2018

  • Lumen Version: 5.7.1
  • PHP Version: 7.1
  • Database Driver & Version: PostgresQL 10.4

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
Copy link
Contributor

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

@maglor
Copy link
Author

maglor commented Oct 27, 2018

Thanks!
It's work for me

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

@maglor maglor closed this as completed Oct 27, 2018
@VusalShahbazov
Copy link

Thanks!
It's work for me

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

For me not working (

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants