HanSon's avatar

How to test multiple file upload ?

  • Laravel Version: 5.4
  • PHP Version: PHP7

Description:

I try to test a Api with upload files .

use Illuminate\Http\UploadedFile;

$response = $this->actingAs(Admin::find(1))->json('post', '/product', [
            'title' => 'product title',
            'images' => [
                new UploadedFile(__DIR__ . '/img/gcu.jpg', 'gcu.jpg', 'image/png', filesize(__DIR__ . '/img/gcu.jpg'), null, true),
                new UploadedFile(__DIR__ . '/img/head.jpg', 'head.jpg', 'image/png', filesize(__DIR__ . '/img/head.jpg'), null, true)
            ]
        ]);

And I got null in my controller throught $request->file('images')

And I also try ‘images[] => []’ too , didn't work

0 likes
2 replies
ohffs's avatar

I've got an almost identical test atm which works ok. The main difference I can see is that instead of ->json('post'... I'm using ->call('POST'...

$filename = 'tests/data/test_cv.pdf';
$file = new \Illuminate\Http\UploadedFile($filename, 'test_cv.pdf', 'application/pdf', filesize($filename), UPLOAD_ERR_OK, true);
$files = [
  'files' => [$file]
];
$response = $this->actingAs($staff)
                        ->call('POST', route('project.update', $project->id), $this->defaultProjectData(), [], $files);

Please or to participate in this conversation.