ldc-file-storage-service/tests/Unit/Lib/PathGenerators/UuidPathGeneratorTest.php

105 lines
3.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Uwil\Storage\Tests\Unit\Lib\PathGenerators;
use Illuminate\Support\Str;
use Uwil\Storage\Config\StorageServiceConfig;
use Uwil\Storage\Lib\PathGenerators\UuidPathGenerator;
use Uwil\Storage\Lib\ValueObjects\DiskName;
use Uwil\Storage\Lib\ValueObjects\FileStream;
use Uwil\Storage\Lib\ValueObjects\SanitizedFile;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
covers(UuidPathGenerator::class);
describe('UuidPathGenerator generation without directory', function (): void {
it('generates a storage file path containing a valid UUID and extension', function (): void {
// GIVEN
$generator = new UuidPathGenerator();
$disk = new DiskName('local', 'local');
$sanitizedFile = new SanitizedFile(
originalName: 'picture.png',
sanitizedName: 'picture.png',
contents: FileStream::fromString('image bytes')
);
// WHEN
$result = $generator->generate($disk, $sanitizedFile);
// THEN
expect($result)->toBeInstanceOf(StorageFilePath::class);
$filename = $result->value;
expect($filename)->toMatch('#^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.png$#');
$uuidOnly = pathinfo($filename, PATHINFO_FILENAME);
expect(Str::isUuid($uuidOnly))->toBeTrue();
});
it('generates a storage file path without extension when file has no extension', function (): void {
// GIVEN
$generator = new UuidPathGenerator();
$disk = new DiskName('local', 'local');
$sanitizedFile = new SanitizedFile(
originalName: 'DOCKERFILE',
sanitizedName: 'DOCKERFILE',
contents: FileStream::fromString('FROM php:8.5')
);
// WHEN
$result = $generator->generate($disk, $sanitizedFile);
// THEN
expect($result)->toBeInstanceOf(StorageFilePath::class)
->and(Str::isUuid($result->value))->toBeTrue();
});
it('generates unique paths on successive calls for the same file', function (): void {
// GIVEN
$generator = new UuidPathGenerator();
$disk = new DiskName('local', 'local');
$sanitizedFile = new SanitizedFile(
originalName: 'data.json',
sanitizedName: 'data.json',
contents: FileStream::fromString('{}')
);
// WHEN
$path1 = $generator->generate($disk, $sanitizedFile);
$path2 = $generator->generate($disk, $sanitizedFile);
// THEN
expect($path1->value)->not->toBe($path2->value);
});
});
describe('UuidPathGenerator generation with prefix', function (): void {
it('prepends the configured prefix to the generated UUID filename', function (): void {
// GIVEN
$config = new StorageServiceConfig(
defaultDisk: 'local',
eligibleDisks: ['local'],
laravelExistingDisks: [(object) ['disk' => 'local', 'driver' => 'local']],
allowedExtensions: ['pdf'],
prefix: 'billing/invoices'
);
$generator = new UuidPathGenerator($config);
$disk = new DiskName('local', 'local');
$sanitizedFile = new SanitizedFile(
originalName: 'invoice.pdf',
sanitizedName: 'invoice.pdf',
contents: FileStream::fromString('PDF DATA')
);
// WHEN
$result = $generator->generate($disk, $sanitizedFile);
// THEN
expect($result)->toBeInstanceOf(StorageFilePath::class)
->and($result->value)->toStartWith('billing/invoices/')
->and($result->value)->toMatch('#^billing/invoices/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.pdf$#');
});
});