28

I'm trying to run this functional test on my laravel controller. I would like to test image processing, but to do so I want to fake image uploading. How do I do this? I found a few examples online but none seem to work for me. Here's what I have:

public function testResizeMethod()
{
    $this->prepareCleanDB();

    $this->_createAccessableCompany();

    $local_file = __DIR__ . '/test-files/large-avatar.jpg';

    $uploadedFile = new Symfony\Component\HttpFoundation\File\UploadedFile(
        $local_file,
        'large-avatar.jpg',
        'image/jpeg',
        null,
        null,
        true
    );


    $values =  array(
        'company_id' => $this->company->id
    );

    $response = $this->action(
        'POST',
        'FileStorageController@store',
        $values,
        ['file' => $uploadedFile]
    );

    $readable_response = $this->getReadableResponseObject($response);
}

But the controller doesn't get passed this check:

elseif (!Input::hasFile('file'))
{
    return Response::error('No file uploaded');
}

So somehow the file isn't passed correctly. How do I go about this?

2
  • I can see you've put this in to test mode. Is the uploaded file larger than the max file size set in php.ini?
    – craig_h
    Oct 22, 2015 at 13:06
  • No that's not it, upload limit is 2mb, testfile is 300kb. Oct 22, 2015 at 13:12

6 Answers 6

19

For anyone else stumbling upon this question, you can nowadays do this:

    $response = $this->postJson('/product-import', [
        'file' => new \Illuminate\Http\UploadedFile(resource_path('test-files/large-avatar.jpg'), 'large-avatar.jpg', null, null, null, true),
    ]);

UPDATE

In Laravel 6 the constructor of \Illuminate\Http\UploadedFile Class has 5 parameters instead of 6. This is the new constructor:

    /**
     * @param string      $path         The full temporary path to the file
     * @param string      $originalName The original file name of the uploaded file
     * @param string|null $mimeType     The type of the file as provided by PHP; null defaults to application/octet-stream
     * @param int|null    $error        The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants); null defaults to UPLOAD_ERR_OK
     * @param bool        $test         Whether the test mode is active
     *                                  Local files are used in test mode hence the code should not enforce HTTP uploads
     *
     * @throws FileException         If file_uploads is disabled
     * @throws FileNotFoundException If the file does not exist
     */
    public function __construct(string $path, string $originalName, string $mimeType = null, int $error = null, $test = false)
    {
        // ...
    }

So the above solution becomes simply:

$response = $this->postJson('/product-import', [
        'file' => new \Illuminate\Http\UploadedFile(resource_path('test-files/large-avatar.jpg'), 'large-avatar.jpg', null, null, true),
    ]);

It works for me.

2
  • For contribution I add the constructor __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false) which gets called from above code. It works excellent with images and PDFs in my unit tests.
    – barfoos
    Jun 27, 2017 at 5:50
  • Thank you so much, you ended an hour of fruitless search!
    – Vincent
    Sep 20, 2019 at 4:00
13
+100

Docs for CrawlerTrait.html#method_action reads:

Parameters
string $method
string $action
array $wildcards
array $parameters
array $cookies
array $files
array $server
string $content

So I assume the correct call should be

$response = $this->action(
    'POST',
    'FileStorageController@store',
    [],
    $values,
    [],
    ['file' => $uploadedFile]
);

unless it requires non-empty wildcards and cookies.

6
  • Trying your suggestion. Test jargon seems so vague to me:p What type of test would this be? Oct 29, 2015 at 8:54
  • 1
    @JasperKennis, it is not jargon, but terminology which helps people to stay on the same page discussing things. Misuse of terminology confuses, and does the opposite. What you are doing is either functional or integration test, depending on intentions. As a rule of thumb, unit test checks imperative part, i.e. logic of a single class, mocking everything (reasonable) else. Integration tests checks declarative part, i.e. configs, templates, etc. Functional tests checks functionality of the application as defined in the spec.
    – Alex Blex
    Oct 29, 2015 at 9:36
  • This worked^^ Test still fails but it get's beyond the specified point where I was stuck at. Note, I'm using Laravel 4.2 and that doesn't take the $wildcards argument. Oct 29, 2015 at 10:20
  • thanks for that explanation too, I changed the question to say functional test. Oct 29, 2015 at 10:23
  • That's strange. $wildcards were there since very first commit of v4.0 github.com/laravel/framework/blob/… There were no $cookies, so $files were 5th parameter tho. Anyway, glad it helped.
    – Alex Blex
    Oct 29, 2015 at 10:43
10

The best and Easiest way : First Import the Necessary things

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

Then make a fake file to upload.

Storage::fake('local');
$file = UploadedFile::fake()->create('file.pdf');

Then make a JSON Data to pass the file. Example

$parameters =[
            'institute'=>'Allen Peter Institute',
            'total_marks'=>'100',
            'aggregate_marks'=>'78',
            'percentage'=>'78',
            'year'=>'2002',
            'qualification_document'=>$file,
        ];

Then send the Data to your API.

$user = User::where('email','candidate@fakemail.com')->first();

$response = $this->json('post', 'api/user', $parameters, $this->headers($user));

$response->assertStatus(200);

I hope it will work.

1
  • is there a way to fake an actual PDF file? my store() validates if it is actually a PDF
    – bilogic
    Sep 21, 2021 at 2:19
5

With phpunit you can attach a file to a form by using attach() method.

Example from lumen docs:

public function testPhotoCanBeUploaded()
{
    $this->visit('/upload')
         ->name('File Name', 'name')
         ->attach($absolutePathToFile, 'photo')
         ->press('Upload')
         ->see('Upload Successful!');
}
1
  • 3
    That doesn't help, I'm testing controller responses using the action method. This is a higher level example. Oct 23, 2015 at 8:19
4

Here is a full example how to test with custom files. I needed this for parsing CSV files with known format so my files had to had exact formatting and contents. If you need just images or random sized files use $file->fake->image() or create() methods. Those come bundled with Laravel.

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

class PanelistImportTest extends TestCase
{
    /** @test */
    public function user_should_be_able_to_upload_csv_file()
    {
        // If your route requires authenticated user
        $user = Factory('App\User')->create();
        $this->actingAs($user);

        // Fake any disk here
        Storage::fake('local');

        $filePath='/tmp/randomstring.csv';

        // Create file
        file_put_contents($filePath, "HeaderA,HeaderB,HeaderC\n");

        $this->postJson('/upload', [
            'file' => new UploadedFile($filePath,'test.csv', null, null, null, true),
        ])->assertStatus(200);

        Storage::disk('local')->assertExists('test.csv');
    }
}

Here is the controller to go with it:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class UploadController extends Controller
{
    public function save(Request $request)
    {
        $file = $request->file('file');

        Storage::disk('local')->putFileAs('', $file, $file->getClientOriginalName());

        return response([
            'message' => 'uploaded'
        ], 200);
    }
}
2

Add similar setUp() method into your testcase:

protected function setUp()
{
    parent::setUp();

    $_FILES = array(
        'image'    =>  array(
            'name'      =>  'test.jpg',
            'tmp_name'  =>  __DIR__ . '/_files/phpunit-test.jpg',
            'type'      =>  'image/jpeg',
            'size'      =>  499,
            'error'     =>  0
        )
    );
}

This will spoof your $_FILES global and let Laravel think that there is something uploaded.

3
  • I tried this, both naming the file image or file, but the check still fails. Is this supposed to work with the test unit ->action method? Oct 28, 2015 at 8:53
  • Have you put the real size and file's path to the array? Also try to switch lines, so that the parent::setUp will be after you spoof your $_FILES array. Oct 28, 2015 at 13:57
  • I updated my question to reflect that too, but no, it didn't help:( Filename and size are correct. Oct 29, 2015 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.