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$#'); }); });