It is a Feature test, and you should use Storage::fake('gcs') to override the disk driver.
Jan 27, 2022
4
Level 3
Writing test for function to store files
I have a function in a trait which stores an attachment to a Google Cloud Storage bucket like:
public function storeFiles($type, $id, $attachments)
{
$folder = $type . '/' . $id;
$disk = \Storage::disk('gcs');
if (!empty($attachments)) {
$output = [];
foreach ($attachments as $attachment) {
$file = $attachment['file'];
$filename = $file->getClientOriginalName();
$file->storeAs($folder, $filename);
array_push($output, [
'name' => $filename,
'notes' => $attachment['notes']
]);
}
return $output;
}
}
This can be called for example like:
$this->storeFiles('avatars', $user->id, $request->all()['attachments'])
I want to test this function, and I am reviewing the docs from https://laravel.com/docs/8.x/http-tests#testing-file-uploads
Q1. Should I write a unit test or a feature test?
- I am guessing Feature test
Q2. Do I have to edit the phpUnit.xml file to use local storage or is there a way to simulate the cloud storage bucket?
- I am guessing I can use
Storage::fake('avatars')to produce a "fake" local storage, but I worry that since I specify the disk in thestoreFilesfunction, it may supersede it?
Level 73
1 like
Please or to participate in this conversation.