108 lines
3.3 KiB
PHP
108 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Infra\Casts;
|
|
|
|
use Domain\Models\ValueObjects\Brand\ArcadiaId;
|
|
use Infra\Casts\ArcadiaIdCast;
|
|
use Infra\Database\Models\Brand;
|
|
|
|
covers(ArcadiaIdCast::class);
|
|
|
|
describe('ArcadiaIdCast Unit Tests', function (): void {
|
|
$cast = new ArcadiaIdCast();
|
|
$model = new Brand();
|
|
|
|
it('transforms integer from database to ArcadiaId instance on get', function () use ($cast, $model): void {
|
|
// GIVEN
|
|
$dbValue = 123;
|
|
|
|
// WHEN
|
|
$result = $cast->get($model, 'arcadia_id', $dbValue, []);
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(ArcadiaId::class)
|
|
->and($result->value)->toBe('123');
|
|
});
|
|
|
|
it('transforms numeric string from database to ArcadiaId instance on get', function () use ($cast, $model): void {
|
|
// GIVEN
|
|
$dbValue = '456';
|
|
|
|
// WHEN
|
|
$result = $cast->get($model, 'arcadia_id', $dbValue, []);
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(ArcadiaId::class)
|
|
->and($result->value)->toBe('456');
|
|
});
|
|
|
|
it('throws InvalidArgumentException on get when value is null', function () use ($cast, $model): void {
|
|
// GIVEN & WHEN
|
|
expect(fn () => $cast->get($model, 'arcadia_id', null, []))
|
|
|
|
// THEN
|
|
->toThrow(\InvalidArgumentException::class, 'ArcadiaId value cannot be null');
|
|
});
|
|
|
|
it('throws InvalidArgumentException on get when value is invalid type', function () use ($cast, $model): void {
|
|
// GIVEN & WHEN
|
|
expect(fn () => $cast->get($model, 'arcadia_id', 'abc', []))
|
|
|
|
// THEN
|
|
->toThrow(\InvalidArgumentException::class, 'Value must be an integer or numeric string');
|
|
});
|
|
|
|
it('transforms ArcadiaId instance to integer on set', function () use ($cast, $model): void {
|
|
// GIVEN
|
|
$arcadiaId = new ArcadiaId('789');
|
|
|
|
// WHEN
|
|
$result = $cast->set($model, 'arcadia_id', $arcadiaId, []);
|
|
|
|
// THEN
|
|
expect($result)->toBe(789);
|
|
});
|
|
|
|
it('transforms raw integer to integer on set', function () use ($cast, $model): void {
|
|
// GIVEN
|
|
$value = 42;
|
|
|
|
// WHEN
|
|
$result = $cast->set($model, 'arcadia_id', $value, []);
|
|
|
|
// THEN
|
|
expect($result)->toBe(42);
|
|
});
|
|
|
|
it('transforms numeric string to integer on set', function () use ($cast, $model): void {
|
|
// GIVEN
|
|
$value = '99';
|
|
|
|
// WHEN
|
|
$result = $cast->set($model, 'arcadia_id', $value, []);
|
|
|
|
// THEN
|
|
expect($result)->toBe(99);
|
|
});
|
|
|
|
it('throws InvalidArgumentException on set when value is null', function () use ($cast, $model): void {
|
|
// GIVEN & WHEN
|
|
expect(fn () => $cast->set($model, 'arcadia_id', null, []))
|
|
|
|
// THEN
|
|
->toThrow(\InvalidArgumentException::class, 'ArcadiaId value cannot be null');
|
|
});
|
|
|
|
it('throws InvalidArgumentException on set when value is invalid type', function () use ($cast, $model): void {
|
|
// GIVEN
|
|
$invalid = ['array-not-supported'];
|
|
|
|
// WHEN
|
|
expect(fn () => $cast->set($model, 'arcadia_id', $invalid, []))
|
|
|
|
// THEN
|
|
->toThrow(\InvalidArgumentException::class, 'Value must be an integer, numeric string, or instance of ArcadiaId');
|
|
});
|
|
});
|