ldc-file-storage-service/tests/Unit/Lib/UtilsTest.php

207 lines
5.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Uwil\Storage\Tests\Unit\Lib;
use Uwil\Storage\Config\StorageServiceConfig;
use Uwil\Storage\Exceptions\UnexpectedStorageException;
use Uwil\Storage\Lib\Utils;
covers(Utils::class);
enum TestUtilsBackedEnum: string
{
case Local = 'local';
case Unknown = 'unknown_disk';
}
enum TestUtilsUnitEnum
{
case local;
case unknown_disk;
}
describe('Utils diskExists', function (): void {
it('returns true when disk exists in storage', function (): void {
// GIVEN
$disk = 'local';
// WHEN
$exists = Utils::diskExists($disk);
// THEN
expect($exists)->toBeTrue();
});
it('returns false when disk does not exist in storage', function (): void {
// GIVEN
$disk = 'unknown_disk_xyz';
// WHEN
$exists = Utils::diskExists($disk);
// THEN
expect($exists)->toBeFalse();
});
it('handles BackedEnum instances', function (): void {
// GIVEN / WHEN
$existsLocal = Utils::diskExists(TestUtilsBackedEnum::Local);
$existsUnknown = Utils::diskExists(TestUtilsBackedEnum::Unknown);
// THEN
expect($existsLocal)->toBeTrue()
->and($existsUnknown)->toBeFalse();
});
it('handles UnitEnum instances', function (): void {
// GIVEN / WHEN
$existsLocal = Utils::diskExists(TestUtilsUnitEnum::local);
$existsUnknown = Utils::diskExists(TestUtilsUnitEnum::unknown_disk);
// THEN
expect($existsLocal)->toBeTrue()
->and($existsUnknown)->toBeFalse();
});
});
describe('Utils isExtensionAllowed', function (): void {
it('delegates to resolved StorageServiceConfig', function (): void {
// GIVEN
$config = new StorageServiceConfig(
defaultDisk: 'local',
eligibleDisks: ['local'],
laravelExistingDisks: [(object) ['disk' => 'local', 'driver' => 'local']],
allowedExtensions: ['pdf', 'txt']
);
app()->instance(StorageServiceConfig::class, $config);
// WHEN
$pdfAllowed = Utils::isExtensionAllowed('pdf');
$exeAllowed = Utils::isExtensionAllowed('exe');
// THEN
expect($pdfAllowed)->toBeTrue()
->and($exeAllowed)->toBeFalse();
});
it('returns true as safe fallback when StorageServiceConfig resolution fails', function (): void {
// GIVEN
app()->forgetInstance(StorageServiceConfig::class);
app()->bind(StorageServiceConfig::class, function () {
throw new \RuntimeException('Config not bound');
});
// WHEN
$fallback = Utils::isExtensionAllowed('any_ext');
// THEN
expect($fallback)->toBeTrue();
});
});
describe('Utils resolveDriver', function (): void {
it('returns direct driver for non-scoped disk', function (): void {
// GIVEN
config()->set('filesystems.disks.my_s3', ['driver' => 's3']);
// WHEN
$driver = Utils::resolveDriver('my_s3');
// THEN
expect($driver)->toBe('s3');
});
it('resolves parent driver for scoped disk from string', function (): void {
// GIVEN
config()->set('filesystems.disks.parent_local', ['driver' => 'local']);
config()->set('filesystems.disks.scoped_disk', [
'driver' => 'scoped',
'disk' => 'parent_local',
]);
// WHEN
$driver = Utils::resolveDriver('scoped_disk');
// THEN
expect($driver)->toBe('local');
});
it('resolves parent driver for scoped disk when driver is given explicitly', function (): void {
// GIVEN
config()->set('filesystems.disks.parent_s3', ['driver' => 's3']);
config()->set('filesystems.disks.scoped_s3', [
'driver' => 'scoped',
'disk' => 'parent_s3',
]);
// WHEN
$driver = Utils::resolveDriver('scoped_s3', 'scoped');
// THEN
expect($driver)->toBe('s3');
});
it('returns explicit non-scoped driver without reading config', function (): void {
// WHEN
$driver = Utils::resolveDriver('not_configured', 'sftp');
// THEN
expect($driver)->toBe('sftp');
});
it('throws exception when parent disk has no driver configured', function (): void {
// GIVEN
config()->set('filesystems.disks.scoped_orphan', [
'driver' => 'scoped',
'disk' => 'missing_parent',
]);
// WHEN
$action = fn () => Utils::resolveDriver('scoped_orphan');
// THEN
expect($action)->toThrow(
UnexpectedStorageException::class,
"Parent disk 'missing_parent' of scoped disk 'scoped_orphan' has no valid driver configured."
);
});
it('throws exception when scoped disk has missing parent disk', function (): void {
// GIVEN
config()->set('filesystems.disks.scoped_invalid', [
'driver' => 'scoped',
]);
// WHEN
$action = fn () => Utils::resolveDriver('scoped_invalid');
// THEN
expect($action)->toThrow(
UnexpectedStorageException::class,
"Scoped disk 'scoped_invalid' is missing a valid parent 'disk' configuration."
);
});
it('throws exception when scoped disk has circular reference', function (): void {
// GIVEN
config()->set('filesystems.disks.scoped_x', [
'driver' => 'scoped',
'disk' => 'scoped_y',
]);
config()->set('filesystems.disks.scoped_y', [
'driver' => 'scoped',
'disk' => 'scoped_x',
]);
// WHEN
$action = fn () => Utils::resolveDriver('scoped_x');
// THEN
expect($action)->toThrow(
UnexpectedStorageException::class,
"Circular reference detected in scoped disk 'scoped_y'."
);
});
});