ldc-boutique-cnc-sable/tests/Unit/App/Actions/Discounts/CreateDiscountInputTest.php
Jérémy Munsch 3398aacc86
refactor(casts): update exception handling to use fully qualified class names
Refactored exception handling in various cast classes to use fully qualified class names for InvalidArgumentException and UnexpectedValueException.
2026-10-27 09:58:45 +01:00

87 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\App\Actions\Discounts;
use App\Actions\Discounts\CreateDiscountInput;
use Domain\Enums\DiscountType;
use Domain\Models\ValueObjects\DatePeriod;
use Domain\Models\ValueObjects\DiscountValue;
use Money\Currency;
use Money\Money;
describe('CreateDiscountInput', function (): void {
it('creates a valid CreateDiscountInput instance with percentage', function (): void {
// GIVEN
$type = DiscountType::PERCENT;
$value = DiscountValue::fromPercentage(20);
$period = new DatePeriod();
// WHEN
$input = new CreateDiscountInput(
type: $type,
value: $value,
is_old_price_displayed: true,
period: $period,
associatedProductsIds: ['018f-product-1', '018f-product-2'],
);
// THEN
expect($input->type)->toBe(DiscountType::PERCENT)
->and($input->value)->toBe($value)
->and($input->is_old_price_displayed)->toBeTrue()
->and($input->period)->toBe($period)
->and($input->associatedProductsIds)->toBe(['018f-product-1', '018f-product-2']);
});
it('creates a valid CreateDiscountInput instance with amount', function (): void {
// GIVEN
$type = DiscountType::AMOUNT;
$value = DiscountValue::fromAmount(new Money(500, new Currency('EUR')));
// WHEN
$input = new CreateDiscountInput(
type: $type,
value: $value,
is_old_price_displayed: false,
);
// THEN
expect($input->type)->toBe(DiscountType::AMOUNT)
->and($input->value)->toBe($value)
->and($input->is_old_price_displayed)->toBeFalse()
->and($input->associatedProductsIds)->toBeEmpty();
});
it('throws when discount value type does not match discount type', function (): void {
// GIVEN
$type = DiscountType::PERCENT;
$value = DiscountValue::fromAmount(new Money(500, new Currency('EUR')));
// WHEN
expect(fn () => new CreateDiscountInput(
type: $type,
value: $value,
is_old_price_displayed: true,
))
// THEN
->toThrow(\InvalidArgumentException::class, 'The discount value type must match the discount type.');
});
it('throws when associatedProductsIds contains non-string items', function (): void {
// GIVEN
$type = DiscountType::PERCENT;
$value = DiscountValue::fromPercentage(10);
// WHEN
expect(fn () => new CreateDiscountInput(
type: $type,
value: $value,
is_old_price_displayed: true,
associatedProductsIds: [123], // @phpstan-ignore argument.type
))
// THEN
->toThrow(\InvalidArgumentException::class);
});
});