ldc-ovoteam/tests/Unit/Services/ImageOptimizerServiceTest.php
Jérémy Munsch dea6dc8a63
feat(test): add unit tests for ImageOptimizerService
- Implemented tests to verify the functionality of the ImageOptimizerService.
- Tests cover optimization of supported image formats, handling of unsupported files, and cleanup of temporary files.
- Added functionality to store optimized images onto disk using a storage service.
2026-11-06 08:51:44 +01:00

136 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Services\AutoCleanupUploadedFile;
use App\Services\ImageOptimizerService;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Uwil\Storage\Contracts\StorageService;
use Uwil\Storage\Lib\ValueObjects\DiskName;
covers(ImageOptimizerService::class);
describe('ImageOptimizerService', function (): void {
$service = new ImageOptimizerService();
beforeEach(fn () => $service = new ImageOptimizerService());
it(
'optimizes supported image formats and leaves unsupported files untouched',
function (string $filename, string $mime, bool $shouldOptimize) use ($service): void {
// GIVEN
$file = str_starts_with($mime, 'image/')
? UploadedFile::fake()->image($filename)
: UploadedFile::fake()->create($filename, 100, $mime);
// WHEN
$result = $service->optimize($file);
// THEN
if ($shouldOptimize) {
expect($result)
->toBeInstanceOf(AutoCleanupUploadedFile::class)
->and($result->getClientOriginalName())->toBe($filename)
->and($result->isValid())->toBeTrue();
} else {
expect($result)->toBe($file);
}
}
)->with([
'jpeg image' => ['picture.jpg', 'image/jpeg', true],
'png image' => ['picture.png', 'image/png', true],
'webp image' => ['picture.webp', 'image/webp', true],
'pdf doc' => ['document.pdf', 'application/pdf', false],
]);
it(
'returns non-image files untouched without optimization',
function () use ($service): void {
// GIVEN
$pdf = UploadedFile::fake()->create('document.pdf', 250, 'application/pdf');
// WHEN
$result = $service->optimize($pdf);
// THEN
expect($result)->toBe($pdf);
}
);
it(
'optimizes a JPEG image with configured quality while preserving name and mime type',
function () use ($service): void {
// GIVEN
$file = UploadedFile::fake()->image('photo.jpg', 800, 800);
// WHEN
$optimized = $service->optimize($file, quality: 70);
// THEN
expect($optimized)
->toBeInstanceOf(AutoCleanupUploadedFile::class)
->and($optimized->getClientOriginalName())->toBe('photo.jpg')
->and($optimized->getMimeType())->toBe('image/jpeg')
->and($optimized->isValid())->toBeTrue()
->and(file_exists($optimized->getPathname()))->toBeTrue();
}
);
it(
'preserves the original PNG format and extension without conversion',
function () use ($service): void {
// GIVEN
$file = UploadedFile::fake()->image('logo.png', 400, 400);
// WHEN
$optimized = $service->optimize($file, quality: 70);
// THEN
expect($optimized)
->toBeInstanceOf(AutoCleanupUploadedFile::class)
->and($optimized->getClientOriginalName())->toBe('logo.png')
->and($optimized->getMimeType())->toBe('image/png')
->and($optimized->isValid())->toBeTrue()
->and(file_exists($optimized->getPathname()))->toBeTrue();
}
);
it(
'cleans up the temporary file automatically when destructed',
function () use ($service): void {
// GIVEN
$file = UploadedFile::fake()->image('avatar.png', 200, 200);
$optimized = $service->optimize($file);
$tempPath = $optimized->getPathname();
expect(file_exists($tempPath))->toBeTrue();
// WHEN
unset($optimized);
// THEN
expect(file_exists($tempPath))->toBeFalse();
}
);
it(
'allows storing the optimized file onto disk through storage service',
function () use ($service): void {
// GIVEN
Storage::fake('s3-public');
$file = UploadedFile::fake()->image('product.png', 800, 800);
$optimized = $service->optimize($file, quality: 70);
/** @var StorageService $storageService */
$storageService = app(StorageService::class);
$disk = new DiskName('s3-public', 's3');
// WHEN
$storedPath = $storageService->storeUploadedOntoDisk($optimized, $disk);
// THEN
expect($storedPath->value)->toEndWith('.png')
->and(Storage::disk('s3-public')->exists($storedPath->value))->toBeTrue();
}
);
});