Laravel - Mocking - Storage Fake
The Storage
facade's fake
method allows you to easily generate a fake disk that, combined with the file generation utilities of the Illuminate\Http\UploadedFile
class, greatly simplifies the testing of file uploads. For example:
json('POST', '/photos', [
UploadedFile::fake()->image('photo1.jpg'),
UploadedFile::fake()->image('photo2.jpg')
]);
// Assert one or more files were stored...
Storage::disk('photos')->assertExists('photo1.jpg');
Storage::disk('photos')->assertExists(['photo1.jpg', 'photo2.jpg']);
// Assert one or more files were not stored...
Storage::disk('photos')->assertMissing('missing.jpg');
Storage::disk('photos')->assertMissing(['missing.jpg', 'non-existing.jpg']);
}
}
For more information on testing file uploads, you may consult the HTTP testing documentation's information on file uploads.
By default, the fake
method will delete all files in its temporary directory. If you would like to keep these files, you may use the "persistentFake" method instead.