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..5d2beb0 --- /dev/null +++ b/src/Domain/Models/Entities/RequestedCoupon.php @@ -0,0 +1,70 @@ + + */ +final readonly class RequestedCoupon implements AggregateRoot, HasTimestampIdentifier +{ + private function __construct( + public RequestedCouponId $id, + public string $name, + public string $productName, + public int $quantity, + public int $warningThresholdQuantity, + public Period $period, + public MoneyAmount $moneyAmount, + + // FROM COUPONS + public $processingCode, + public $couponPicture, + public $gs1, + public $validity, + 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, + string $name, + string $productName, + int $quantity, + int $warningThresholdQuantity, + Period $period, + ?\DateTimeInterface $updatedAt = null, + ): self { + return new self( + id: $id, + name: $name, + productName: $productName, + quantity: $quantity, + warningThresholdQuantity: $warningThresholdQuantity, + period: $period, + 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/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); + }); +});