feat: ✨ add campaign request creation use case
This commit is contained in:
parent
87dace1a23
commit
de9bb5d2c6
15 changed files with 479 additions and 54 deletions
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\CampaignRequest\Create;
|
||||
|
||||
use Domain\Exceptions\BrandNotFoundException;
|
||||
use Domain\Models\Entities\CampaignRequest;
|
||||
use Domain\Ports\Repositories\BrandRepository;
|
||||
use Domain\Ports\Repositories\CampaignRequestRepository;
|
||||
use Shared\DDD\UseCases\Input as BaseInput;
|
||||
use Shared\DDD\UseCases\Transactional;
|
||||
use Shared\DDD\UseCases\UseCase;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/** @implements UseCase<\Application\UseCases\CampaignRequest\Create\Input, \Application\UseCases\CampaignRequest\Create\Output> */
|
||||
final readonly class CreateCampaignRequest implements UseCase, Transactional
|
||||
{
|
||||
public function __construct(
|
||||
private CampaignRequestRepository $campaignRequestRepository,
|
||||
private BrandRepository $brandRepository
|
||||
) {
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function execute(BaseInput $input): Output
|
||||
{
|
||||
Assert::isInstanceOf($input, Input::class);
|
||||
|
||||
if (!$this->brandRepository->existsByCode($input->brandCode)) {
|
||||
throw new BrandNotFoundException($input->brandCode);
|
||||
}
|
||||
|
||||
$campaignRequest = CampaignRequest::create(
|
||||
brandCode: $input->brandCode,
|
||||
name: $input->name,
|
||||
applicant: $input->applicant,
|
||||
campaignType: $input->campaignType,
|
||||
requestedCoupon: $input->requestedCoupon,
|
||||
);
|
||||
|
||||
$this->campaignRequestRepository->save($campaignRequest);
|
||||
|
||||
return new Output(
|
||||
id: $campaignRequest->id(),
|
||||
brandCode: $campaignRequest->brandCode,
|
||||
name: $campaignRequest->name,
|
||||
applicant: $campaignRequest->applicant,
|
||||
campaignType: $campaignRequest->campaignType,
|
||||
requestedCoupon: $campaignRequest->requestedCoupon,
|
||||
status: $campaignRequest->status,
|
||||
createdAt: $campaignRequest->createdAt(),
|
||||
);
|
||||
}
|
||||
}
|
||||
24
src/Application/UseCases/CampaignRequest/Create/Input.php
Normal file
24
src/Application/UseCases/CampaignRequest/Create/Input.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\CampaignRequest\Create;
|
||||
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignName;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\Applicant;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestType;
|
||||
use Shared\DDD\UseCases\Input as UseCaseInput;
|
||||
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCoupon;
|
||||
|
||||
final readonly class Input implements UseCaseInput
|
||||
{
|
||||
public function __construct(
|
||||
public BrandCode $brandCode,
|
||||
public CampaignName $name,
|
||||
public Applicant $applicant,
|
||||
public CampaignRequestType $campaignType,
|
||||
public RequestedCoupon $requestedCoupon,
|
||||
) {
|
||||
}
|
||||
}
|
||||
29
src/Application/UseCases/CampaignRequest/Create/Output.php
Normal file
29
src/Application/UseCases/CampaignRequest/Create/Output.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\CampaignRequest\Create;
|
||||
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignName;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\Applicant;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestStatus;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestType;
|
||||
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCoupon;
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
|
||||
final readonly class Output implements UseCaseOutput
|
||||
{
|
||||
public function __construct(
|
||||
public CampaignRequestId $id,
|
||||
public BrandCode $brandCode,
|
||||
public CampaignName $name,
|
||||
public Applicant $applicant,
|
||||
public CampaignRequestType $campaignType,
|
||||
public RequestedCoupon $requestedCoupon,
|
||||
public CampaignRequestStatus $status,
|
||||
public \DateTimeInterface $createdAt,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -4,12 +4,12 @@ declare(strict_types=1);
|
|||
|
||||
namespace Domain\Models\Entities;
|
||||
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestStatus;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestType;
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignName;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\Applicant;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestStatus;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestType;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\RejectionReason;
|
||||
use Domain\Models\ValueObjects\RequestedCoupon\RequestedCoupon;
|
||||
use Shared\DDD\Models\AggregateRoot;
|
||||
|
|
@ -56,6 +56,26 @@ final readonly class CampaignRequest implements AggregateRoot, HasTimestampIdent
|
|||
return $this->updatedAt;
|
||||
}
|
||||
|
||||
public static function create(
|
||||
BrandCode $brandCode,
|
||||
CampaignName $name,
|
||||
Applicant $applicant,
|
||||
CampaignRequestType $campaignType,
|
||||
RequestedCoupon $requestedCoupon,
|
||||
?\DateTimeInterface $updatedAt = null,
|
||||
): self {
|
||||
return new self(
|
||||
id: CampaignRequestId::generate(),
|
||||
brandCode: $brandCode,
|
||||
name: $name,
|
||||
applicant: $applicant,
|
||||
campaignType: $campaignType,
|
||||
requestedCoupon: $requestedCoupon,
|
||||
status: CampaignRequestStatus::CREATED,
|
||||
rejectionReason: null,
|
||||
updatedAt: $updatedAt,
|
||||
);
|
||||
}
|
||||
|
||||
public static function hydrate(
|
||||
CampaignRequestId $id,
|
||||
|
|
|
|||
|
|
@ -12,5 +12,8 @@ final readonly class Period
|
|||
public DateTime $start,
|
||||
public DateTime $end,
|
||||
) {
|
||||
if ($this->end < $this->start) {
|
||||
throw new \DomainException('Period end date cannot be before its start date.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,9 +15,16 @@ use Domain\Models\ValueObjects\Period;
|
|||
use Domain\Models\ValueObjects\PrintRules;
|
||||
use Domain\Models\ValueObjects\ProcessingCode;
|
||||
use Domain\Models\ValueObjects\WarningThresholdQuantity;
|
||||
use Shared\Enums\Country;
|
||||
|
||||
final readonly class RequestedCoupon
|
||||
{
|
||||
private const int DEFAULT_WARNING_THRESHOLD_QUANTITY = 100;
|
||||
private const int MIN_FACE_VALUE_IN_CENTS = 1;
|
||||
private const int MAX_FACE_VALUE_IN_CENTS = 300;
|
||||
|
||||
public WarningThresholdQuantity $warningThresholdQuantity;
|
||||
|
||||
/**
|
||||
* @param array<CouponValidityInformation> $couponValidityInformation
|
||||
* @param array<IncoCode> $products
|
||||
|
|
@ -34,12 +41,42 @@ final readonly class RequestedCoupon
|
|||
public array $products = [],
|
||||
public ?PrintRules $printRules = null,
|
||||
public ?GS1 $gs1 = null,
|
||||
public ?WarningThresholdQuantity $warningThresholdQuantity = null,
|
||||
?WarningThresholdQuantity $warningThresholdQuantity = null,
|
||||
) {
|
||||
$money = $this->moneyAmount->value;
|
||||
|
||||
foreach ($this->couponValidityInformation as $validityInformation) {
|
||||
if (!$validityInformation instanceof CouponValidityInformation) {
|
||||
throw new \DomainException('Coupon validity information must be CouponValidityInformation instances.');
|
||||
}
|
||||
}
|
||||
|
||||
if ($money->value < self::MIN_FACE_VALUE_IN_CENTS || $money->value > self::MAX_FACE_VALUE_IN_CENTS) {
|
||||
throw new \DomainException(sprintf(
|
||||
'Coupon face value must be between 0.01€ and 3.00€, %s given.',
|
||||
$money,
|
||||
));
|
||||
}
|
||||
|
||||
if (!$this->hasValidityInformationFor(Country::FRANCE)) {
|
||||
throw new \DomainException('A coupon requires validity information for France.');
|
||||
}
|
||||
|
||||
if ($this->gs1 instanceof GS1 && !$this->hasValidityInformationFor(Country::BENELUX)) {
|
||||
throw new \DomainException('A GS1 coupon requires validity information for Benelux.');
|
||||
}
|
||||
|
||||
$this->warningThresholdQuantity = $warningThresholdQuantity ?? new WarningThresholdQuantity(self::DEFAULT_WARNING_THRESHOLD_QUANTITY);
|
||||
}
|
||||
|
||||
private function hasValidityInformationFor(Country $country): bool
|
||||
{
|
||||
foreach ($this->couponValidityInformation as $validityInformation) {
|
||||
if ($validityInformation->forLocale === $country) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use Shared\ValueObjects\Pagination\Paginated;
|
|||
use Shared\ValueObjects\Pagination\Pagination;
|
||||
|
||||
/**
|
||||
* @extends \Shared\DDD\Ports\Repository<\Domain\Models\ValueObjects\Brand\BrandId, \Domain\Models\Entities\Brand>
|
||||
* @extends \Shared\DDD\Ports\Repository<\Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId, \Domain\Models\Entities\CampaignRequest>
|
||||
*/
|
||||
interface CampaignRequestRepository extends Repository
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Application\UseCases\CampaignRequest\Create;
|
||||
|
||||
use Application\UseCases\CampaignRequest\Create\CreateCampaignRequest;
|
||||
use Application\UseCases\CampaignRequest\Create\Input;
|
||||
use Application\UseCases\CampaignRequest\Create\Output;
|
||||
use DateTime;
|
||||
use Domain\Exceptions\BrandNotFoundException;
|
||||
use Domain\Models\Entities\CampaignRequest;
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignName;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\Applicant;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestStatus;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestType;
|
||||
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\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 Domain\Ports\Repositories\BrandRepository;
|
||||
use Domain\Ports\Repositories\CampaignRequestRepository;
|
||||
use Shared\Enums\Country;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\ValueObjects\Email;
|
||||
use Shared\ValueObjects\Money;
|
||||
|
||||
covers(CreateCampaignRequest::class, Input::class, Output::class);
|
||||
|
||||
beforeEach(function (): void {
|
||||
$this->input = new Input(
|
||||
brandCode: new BrandCode('AAA01'),
|
||||
name: new CampaignName('Campaign name'),
|
||||
applicant: new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
||||
campaignType: CampaignRequestType::CLUB,
|
||||
requestedCoupon: new RequestedCoupon(
|
||||
name: new CouponName('Coupon A'),
|
||||
quantity: new RequestedCouponQuantity(100),
|
||||
diffusion: new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01')),
|
||||
moneyAmount: new MoneyAmount(new Money(Currency::EUR, 300)),
|
||||
processingCode: new ProcessingCode('PC1'),
|
||||
couponPicture: new CouponPicture(
|
||||
new PicturePath('coupons/product.jpg'),
|
||||
new PicturePath('coupons/thumbnail.jpg'),
|
||||
new PicturePath('coupons/inactive_thumbnail.jpg')
|
||||
),
|
||||
couponValidity: new CouponValidity(30, new DateTime('2026-02-01')),
|
||||
couponValidityInformation: [
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE),
|
||||
],
|
||||
products: [new IncoCode(35784), new IncoCode(15522)],
|
||||
printRules: new PrintRules(maxPerCustomer: 3),
|
||||
warningThresholdQuantity: new WarningThresholdQuantity(10),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
describe('create campaign request use case', function (): void {
|
||||
it('creates a campaign request when the brand exists', function (): void {
|
||||
// GIVEN
|
||||
$brandRepository = \Mockery::mock(BrandRepository::class);
|
||||
$brandRepository->shouldReceive('existsByCode')->once()->with($this->input->brandCode)->andReturn(true);
|
||||
|
||||
$campaignRequestRepository = \Mockery::mock(CampaignRequestRepository::class);
|
||||
$campaignRequestRepository->shouldReceive('save')->once()->with(\Mockery::type(CampaignRequest::class));
|
||||
|
||||
$useCase = new CreateCampaignRequest($campaignRequestRepository, $brandRepository);
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($this->input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(Output::class)
|
||||
->and($result->status)->toBe(CampaignRequestStatus::CREATED)
|
||||
->and($result->brandCode)->toBe($this->input->brandCode);
|
||||
});
|
||||
});
|
||||
|
||||
describe('create campaign request use case validation rules', function (): void {
|
||||
it('throws when the brand does not exist', function (): void {
|
||||
// GIVEN
|
||||
$brandRepository = \Mockery::mock(BrandRepository::class);
|
||||
$brandRepository->shouldReceive('existsByCode')->once()->with($this->input->brandCode)->andReturn(false);
|
||||
|
||||
$campaignRequestRepository = \Mockery::mock(CampaignRequestRepository::class);
|
||||
$campaignRequestRepository->shouldNotReceive('save');
|
||||
|
||||
$useCase = new CreateCampaignRequest($campaignRequestRepository, $brandRepository);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($this->input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(BrandNotFoundException::class);
|
||||
});
|
||||
});
|
||||
|
|
@ -62,7 +62,10 @@ describe('find campaign request use case', function (): void {
|
|||
new PicturePath('coupons/inactive_thumbnail.jpg')
|
||||
),
|
||||
new CouponValidity(30, new DateTime('2026-02-01')),
|
||||
[new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)],
|
||||
[
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE),
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX)
|
||||
],
|
||||
[new IncoCode(35784), new IncoCode(15522)],
|
||||
new PrintRules(maxPerCustomer: 3),
|
||||
new GS1('01234567890128', BarcodeFormat::EAN13),
|
||||
|
|
|
|||
|
|
@ -55,7 +55,10 @@ describe('list campaigns request use case', function (): void {
|
|||
new PicturePath('coupons/inactive_thumbnail.jpg')
|
||||
),
|
||||
new CouponValidity(30, new DateTime('2026-02-01')),
|
||||
[new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)],
|
||||
[
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE),
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX)
|
||||
],
|
||||
[new IncoCode(35784), new IncoCode(15522)],
|
||||
new PrintRules(maxPerCustomer: 3),
|
||||
new GS1('01234567890128', BarcodeFormat::EAN13),
|
||||
|
|
@ -74,7 +77,10 @@ describe('list campaigns request use case', function (): void {
|
|||
new PicturePath('coupons/inactive_thumbnail.jpg')
|
||||
),
|
||||
new CouponValidity(30, new DateTime('2026-02-01')),
|
||||
[new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)],
|
||||
[
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE),
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX)
|
||||
],
|
||||
[new IncoCode(24514), new IncoCode(46259)],
|
||||
new PrintRules(maxPerCustomer: 3),
|
||||
new GS1('01234567890128', BarcodeFormat::EAN13),
|
||||
|
|
|
|||
|
|
@ -56,7 +56,10 @@ describe('reject campaign request use case', function (): void {
|
|||
new PicturePath('coupons/inactive_thumbnail.jpg')
|
||||
),
|
||||
new CouponValidity(30, new DateTime('2026-02-01')),
|
||||
[new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)],
|
||||
[
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE),
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX)
|
||||
],
|
||||
[new IncoCode(35784), new IncoCode(15522)],
|
||||
new PrintRules(maxPerCustomer: 3),
|
||||
new GS1('01234567890128', BarcodeFormat::EAN13),
|
||||
|
|
|
|||
|
|
@ -55,7 +55,10 @@ describe('campaign request', function (): void {
|
|||
new PicturePath('coupons/inactive_thumbnail.jpg')
|
||||
),
|
||||
new CouponValidity(30, new DateTime('2026-02-01')),
|
||||
[new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)],
|
||||
[
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE),
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX)
|
||||
],
|
||||
[new IncoCode(35784), new IncoCode(15522)],
|
||||
new PrintRules(maxPerCustomer: 3),
|
||||
new GS1('01234567890128', BarcodeFormat::EAN13),
|
||||
|
|
|
|||
|
|
@ -424,7 +424,10 @@ describe('coupon', function (): void {
|
|||
),
|
||||
new CouponDiffusion([new Period(new DateTime('2026-01-01'), new DateTime('2026-02-01'))]),
|
||||
new CouponValidity(30, new DateTime('2026-02-01')),
|
||||
[new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)],
|
||||
[
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE),
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX)
|
||||
],
|
||||
[new CouponLegalInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE)],
|
||||
new ProcessingCode('5425003944130'),
|
||||
new BrandCode('AAA01'),
|
||||
|
|
|
|||
|
|
@ -23,4 +23,23 @@ describe('period', function (): void {
|
|||
->and($period->start)->toBe($start)
|
||||
->and($period->end)->toBe($end);
|
||||
});
|
||||
|
||||
it('accepts a period whose end equals its start', function (): void {
|
||||
// GIVEN
|
||||
$start = new DateTime('2026-01-01');
|
||||
$end = new DateTime('2026-01-01');
|
||||
|
||||
// WHEN
|
||||
$period = new Period($start, $end);
|
||||
|
||||
// THEN
|
||||
expect($period->start)->toBe($start)
|
||||
->and($period->end)->toBe($end);
|
||||
});
|
||||
|
||||
it('throws when the end is before the start', function (): void {
|
||||
// GIVEN / WHEN / THEN
|
||||
expect(fn () => new Period(new DateTime('2026-02-01'), new DateTime('2026-01-01')))
|
||||
->toThrow(\DomainException::class, 'Period end date cannot be before its start date.');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -29,27 +29,33 @@ 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(
|
||||
$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->couponValidity = new CouponValidity(30, new DateTime('2026-02-01'));
|
||||
$this->franceValidityInformation = new CouponValidityInformation(
|
||||
'Lorem ipsum dolor sit amet consectetur adipiscing elit.',
|
||||
Country::FRANCE
|
||||
);
|
||||
$this->beneluxValidityInformation = new CouponValidityInformation(
|
||||
'Lorem ipsum dolor sit amet consectetur adipiscing elit.',
|
||||
Country::BENELUX
|
||||
);
|
||||
$this->couponValidityInformation = [
|
||||
$this->franceValidityInformation,
|
||||
$this->beneluxValidityInformation,
|
||||
];
|
||||
$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);
|
||||
$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 {
|
||||
|
|
@ -96,7 +102,7 @@ describe('requested coupon', function (): void {
|
|||
diffusion: $this->diffusion,
|
||||
moneyAmount: $this->moneyAmount,
|
||||
processingCode: $this->processingCode,
|
||||
couponPicture:$this->couponPicture,
|
||||
couponPicture: $this->couponPicture,
|
||||
couponValidity: $this->couponValidity,
|
||||
couponValidityInformation: $this->couponValidityInformation,
|
||||
printRules: $this->printRules,
|
||||
|
|
@ -119,7 +125,7 @@ describe('requested coupon', function (): void {
|
|||
diffusion: $this->diffusion,
|
||||
moneyAmount: $this->moneyAmount,
|
||||
processingCode: $this->processingCode,
|
||||
couponPicture:$this->couponPicture,
|
||||
couponPicture: $this->couponPicture,
|
||||
couponValidity: $this->couponValidity,
|
||||
couponValidityInformation: $this->couponValidityInformation,
|
||||
products: $this->products,
|
||||
|
|
@ -142,7 +148,7 @@ describe('requested coupon', function (): void {
|
|||
diffusion: $this->diffusion,
|
||||
moneyAmount: $this->moneyAmount,
|
||||
processingCode: $this->processingCode,
|
||||
couponPicture:$this->couponPicture,
|
||||
couponPicture: $this->couponPicture,
|
||||
couponValidity: $this->couponValidity,
|
||||
couponValidityInformation: $this->couponValidityInformation,
|
||||
products: $this->products,
|
||||
|
|
@ -157,7 +163,7 @@ describe('requested coupon', function (): void {
|
|||
->and($coupon->warningThresholdQuantity)->toBe($this->warningThresholdQuantity);
|
||||
});
|
||||
|
||||
it('hydrates a requested coupon without warning threshold quantity', function (): void {
|
||||
it('defaults the warning threshold quantity to 100 when not provided', function (): void {
|
||||
// GIVEN / WHEN
|
||||
$coupon = new RequestedCoupon(
|
||||
name: $this->name,
|
||||
|
|
@ -165,7 +171,7 @@ describe('requested coupon', function (): void {
|
|||
diffusion: $this->diffusion,
|
||||
moneyAmount: $this->moneyAmount,
|
||||
processingCode: $this->processingCode,
|
||||
couponPicture:$this->couponPicture,
|
||||
couponPicture: $this->couponPicture,
|
||||
couponValidity: $this->couponValidity,
|
||||
couponValidityInformation: $this->couponValidityInformation,
|
||||
products: $this->products,
|
||||
|
|
@ -174,7 +180,7 @@ describe('requested coupon', function (): void {
|
|||
);
|
||||
|
||||
// THEN
|
||||
expect($coupon->warningThresholdQuantity)->toBeNull()
|
||||
expect($coupon->warningThresholdQuantity)->toEqual(new WarningThresholdQuantity(100))
|
||||
->and($coupon->products)->toBe($this->products)
|
||||
->and($coupon->printRules)->toBe($this->printRules)
|
||||
->and($coupon->gs1)->toBe($this->gs1);
|
||||
|
|
@ -188,7 +194,7 @@ describe('requested coupon', function (): void {
|
|||
diffusion: $this->diffusion,
|
||||
moneyAmount: $this->moneyAmount,
|
||||
processingCode: $this->processingCode,
|
||||
couponPicture:$this->couponPicture,
|
||||
couponPicture: $this->couponPicture,
|
||||
couponValidity: $this->couponValidity,
|
||||
couponValidityInformation: $this->couponValidityInformation,
|
||||
);
|
||||
|
|
@ -197,28 +203,21 @@ describe('requested coupon', function (): void {
|
|||
expect($coupon->products)->toBe([])
|
||||
->and($coupon->printRules)->toBeNull()
|
||||
->and($coupon->gs1)->toBeNull()
|
||||
->and($coupon->warningThresholdQuantity)->toBeNull();
|
||||
->and($coupon->warningThresholdQuantity)->toEqual(new WarningThresholdQuantity(100));
|
||||
});
|
||||
|
||||
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 there is no validity information for france', function (): void {
|
||||
// GIVEN / WHEN / THEN
|
||||
expect(fn () => new RequestedCoupon(
|
||||
name: $this->name,
|
||||
quantity: $this->quantity,
|
||||
diffusion: $this->diffusion,
|
||||
moneyAmount: $this->moneyAmount,
|
||||
processingCode: $this->processingCode,
|
||||
couponPicture: $this->couponPicture,
|
||||
couponValidity: $this->couponValidity,
|
||||
couponValidityInformation: [],
|
||||
))->toThrow(\DomainException::class, 'A coupon requires validity information for France.');
|
||||
});
|
||||
|
||||
it('throws when validity information contains an invalid item', function (): void {
|
||||
|
|
@ -238,4 +237,119 @@ describe('requested coupon', function (): void {
|
|||
$this->warningThresholdQuantity
|
||||
))->toThrow(\DomainException::class, 'Coupon validity information must be CouponValidityInformation instances.');
|
||||
});
|
||||
|
||||
it('accepts the minimum face value of 0.01€', function (): void {
|
||||
// GIVEN
|
||||
$moneyAmount = new MoneyAmount(new Money(Currency::EUR, 1));
|
||||
|
||||
// WHEN
|
||||
$coupon = new RequestedCoupon(
|
||||
name: $this->name,
|
||||
quantity: $this->quantity,
|
||||
diffusion: $this->diffusion,
|
||||
moneyAmount: $moneyAmount,
|
||||
processingCode: $this->processingCode,
|
||||
couponPicture: $this->couponPicture,
|
||||
couponValidity: $this->couponValidity,
|
||||
couponValidityInformation: $this->couponValidityInformation,
|
||||
);
|
||||
|
||||
// THEN
|
||||
expect($coupon->moneyAmount)->toBe($moneyAmount);
|
||||
});
|
||||
|
||||
it('accepts the maximum face value of 3.00€', function (): void {
|
||||
// GIVEN
|
||||
$moneyAmount = new MoneyAmount(new Money(Currency::EUR, 300));
|
||||
|
||||
// WHEN
|
||||
$coupon = new RequestedCoupon(
|
||||
name: $this->name,
|
||||
quantity: $this->quantity,
|
||||
diffusion: $this->diffusion,
|
||||
moneyAmount: $moneyAmount,
|
||||
processingCode: $this->processingCode,
|
||||
couponPicture: $this->couponPicture,
|
||||
couponValidity: $this->couponValidity,
|
||||
couponValidityInformation: $this->couponValidityInformation,
|
||||
);
|
||||
|
||||
// THEN
|
||||
expect($coupon->moneyAmount)->toBe($moneyAmount);
|
||||
});
|
||||
|
||||
it('throws when the face value is zero', function (): void {
|
||||
// GIVEN / WHEN / THEN
|
||||
expect(fn () => new RequestedCoupon(
|
||||
name: $this->name,
|
||||
quantity: $this->quantity,
|
||||
diffusion: $this->diffusion,
|
||||
moneyAmount: new MoneyAmount(new Money(Currency::EUR, 0)),
|
||||
processingCode: $this->processingCode,
|
||||
couponPicture: $this->couponPicture,
|
||||
couponValidity: $this->couponValidity,
|
||||
couponValidityInformation: $this->couponValidityInformation,
|
||||
))->toThrow(\DomainException::class, 'Coupon face value must be between 0.01€ and 3.00€');
|
||||
});
|
||||
|
||||
it('throws when the face value is above 3.00€', function (): void {
|
||||
// GIVEN / WHEN / THEN
|
||||
expect(fn () => new RequestedCoupon(
|
||||
name: $this->name,
|
||||
quantity: $this->quantity,
|
||||
diffusion: $this->diffusion,
|
||||
moneyAmount: new MoneyAmount(new Money(Currency::EUR, 301)),
|
||||
processingCode: $this->processingCode,
|
||||
couponPicture: $this->couponPicture,
|
||||
couponValidity: $this->couponValidity,
|
||||
couponValidityInformation: $this->couponValidityInformation,
|
||||
))->toThrow(\DomainException::class, 'Coupon face value must be between 0.01€ and 3.00€');
|
||||
});
|
||||
|
||||
it('throws when a gs1 coupon has no benelux validity information', function (): void {
|
||||
// GIVEN / WHEN / THEN
|
||||
expect(fn () => 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->franceValidityInformation],
|
||||
gs1: $this->gs1,
|
||||
))->toThrow(\DomainException::class, 'A GS1 coupon requires validity information for Benelux.');
|
||||
});
|
||||
|
||||
it('throws when a gs1 coupon has only benelux validity information', function (): void {
|
||||
// GIVEN / WHEN / THEN
|
||||
expect(fn () => 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->beneluxValidityInformation],
|
||||
gs1: $this->gs1,
|
||||
))->toThrow(\DomainException::class, 'A coupon requires validity information for France.');
|
||||
});
|
||||
|
||||
it('accepts a coupon without gs1 and without benelux validity information', 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->franceValidityInformation],
|
||||
);
|
||||
|
||||
// THEN
|
||||
expect($coupon->gs1)->toBeNull();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue