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
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
}
}
This is happening because the files parameter passed to the call method in the post call is hardcoded as an empty array.
Thanks!
It's work for me
$this->call('POST', route('image_store'), [], [], ['image' => $file], []);
Thanks!
It's work for me$this->call('POST', route('image_store'), [], [], ['image' => $file], []);
For me not working (
Most helpful comment
Thanks!
It's work for me