From b6837c07c8d4262a61a63f825d2c07f1f5865825 Mon Sep 17 00:00:00 2001 From: Dan Untenzu Date: Wed, 10 Nov 2021 18:40:25 +0100 Subject: [PATCH 1/3] [FEATURE] Image Minify: Add test for errors --- tests/integration/AssetsTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/integration/AssetsTest.php b/tests/integration/AssetsTest.php index 6f45aeaa..b48407f6 100644 --- a/tests/integration/AssetsTest.php +++ b/tests/integration/AssetsTest.php @@ -76,4 +76,15 @@ public function testImageMinification() $this->assertLessThan($initialFileSize, $minifiedFileSize, 'Minified file is smaller than the source file'); $this->assertGreaterThan(0, $minifiedFileSize, 'Minified file is not empty'); } + + public function testImageMinificationErrors() + { + $this->fixtures->createAndCdToSandbox(); + + // fails because file is not an image + $result = $this->taskImageMinify($this->fixtures->dataFile('sample.css')) + ->to(realpath('') . '/dist') + ->run(); + $this->assertFalse($result->wasSuccessful(), $result->getMessage()); + } } From fe061fc9d2eecc736afd9719e64940aa02b0a51b Mon Sep 17 00:00:00 2001 From: Dan Untenzu Date: Wed, 10 Nov 2021 18:52:51 +0100 Subject: [PATCH 2/3] [BUGFIX] Image Minify: Replace missing error message placeholders In case of an error the message contains placeholders which are not replaced anymore downstream (`Minified {filecount} out of {filetotal} images into {destination}`). Print the result message as info and then return a short task message without placeholders instead. --- src/Task/Assets/ImageMinify.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Task/Assets/ImageMinify.php b/src/Task/Assets/ImageMinify.php index 87c5a215..af044d66 100644 --- a/src/Task/Assets/ImageMinify.php +++ b/src/Task/Assets/ImageMinify.php @@ -216,15 +216,15 @@ public function run() } $amount = (count($files) == 1 ? 'image' : 'images'); - $message = "Minified {filecount} out of {filetotal} $amount into {destination}"; + $message = 'Minified {filecount} out of {filetotal} ' . $amount . ' into {destination}'; $context = ['filecount' => count($this->results['success']), 'filetotal' => count($files), 'destination' => $this->to]; - if (count($this->results['success']) == count($files)) { - $this->printTaskSuccess($message, $context); + $this->printTaskInfo($message, $context); - return Result::success($this, $message, $context); + if (count($this->results['success']) == count($files)) { + return Result::success($this, ucfirst($amount) . ' minified.'); } else { - return Result::error($this, $message, $context); + return Result::error($this, ucfirst($amount) . ' minification failed.'); } } From c857919ddc766c200cd6b92559527d084beec2f5 Mon Sep 17 00:00:00 2001 From: Dan Untenzu Date: Wed, 10 Nov 2021 18:54:42 +0100 Subject: [PATCH 3/3] [BUGFIX] Image Minify: Skip files without minifier MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Early return for all files which have no minifier set, to avoid fatal error with ambigous error message “Minifier method cannot be found!”. For example when webp images exist in the image directory. The files are added to the error list right away, to that the task may continue to minify other images and show a result message (“Minified X of Y files”) at the end. --- src/Task/Assets/ImageMinify.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Task/Assets/ImageMinify.php b/src/Task/Assets/ImageMinify.php index af044d66..68c2bd75 100644 --- a/src/Task/Assets/ImageMinify.php +++ b/src/Task/Assets/ImageMinify.php @@ -389,6 +389,12 @@ protected function minify($files) $minifier = 'svgo'; break; } + + // Skip files without available minifier + if ($minifier === '') { + $this->results['error'][] = $from; + continue; + } } else { if (!in_array($this->minifier, $this->minifiers, true) && !is_callable(strtr($this->minifier, '-', '_'))