Simulating posting a file to a controller in Symfony2 Symfony PHP Object Orientated Programming Unit Testing

A lot of my work consists of building and maintaining a restful api. An important aspect of this is writing unit tests which help enforce consistency across our ever expanding codebase.

One unit test that needed to be implemented was testing a route which allowed a file upload. Since there isn’t a way to post a form, the test needed to simluate the file upload using another method. Fortunately, this is were Symfony’s UploadedFile object is useful.

The gist below is a quick example of what was required to test the controller endpoint. Feel free to make any suggestions or improvements.