Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test file upload in Laravel 5.2

Im trying to test an upload API but it fails every time:

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? How to use call method to upload a file?

like image 201
Ramin Omrani Avatar asked Apr 04 '16 16:04

Ramin Omrani


People also ask

How do I upload a file in Laravel?

If you need just images or random sized files use $file->fake->image () or create () methods. Those come bundled with Laravel. Then make a fake file to upload. Then make a JSON Data to pass the file.

How easy is it to work with Laravel for a developer?

As you know, Laravel provides awesome feature and It's very easy PHP Framework for developer to work with Laravel. Working with upload file for image with validation in Laravel 5.2 is very easier and easy to validate files and their type in Laravel 5.2.

Where can I find the file system configuration in Laravel?

Laravel's filesystem configuration file is located at config/filesystems.php. Within this file, you may configure all of your filesystem "disks". Each disk represents a particular storage driver and storage location.

How to test uploadedfile?

When you create an instance of UploadedFile set the last parameter $test to true. $file = new UploadedFile ($path, $name, filesize ($path), 'image/png', null, true); ^^^^ Here is a quick example of a working test. It expects that you have a stub test.png file in tests/stubs folder.


1 Answers

When you create an instance of UploadedFile set the last parameter $test to true.

$file = new UploadedFile($path, $name, filesize($path), 'image/png', null, true);                                                                            ^^^^ 

Here is a quick example of a working test. It expects that you have a stub test.png file in tests/stubs folder.

class UploadTest extends TestCase {     public function test_upload_works()     {         $stub = __DIR__.'/stubs/test.png';         $name = str_random(8).'.png';         $path = sys_get_temp_dir().'/'.$name;          copy($stub, $path);          $file = new UploadedFile($path, $name, filesize($path), 'image/png', null, true);         $response = $this->call('POST', '/upload', [], [], ['photo' => $file], ['Accept' => 'application/json']);          $this->assertResponseOk();         $content = json_decode($response->getContent());         $this->assertObjectHasAttribute('name', $content);          $uploaded = 'uploads'.DIRECTORY_SEPARATOR.$content->name;         $this->assertFileExists(public_path($uploaded));          @unlink($uploaded);     } } 
 ➔ phpunit tests/UploadTest.php PHPUnit 4.8.24 by Sebastian Bergmann and contributors.  .  Time: 2.97 seconds, Memory: 14.00Mb  OK (1 test, 3 assertions) 
like image 178
peterm Avatar answered Sep 26 '22 06:09

peterm