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

102 lines
2.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Uwil\Storage\Tests\Unit\Lib;
use Uwil\Storage\Config\StorageServiceConfig;
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();
});
});