How to test uploading and returning files in Laravel
Last week we looked at creating temporary urls to allow API clients to get uploaded files without exposing those files to the public internet (Returning secure files from an API with temporary URLs).
Uploading and returning files is a very common requirement of web applications and so it’s probably very likely that you will need to implement it at some point in your career.
However, testing this functionality isn’t very straight forward. Uploading and returning a file is quite different to just sending a POST request with a body and asserting the correct JSON payload was returned.
In today’s tutorial we will be looking at writing integration tests for uploading files and returning responses.
Configuring Laravel
We pretty much have everything we need out of the box in Laravel to make this easy, but there are a couple of things I like to set up to make things easier.
First I will set the default storage system to be an environment variable under filesystems.php:
'default' => env('FILESYSTEM_DEFAULT', 'local')
Locally I’m just going to use the local filesystem, but in development and production I will want to use S3.
Laravel’s filesystem abstraction means we don’t need to mess about with crazy mocks, so this will make things a lot easier.
Secondly I like to create a new directory under storage called testing. In this directory I will keep a copy of a few different file types that will likely be uploaded to the application, for example, a couple of different image formats, pdfs, Word documents, Excel spreadsheets etc.
Link:
This post is submitted by our members. Submit a new post.
Tags: Tutorials Laravel 5 Laravel 5.1