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

165 lines
4 KiB
PHP

<?php
declare(strict_types=1);
use App\Casts\DiscountValueCast;
use Domain\Enums\DiscountType;
use Domain\Models\ValueObjects\DiscountValue;
use Illuminate\Database\Eloquent\Model;
use Money\Currency;
use Money\Money;
it('gets a discount value from percentage', function () {
// GIVEN
$cast = new DiscountValueCast();
$model = \Mockery::mock(Model::class);
// WHEN
$value = $cast->get(
$model,
'discount',
10,
['type' => DiscountType::PERCENT->value]
);
// THEN
expect($value)->toBeInstanceOf(DiscountValue::class)
->and($value->value())->toBe(10);
});
it('gets a discount value from amount', function () {
// GIVEN
$cast = new DiscountValueCast();
$model = \Mockery::mock(Model::class);
// WHEN
$value = $cast->get(
$model,
'discount',
1000,
['type' => DiscountType::AMOUNT->value]
);
// THEN
expect($value)->toBeInstanceOf(DiscountValue::class)
->and($value->value())->toBeInstanceOf(Money::class)
->and($value->value()->getAmount())->toBe('1000');
});
it('gets a discount value from amount string', function () {
// GIVEN
$cast = new DiscountValueCast();
$model = \Mockery::mock(Model::class);
// WHEN
$value = $cast->get(
$model,
'discount',
'1000',
['type' => (string) DiscountType::AMOUNT->value]
);
// THEN
expect($value)->toBeInstanceOf(DiscountValue::class)
->and($value->value())->toBeInstanceOf(Money::class)
->and($value->value()->getAmount())->toBe('1000');
});
it('throws exception when type is missing', function () {
// GIVEN
$cast = new DiscountValueCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'discount', 10, []);
// THEN
})->throws(
\UnexpectedValueException::class,
'The discount type attribute is missing to cast the value.'
);
it('throws exception when type is invalid', function () {
// GIVEN
$cast = new DiscountValueCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get($model, 'discount', 10, ['type' => 'invalid']);
// THEN
})->throws(
\UnexpectedValueException::class,
'Unknown discount type raw value: invalid'
);
it('throws exception when value is invalid', function () {
// GIVEN
$cast = new DiscountValueCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->get(
$model,
'discount',
'invalid',
['type' => DiscountType::PERCENT->value]
);
// THEN
})->throws(
\UnexpectedValueException::class,
'The discount value must be an integer or a numeric string.'
);
it('throws exception when setting null', function () {
// GIVEN
$cast = new DiscountValueCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->set($model, 'discount', null, []);
// THEN
})->throws(\InvalidArgumentException::class, 'The value cannot be null.');
it('throws exception when setting invalid type', function () {
// GIVEN
$cast = new DiscountValueCast();
$model = \Mockery::mock(Model::class);
// WHEN
$cast->set($model, 'discount', 10, []);
// THEN
})->throws(
\InvalidArgumentException::class,
'The value must be an instance of DiscountValue.'
);
it('sets a discount value percentage', function () {
// GIVEN
$cast = new DiscountValueCast();
$model = \Mockery::mock(Model::class);
$discount = DiscountValue::fromPercentage(10);
// WHEN
$value = $cast->set($model, 'discount', $discount, []);
// THEN
expect($value)->toBe(10);
});
it('sets a discount value amount', function () {
// GIVEN
$cast = new DiscountValueCast();
$model = \Mockery::mock(Model::class);
$currency = new Currency('EUR');
$discount = DiscountValue::fromAmount(new Money(1000, $currency));
// WHEN
$value = $cast->set($model, 'discount', $discount, []);
// THEN
expect($value)->toBe(1000);
});