From 37c0cbffba6bd51608ca91f32086e66e0a10fe1a Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 30 Oct 2021 21:13:55 +0200 Subject: [PATCH] Add exception when root file cannot be found --- classes/ImageResizer.php | 5 +++++ composer.json | 3 ++- exceptions/ResizerException.php | 9 +++++++++ lib/MediaPath.php | 7 ++++++- tests/ResizerTest.php | 15 ++++++++++++++- 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 exceptions/ResizerException.php diff --git a/classes/ImageResizer.php b/classes/ImageResizer.php index ec42644..198af4d 100644 --- a/classes/ImageResizer.php +++ b/classes/ImageResizer.php @@ -2,6 +2,7 @@ namespace Aweos\Resizer\Classes; +use Aweos\Resizer\Exceptions\ResizerException; use Aweos\Resizer\Lib\MediaPath; use Aweos\Resizer\Models\Setting; use Illuminate\Filesystem\FilesystemAdapter; @@ -29,6 +30,10 @@ class ImageResizer { $this->file = $file; + if (!$file->exists()) { + throw new ResizerException('File versions cannot be generated. Root file "'.$file->root().'" doesnt exist.'); + } + if ($this->file->compressor()->shouldGenerateVersions()) { $this->generateVersions(); } diff --git a/composer.json b/composer.json index 76d7265..5218ec4 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ ], "autoload": { "psr-4": { - "Aweos\\Resizer\\Compilers\\": "./compilers" + "Aweos\\Resizer\\Compilers\\": "./compilers", + "Aweos\\Resizer\\Exceptions\\": "./exceptions", } } } diff --git a/exceptions/ResizerException.php b/exceptions/ResizerException.php new file mode 100644 index 0000000..694b709 --- /dev/null +++ b/exceptions/ResizerException.php @@ -0,0 +1,9 @@ +path, PATHINFO_FILENAME); } + public function exists(): bool + { + return file_exists($this->root()); + } + public function type(): ?string { - if (!file_exists($this->root())) { + if (!$this->exists()) { return null; } diff --git a/tests/ResizerTest.php b/tests/ResizerTest.php index 214f6a8..e73b0a7 100644 --- a/tests/ResizerTest.php +++ b/tests/ResizerTest.php @@ -2,6 +2,7 @@ namespace Aweos\Resizer\Tests; +use Aweos\Resizer\Exceptions\ResizerException; use Aweos\Resizer\Models\Setting; use Aweos\Resizer\Tests\TestCase; use Event; @@ -24,9 +25,21 @@ class ResizerTest extends TestCase Setting::set('folders', []); Setting::set('sizes', []); Setting::set('breakpoints', []); - $file = UploadedFile::fake()->image('test.jpg', 500, 600); $this->media->put('/pages/test.jpg', $file); + + Event::fire('media.file.upload', [null, '/pages/test.jpg', null]); + + $this->assertFileCount(0, ''); + } + + public function testThrowExceptionWhenOriginalFileNotFound(): void + { + $this->expectException(ResizerException::class); + Setting::set('folders', [['folder' => '/pages']]); + Setting::set('sizes', []); + Setting::set('breakpoints', []); + Event::fire('media.file.upload', [null, '/pages/test.jpg', null]); $this->assertFileCount(0, '');