ldc-cocotte/tests/Integration/WithApp/App/Casts/MoneyCastTest.php
Jérémy Munsch 5197e796c0
test(casts): add tests for various cast classes
- Implement tests for DiscountValueCast to validate percentage and amount handling.
- Add tests for EmailCast to ensure proper email handling.
- Create tests for EmployeeSitesCast to validate JSON and array handling.
- Implement tests for MatriculeCast to validate matricule handling.
- Add tests for MoneyCast to ensure proper money handling.
- Create tests for NutritionalValuesCast to validate nutritional values handling.
- Implement tests for OrderEmployeeCast to validate employee data handling.
- Add tests for OrderLineProductCast to validate order line product handling.
- Create tests for OrderReferenceCast to validate order reference handling.
- Implement tests for ShopperEstablishmentCodeCast to validate establishment code handling.
- Add tests for ShopperServiceCodeCast to validate service code handling.
- Create tests for ShopperSiteCodeCast to validate site code handling.
2026-11-15 18:56:19 +01:00

89 lines
2 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('EUR');
});
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, new Currency('EUR'));
// 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.'
);