Testing upload file with Slim 3 (a better way)
Published 4 years and 1 month ago, Categorized under: Code, PHP

In my last article, I talked about how we can mock the upload functionality, but without testing the existing of the file, this will work with you without any problem, but now to test the existence of the file we can do it like this:

This is the content of the upload function from the upload controller:

    public function upload(Request $request, Response $response)
        $files = $request->getUploadedFiles();

        if (empty($files'file')) {
            throw new \RuntimeException('Expected a newfile');

        $file = $files'file';

        if ($file->getError() === UPLOAD_ERR_OK) {
            $fileName = $file->getClientFilename();

            return $response->withJson(
                'result' => 
                    'fileName' => $fileName

        return $response
                'error' => 'Nothing was uploaded'

As you can see nothing too fancy, we get the uploaded files, we check that the upload worked then we moved the file to the storage directory, now the testing for the functionality with a real file will be like this:

    public function testUploadToLocal()
        $randomName = md5('4zJYinT - Imgur');
        copy(__DIR__.'/../_Files/4zJYinT - Imgur.jpg', __DIR__.'/../_Files/'.$randomName.'.jpg');

        $file = new UploadedFile(

        $response = $this->runApp('POST', '/upload', null, 
            'file' => $file

        $responseAsArray = json_decode((string) $response->getBody(), true)'result';

        $this->assertFileExists(storage_path('/images/'.$randomName.'.jpg'), 'File does not exists.');
        $this->assertArrayHasKey('fileName', $responseAsArray);
        $this->assertEquals($randomName.'.jpg', $responseAsArray'fileName');
        $this->assertEquals(200, $response->getStatusCode());


Basically, this is what we do:

1- we copy the file we want to upload and create a random file name, am using MD5 to get a random name.

2- we pass the new file and the other required parameter to the UploadedFile function

     * Construct a new UploadedFile instance.
     * @param  string      $file The full path to the uploaded file provided by the client.
     * @param  string|null $name The file name.
     * @param  string|null $type The file media type.
     * @param  int|null    $size The file size in bytes.
     * @param  int         $error The UPLOAD_ERR_XXX code representing the status of the upload.
     * @param  bool        $sapi Indicates if the upload is in a SAPI environment.
    public function __construct($file, $name = null, $type = null, $size = null, $error = UPLOAD_ERR_OK, $sapi = false);

3- we then Post the data to the controller and get the body content, and convert it from JSON to Array.

4- we do small tests to make sure that:

  • The file exists in the uploaded folder
  • We got the file name key in the results
  • The file name we got is equal to the file name we sent
  • Finally, we received response code 200.

5- we delete the file from the uploaded storage directory, we don't need to delete it from the test directory cause we use the functionmoveTo in our upload controller.

and finally, we got green colour for the test.

Share It via: