95 lines
2.9 KiB
PHP
95 lines
2.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Tests\Unit\App\Casts;
|
||
|
|
|
||
|
|
use App\Casts\FilePathCast;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
||
|
|
|
||
|
|
describe('FilePathCast', function (): void {
|
||
|
|
$makeModel = static fn (): Model => new class () extends Model
|
||
|
|
{
|
||
|
|
};
|
||
|
|
|
||
|
|
it('returns null on get when value is null or empty string', function (mixed $raw) use ($makeModel): void {
|
||
|
|
// GIVEN
|
||
|
|
$cast = new FilePathCast();
|
||
|
|
$model = $makeModel();
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$result = $cast->get($model, 'picture', $raw, []);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($result)->toBeNull();
|
||
|
|
})->with([
|
||
|
|
'null' => [null],
|
||
|
|
'empty string' => [''],
|
||
|
|
]);
|
||
|
|
|
||
|
|
it('hydrates a StorageFilePath on get when value is a valid path string', function () use ($makeModel): void {
|
||
|
|
// GIVEN
|
||
|
|
$cast = new FilePathCast();
|
||
|
|
$model = $makeModel();
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$result = $cast->get($model, 'picture', 'products/poulet.webp', []);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($result)->toBeInstanceOf(StorageFilePath::class)
|
||
|
|
->and($result?->value)->toBe('products/poulet.webp');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws InvalidArgumentException on get when value is not a string or null', function () use ($makeModel): void {
|
||
|
|
// GIVEN
|
||
|
|
$cast = new FilePathCast();
|
||
|
|
$model = $makeModel();
|
||
|
|
|
||
|
|
// WHEN / THEN
|
||
|
|
expect(fn () => $cast->get($model, 'picture', 123, []))
|
||
|
|
->toThrow(\InvalidArgumentException::class);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('returns null on set when value is null or empty string', function (?string $value) use ($makeModel): void {
|
||
|
|
// GIVEN
|
||
|
|
$cast = new FilePathCast();
|
||
|
|
$model = $makeModel();
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$result = $cast->set($model, 'picture', $value, []);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($result)->toBeNull();
|
||
|
|
})->with([
|
||
|
|
'null' => [null],
|
||
|
|
'empty string' => [''],
|
||
|
|
]);
|
||
|
|
|
||
|
|
it('returns raw string on set for StorageFilePath or valid path string', function () use ($makeModel): void {
|
||
|
|
// GIVEN
|
||
|
|
$cast = new FilePathCast();
|
||
|
|
$model = $makeModel();
|
||
|
|
$vo = new StorageFilePath('products/poulet.webp');
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$fromVo = $cast->set($model, 'picture', $vo, []);
|
||
|
|
$fromString = $cast->set($model, 'picture', 'products/poulet.webp', []);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($fromVo)->toBe('products/poulet.webp')
|
||
|
|
->and($fromString)->toBe('products/poulet.webp');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws InvalidArgumentException on set when value is not StorageFilePath, string, or null', function () use ($makeModel): void {
|
||
|
|
// GIVEN
|
||
|
|
$cast = new FilePathCast();
|
||
|
|
$model = $makeModel();
|
||
|
|
|
||
|
|
// WHEN / THEN
|
||
|
|
/** @phpstan-ignore argument.type */
|
||
|
|
expect(fn () => $cast->set($model, 'picture', 123, []))
|
||
|
|
->toThrow(\InvalidArgumentException::class);
|
||
|
|
});
|
||
|
|
});
|