ldc-mesbr-app/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponTest.php
2026-10-02 12:48:00 +02:00

332 lines
13 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Domain\Models\ValueObjects\RequestedCoupon;
use Carbon\CarbonImmutable;
use DateTime;
use Domain\Enums\BarcodeFormat;
use Domain\Models\ValueObjects\Coupon\CouponValidity;
use Domain\Models\ValueObjects\Coupon\CouponValidityInformation;
use Domain\Models\ValueObjects\CouponName;
use Domain\Models\ValueObjects\GS1;
use Domain\Models\ValueObjects\IncoCode;
use Domain\Models\ValueObjects\MoneyAmount;
use Domain\Models\ValueObjects\Period;
use Domain\Models\ValueObjects\PrintRules;
use Domain\Models\ValueObjects\ProcessingCode;
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCoupon;
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCouponQuantity;
use Domain\Models\ValueObjects\WarningThresholdQuantity;
use Shared\Enums\Country;
use Shared\Enums\Currency;
use Shared\ValueObjects\Money;
covers(RequestedCoupon::class);
describe('requested coupon', function (): void {
beforeEach(function (): void {
$this->name = new CouponName('Coupon A');
$this->quantity = new RequestedCouponQuantity(100);
$this->diffusion = new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01'));
$this->moneyAmount = new MoneyAmount(new Money(Currency::EUR, 300));
$this->processingCode = new ProcessingCode('PC1');
$this->couponValidity = new CouponValidity(30, new DateTime('2026-02-01'));
$this->franceValidityInformation = new CouponValidityInformation(
'Lorem ipsum dolor sit amet consectetur adipiscing elit.',
Country::FRANCE
);
$this->beneluxValidityInformation = new CouponValidityInformation(
'Lorem ipsum dolor sit amet consectetur adipiscing elit.',
Country::BENELUX
);
$this->couponValidityInformation = [
$this->franceValidityInformation,
$this->beneluxValidityInformation,
];
$this->products = [new IncoCode(35784), new IncoCode(15522)];
$this->printRules = new PrintRules(maxPerCustomer: 3);
$this->gs1 = new GS1('01234567890128', BarcodeFormat::EAN13);
$this->warningThresholdQuantity = new WarningThresholdQuantity(10);
});
afterEach(function (): void {
CarbonImmutable::setTestNow();
});
it('hydrates a requested coupon with all its fields', function (): void {
// GIVEN / WHEN
$coupon = new RequestedCoupon(
$this->name,
$this->quantity,
$this->diffusion,
$this->moneyAmount,
$this->processingCode,
$this->couponValidity,
$this->couponValidityInformation,
$this->products,
$this->printRules,
$this->gs1,
$this->warningThresholdQuantity
);
// THEN
expect($coupon->name)->toBe($this->name)
->and($coupon->quantity)->toBe($this->quantity)
->and($coupon->diffusion)->toBe($this->diffusion)
->and($coupon->moneyAmount)->toBe($this->moneyAmount)
->and($coupon->processingCode)->toBe($this->processingCode)
->and($coupon->couponValidity)->toBe($this->couponValidity)
->and($coupon->couponValidityInformation)->toBe($this->couponValidityInformation)
->and($coupon->products)->toBe($this->products)
->and($coupon->printRules)->toBe($this->printRules)
->and($coupon->gs1)->toBe($this->gs1)
->and($coupon->warningThresholdQuantity)->toBe($this->warningThresholdQuantity);
});
it('hydrates a requested coupon without products', function (): void {
// GIVEN / WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $this->moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
printRules: $this->printRules,
gs1: $this->gs1,
warningThresholdQuantity: $this->warningThresholdQuantity
);
// THEN
expect($coupon->products)->toBe([])
->and($coupon->printRules)->toBe($this->printRules)
->and($coupon->gs1)->toBe($this->gs1)
->and($coupon->warningThresholdQuantity)->toBe($this->warningThresholdQuantity);
});
it('hydrates a requested coupon without print rules', function (): void {
// GIVEN / WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $this->moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
products: $this->products,
gs1: $this->gs1,
warningThresholdQuantity: $this->warningThresholdQuantity
);
// THEN
expect($coupon->printRules)->toBeNull()
->and($coupon->products)->toBe($this->products)
->and($coupon->gs1)->toBe($this->gs1)
->and($coupon->warningThresholdQuantity)->toBe($this->warningThresholdQuantity);
});
it('hydrates a requested coupon without gs1', function (): void {
// GIVEN / WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $this->moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
products: $this->products,
printRules: $this->printRules,
warningThresholdQuantity: $this->warningThresholdQuantity
);
// THEN
expect($coupon->gs1)->toBeNull()
->and($coupon->products)->toBe($this->products)
->and($coupon->printRules)->toBe($this->printRules)
->and($coupon->warningThresholdQuantity)->toBe($this->warningThresholdQuantity);
});
it('defaults the warning threshold quantity to 100 when not provided', function (): void {
// GIVEN / WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $this->moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
products: $this->products,
printRules: $this->printRules,
gs1: $this->gs1,
);
// THEN
expect($coupon->warningThresholdQuantity)->toEqual(new WarningThresholdQuantity(100))
->and($coupon->products)->toBe($this->products)
->and($coupon->printRules)->toBe($this->printRules)
->and($coupon->gs1)->toBe($this->gs1);
});
it('hydrates a requested coupon with only required fields', function (): void {
// GIVEN / WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $this->moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
);
// THEN
expect($coupon->products)->toBe([])
->and($coupon->printRules)->toBeNull()
->and($coupon->gs1)->toBeNull()
->and($coupon->warningThresholdQuantity)->toEqual(new WarningThresholdQuantity(100));
});
it('throws when there is no validity information for france', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $this->moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: [],
))->toThrow(\DomainException::class, 'A coupon requires validity information for France.');
});
it('throws when validity information contains an invalid item', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new RequestedCoupon(
$this->name,
$this->quantity,
$this->diffusion,
$this->moneyAmount,
$this->processingCode,
$this->couponValidity,
['Not a CouponValidityInformation'],
$this->products,
$this->printRules,
$this->gs1,
$this->warningThresholdQuantity
))->toThrow(\DomainException::class, 'Coupon validity information must be CouponValidityInformation instances.');
});
it('accepts the minimum face value of 0.01€', function (): void {
// GIVEN
$moneyAmount = new MoneyAmount(new Money(Currency::EUR, 1));
// WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
);
// THEN
expect($coupon->moneyAmount)->toBe($moneyAmount);
});
it('accepts the maximum face value of 3.00€', function (): void {
// GIVEN
$moneyAmount = new MoneyAmount(new Money(Currency::EUR, 300));
// WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
);
// THEN
expect($coupon->moneyAmount)->toBe($moneyAmount);
});
it('throws when the face value is zero', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: new MoneyAmount(new Money(Currency::EUR, 0)),
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
))->toThrow(\DomainException::class, 'Coupon face value must be between 0.01€ and 3.00€');
});
it('throws when the face value is above 3.00€', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: new MoneyAmount(new Money(Currency::EUR, 301)),
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
))->toThrow(\DomainException::class, 'Coupon face value must be between 0.01€ and 3.00€');
});
it('throws when a gs1 coupon has no benelux validity information', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $this->moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: [$this->franceValidityInformation],
gs1: $this->gs1,
))->toThrow(\DomainException::class, 'A GS1 coupon requires validity information for Benelux.');
});
it('throws when a gs1 coupon has only benelux validity information', function (): void {
// GIVEN / WHEN / THEN
expect(fn () => new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $this->moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: [$this->beneluxValidityInformation],
gs1: $this->gs1,
))->toThrow(\DomainException::class, 'A coupon requires validity information for France.');
});
it('accepts a coupon without gs1 and without benelux validity information', function (): void {
// GIVEN / WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $this->moneyAmount,
processingCode: $this->processingCode,
couponValidity: $this->couponValidity,
couponValidityInformation: [$this->franceValidityInformation],
);
// THEN
expect($coupon->gs1)->toBeNull();
});
});