lundi 4 avril 2016

How to upload a file while testing in Laravel

I'm testing my upload API in laravel. I want to upload a photo using my upload API. but I don't know how to include my file in call method Arguments. It throws an error, doesn't upload the file and fails the test:

Test Code :

$JSONResponse = $this->call('POST', '/upload', [], [], [
    'photo' => new UploadedFile(base_path('public/uploads/test') . '/34610974.jpg', '34610974.jpg')
]);

$this->assertResponseOk();
$this->seeJsonStructure(['name']);

$response = json_decode($JSONResponse);
$this->assertTrue(file_exists(base_path('public/uploads') . '/' . $response['name']));

file path is /public/uploads/test/34610974.jpg

Here is My Upload code in a controller :

$this->validate($request, [
    'photo' => 'bail|required|image|max:1024'
]);

$name = 'adummyname' . '.' . $request->file('photo')->getClientOriginalExtension();

$request->file('photo')->move('/uploads', $name);

return response()->json(['name' => $name]);

How should I test file upload in Laravel 5.2?

Aucun commentaire:

Enregistrer un commentaire