291 lines
11 KiB
PHP
291 lines
11 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Models\Entities;
|
|
|
|
use Domain\Enums\BarcodeFormat;
|
|
use Domain\Enums\Campaign\CampaignStatus;
|
|
use Domain\Enums\Campaign\CampaignType;
|
|
use Domain\Enums\Country;
|
|
use Domain\Models\Entities\CampaignRequest;
|
|
use Domain\Models\Entities\RequestedCoupon;
|
|
use Domain\Models\ValueObjects\Brand\BrandCode;
|
|
use Domain\Models\ValueObjects\CampaignRequest\Applicant;
|
|
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId;
|
|
use Domain\Models\ValueObjects\CampaignRequest\RejectionReason;
|
|
use Domain\Models\ValueObjects\Coupon\CouponValidity;
|
|
use Domain\Models\ValueObjects\CouponName;
|
|
use Domain\Models\ValueObjects\CouponPicture;
|
|
use Domain\Models\ValueObjects\CouponProductName;
|
|
use Domain\Models\ValueObjects\GS1;
|
|
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\RequestedCouponId;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Shared\Enums\Currency;
|
|
use Shared\ValueObjects\DateTime;
|
|
use Shared\ValueObjects\Email;
|
|
use Shared\ValueObjects\Money;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
covers(CampaignRequest::class);
|
|
|
|
describe('campaign request', function (): void {
|
|
afterEach(function (): void {
|
|
CarbonImmutable::setTestNow();
|
|
});
|
|
|
|
beforeEach(function (): void {
|
|
$this->requestedCoupon = RequestedCoupon::hydrate(
|
|
RequestedCouponId::generate(),
|
|
new CouponName('Coupon A'),
|
|
new CouponProductName('Product A'),
|
|
100,
|
|
10,
|
|
new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01')),
|
|
new MoneyAmount(new Money(Currency::EUR, 300)),
|
|
new ProcessingCode('PC1'),
|
|
new CouponPicture(new PicturePath('coupons/product.jpg'), new PicturePath('coupons/thumbnail.jpg')),
|
|
new GS1('01234567890128', BarcodeFormat::EAN13),
|
|
new CouponValidity(30, DateTime::now()),
|
|
);
|
|
});
|
|
|
|
it('hydrates a campaign request with all its fields', function (): void {
|
|
// GIVEN
|
|
$id = CampaignRequestId::generate();
|
|
$code = new BrandCode('AAA01');
|
|
$applicant = new Applicant('John', 'Doe', new Email('john.doe@example.com'));
|
|
$country = Country::FR;
|
|
$campaignType = CampaignType::CLUB;
|
|
$printRules = new PrintRules(maxPerCustomer: 3);
|
|
$requestedCoupon = $this->requestedCoupon;
|
|
$status = CampaignStatus::CREATED;
|
|
|
|
// WHEN
|
|
$campaignRequest = CampaignRequest::hydrate(
|
|
$id,
|
|
$code,
|
|
$applicant,
|
|
$country,
|
|
$campaignType,
|
|
$printRules,
|
|
$requestedCoupon,
|
|
$status,
|
|
);
|
|
|
|
// THEN
|
|
expect($campaignRequest->id())->toBe($id)
|
|
->and($campaignRequest->code)->toBe($code)
|
|
->and($campaignRequest->applicant)->toBe($applicant)
|
|
->and($campaignRequest->country)->toBe($country)
|
|
->and($campaignRequest->campaignType)->toBe($campaignType)
|
|
->and($campaignRequest->printRules)->toBe($printRules)
|
|
->and($campaignRequest->requestedCoupon)->toBe($requestedCoupon)
|
|
->and($campaignRequest->status)->toBe($status)
|
|
->and($campaignRequest->updatedAt)->toBeNull()
|
|
->and($campaignRequest->updatedAt())->toBeNull();
|
|
});
|
|
|
|
it('hydrates a campaign request with a null printRules', function (): void {
|
|
// WHEN
|
|
$campaignRequest = CampaignRequest::hydrate(
|
|
CampaignRequestId::generate(),
|
|
new BrandCode('AAA01'),
|
|
new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
|
Country::FR,
|
|
CampaignType::CLUB,
|
|
null,
|
|
$this->requestedCoupon,
|
|
CampaignStatus::CREATED,
|
|
);
|
|
|
|
// THEN
|
|
expect($campaignRequest->printRules)->toBeNull();
|
|
});
|
|
|
|
it('hydrates a campaign request with an explicit updatedAt date', function (): void {
|
|
// GIVEN
|
|
$updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC'));
|
|
|
|
// WHEN
|
|
$campaignRequest = CampaignRequest::hydrate(
|
|
CampaignRequestId::generate(),
|
|
new BrandCode('AAA01'),
|
|
new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
|
Country::FR,
|
|
CampaignType::CLUB,
|
|
new PrintRules(maxPerCustomer: 3),
|
|
$this->requestedCoupon,
|
|
CampaignStatus::CREATED,
|
|
null,
|
|
$updatedAt,
|
|
);
|
|
|
|
// THEN
|
|
expect($campaignRequest->updatedAt)->toBe($updatedAt)
|
|
->and($campaignRequest->updatedAt())->toBe($updatedAt);
|
|
});
|
|
|
|
it('hydrates a rejected campaign request with a rejection reason', function (): void {
|
|
// GIVEN
|
|
$rejectionReason = new RejectionReason('Missing legal documents.');
|
|
|
|
// WHEN
|
|
$campaignRequest = CampaignRequest::hydrate(
|
|
CampaignRequestId::generate(),
|
|
new BrandCode('AAA01'),
|
|
new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
|
Country::FR,
|
|
CampaignType::CLUB,
|
|
new PrintRules(maxPerCustomer: 3),
|
|
$this->requestedCoupon,
|
|
CampaignStatus::REJECTED,
|
|
$rejectionReason,
|
|
);
|
|
|
|
// THEN
|
|
expect($campaignRequest->status)->toBe(CampaignStatus::REJECTED)
|
|
->and($campaignRequest->rejectionReason)->toBe($rejectionReason);
|
|
});
|
|
|
|
it('rejects a rejected campaign request without a rejection reason', function (): void {
|
|
// WHEN
|
|
$action = fn (): CampaignRequest => CampaignRequest::hydrate(
|
|
CampaignRequestId::generate(),
|
|
new BrandCode('AAA01'),
|
|
new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
|
Country::FR,
|
|
CampaignType::CLUB,
|
|
new PrintRules(maxPerCustomer: 3),
|
|
$this->requestedCoupon,
|
|
CampaignStatus::REJECTED,
|
|
);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(
|
|
\DomainException::class,
|
|
'A rejection reason is required when the campaign request is rejected.'
|
|
);
|
|
});
|
|
|
|
it('rejects a non-rejected campaign request with a rejection reason', function (): void {
|
|
// WHEN
|
|
$action = fn (): CampaignRequest => CampaignRequest::hydrate(
|
|
CampaignRequestId::generate(),
|
|
new BrandCode('AAA01'),
|
|
new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
|
Country::FR,
|
|
CampaignType::CLUB,
|
|
new PrintRules(maxPerCustomer: 3),
|
|
$this->requestedCoupon,
|
|
CampaignStatus::CREATED,
|
|
new RejectionReason('Missing legal documents.'),
|
|
);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(
|
|
\DomainException::class,
|
|
'A rejection reason can only be set when the campaign request is rejected.'
|
|
);
|
|
});
|
|
|
|
it('rejects a campaign request and sets the rejection reason and updatedAt', function (): void {
|
|
// GIVEN
|
|
CarbonImmutable::setTestNow('2026-06-15 12:00:00');
|
|
$campaignRequest = CampaignRequest::hydrate(
|
|
CampaignRequestId::generate(),
|
|
new BrandCode('AAA01'),
|
|
new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
|
Country::FR,
|
|
CampaignType::CLUB,
|
|
new PrintRules(maxPerCustomer: 3),
|
|
$this->requestedCoupon,
|
|
CampaignStatus::CREATED,
|
|
);
|
|
$reason = new RejectionReason('Missing legal documents.');
|
|
|
|
// WHEN
|
|
$rejected = $campaignRequest->reject($reason);
|
|
|
|
// THEN
|
|
expect($rejected->status)->toBe(CampaignStatus::REJECTED)
|
|
->and($rejected->rejectionReason)->toBe($reason)
|
|
->and($rejected->id())->toBe($campaignRequest->id())
|
|
->and($rejected->code)->toBe($campaignRequest->code)
|
|
->and($rejected->applicant)->toBe($campaignRequest->applicant)
|
|
->and($rejected->country)->toBe($campaignRequest->country)
|
|
->and($rejected->campaignType)->toBe($campaignRequest->campaignType)
|
|
->and($rejected->printRules)->toBe($campaignRequest->printRules)
|
|
->and($rejected->requestedCoupon)->toBe($campaignRequest->requestedCoupon)
|
|
->and($rejected->updatedAt)->not->toBeNull()
|
|
->and($rejected->updatedAt()?->format('Y-m-d H:i:s'))->toBe('2026-06-15 12:00:00');
|
|
});
|
|
|
|
it('rejects rejecting a transformed campaign request', function (): void {
|
|
// GIVEN
|
|
$campaignRequest = CampaignRequest::hydrate(
|
|
CampaignRequestId::generate(),
|
|
new BrandCode('AAA01'),
|
|
new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
|
Country::FR,
|
|
CampaignType::CLUB,
|
|
new PrintRules(maxPerCustomer: 3),
|
|
$this->requestedCoupon,
|
|
CampaignStatus::TRANSFORMED,
|
|
);
|
|
|
|
// WHEN
|
|
$action = fn (): CampaignRequest => $campaignRequest->reject(new RejectionReason('Missing legal documents.'));
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class, 'Only a created campaign request can be rejected.');
|
|
});
|
|
|
|
it('rejects rejecting an already rejected campaign request', function (): void {
|
|
// GIVEN
|
|
$campaignRequest = CampaignRequest::hydrate(
|
|
CampaignRequestId::generate(),
|
|
new BrandCode('AAA01'),
|
|
new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
|
Country::FR,
|
|
CampaignType::CLUB,
|
|
new PrintRules(maxPerCustomer: 3),
|
|
$this->requestedCoupon,
|
|
CampaignStatus::REJECTED,
|
|
new RejectionReason('First reason.'),
|
|
);
|
|
|
|
// WHEN
|
|
$action = fn (): CampaignRequest => $campaignRequest->reject(new RejectionReason('Second reason.'));
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class, 'Only a created campaign request can be rejected.');
|
|
});
|
|
|
|
it('returns the creation date extracted from the id', function (): void {
|
|
// GIVEN
|
|
$expected = new \DateTimeImmutable('2024-06-15 12:34:56.789000', new \DateTimeZone('UTC'));
|
|
$id = new CampaignRequestId(Uuid::uuid7($expected));
|
|
$campaignRequest = CampaignRequest::hydrate(
|
|
$id,
|
|
new BrandCode('AAA01'),
|
|
new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
|
Country::FR,
|
|
CampaignType::CLUB,
|
|
new PrintRules(maxPerCustomer: 3),
|
|
$this->requestedCoupon,
|
|
CampaignStatus::CREATED,
|
|
);
|
|
|
|
// WHEN
|
|
$result = $campaignRequest->createdAt();
|
|
|
|
// THEN
|
|
expect($result->format('Y-m-d H:i:s.v'))->toBe($expected->format('Y-m-d H:i:s.v'));
|
|
});
|
|
});
|