diff --git a/src/Domain/Enums/Campaign/CampaignStatus.php b/src/Domain/Enums/Campaign/CampaignStatus.php new file mode 100644 index 0000000..96142de --- /dev/null +++ b/src/Domain/Enums/Campaign/CampaignStatus.php @@ -0,0 +1,28 @@ + */ + use BaseEnum; + + case CREATED = 1; + case TRANSFORMED = 2; + case REJECTED = 3; + + /** + * Optional labels definition + * + * @phpstan-ignore-next-line + */ + private const LABELS = [ + self::CREATED->name => 'Crée', + self::TRANSFORMED->name => 'Transformé', + self::REJECTED->name => 'Rejeté' + ]; +} diff --git a/src/Domain/Enums/Campaign/CampaignType.php b/src/Domain/Enums/Campaign/CampaignType.php new file mode 100644 index 0000000..5728cb3 --- /dev/null +++ b/src/Domain/Enums/Campaign/CampaignType.php @@ -0,0 +1,26 @@ + */ + use BaseEnum; + + case CLUB = 1; + case GAME = 2; + + /** + * Optional labels definition + * + * @phpstan-ignore-next-line + */ + private const LABELS = [ + self::CLUB->name => 'CLUB', + self::GAME->name => 'GAME', + ]; +} diff --git a/src/Domain/Models/Entities/CampaignRequest.php b/src/Domain/Models/Entities/CampaignRequest.php new file mode 100644 index 0000000..6512dff --- /dev/null +++ b/src/Domain/Models/Entities/CampaignRequest.php @@ -0,0 +1,73 @@ + + */ +final readonly class CampaignRequest implements AggregateRoot, HasTimestampIdentifier +{ + private function __construct( + public CampaignRequestId $id, + public BrandCode $code, + public Applicant $applicant, + public Country $country, + public CampaignType $campaignType, + public ?PrintRules $printRules, + public RequestedCoupon $requestedCoupon, + public CampaignStatus $status, + public ?\DateTimeInterface $updatedAt = null, + ) {} + + public function id(): CampaignRequestId + { + return $this->id; + } + + public function createdAt(): \DateTimeInterface + { + return $this->id->toDateTime(); + } + + public function updatedAt(): ?\DateTimeInterface + { + return $this->updatedAt; + } + + + public static function hydrate( + CampaignRequestId $id, + BrandCode $code, + Applicant $applicant, + Country $country, + CampaignType $campaignType, + ?PrintRules $printRules, + RequestedCoupon $requestedCoupon, + CampaignStatus $status, + ?\DateTimeInterface $updatedAt = null, + ): self { + return new self( + id: $id, + code: $code, + applicant: $applicant, + country: $country, + campaignType: $campaignType, + printRules: $printRules, + requestedCoupon: $requestedCoupon, + status: $status, + updatedAt: $updatedAt, + ); + } +} diff --git a/src/Domain/Models/Entities/RequestedCoupon.php b/src/Domain/Models/Entities/RequestedCoupon.php new file mode 100644 index 0000000..3ef925f --- /dev/null +++ b/src/Domain/Models/Entities/RequestedCoupon.php @@ -0,0 +1,84 @@ + + */ +final readonly class RequestedCoupon implements AggregateRoot, HasTimestampIdentifier +{ + private function __construct( + public RequestedCouponId $id, + public CouponName $name, + public CouponProductName $productName, + public int $quantity, + public int $warningThresholdQuantity, + public Period $period, + public MoneyAmount $moneyAmount, + public ProcessingCode $processingCode, + public CouponPicture $couponPicture, + public GS1 $gs1, + public CouponValidity $couponValidity, + public ?\DateTimeInterface $updatedAt = null, + ) { + } + + public function id(): RequestedCouponId + { + return $this->id; + } + + public function createdAt(): \DateTimeInterface + { + return $this->id->toDateTime(); + } + + public function updatedAt(): ?\DateTimeInterface + { + return $this->updatedAt; + } + + public static function hydrate( + RequestedCouponId $id, + CouponName $name, + CouponProductName $productName, + int $quantity, + int $warningThresholdQuantity, + Period $period, + MoneyAmount $moneyAmount, + ProcessingCode $processingCode, + CouponPicture $couponPicture, + GS1 $gs1, + CouponValidity $couponValidity, + ?\DateTimeInterface $updatedAt = null, + ): self { + return new self( + id: $id, + name: $name, + productName: $productName, + quantity: $quantity, + warningThresholdQuantity: $warningThresholdQuantity, + period: $period, + moneyAmount: $moneyAmount, + processingCode: $processingCode, + couponPicture: $couponPicture, + gs1: $gs1, + couponValidity: $couponValidity, + updatedAt: $updatedAt, + ); + } +} diff --git a/src/Domain/Models/ValueObjects/CampaignRequest/Applicant.php b/src/Domain/Models/ValueObjects/CampaignRequest/Applicant.php new file mode 100644 index 0000000..7375c56 --- /dev/null +++ b/src/Domain/Models/ValueObjects/CampaignRequest/Applicant.php @@ -0,0 +1,36 @@ + 200) { + throw new \DomainException('Applicant firstname exceeds the maximum allowed length.'); + } + + $trimmedLastname = trim($lastname); + + if ($trimmedLastname === '') { + throw new \DomainException('Applicant lastname cannot be empty.'); + } + + if (mb_strlen($trimmedLastname) > 200) { + throw new \DomainException('Applicant lastname exceeds the maximum allowed length.'); + } + } +} diff --git a/src/Domain/Models/ValueObjects/CampaignRequest/CampaignRequestId.php b/src/Domain/Models/ValueObjects/CampaignRequest/CampaignRequestId.php new file mode 100644 index 0000000..459d131 --- /dev/null +++ b/src/Domain/Models/ValueObjects/CampaignRequest/CampaignRequestId.php @@ -0,0 +1,16 @@ +value())->toBe(1) + ->and(CampaignStatus::TRANSFORMED->value())->toBe(2) + ->and(CampaignStatus::REJECTED->value())->toBe(3); + }); + + it('exposes its case name', function (): void { + // THEN + expect(CampaignStatus::CREATED->name())->toBe('CREATED'); + }); + + it('returns the defined label', function (): void { + // THEN + expect(CampaignStatus::CREATED->label())->toBe('Crée') + ->and(CampaignStatus::TRANSFORMED->label())->toBe('Transformé') + ->and(CampaignStatus::REJECTED->label())->toBe('Rejeté'); + }); + + it('lists all labels keyed by case name', function (): void { + // THEN + expect(CampaignStatus::toLabels())->toBe([ + 'CREATED' => 'Crée', + 'TRANSFORMED' => 'Transformé', + 'REJECTED' => 'Rejeté', + ]); + }); + + it('lists all values keyed by case name', function (): void { + // THEN + expect(CampaignStatus::toValues())->toBe([ + 'CREATED' => 1, + 'TRANSFORMED' => 2, + 'REJECTED' => 3, + ]); + }); + + it('makes an instance from its backing value', function (): void { + // THEN + expect(CampaignStatus::make(1))->toBe(CampaignStatus::CREATED); + }); + + it('makes an instance from a label', function (): void { + // THEN + expect(CampaignStatus::make('Transformé', 'label'))->toBe(CampaignStatus::TRANSFORMED); + }); + + it('returns the same instance when already an enum case', function (): void { + // THEN + expect(CampaignStatus::make(CampaignStatus::REJECTED))->toBe(CampaignStatus::REJECTED); + }); + + it('returns null for an unknown value', function (): void { + // THEN + expect(CampaignStatus::make(99))->toBeNull(); + }); + + it('implements equals correctly', function (): void { + // THEN + expect(CampaignStatus::CREATED->equals(CampaignStatus::CREATED))->toBeTrue() + ->and(CampaignStatus::CREATED->equals(CampaignStatus::REJECTED))->toBeFalse() + ->and(CampaignStatus::CREATED->equals(null))->toBeFalse(); + }); + + it('serializes to json as its backing value', function (): void { + // THEN + expect(CampaignStatus::CREATED->jsonSerialize())->toBe(1) + ->and(json_encode(CampaignStatus::CREATED))->toBe('1'); + }); +}); diff --git a/tests/Unit/Domain/Enums/Campaign/CampaignTypeTest.php b/tests/Unit/Domain/Enums/Campaign/CampaignTypeTest.php new file mode 100644 index 0000000..2504339 --- /dev/null +++ b/tests/Unit/Domain/Enums/Campaign/CampaignTypeTest.php @@ -0,0 +1,77 @@ +value())->toBe(1) + ->and(CampaignType::GAME->value())->toBe(2); + }); + + it('exposes its case name', function (): void { + // THEN + expect(CampaignType::CLUB->name())->toBe('CLUB'); + }); + + it('returns the defined label', function (): void { + // THEN + expect(CampaignType::CLUB->label())->toBe('CLUB') + ->and(CampaignType::GAME->label())->toBe('GAME'); + }); + + it('lists all labels keyed by case name', function (): void { + // THEN + expect(CampaignType::toLabels())->toBe([ + 'CLUB' => 'CLUB', + 'GAME' => 'GAME', + ]); + }); + + it('lists all values keyed by case name', function (): void { + // THEN + expect(CampaignType::toValues())->toBe([ + 'CLUB' => 1, + 'GAME' => 2, + ]); + }); + + it('makes an instance from its backing value', function (): void { + // THEN + expect(CampaignType::make(1))->toBe(CampaignType::CLUB); + }); + + it('makes an instance from a label', function (): void { + // THEN + expect(CampaignType::make('GAME', 'label'))->toBe(CampaignType::GAME); + }); + + it('returns the same instance when already an enum case', function (): void { + // THEN + expect(CampaignType::make(CampaignType::CLUB))->toBe(CampaignType::CLUB); + }); + + it('returns null for an unknown value', function (): void { + // THEN + expect(CampaignType::make(99))->toBeNull(); + }); + + it('implements equals correctly', function (): void { + // THEN + expect(CampaignType::CLUB->equals(CampaignType::CLUB))->toBeTrue() + ->and(CampaignType::CLUB->equals(CampaignType::GAME))->toBeFalse() + ->and(CampaignType::CLUB->equals(null))->toBeFalse(); + }); + + it('serializes to json as its backing value', function (): void { + // THEN + expect(CampaignType::CLUB->jsonSerialize())->toBe(1) + ->and(json_encode(CampaignType::CLUB))->toBe('1'); + }); +}); diff --git a/tests/Unit/Domain/Models/Entities/CampaignRequestTest.php b/tests/Unit/Domain/Models/Entities/CampaignRequestTest.php new file mode 100644 index 0000000..64000d9 --- /dev/null +++ b/tests/Unit/Domain/Models/Entities/CampaignRequestTest.php @@ -0,0 +1,154 @@ +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, + aRequestedCoupon(), + 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), + aRequestedCoupon(), + CampaignStatus::CREATED, + $updatedAt, + ); + + // THEN + expect($campaignRequest->updatedAt)->toBe($updatedAt) + ->and($campaignRequest->updatedAt())->toBe($updatedAt); + }); + + 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), + aRequestedCoupon(), + 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')); + }); +}); diff --git a/tests/Unit/Domain/Models/Entities/RequestedCouponTest.php b/tests/Unit/Domain/Models/Entities/RequestedCouponTest.php new file mode 100644 index 0000000..b69de01 --- /dev/null +++ b/tests/Unit/Domain/Models/Entities/RequestedCouponTest.php @@ -0,0 +1,126 @@ +id())->toBe($id) + ->and($coupon->name)->toBe($name) + ->and($coupon->productName)->toBe($productName) + ->and($coupon->quantity)->toBe($quantity) + ->and($coupon->warningThresholdQuantity)->toBe($warningThresholdQuantity) + ->and($coupon->period)->toBe($period) + ->and($coupon->moneyAmount)->toBe($moneyAmount) + ->and($coupon->processingCode)->toBe($processingCode) + ->and($coupon->couponPicture)->toBe($couponPicture) + ->and($coupon->gs1)->toBe($gs1) + ->and($coupon->couponValidity)->toBe($couponValidity) + ->and($coupon->updatedAt)->toBeNull() + ->and($coupon->updatedAt())->toBeNull(); + }); + + it('hydrates a requested coupon with an explicit updatedAt date', function (): void { + // GIVEN + $updatedAt = new \DateTimeImmutable('2026-06-15 12:00:00', new \DateTimeZone('UTC')); + + // WHEN + $coupon = 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()), + $updatedAt, + ); + + // THEN + expect($coupon->updatedAt)->toBe($updatedAt) + ->and($coupon->updatedAt())->toBe($updatedAt); + }); + + 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 RequestedCouponId(Uuid::uuid7($expected)); + $coupon = RequestedCoupon::hydrate( + $id, + 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()), + ); + + // WHEN + $result = $coupon->createdAt(); + + // THEN + expect($result->format('Y-m-d H:i:s.v'))->toBe($expected->format('Y-m-d H:i:s.v')); + }); +}); diff --git a/tests/Unit/Domain/Models/ValueObjects/CampaignRequest/ApplicantTest.php b/tests/Unit/Domain/Models/ValueObjects/CampaignRequest/ApplicantTest.php new file mode 100644 index 0000000..e46349c --- /dev/null +++ b/tests/Unit/Domain/Models/ValueObjects/CampaignRequest/ApplicantTest.php @@ -0,0 +1,94 @@ +toBeInstanceOf(Applicant::class) + ->and($applicant->firstname)->toBe($firstname) + ->and($applicant->lastname)->toBe($lastname) + ->and($applicant->email)->toBe($email); + }); + + it('rejects an empty or whitespace-only firstname', function (string $firstname): void { + // WHEN + $action = fn (): Applicant => new Applicant($firstname, 'Doe', new Email('john.doe@example.com')); + + // THEN + expect($action)->toThrow(\DomainException::class, 'Applicant firstname cannot be empty.'); + })->with([ + 'empty' => '', + 'spaces' => ' ', + ]); + + it('rejects a firstname that exceeds the maximum allowed length', function (): void { + // GIVEN + $firstname = str_repeat('a', 201); + + // WHEN + $action = fn (): Applicant => new Applicant($firstname, 'Doe', new Email('john.doe@example.com')); + + // THEN + expect($action)->toThrow(\DomainException::class, 'Applicant firstname exceeds the maximum allowed length.'); + }); + + it('accepts a firstname of exactly the maximum length', function (): void { + // GIVEN + $firstname = str_repeat('a', 200); + + // WHEN + $applicant = new Applicant($firstname, 'Doe', new Email('john.doe@example.com')); + + // THEN + expect(mb_strlen($applicant->firstname))->toBe(200); + }); + + it('rejects an empty or whitespace-only lastname', function (string $lastname): void { + // WHEN + $action = fn (): Applicant => new Applicant('John', $lastname, new Email('john.doe@example.com')); + + // THEN + expect($action)->toThrow(\DomainException::class, 'Applicant lastname cannot be empty.'); + })->with([ + 'empty' => '', + 'spaces' => ' ', + ]); + + it('rejects a lastname that exceeds the maximum allowed length', function (): void { + // GIVEN + $lastname = str_repeat('a', 201); + + // WHEN + $action = fn (): Applicant => new Applicant('John', $lastname, new Email('john.doe@example.com')); + + // THEN + expect($action)->toThrow(\DomainException::class, 'Applicant lastname exceeds the maximum allowed length.'); + }); + + it('accepts a lastname of exactly the maximum length', function (): void { + // GIVEN + $lastname = str_repeat('a', 200); + + // WHEN + $applicant = new Applicant('John', $lastname, new Email('john.doe@example.com')); + + // THEN + expect(mb_strlen($applicant->lastname))->toBe(200); + }); +}); diff --git a/tests/Unit/Domain/Models/ValueObjects/CampaignRequest/CampaignRequestIdTest.php b/tests/Unit/Domain/Models/ValueObjects/CampaignRequest/CampaignRequestIdTest.php new file mode 100644 index 0000000..5de8df4 --- /dev/null +++ b/tests/Unit/Domain/Models/ValueObjects/CampaignRequest/CampaignRequestIdTest.php @@ -0,0 +1,28 @@ +toBeInstanceOf(CampaignRequestId::class); + }); + + it('generates different ids on each call', function (): void { + // WHEN + $first = CampaignRequestId::generate(); + $second = CampaignRequestId::generate(); + + // THEN + expect((string) $first)->not->toBe((string) $second); + }); +}); diff --git a/tests/Unit/Domain/Models/ValueObjects/PrintRulesTest.php b/tests/Unit/Domain/Models/ValueObjects/PrintRulesTest.php new file mode 100644 index 0000000..26092f5 --- /dev/null +++ b/tests/Unit/Domain/Models/ValueObjects/PrintRulesTest.php @@ -0,0 +1,48 @@ +toBeInstanceOf(PrintRules::class) + ->and($printRules->maxPerCustomer)->toBe(3) + ->and($printRules->cooldownDays)->toBe(30); + }); + + it('creates print rules with only maxPerCustomer set', function (): void { + // WHEN + $printRules = new PrintRules(maxPerCustomer: 5); + + // THEN + expect($printRules->maxPerCustomer)->toBe(5) + ->and($printRules->cooldownDays)->toBeNull(); + }); + + it('creates print rules with only cooldownDays set', function (): void { + // WHEN + $printRules = new PrintRules(cooldownDays: 45); + + // THEN + expect($printRules->maxPerCustomer)->toBeNull() + ->and($printRules->cooldownDays)->toBe(45); + }); + + it('creates print rules with both fields null by default', function (): void { + // WHEN + $printRules = new PrintRules(); + + // THEN + expect($printRules->maxPerCustomer)->toBeNull() + ->and($printRules->cooldownDays)->toBeNull(); + }); +}); diff --git a/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponIdTest.php b/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponIdTest.php new file mode 100644 index 0000000..017f4ac --- /dev/null +++ b/tests/Unit/Domain/Models/ValueObjects/RequestedCoupon/RequestedCouponIdTest.php @@ -0,0 +1,28 @@ +toBeInstanceOf(RequestedCouponId::class); + }); + + it('generates different ids on each call', function (): void { + // WHEN + $first = RequestedCouponId::generate(); + $second = RequestedCouponId::generate(); + + // THEN + expect((string) $first)->not->toBe((string) $second); + }); +});