2
votes

I'm using

if (Input::hasFile('index')) doSomething();

to check if the file was uploaded.

Now I wanna write a PHPUnit test case where the system needs to behave like the "index" file was uploaded.

Input::shouldReceive('hasFile')->andReturn(true)

does not work.

Also there's a note in laravel docs: You should not mock the Request facade. Instead, pass the input you desire into the call method when running your test.

So maybe this isn't such a great idea after all.

I tried mocking \Symfony\Component\HttpFoundation\File\UploadedFile and passing it to my call method but after I define "shouldReceive" for all methods the system is expecting it still doesn't work as I need it to.

Is there any way to test the call so that Input::hasFile would return TRUE without uploading an actual file?

Thank you

1

1 Answers

0
votes

I am not sure how you write mock test code. But you may try it in the following way:

$mockInput = Mockery::mock('\Illuminate\Http\Request');
$mockInput->shouldReceive('file')->andReturn($my_test_data);
Input::swap($mockInput);

Hope this helps.

file here is the input name in your form or API to receive uploaded file. You may change it to the name of your parameter.