- Implement tests for DiskName instantiation and validation. - Add tests for FileStream instantiation and methods. - Create tests for SanitizedFile instantiation and validation. - Introduce TestDiskBackedEnum and TestDiskUnitEnum for testing purposes.
107 lines
2.9 KiB
PHP
107 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Uwil\Storage\Tests\Unit\Lib\ValueObjects;
|
|
|
|
use Uwil\Storage\Exceptions\UnexpectedStorageException;
|
|
use Uwil\Storage\Lib\ValueObjects\DiskName;
|
|
|
|
covers(DiskName::class);
|
|
|
|
describe('DiskName instantiation', function (): void {
|
|
it('can instantiate with a valid string disk name and allowed driver', function (string $driver): void {
|
|
// GIVEN
|
|
$diskName = 'local';
|
|
|
|
// WHEN
|
|
$vo = new DiskName($diskName, $driver);
|
|
|
|
// THEN
|
|
expect($vo)->toBeInstanceOf(DiskName::class)
|
|
->and($vo->value)->toBe('local')
|
|
->and($vo->driver)->toBe($driver);
|
|
})->with([
|
|
'local driver' => 'local',
|
|
'ftp driver' => 'ftp',
|
|
'sftp driver' => 'sftp',
|
|
's3 driver' => 's3',
|
|
]);
|
|
|
|
it('can instantiate with a BackedEnum value', function (): void {
|
|
// GIVEN
|
|
$enumValue = TestDiskBackedEnum::Local;
|
|
$driver = 'local';
|
|
|
|
// WHEN
|
|
$vo = new DiskName($enumValue, $driver);
|
|
|
|
// THEN
|
|
expect($vo)->toBeInstanceOf(DiskName::class)
|
|
->and($vo->value)->toBe('local')
|
|
->and($vo->driver)->toBe('local');
|
|
});
|
|
|
|
it('can instantiate with a UnitEnum value', function (): void {
|
|
// GIVEN
|
|
$enumValue = TestDiskUnitEnum::local;
|
|
$driver = 'local';
|
|
|
|
// WHEN
|
|
$vo = new DiskName($enumValue, $driver);
|
|
|
|
// THEN
|
|
expect($vo)->toBeInstanceOf(DiskName::class)
|
|
->and($vo->value)->toBe('local')
|
|
->and($vo->driver)->toBe('local');
|
|
});
|
|
});
|
|
|
|
describe('DiskName validation', function (): void {
|
|
it('throws exception if disk does not exist', function (): void {
|
|
// GIVEN
|
|
$nonExistentDisk = 'non_existent_disk';
|
|
$driver = 'local';
|
|
|
|
// WHEN
|
|
$action = fn () => new DiskName($nonExistentDisk, $driver);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(
|
|
UnexpectedStorageException::class,
|
|
"Disk 'non_existent_disk' does not exist."
|
|
);
|
|
});
|
|
|
|
it('throws exception if disk driver is not allowed', function (string $invalidDriver): void {
|
|
// GIVEN
|
|
$diskName = 'local';
|
|
|
|
// WHEN
|
|
$action = fn () => new DiskName($diskName, $invalidDriver);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(
|
|
UnexpectedStorageException::class,
|
|
"Disk driver '{$invalidDriver}' is not allowed."
|
|
);
|
|
})->with([
|
|
'unsupported driver dropbox' => 'dropbox',
|
|
'unsupported driver gcs' => 'gcs',
|
|
'empty driver' => '',
|
|
]);
|
|
});
|
|
|
|
describe('DiskName string representation', function (): void {
|
|
it('can be cast to string', function (): void {
|
|
// GIVEN
|
|
$vo = new DiskName('local', 'local');
|
|
|
|
// WHEN
|
|
$str = (string) $vo;
|
|
|
|
// THEN
|
|
expect($str)->toBe('local')
|
|
->and($vo->__toString())->toBe('local');
|
|
});
|
|
});
|