ldc-cocotte/tests/Integration/Standalones/App/Services/ImageOptimizerServiceTest.php

55 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\Standalones\App\Services;
use App\Services\ImageOptimizerService;
use Illuminate\Http\UploadedFile;
use Psr\Log\NullLogger;
use Shared\Filesystem\TemporaryFileFactory;
covers(ImageOptimizerService::class);
describe('ImageOptimizerService', function (): void {
$makeService = static fn (): ImageOptimizerService => new ImageOptimizerService(
new TemporaryFileFactory(),
new NullLogger(),
);
it('creates a real temp file for optimized images with the original extension', function () use ($makeService): void {
// GIVEN
$service = $makeService();
$file = UploadedFile::fake()->image('picture.jpg', 800, 600);
// WHEN
$optimized = $service->optimize($file, quality: 70);
$path = $optimized->getRealPath();
$optimized->rewind();
$content = $optimized->fread(16);
// THEN
expect($path)->not->toBeFalse()
->and(file_exists($path))->toBeTrue()
->and(pathinfo($path, PATHINFO_EXTENSION))->toBe('jpg')
->and($content)->toBeString();
});
it('creates a real temp file for non-image uploads with a readable path', function () use ($makeService): void {
// GIVEN
$service = $makeService();
$pdf = UploadedFile::fake()->create('document.pdf', 250, 'application/pdf');
// WHEN
$result = $service->optimize($pdf);
$path = $result->getRealPath();
$result->rewind();
$content = $result->fread(16);
// THEN
expect($path)->not->toBeFalse()
->and(file_exists($path))->toBeTrue()
->and(pathinfo($path, PATHINFO_EXTENSION))->toBe('pdf')
->and($content)->toBeString();
});
});