Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image Minify: Skip files without minifier & replace faulty message (Backport to 2.x) #1058

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/Task/Assets/ImageMinify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}

Expand Down Expand Up @@ -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, '-', '_'))
Expand Down
11 changes: 11 additions & 0 deletions tests/integration/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}