Adds a new HttpFacadeDownloader implementing the Downloader interface.
Adds a test for the new Downloader.
Why
I find it difficult to write tests with the current downloader as it can't even be mocked by the container and requires a config change. I thought at a minimum this might be a small improvement as the Http facade is pretty good for mocking HTTP calls.
Usage
The downloader is used as normal.
// config/media-library.php
/*
* When using the addMediaFromUrl method you may want to replace the default downloader.
* This is particularly useful when the url of the image is behind a firewall and
* need to add additional flags, possibly using curl.
*/
'media_downloader' => Spatie\MediaLibrary\Downloaders\HttpFacadeDownloader::class,
This then makes it easier in tests to mock the download of files.
Http::fake([
// Stub a response where the body will be the contents of the file
'http://medialibrary.spatie.be/assets/images/mountain.jpg' => Http::response('::file::'),
]);
// Execute code for the test
// Then check that a request for the file was made
Http::assertSent(function (Request $request) {
return $request->url() == 'http://medialibrary.spatie.be/assets/images/mountain.jpg';
});
// We may also assert that the contents of any files created
// will contain `::file::`
Changes
Why
I find it difficult to write tests with the current downloader as it can't even be mocked by the container and requires a config change. I thought at a minimum this might be a small improvement as the Http facade is pretty good for mocking HTTP calls.
Usage
The downloader is used as normal.
This then makes it easier in tests to mock the download of files.
with a test like this: