58 lines
1.8 KiB
PHP
58 lines
1.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
use App\Services\ImageOptimizerService;
|
||
|
|
use Illuminate\Http\UploadedFile;
|
||
|
|
use Psr\Log\NullLogger;
|
||
|
|
use Shared\Filesystem\TemporaryFileFactory;
|
||
|
|
|
||
|
|
covers(ImageOptimizerService::class);
|
||
|
|
|
||
|
|
describe('ImageOptimizerService', function (): void {
|
||
|
|
$service = \Mockery::mock(ImageOptimizerService::class);
|
||
|
|
|
||
|
|
beforeEach(function () use (&$service) {
|
||
|
|
$service = new ImageOptimizerService(
|
||
|
|
new TemporaryFileFactory(),
|
||
|
|
new NullLogger(),
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('creates a real temp file for optimized images with the original extension', function () use (&$service): void {
|
||
|
|
// GIVEN
|
||
|
|
$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($optimized)->toBeInstanceOf(\SplFileObject::class)
|
||
|
|
->and($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 (&$service): void {
|
||
|
|
// GIVEN
|
||
|
|
$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($result)->toBeInstanceOf(\SplFileObject::class)
|
||
|
|
->and($path)->not->toBeFalse()
|
||
|
|
->and(file_exists($path))->toBeTrue()
|
||
|
|
->and(pathinfo($path, PATHINFO_EXTENSION))->toBe('pdf')
|
||
|
|
->and($content)->toBeString();
|
||
|
|
});
|
||
|
|
});
|