Testing upload file with Slim 3 (a better way)

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:

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:

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

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.

Filed under: Code, PHP

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *

Name *
Email *
Website

five − 2 =