ldc-cocotte/tests/Integration/WithApp/App/Casts/NutritionalValuesCastTest.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

84 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
use App\Casts\NutritionalValuesCast;
use Domain\Models\ValueObjects\NutritionalValues;
use Illuminate\Database\Eloquent\Model;
it('gets nutritional values', function () {
// GIVEN
$cast = new NutritionalValuesCast();
$model = \Mockery::mock(Model::class);
// WHEN
$values = $cast->get($model, 'nutritional', null, [
'nv_ref_weight_g' => 100.5,
'nv_kj' => 200.0,
'nv_kcal' => null,
]);
// THEN
expect($values)->toBeInstanceOf(NutritionalValues::class)
->and($values->refWeightG)->toBe(100.5)
->and($values->kj)->toBe(200.0)
->and($values->kcal)->toBeNull();
});
it('throws exception when getting invalid float type', function () {
// GIVEN
$cast = new NutritionalValuesCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'nutritional', null, [
'nv_ref_weight_g' => 'abc',
]);
// THEN
})->throws(
\Exception::class,
'Expected `nv_ref_weight_g` to be numeric of null'
);
it('sets nutritional values', function () {
// GIVEN
$cast = new NutritionalValuesCast();
$model = \Mockery::mock(Model::class);
$values = new NutritionalValues(refWeightG: 100.5, kj: 200.0);
// WHEN
$array = $cast->set($model, 'nutritional', $values, []);
// THEN
expect($array)->toBeArray()
->and($array['nv_ref_weight_g'])->toBe(100.5)
->and($array['nv_kj'])->toBe(200.0);
});
it('sets default when null', function () {
// GIVEN
$cast = new NutritionalValuesCast();
$model = \Mockery::mock(Model::class);
// WHEN
$array = $cast->set($model, 'nutritional', null, []);
// THEN
expect($array)->toBeArray()
->and($array['nv_ref_weight_g'])->toBeNull();
});
it('throws exception when setting invalid type', function () {
// GIVEN
$cast = new NutritionalValuesCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->set($model, 'nutritional', 'invalid', []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The given value is not a NutritionalValues instance.'
);