Hi guys, I went some steps ahead and find a draft solution.
Here the code:
public function testTodoWithImageUpload()
{
Storage::fake('images');
$this->actingAs($this->user)->json('POST', '/todo', [
'todo' => 'Todo Title',
'category' => 'Todo Category',
'description' => 'Todo Description',
'user_id' => $this->user->id,
'image' => UploadedFile::fake()->image('avatar.jpg')
]);
Storage::disk('images')->assertExists('image/'.$_SESSION['testing']);
}
In phpunit.xml
<env name="FILESYSTEM_DRIVER" value="images"/>
Basically I noticed that it had create a folder in storage/framework/testing/images/image/ where it was saving the testing image. The problem was to find the correct path and filename for the final assertExists.
My method that handles the storage is implemented in a way, that it changes the name of file (generating an hashName) and stores it in a subfolder named "image".
So I added the subfolder name in the assertExists and passed the hashName in a session variable (see the code above).
Here is the code of the function in charge of the Todo storage:
public function handleStoreTodo($request)
{
$parameters=$request->all();
$parameters['image'] = ( null !== $request->file('image'))
? $request->file('image')->store('image')
: 'not-set';
$_SESSION['testing'] = $request->file('image')->hashName();
$parameters['user_id'] = Auth::user()->id;
$todo = $this->todo_rep->create($parameters);
return $todo;
}
Obviously is not yet a good solution, because I don't want to leave this $_SESSION variable usage there: I don't need it for production purpose (just for testing). So the question is: how do I get the hashName of the file in the testing function?
Thanks a lot for help! :)