89 lines
2.7 KiB
PHP
89 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Tests\Unit\Infra\Casts;
|
||
|
|
|
||
|
|
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||
|
|
use Infra\Casts\BrandCodeCast;
|
||
|
|
use Infra\Database\Models\Brand;
|
||
|
|
|
||
|
|
covers(BrandCodeCast::class);
|
||
|
|
|
||
|
|
describe('BrandCodeCast Unit Tests', function (): void {
|
||
|
|
$cast = new BrandCodeCast();
|
||
|
|
$model = new Brand();
|
||
|
|
|
||
|
|
it('transforms string from database to BrandCode instance on get', function () use ($cast, $model): void {
|
||
|
|
// GIVEN
|
||
|
|
$dbValue = 'LGL89';
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$result = $cast->get($model, 'uwil_public_id', $dbValue, []);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($result)->toBeInstanceOf(BrandCode::class)
|
||
|
|
->and($result->value)->toBe('LGL89');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws InvalidArgumentException on get when value is null', function () use ($cast, $model): void {
|
||
|
|
// GIVEN & WHEN
|
||
|
|
expect(fn () => $cast->get($model, 'uwil_public_id', null, []))
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
->toThrow(\InvalidArgumentException::class, 'BrandCode value cannot be null');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws InvalidArgumentException on get when value is not a string', function () use ($cast, $model): void {
|
||
|
|
// GIVEN
|
||
|
|
/** @var mixed $invalid */
|
||
|
|
$invalid = 12345;
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
expect(fn () => $cast->get($model, 'uwil_public_id', $invalid, []))
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
->toThrow(\InvalidArgumentException::class, 'Value must be a string');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('transforms BrandCode instance to string on set', function () use ($cast, $model): void {
|
||
|
|
// GIVEN
|
||
|
|
$brandCode = new BrandCode('ABC12');
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$result = $cast->set($model, 'uwil_public_id', $brandCode, []);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($result)->toBe('ABC12');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('transforms raw string to validated string on set', function () use ($cast, $model): void {
|
||
|
|
// GIVEN
|
||
|
|
$value = 'XYZ98';
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$result = $cast->set($model, 'uwil_public_id', $value, []);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($result)->toBe('XYZ98');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws InvalidArgumentException on set when value is null', function () use ($cast, $model): void {
|
||
|
|
// GIVEN & WHEN
|
||
|
|
expect(fn () => $cast->set($model, 'uwil_public_id', null, []))
|
||
|
|
// THEN
|
||
|
|
->toThrow(\InvalidArgumentException::class, 'BrandCode value cannot be null');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('throws InvalidArgumentException on set when value is not a string or BrandCode', function () use ($cast, $model): void {
|
||
|
|
// GIVEN
|
||
|
|
$invalid = 12345;
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
expect(fn () => $cast->set($model, 'uwil_public_id', $invalid, []))
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
->toThrow(\InvalidArgumentException::class, 'Value must be a string or instance of BrandCode');
|
||
|
|
});
|
||
|
|
});
|