johnny's avatar

Testing uploaded files

I'm using Laravel 5.2 with a small project and I got stucked with testing a file uploading feature. Basically, I'd to upload a pdf file, which I want to move to a specific folder on server and process it with a queue.

When I'm testing this feature, I always got the following exception:

1) UploadPayslipTest::an_administrator_can_upload_a_pdf_for_processing
Symfony\Component\HttpFoundation\File\Exception\FileException: The file "payslip.pdf" was not uploaded due to an unknown error.

In my test I'm using the following snippet:

protected function prepareFileUpload($path, $filename = 'payslip.pdf')
    {
        $this->assertFileExists($path);

        $finfo = finfo_open(FILEINFO_MIME_TYPE);

        $mime = finfo_file($finfo, $path);

        return new Symfony\Component\HttpFoundation\File\UploadedFile($path, $filename, $mime, null, null, true);
    } 

If i check the returned object, I got something like this:

Symfony\Component\HttpFoundation\File\UploadedFile {#631
  -test: true
  -originalName: "payslip.pdf"
  -mimeType: "application/pdf"
  -size: null
  -error: 0
  path: "/home/jpapp/Code/storage/../tests/files"
  filename: "payslip.pdf"
  basename: "payslip.pdf"
  pathname: "/home/jpapp/Code/storage/../tests/files/payslip.pdf"
  extension: "pdf"
  realPath: "/home/jpapp/Code/tests/files/payslip.pdf"
  aTime: 2016-02-29 21:04:13
  mTime: 2016-02-29 20:59:53
  cTime: 2016-02-29 21:02:31
  inode: 4341065
  size: 5797
  perms: 0100664
  owner: 1000
  group: 1000
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
}

Which is fine. The next call is:

$response = $this->call('POST', '/admin/payslips/store', $parameters, [], [
            'payslip' => $payslip
        ]);

If I dd the uploaded file in the given controller method I get the following:

Illuminate\Http\UploadedFile {#692
  -test: false
  -originalName: "payslip.pdf"
  -mimeType: "application/pdf"
  -size: null
  -error: 0
  path: "/home/jpapp/Code/storage/../tests/files"
  filename: "payslip.pdf"
  basename: "payslip.pdf"
  pathname: "/home/jpapp/Code/storage/../tests/files/payslip.pdf"
  extension: "pdf"
  realPath: "/home/jpapp/Code/tests/files/payslip.pdf"
  aTime: 2016-02-29 21:04:13
  mTime: 2016-02-29 20:59:53
  cTime: 2016-02-29 21:02:31
  inode: 4341065
  size: 5797
  perms: 0100664
  owner: 1000
  group: 1000
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
}

Since it's no longer in test (please see the private property above), I can't call the move method on this file, 'cause I get the above mentioned exception.

Any idea how to test it properly?

0 likes
3 replies
sapagat's avatar

Take a look to vfsstream repo. You can also use a specific file driver in order to use the storage facade in your test. That made it for me (phpunit).

You can create a virtual file system in order to test the functionality where you are storing your data in the server.

You can test your controller with an API test with some dumny data (not necessary a pdf real data). And just check in your vfs that it is stored successfully.

1 like

Please or to participate in this conversation.