2026-10-12 17:08:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Domain\Models\ValueObjects\Campaign;
|
|
|
|
|
|
|
|
|
|
use Domain\Models\ValueObjects\Campaign\CampaignCoupon;
|
|
|
|
|
use Domain\Models\ValueObjects\Campaign\CampaignCouponArray;
|
|
|
|
|
use Domain\Models\ValueObjects\Coupon\CouponCode;
|
|
|
|
|
use Domain\Models\ValueObjects\PrintRules;
|
|
|
|
|
|
|
|
|
|
covers(CampaignCouponArray::class);
|
|
|
|
|
|
|
|
|
|
describe('campaign coupon array', function (): void {
|
|
|
|
|
$coupon = new CampaignCoupon(
|
|
|
|
|
new CouponCode('AAA01'),
|
|
|
|
|
new PrintRules(maxPerCustomer: 3),
|
|
|
|
|
);
|
|
|
|
|
|
2026-10-20 17:39:31 +02:00
|
|
|
$coupon2 = new CampaignCoupon(
|
|
|
|
|
new CouponCode('AAA02'),
|
|
|
|
|
new PrintRules(maxPerCustomer: 3),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
it('creates an array with valid coupons', function () use ($coupon, $coupon2): void {
|
2026-10-12 17:08:07 +02:00
|
|
|
// GIVEN
|
2026-10-20 17:39:31 +02:00
|
|
|
$coupons = [$coupon, $coupon2];
|
2026-10-12 17:08:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$array = new CampaignCouponArray($coupons);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-10-20 17:39:31 +02:00
|
|
|
expect($array->value)->toBe($coupons)
|
|
|
|
|
->and($array->value)->toHaveCount(2);
|
2026-10-12 17:08:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('rejects an empty array', function (): void {
|
|
|
|
|
// GIVEN / WHEN
|
|
|
|
|
$action = fn (): CampaignCouponArray => new CampaignCouponArray([]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class, 'Coupons requires at least one CampaignCoupon.');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('rejects a non-coupon element', function (): void {
|
|
|
|
|
// GIVEN / WHEN
|
|
|
|
|
$action = fn (): CampaignCouponArray => new CampaignCouponArray(['PC1']);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class, 'Coupons must be CampaignCoupon instances.');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('rejects a non-coupon element mixed with valid coupons', function () use ($coupon): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$coupons = [$coupon, 'PC1'];
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn (): CampaignCouponArray => new CampaignCouponArray($coupons);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class, 'Coupons must be CampaignCoupon instances.');
|
|
|
|
|
});
|
2026-10-20 17:39:31 +02:00
|
|
|
|
2026-10-21 17:13:40 +02:00
|
|
|
it('rejects the same coupon twice at initialisation', function (): void {
|
2026-10-20 17:39:31 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$coupons = [
|
|
|
|
|
new CampaignCoupon(new CouponCode('AAA01')),
|
|
|
|
|
new CampaignCoupon(new CouponCode('AAA01')),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$action = fn (): CampaignCouponArray => new CampaignCouponArray($coupons);
|
|
|
|
|
|
|
|
|
|
//WHEN / THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class, 'Coupon "AAA01" is present more than once.');
|
|
|
|
|
});
|
2026-10-21 17:13:40 +02:00
|
|
|
|
|
|
|
|
it('adds a coupon to a campaign coupon array', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$coupons = [
|
|
|
|
|
new CampaignCoupon(new CouponCode('AAA01')),
|
|
|
|
|
new CampaignCoupon(new CouponCode('AAA02')),
|
|
|
|
|
];
|
2026-10-22 10:43:36 +02:00
|
|
|
$array = new CampaignCouponArray($coupons);
|
2026-10-21 17:13:40 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$array = $array->addCoupon(new CampaignCoupon(new CouponCode('AAA03')));
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($array->value)->toHaveCount(3);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('rejects the same coupon twice on update', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$coupons = [
|
|
|
|
|
new CampaignCoupon(new CouponCode('AAA01')),
|
|
|
|
|
new CampaignCoupon(new CouponCode('AAA02')),
|
|
|
|
|
];
|
2026-10-22 10:43:36 +02:00
|
|
|
$array = new CampaignCouponArray($coupons);
|
2026-10-21 17:13:40 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
2026-10-22 10:43:36 +02:00
|
|
|
$action = fn () => $array->addCoupon(new CampaignCoupon(new CouponCode('AAA01')));
|
2026-10-21 17:13:40 +02:00
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class, 'Coupon is already in the campaign.');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('removes a coupon to a campaign coupon array', function () use ($coupon, $coupon2): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$coupons = [$coupon, $coupon2];
|
2026-10-22 10:43:36 +02:00
|
|
|
$array = new CampaignCouponArray($coupons);
|
2026-10-21 17:13:40 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$array = $array->removeCoupon($coupon2->couponCode);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($array->value)->toHaveCount(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cant remove a coupon which is not on the campaign', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$coupons = [
|
|
|
|
|
new CampaignCoupon(new CouponCode('AAA01')),
|
|
|
|
|
new CampaignCoupon(new CouponCode('AAA02')),
|
|
|
|
|
];
|
2026-10-22 10:43:36 +02:00
|
|
|
$array = new CampaignCouponArray($coupons);
|
2026-10-21 17:13:40 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
2026-10-22 10:43:36 +02:00
|
|
|
$action = fn () => $array->removeCoupon(new CouponCode('AAA03'));
|
2026-10-21 17:13:40 +02:00
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($action)->toThrow(\DomainException::class, 'Coupon is not in the campaign.');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns all coupon codes', function () use ($coupon, $coupon2): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$coupons = [$coupon, $coupon2];
|
2026-10-22 10:43:36 +02:00
|
|
|
$array = new CampaignCouponArray($coupons);
|
2026-10-21 17:13:40 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $array->getCouponCodes();
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($result)->toBe([$coupon->couponCode, $coupon2->couponCode]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns the requested coupon', function () use ($coupon, $coupon2): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$coupons = [$coupon, $coupon2];
|
2026-10-22 10:43:36 +02:00
|
|
|
$array = new CampaignCouponArray($coupons);
|
2026-10-21 17:13:40 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $array->findCoupon($coupon->couponCode);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($result)->toBe($coupon);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns null for an unexisting coupon', function () use ($coupon, $coupon2): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$coupons = [$coupon];
|
2026-10-22 10:43:36 +02:00
|
|
|
$array = new CampaignCouponArray($coupons);
|
2026-10-21 17:13:40 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $array->findCoupon($coupon2->couponCode);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($result)->toBeNull();
|
|
|
|
|
});
|
2026-10-12 17:08:07 +02:00
|
|
|
});
|