ldc-boutique-cnc-sable/tests/Unit/App/Casts/CodeCastTest.php

62 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
use App\Casts\CodeCast;
use Domain\Models\ValueObjects\Code;
use Illuminate\Database\Eloquent\Model;
it('gets a code', function () {
// GIVEN
$cast = new CodeCast();
$model = \Mockery::mock(Model::class);
// WHEN
$code = $cast->get($model, 'code', 'ABCDEFGH', []);
// THEN
expect($code)->toBeInstanceOf(Code::class)
->and($code->value)->toBe('ABCDEFGH');
});
it('throws exception when getting null code', function () {
// GIVEN
$cast = new CodeCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'code', null, []);
// THEN
})->throws(
\UnexpectedValueException::class,
'Code must be a string.'
);
it('sets a code', function () {
// GIVEN
$cast = new CodeCast();
$model = \Mockery::mock(Model::class);
$code = new Code('ABCDEFGH');
// WHEN
$value = $cast->set($model, 'code', $code, []);
// THEN
expect($value)->toBe('ABCDEFGH');
});
it('throws exception when setting invalid code', function () {
// GIVEN
$cast = new CodeCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->set($model, 'code', 'ABCDEFGH', []);
// THEN
})->throws(
\UnexpectedValueException::class,
'The given value is not an instance of Code.'
);