Skip to content

Commit

Permalink
Merge pull request #691 from carlos-granados/test-optimize-image-with…
Browse files Browse the repository at this point in the history
…-image-with-multiple-frames

Adds a test for the optimizeImage function with an image that includes multiple frames so that overall code coverage is 100%
  • Loading branch information
nunomaduro authored Oct 9, 2024
2 parents c122702 + f10da9d commit 3026219
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/Unit/Livewire/Questions/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,62 @@
->and($image->height())->toBeLessThanOrEqual(1000);
});

test('optimizeImage method resizes and saves image with multiple frames', function () {
Storage::fake('public');

$user = User::factory()->create();

$gif = new Imagick();

$gif->setFormat('gif');

for ($i = 0; $i < 3; $i++) {
$frame = new Imagick();

$frame->newImage(1200, 1200, new ImagickPixel(match ($i) {
0 => 'red',
1 => 'green',
2 => 'blue',
}));

$frame->setImageFormat('gif');

$gif->addImage($frame);
}

$testImage = UploadedFile::fake()->createWithContent('test.gif', $gif->getImagesBlob());

$path = $testImage->store('images', 'public');

$component = Livewire::actingAs($user)->test(Create::class, [
'toId' => $user->id,
]);

$component->call('optimizeImage', $path);

Storage::disk('public')->assertExists($path);

$optimizedImagePath = Storage::disk('public')->path($path);

$originalImageSize = filesize($testImage->getPathname());
$optimizedImageSize = filesize($optimizedImagePath);

expect($optimizedImageSize)->toBeLessThan($originalImageSize);

$optimizedImage = new Imagick($optimizedImagePath);

expect($optimizedImage->getImageWidth())->toBeLessThanOrEqual(1000)
->and($optimizedImage->getImageHeight())->toBeLessThanOrEqual(1000)
->and($optimizedImage->getNumberImages())->toBe(3);

$frames = $optimizedImage->coalesceImages();

foreach ($frames as $frame) {
expect($frame->getImageWidth())->toBeLessThanOrEqual(1000)
->and($frame->getImageHeight())->toBeLessThanOrEqual(1000);
}
});

test('maxFileSize and maxImages', function () {
$user = User::factory()->create();

Expand Down

0 comments on commit 3026219

Please sign in to comment.