54

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?

Ramin Omrani
  • 3,673
  • 8
  • 34
  • 60
  • 1
    Laravel's documentation (5.8) on faking file uploads: https://laravel.com/docs/5.8/http-tests#testing-file-uploads – Soulriser Apr 26 '19 at 21:36

5 Answers5

66

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)
peterm
  • 91,357
  • 15
  • 148
  • 157
  • 1
    That should work, but it doesn't. `$request->file('photo')` has indeed the UploadFile object, but `$test` in this object has its default value `false`. Strange, because the new UploadedFile has the $test = true parameter. – schellingerht Apr 25 '16 at 21:04
  • 1
    The answer above is not enough for > 5.2.14. The right answer could you find here: http://stackoverflow.com/questions/36857800/laravel-5-2-testing-uploadedfile-misses-the-test-value-after-post-bug – schellingerht May 18 '16 at 18:40
  • 6
    It should be `$file = new UploadedFile($path, $name, 'image/png', filesize($path), null, true);` – Hanson Mar 05 '17 at 03:50
  • 1
    For `Symfony 4.1` the correct implementation is `$file = new UploadedFile($path, $name, 'image/png', null, true);` – Fanan Dala Sep 04 '20 at 12:52
  • If you want a more real test, you need to try only your api end point to made all the process, I mean the '/upload' end-point has to deal with the upload not the test. – Miharbi Hernandez Oct 17 '22 at 15:37
25

In Laravel 5.4 you can also use \Illuminate\Http\UploadedFile::fake(). A simple example below:

/**
 * @test
 */
public function it_should_allow_to_upload_an_image_attachment()
{
    $this->post(
        action('AttachmentController@store'),
        ['file' => UploadedFile::fake()->image('file.png', 600, 600)]
    );

    /** @var \App\Attachment $attachment */
    $this->assertNotNull($attachment = Attachment::query()->first());
    $this->assertFileExists($attachment->path());
    @unlink($attachment->path());
}

If you want to fake a different file type you can use

UploadedFile::fake()->create($name, $kilobytes = 0)

More information directly on Laravel Documentation.

Alessandro Benoit
  • 903
  • 10
  • 19
  • But the create() method does not pass the validation if you only ask a specific MIME type such as mp3... – Syl Jul 26 '17 at 11:30
3

I think this is the easiest way to do it

$file=UploadedFile::fake()->image('file.png', 600, 600)];
$this->post(route("user.store"),["file" =>$file));

$user= User::first();

//check file exists in the directory
Storage::disk("local")->assertExists($user->file); 

and I think the best way to delete uploaded files in the test is by using tearDownAfterClass static method, this will delete all uploaded files

use Illuminate\Filesystem\Filesystem;

public static function tearDownAfterClass():void{
        $file=new Filesystem;
        $file->cleanDirectory("storage/app/public/images");
}
Mahdi mehrabi
  • 1,486
  • 2
  • 18
  • 21
1

The laravel documentation has an answer for when you want to test a fake file. When you want to test using a real file in laravel 6 you can do the following:

namespace Tests\Feature;

use Illuminate\Http\UploadedFile;
use Tests\TestCase;

class UploadsTest extends TestCase
{
    // This authenticates a user, useful for authenticated routes
    public function setUp(): void
    {
        parent::setUp();
        $user = User::first();
        $this->actingAs($user);
    }    

    public function testUploadFile()
    {
        $name = 'file.xlsx';
        $path = 'absolute_directory_of_file/' . $name;
        $file = new UploadedFile($path, $name, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', null, true);
        $route = 'route_for_upload';
        // Params contains any post parameters
        $params = [];
        $response = $this->call('POST', $route, $params, [], ['upload' => $file]);
        $response->assertStatus(200);
    }  

}
joel
  • 161
  • 1
  • 6
0

You can find this code at this link

Setup

/**
 * @param      $fileName
 * @param      $stubDirPath
 * @param null $mimeType
 * @param null $size
 *
 * @return  \Illuminate\Http\UploadedFile
 */
public static function getTestingFile($fileName, $stubDirPath, $mimeType = null, $size = null)
{
    $file =  $stubDirPath . $fileName;

    return new \Illuminate\Http\UploadedFile\UploadedFile($file, $fileName, $mimeType, $size, $error = null, $testMode = true);
}

Usage

    $fileName = 'orders.csv';
    $filePath = __DIR__ . '/Stubs/';

    $file = $this->getTestingFile($fileName, $filePath, 'text/csv', 2100);

Folder Structure:

- MyTests
  - TestA.php
  - Stubs
    - orders.csv
Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83