From 5f1259a462e554e867e617c1225f0710117d013a Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 17 Dec 2021 01:44:38 +0100 Subject: [PATCH] Fixed: Throw exception if size is not found --- classes/TagGenerator.php | 5 +++++ tests/ImageTagTest.php | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/classes/TagGenerator.php b/classes/TagGenerator.php index 3148f47..6f4f523 100644 --- a/classes/TagGenerator.php +++ b/classes/TagGenerator.php @@ -4,6 +4,7 @@ namespace Aweos\Resizer\Classes; use Aweos\Resizer\Compressors\CompressorNotFoundException; use Aweos\Resizer\Compressors\Factory as CompressorFactory; +use Aweos\Resizer\Exceptions\ResizerException; use Aweos\Resizer\Lib\MediaPath; use Aweos\Resizer\Models\Setting; use Cache; @@ -32,6 +33,10 @@ class TagGenerator { $size = collect(Setting::get('sizes'))->filter(fn ($size) => $size['name'] === $name) ->first(); + if (is_null($size)) { + throw new ResizerException("Size with name {$name} not found."); + } + return explode('x', $size['aspect_ratio']); } diff --git a/tests/ImageTagTest.php b/tests/ImageTagTest.php index a576886..26ebfe4 100644 --- a/tests/ImageTagTest.php +++ b/tests/ImageTagTest.php @@ -4,6 +4,7 @@ namespace Aweos\Resizer\Tests; use Aweos\Resizer\Classes\CacheManager; use Aweos\Resizer\Classes\TagGenerator; +use Aweos\Resizer\Exceptions\ResizerException; use Aweos\Resizer\Lib\MediaPath; use Aweos\Resizer\Models\Setting; use Aweos\Resizer\Tests\TestCase; @@ -38,6 +39,18 @@ class ImageTagTest extends TestCase ); } + public function testItThrowsExceptionWhenSizeNotFound() + { + $this->expectException(ResizerException::class); + Setting::set('folders', ['pages']); + Setting::set('sizes', []); + Setting::set('breakpoints', []); + $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); + UploadedFile::fake()->image('test.jpg', 500, 500)->storeAs('uploads/public/c/pages', 'test-500x500.jpg', 'local'); + + dd(app(CacheManager::class)->get(new MediaPath('pages/test.jpg'), 'notfound', null)); + } + public function testFetchPdfImage() { Setting::set('folders', ['pages']);