George
George

Reputation: 3953

How to Mock File upload in Laravel

I am using a 3rd party API to manage my uploaded images, I have abstracted this API with my own class, and also written class to test file upload, but the files get uploaded to the web every time I run the test, I will like to prevent this and use mock to test that the upload function is being called instead.

How do I go about this?

Upvotes: 0

Views: 2526

Answers (1)

ddutra
ddutra

Reputation: 1509

Here is what I do.

I create a UploadedFile isntance (mock a uploaded file) and pass this to my Service Class - This is my approach for testing my Service Class that handles file uploads.

If you want to test the Controller method, I suggest you go with a Acceptance Test and use a real browser to send the request. I could'nt find a decent way of doing this.

//Mock a uploaded file
  $uploadedFile = new UploadedFile(codecept_data_dir().'/attachments/temporary/' . $filename, $filename, $mimeType, $size);

You will need to use \Symfony\Component\HttpFoundation\File\UploadedFile; on top.

Upvotes: 2

Related Questions