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

241 lines
9.5 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\CouponPicture;
use Domain\Models\ValueObjects\GS1;
use Domain\Models\ValueObjects\IncoCode;
use Domain\Models\ValueObjects\MoneyAmount;
use Domain\Models\ValueObjects\Period;
use Domain\Models\ValueObjects\PicturePath;
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->couponPicture = new CouponPicture(
new PicturePath('coupons/product.jpg'),
new PicturePath('coupons/thumbnail.jpg'),
new PicturePath('coupons/inactive_thumbnail.jpg')
);
$this->couponValidity = new CouponValidity(30, new DateTime('2026-02-01'));
$this->couponValidityInformation = [
new CouponValidityInformation(
'Lorem ipsum dolor sit amet consectetur adipiscing elit.',
Country::FRANCE
),
];
$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->couponPicture,
$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->couponPicture)->toBe($this->couponPicture)
->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,
couponPicture:$this->couponPicture,
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,
couponPicture:$this->couponPicture,
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,
couponPicture:$this->couponPicture,
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('hydrates a requested coupon without warning threshold quantity', function (): void {
// GIVEN / WHEN
$coupon = new RequestedCoupon(
name: $this->name,
quantity: $this->quantity,
diffusion: $this->diffusion,
moneyAmount: $this->moneyAmount,
processingCode: $this->processingCode,
couponPicture:$this->couponPicture,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
products: $this->products,
printRules: $this->printRules,
gs1: $this->gs1,
);
// THEN
expect($coupon->warningThresholdQuantity)->toBeNull()
->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,
couponPicture:$this->couponPicture,
couponValidity: $this->couponValidity,
couponValidityInformation: $this->couponValidityInformation,
);
// THEN
expect($coupon->products)->toBe([])
->and($coupon->printRules)->toBeNull()
->and($coupon->gs1)->toBeNull()
->and($coupon->warningThresholdQuantity)->toBeNull();
});
it('accepts an empty validity information list', function (): void {
// GIVEN / WHEN
$coupon = new RequestedCoupon(
$this->name,
$this->quantity,
$this->diffusion,
$this->moneyAmount,
$this->processingCode,
$this->couponPicture,
$this->couponValidity,
[],
$this->products,
$this->printRules,
$this->gs1,
$this->warningThresholdQuantity
);
// THEN
expect($coupon->couponValidityInformation)->toBe([]);
});
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->couponPicture,
$this->couponValidity,
['Not a CouponValidityInformation'],
$this->products,
$this->printRules,
$this->gs1,
$this->warningThresholdQuantity
))->toThrow(\DomainException::class, 'Coupon validity information must be CouponValidityInformation instances.');
});
});