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

89 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
use App\Casts\MoneyCast;
use Illuminate\Database\Eloquent\Model;
use Money\Currency;
use Money\Money;
it('gets a money from string', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
// WHEN
$money = $cast->get($model, 'price', '1000', []);
// THEN
expect($money)->toBeInstanceOf(Money::class)
->and($money->getAmount())->toBe('1000')
->and($money->getCurrency()->getCode())->toBe(resolve(Currency::class)->getCode());
});
it('gets a money from int', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
// WHEN
$money = $cast->get($model, 'price', 1000, []);
// THEN
expect($money)->toBeInstanceOf(Money::class)
->and($money->getAmount())->toBe('1000');
});
it('throws exception when getting invalid money type', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'price', 'abc', []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The given value is not numeric.'
);
it('throws exception when setting invalid money type', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->set($model, 'price', '1000', []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The given value is not an instance of Money.'
);
it('sets a money', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
$money = new Money(1000, resolve(Currency::class));
// WHEN
$value = $cast->set($model, 'price', $money, []);
// THEN
expect($value)->toBe('1000');
});
it('throws exception when getting float', function () {
// GIVEN
$cast = new MoneyCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'price', 10.5, []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The given value must be a string or integer.'
);