ldc-boutique-cnc-sable/tests/Unit/App/Casts/EmailCastTest.php
2026-10-27 09:58:12 +01:00

80 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
use App\Casts\EmailCast;
use Domain\Models\ValueObjects\Email;
use Illuminate\Database\Eloquent\Model;
it('gets an email', function () {
// GIVEN
$cast = new EmailCast();
$model = \Mockery::mock(Model::class);
// WHEN
$email = $cast->get($model, 'email', 'test@example.com', []);
// THEN
expect($email)->toBeInstanceOf(Email::class)
->and($email->value)->toBe('test@example.com');
});
it('throws exception when getting invalid email', function () {
// GIVEN
$cast = new EmailCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'email', 123, []);
// THEN
})->throws(\UnexpectedValueException::class, 'Email must be a string.');
it('throws exception when getting null email', function () {
// GIVEN
$cast = new EmailCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'email', null, []);
// THEN
})->throws(\UnexpectedValueException::class, 'Email must be a string.');
it('throws exception when setting null email', function () {
// GIVEN
$cast = new EmailCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->set($model, 'email', null, []);
// THEN
})->throws(\InvalidArgumentException::class, 'Email cannot be null.');
it('throws exception when setting invalid email type', function () {
// GIVEN
$cast = new EmailCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->set($model, 'email', 'test@example.com', []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The given value is not an instance of Email.'
);
it('sets an email', function () {
// GIVEN
$cast = new EmailCast();
$model = \Mockery::mock(Model::class);
$email = new Email('test@example.com');
// WHEN
$value = $cast->set($model, 'email', $email, []);
// THEN
expect($value)->toBe('test@example.com');
});