feat: ✨ add the transform campaign request method
This commit is contained in:
parent
885841a881
commit
ae86a72bfc
16 changed files with 391 additions and 134 deletions
|
|
@ -4,13 +4,56 @@ declare(strict_types=1);
|
|||
|
||||
namespace Application\UseCases\Campaign\Transform;
|
||||
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignName;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponAvailabilityCalendar;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponCode;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponDiffusion;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponLegalInformation;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponQuantity;
|
||||
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\GS1;
|
||||
use Domain\Models\ValueObjects\IncoCode;
|
||||
use Domain\Models\ValueObjects\MoneyAmount;
|
||||
use Domain\Models\ValueObjects\PrintRules;
|
||||
use Domain\Models\ValueObjects\ProcessingCode;
|
||||
use Domain\Models\ValueObjects\WarningThresholdQuantity;
|
||||
use Shared\DDD\UseCases\Input as UseCaseInput;
|
||||
use Shared\Enums\Country;
|
||||
|
||||
final readonly class Input implements UseCaseInput
|
||||
{
|
||||
/**
|
||||
* @param array<CouponValidityInformation> $couponValidityInformation
|
||||
* @param array<CouponLegalInformation> $couponLegalInformation
|
||||
* @param array<Country> $availableIn
|
||||
* @param array<IncoCode> $products
|
||||
*/
|
||||
public function __construct(
|
||||
public CampaignRequestId $id,
|
||||
public CouponCode $couponCode,
|
||||
public CouponName $couponName,
|
||||
public MoneyAmount $amount,
|
||||
public CouponPicture $picture,
|
||||
public CouponDiffusion $diffusion,
|
||||
public CouponValidity $validity,
|
||||
public array $couponValidityInformation,
|
||||
public array $couponLegalInformation,
|
||||
public ProcessingCode $processingCode,
|
||||
public CampaignCode $campaignCode,
|
||||
public CampaignName $campaignName,
|
||||
public bool $publication,
|
||||
public CouponQuantity $quantity,
|
||||
public array $availableIn,
|
||||
public array $products = [],
|
||||
public ?CouponAvailabilityCalendar $availabilityCalendar = null,
|
||||
public ?GS1 $gs1 = null,
|
||||
public ?WarningThresholdQuantity $warningThresholdQuantity = null,
|
||||
public ?PrintRules $printRules = null,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\CampaignRequest\Validate\Output;
|
||||
namespace Application\UseCases\Campaign\Transform\Output;
|
||||
|
||||
final readonly class CampaignRequestNotFound implements Output
|
||||
{
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Campaign\Transform\Output;
|
||||
|
||||
use Domain\Models\Entities\Campaign;
|
||||
|
||||
final readonly class CampaignRequestTransformed implements Output
|
||||
{
|
||||
public function __construct(
|
||||
public Campaign $campaign,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\CampaignRequest\Validate\Output;
|
||||
namespace Application\UseCases\Campaign\Transform\Output;
|
||||
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
|
||||
|
|
@ -4,24 +4,30 @@ declare(strict_types=1);
|
|||
|
||||
namespace Application\UseCases\Campaign\Transform;
|
||||
|
||||
use Application\UseCases\CampaignRequest\Find\Output\CampaignRequestFound;
|
||||
use Application\UseCases\CampaignRequest\Find\Output\CampaignRequestNotFound;
|
||||
use Application\UseCases\Campaign\Transform\Output\CampaignRequestNotFound;
|
||||
use Application\UseCases\Campaign\Transform\Output\CampaignRequestTransformed;
|
||||
use Application\UseCases\Campaign\Transform\Output\Output;
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Models\Entities\CampaignRequest;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Models\ValueObjects\Actor\ActorId;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCoupon;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestType;
|
||||
use Domain\Ports\Repositories\BrandRepository;
|
||||
use Domain\Ports\Repositories\CampaignRepository;
|
||||
use Domain\Ports\Repositories\CampaignRequestRepository;
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
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\Find\Input, \Application\UseCases\CampaignRequest\Find\Output\Output> */
|
||||
final readonly class TransformCampaignRequest implements UseCase
|
||||
/** @implements UseCase<\Application\UseCases\Campaign\Transform\Input, \Application\UseCases\Campaign\Transform\Output\Output> */
|
||||
final readonly class TransformCampaignRequest implements UseCase, Transactional
|
||||
{
|
||||
public function __construct(
|
||||
private CampaignRequestRepository $campaignRequestRepository
|
||||
private CampaignRequestRepository $campaignRequestRepository,
|
||||
private CouponRepository $couponRepository,
|
||||
private CampaignRepository $campaignRepository,
|
||||
) {}
|
||||
|
||||
#[\Override]
|
||||
|
|
@ -35,22 +41,56 @@ final readonly class TransformCampaignRequest implements UseCase
|
|||
return new CampaignRequestNotFound();
|
||||
}
|
||||
|
||||
$isGame = $campaignRequest->campaignType === CampaignRequestType::GAME;
|
||||
// Use it early to check if this campaignRequest can be validated.
|
||||
$campaignRequest = $campaignRequest->validate();
|
||||
|
||||
// TODO: get the actuel ActorID
|
||||
// TODO: get the actual ActorId
|
||||
$actorId = ActorId::generate();
|
||||
|
||||
$campaignCreate = Campaign::create(
|
||||
// TODO: use a true name generation
|
||||
code: new CampaignCode("ddd"),
|
||||
name: $campaignRequest->name,
|
||||
publication: false,
|
||||
printAllAtOnce: $isGame,
|
||||
coupons: [],
|
||||
// FIXME : ajout de coupons
|
||||
$coupon = Coupon::create(
|
||||
code: $input->couponCode,
|
||||
name: $input->couponName,
|
||||
amount: $input->amount,
|
||||
picture: $input->picture,
|
||||
diffusion: $input->diffusion,
|
||||
validity: $input->validity,
|
||||
couponValidityInformation: $input->couponValidityInformation,
|
||||
couponLegalInformation: $input->couponLegalInformation,
|
||||
processingCode: $input->processingCode,
|
||||
brandCode: $campaignRequest->brandCode,
|
||||
actorId: $actorId
|
||||
createdBy: $actorId,
|
||||
products: $input->products,
|
||||
availabilityCalendar: $input->availabilityCalendar,
|
||||
gs1: $input->gs1,
|
||||
);
|
||||
$this->couponRepository->save($coupon);
|
||||
|
||||
$isGame = $campaignRequest->campaignType === CampaignRequestType::GAME;
|
||||
|
||||
$campaign = Campaign::create(
|
||||
code: $input->campaignCode,
|
||||
name: $input->campaignName,
|
||||
publication: $input->publication,
|
||||
printAllAtOnce: $isGame,
|
||||
coupons: [
|
||||
new CampaignCoupon(
|
||||
processingCode: $coupon->processingCode,
|
||||
quantity: $input->quantity,
|
||||
availableIn: $input->availableIn,
|
||||
warningThresholdQuantity: $input->warningThresholdQuantity,
|
||||
printRules: $input->printRules,
|
||||
),
|
||||
],
|
||||
brandCode: $campaignRequest->brandCode,
|
||||
actorId: $actorId,
|
||||
);
|
||||
$this->campaignRepository->save($campaign);
|
||||
|
||||
$this->campaignRequestRepository->save($campaignRequest);
|
||||
|
||||
|
||||
return new CampaignRequestTransformed(
|
||||
campaign: $campaign,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\CampaignRequest\Validate;
|
||||
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId;
|
||||
use Shared\DDD\UseCases\Input as UseCaseInput;
|
||||
|
||||
final readonly class Input implements UseCaseInput
|
||||
{
|
||||
public function __construct(
|
||||
public CampaignRequestId $id
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\CampaignRequest\Validate\Output;
|
||||
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestId;
|
||||
use Domain\Models\ValueObjects\CampaignRequest\CampaignRequestStatus;
|
||||
|
||||
final readonly class CampaignRequestValidated implements Output
|
||||
{
|
||||
public function __construct(
|
||||
public CampaignRequestId $id,
|
||||
public CampaignRequestStatus $status,
|
||||
public \DateTimeInterface $updatedAt,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -1,47 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\CampaignRequest\Validate;
|
||||
|
||||
use Application\UseCases\CampaignRequest\Validate\Output\CampaignRequestNotFound;
|
||||
use Application\UseCases\CampaignRequest\Validate\Output\CampaignRequestValidated;
|
||||
use Application\UseCases\CampaignRequest\Validate\Output\Output;
|
||||
use Domain\Models\Entities\CampaignRequest;
|
||||
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\Validate\Input, \Application\UseCases\CampaignRequest\Validate\Output\Output> */
|
||||
final readonly class ValidateCampaignRequest implements UseCase, Transactional
|
||||
{
|
||||
public function __construct(
|
||||
private CampaignRequestRepository $repository,
|
||||
) {
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function execute(BaseInput $input): Output
|
||||
{
|
||||
Assert::isInstanceOf($input, Input::class);
|
||||
|
||||
$campaignRequest = $this->repository->findById($input->id);
|
||||
|
||||
if (! $campaignRequest instanceof CampaignRequest) {
|
||||
return new CampaignRequestNotFound();
|
||||
}
|
||||
|
||||
// TODO: ici viendront les méthodes de création de campaign / coupon + leur association
|
||||
$campaignRequest = $campaignRequest->validate();
|
||||
|
||||
$this->repository->save($campaignRequest);
|
||||
|
||||
return new CampaignRequestValidated(
|
||||
id: $campaignRequest->id(),
|
||||
status: $campaignRequest->status,
|
||||
updatedAt: $campaignRequest->updatedAt(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -47,7 +47,7 @@ final readonly class Coupon implements AggregateRoot, HasTimestampIdentifier
|
|||
public ActorId $createdBy,
|
||||
public array $products = [],
|
||||
public ?CouponAvailabilityCalendar $availabilityCalendar = null,
|
||||
public ?GS1 $gtin = null,
|
||||
public ?GS1 $gs1 = null,
|
||||
public ?\DateTimeInterface $updatedAt = null,
|
||||
) {
|
||||
foreach ($this->couponValidityInformation as $validityInformation) {
|
||||
|
|
@ -97,7 +97,7 @@ final readonly class Coupon implements AggregateRoot, HasTimestampIdentifier
|
|||
ActorId $createdBy,
|
||||
array $products = [],
|
||||
?CouponAvailabilityCalendar $availabilityCalendar = null,
|
||||
?GS1 $gtin = null,
|
||||
?GS1 $gs1 = null,
|
||||
?\DateTimeInterface $updatedAt = null,
|
||||
): Coupon {
|
||||
return new self(
|
||||
|
|
@ -115,7 +115,7 @@ final readonly class Coupon implements AggregateRoot, HasTimestampIdentifier
|
|||
createdBy: $createdBy,
|
||||
products: $products,
|
||||
availabilityCalendar: $availabilityCalendar,
|
||||
gtin: $gtin,
|
||||
gs1: $gs1,
|
||||
updatedAt: $updatedAt,
|
||||
);
|
||||
}
|
||||
|
|
@ -140,7 +140,7 @@ final readonly class Coupon implements AggregateRoot, HasTimestampIdentifier
|
|||
ActorId $createdBy,
|
||||
array $products = [],
|
||||
?CouponAvailabilityCalendar $availabilityCalendar = null,
|
||||
?GS1 $gtin = null,
|
||||
?GS1 $gs1 = null,
|
||||
?\DateTimeInterface $updatedAt = null,
|
||||
): self {
|
||||
return new self(
|
||||
|
|
@ -158,7 +158,7 @@ final readonly class Coupon implements AggregateRoot, HasTimestampIdentifier
|
|||
createdBy: $createdBy,
|
||||
products: $products,
|
||||
availabilityCalendar: $availabilityCalendar,
|
||||
gtin: $gtin,
|
||||
gs1: $gs1,
|
||||
updatedAt: $updatedAt,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,12 +12,34 @@ use Shared\Enums\Country;
|
|||
|
||||
final readonly class CampaignCoupon
|
||||
{
|
||||
private const int MAX_LOCALES = 2;
|
||||
|
||||
/**
|
||||
* @param array<Country> $availableIn
|
||||
*/
|
||||
public function __construct(
|
||||
public ProcessingCode $processingCode,
|
||||
public CouponQuantity $quantity,
|
||||
public Country $availableIn,
|
||||
public array $availableIn,
|
||||
public ?WarningThresholdQuantity $warningThresholdQuantity = null,
|
||||
public ?PrintRules $printRules = null,
|
||||
) {
|
||||
if ($this->availableIn === []) {
|
||||
throw new \DomainException('Campaign coupon must be available in at least one country.');
|
||||
}
|
||||
|
||||
if (count($this->availableIn) > self::MAX_LOCALES) {
|
||||
throw new \DomainException(sprintf('Campaign coupon cannot be available in more than %d countries.', self::MAX_LOCALES));
|
||||
}
|
||||
|
||||
foreach ($this->availableIn as $country) {
|
||||
if (! $country instanceof Country) {
|
||||
throw new \DomainException('Campaign coupon availableIn must contain Country instances.');
|
||||
}
|
||||
}
|
||||
|
||||
if (! in_array(Country::FRANCE, $this->availableIn, true)) {
|
||||
throw new \DomainException('Campaign coupon must always be available in France.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Domain\Ports\Repositories;
|
||||
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Shared\DDD\Ports\Repository;
|
||||
|
||||
/**
|
||||
|
|
@ -11,4 +12,5 @@ use Shared\DDD\Ports\Repository;
|
|||
*/
|
||||
interface CampaignRepository extends Repository
|
||||
{
|
||||
public function save(Campaign $campaign): void;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Domain\Ports\Repositories;
|
||||
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Shared\DDD\Ports\Repository;
|
||||
|
||||
/**
|
||||
|
|
@ -11,4 +12,5 @@ use Shared\DDD\Ports\Repository;
|
|||
*/
|
||||
interface CouponRepository extends Repository
|
||||
{
|
||||
public function save(Coupon $coupon): void;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,215 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Application\UseCases\Campaign\Transform;
|
||||
|
||||
use Application\UseCases\Campaign\Transform\Input;
|
||||
use Application\UseCases\Campaign\Transform\Output\CampaignRequestNotFound;
|
||||
use Application\UseCases\Campaign\Transform\Output\CampaignRequestTransformed;
|
||||
use Application\UseCases\Campaign\Transform\TransformCampaignRequest;
|
||||
use Carbon\CarbonImmutable;
|
||||
use DateTime;
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Models\Entities\CampaignRequest;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCode;
|
||||
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\Coupon\CouponCode;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponDiffusion;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponLegalInformation;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponQuantity;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponValidity;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponValidityInformation;
|
||||
use Domain\Models\ValueObjects\CouponName;
|
||||
use Domain\Models\ValueObjects\CouponPicture;
|
||||
use Application\UseCases\Campaign\Transform\Output\Output;
|
||||
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\CampaignRepository;
|
||||
use Domain\Ports\Repositories\CampaignRequestRepository;
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
use Shared\Enums\Country;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\ValueObjects\Email;
|
||||
use Shared\ValueObjects\Money;
|
||||
|
||||
covers(TransformCampaignRequest::class, Input::class, CampaignRequestTransformed::class, CampaignRequestNotFound::class);
|
||||
|
||||
describe('transform campaign request use case', function (): void {
|
||||
beforeEach(function (): void {
|
||||
$requestedCoupon = new RequestedCoupon(
|
||||
new CouponName('Coupon A'),
|
||||
new RequestedCouponQuantity(100),
|
||||
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 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 IncoCode(35784), new IncoCode(15522)],
|
||||
new PrintRules(maxPerCustomer: 3),
|
||||
null,
|
||||
new WarningThresholdQuantity(10)
|
||||
);
|
||||
|
||||
// The input mirrors an existing campaign request: at transformation time,
|
||||
// the caller may have edited any of these fields before submitting.
|
||||
$this->campaignRequest = CampaignRequest::hydrate(
|
||||
CampaignRequestId::generate(),
|
||||
new BrandCode('AAA01'),
|
||||
new CampaignName('Campaign A'),
|
||||
new Applicant('John', 'Doe', new Email('john.doe@example.com')),
|
||||
CampaignRequestType::CLUB,
|
||||
$requestedCoupon,
|
||||
CampaignRequestStatus::CREATED,
|
||||
);
|
||||
|
||||
$this->input = new Input(
|
||||
id: $this->campaignRequest->id(),
|
||||
couponCode: new CouponCode('COU01'),
|
||||
couponName: $this->campaignRequest->requestedCoupon->name,
|
||||
amount: $this->campaignRequest->requestedCoupon->moneyAmount,
|
||||
picture: $this->campaignRequest->requestedCoupon->couponPicture,
|
||||
diffusion: new CouponDiffusion([$this->campaignRequest->requestedCoupon->diffusion]),
|
||||
validity: $this->campaignRequest->requestedCoupon->couponValidity,
|
||||
couponValidityInformation: $this->campaignRequest->requestedCoupon->couponValidityInformation,
|
||||
couponLegalInformation: [new CouponLegalInformation('Legal text.', Country::FRANCE)],
|
||||
processingCode: $this->campaignRequest->requestedCoupon->processingCode,
|
||||
campaignCode: new CampaignCode('CAMP001'),
|
||||
campaignName: $this->campaignRequest->name,
|
||||
publication: false,
|
||||
quantity: new CouponQuantity(100),
|
||||
availableIn: [Country::FRANCE],
|
||||
products: $this->campaignRequest->requestedCoupon->products,
|
||||
warningThresholdQuantity: $this->campaignRequest->requestedCoupon->warningThresholdQuantity,
|
||||
printRules: $this->campaignRequest->requestedCoupon->printRules,
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
CarbonImmutable::setTestNow();
|
||||
});
|
||||
|
||||
it('transforms a campaign request into a coupon and a campaign', function (): void {
|
||||
// GIVEN
|
||||
CarbonImmutable::setTestNow('2026-06-15 12:00:00');
|
||||
$id = $this->campaignRequest->id();
|
||||
$brandCode = $this->campaignRequest->brandCode;
|
||||
|
||||
$campaignRequestRepository = \Mockery::mock(CampaignRequestRepository::class);
|
||||
$campaignRequestRepository->shouldReceive('findById')->once()->with($id)->andReturn($this->campaignRequest);
|
||||
$campaignRequestRepository->shouldReceive('save')->once()->with(\Mockery::on(
|
||||
fn(CampaignRequest $saved): bool => $saved->status === CampaignRequestStatus::TRANSFORMED
|
||||
&& $saved->updatedAt !== null,
|
||||
));
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldReceive('save')->once()->with(\Mockery::on(
|
||||
fn(Coupon $saved): bool => $saved->code === $this->input->couponCode
|
||||
&& $saved->name === $this->input->couponName
|
||||
&& $saved->amount === $this->input->amount
|
||||
&& $saved->couponLegalInformation === $this->input->couponLegalInformation
|
||||
&& $saved->brandCode === $brandCode,
|
||||
));
|
||||
|
||||
$campaignRepository = \Mockery::mock(CampaignRepository::class);
|
||||
$campaignRepository->shouldReceive('save')->once()->with(\Mockery::on(
|
||||
fn(Campaign $saved): bool => $saved->code === $this->input->campaignCode
|
||||
&& $saved->name === $this->input->campaignName
|
||||
&& $saved->brandCode === $brandCode
|
||||
&& count($saved->coupons) === 1
|
||||
&& $saved->coupons[0]->availableIn === $this->input->availableIn,
|
||||
));
|
||||
|
||||
$useCase = new TransformCampaignRequest(
|
||||
$campaignRequestRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($this->input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignRequestTransformed::class)
|
||||
->and($result->campaign->code)->toBe($this->input->campaignCode)
|
||||
->and($result->campaign->coupons[0]->availableIn)->toBe($this->input->availableIn);
|
||||
});
|
||||
|
||||
it('returns CampaignRequestNotFound when the id is not found', function (): void {
|
||||
// GIVEN
|
||||
$campaignRequestRepository = \Mockery::mock(CampaignRequestRepository::class);
|
||||
$campaignRequestRepository->shouldReceive('findById')->once()->with($this->campaignRequest->id())->andReturn(null);
|
||||
$campaignRequestRepository->shouldNotReceive('save');
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldNotReceive('save');
|
||||
|
||||
$campaignRepository = \Mockery::mock(CampaignRepository::class);
|
||||
$campaignRepository->shouldNotReceive('save');
|
||||
|
||||
$useCase = new TransformCampaignRequest(
|
||||
$campaignRequestRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($this->input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignRequestNotFound::class);
|
||||
});
|
||||
|
||||
it('propagates the domain exception when the campaign request cannot be validated', function (): void {
|
||||
// GIVEN
|
||||
$campaignRequest = CampaignRequest::hydrate(
|
||||
$this->campaignRequest->id(),
|
||||
$this->campaignRequest->brandCode,
|
||||
$this->campaignRequest->name,
|
||||
$this->campaignRequest->applicant,
|
||||
$this->campaignRequest->campaignType,
|
||||
$this->campaignRequest->requestedCoupon,
|
||||
CampaignRequestStatus::TRANSFORMED,
|
||||
);
|
||||
|
||||
$campaignRequestRepository = \Mockery::mock(CampaignRequestRepository::class);
|
||||
$campaignRequestRepository->shouldReceive('findById')->once()->with($this->input->id)->andReturn($campaignRequest);
|
||||
$campaignRequestRepository->shouldNotReceive('save');
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldNotReceive('save');
|
||||
|
||||
$campaignRepository = \Mockery::mock(CampaignRepository::class);
|
||||
$campaignRepository->shouldNotReceive('save');
|
||||
|
||||
$useCase = new TransformCampaignRequest(
|
||||
$campaignRequestRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn(): Output => $useCase->execute($this->input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Only a created campaign request can be validated.');
|
||||
});
|
||||
});
|
||||
|
|
@ -7,7 +7,6 @@ namespace Tests\Unit\Domain\Models\Entities;
|
|||
use Carbon\CarbonImmutable;
|
||||
use Shared\Enums\Country;
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Models\Entities\Tag;
|
||||
use Domain\Models\ValueObjects\Actor\ActorId;
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCode;
|
||||
|
|
@ -39,7 +38,7 @@ describe('campaign', function (): void {
|
|||
new CampaignCoupon(
|
||||
new ProcessingCode('PC1'),
|
||||
new CouponQuantity(100),
|
||||
Country::FRANCE,
|
||||
[Country::FRANCE],
|
||||
new WarningThresholdQuantity(100),
|
||||
new PrintRules(maxPerCustomer: 3)
|
||||
)
|
||||
|
|
@ -109,7 +108,7 @@ describe('campaign', function (): void {
|
|||
new CampaignCoupon(
|
||||
new ProcessingCode('PC1'),
|
||||
new CouponQuantity(100),
|
||||
Country::FRANCE,
|
||||
[Country::FRANCE],
|
||||
new WarningThresholdQuantity(100),
|
||||
new PrintRules(maxPerCustomer: 3)
|
||||
)
|
||||
|
|
@ -146,7 +145,7 @@ describe('campaign', function (): void {
|
|||
new CampaignCoupon(
|
||||
new ProcessingCode('PC1'),
|
||||
new CouponQuantity(100),
|
||||
Country::FRANCE,
|
||||
[Country::FRANCE],
|
||||
new WarningThresholdQuantity(100),
|
||||
new PrintRules(maxPerCustomer: 3)
|
||||
)
|
||||
|
|
@ -188,7 +187,7 @@ describe('campaign', function (): void {
|
|||
new CampaignCoupon(
|
||||
new ProcessingCode('PC1'),
|
||||
new CouponQuantity(100),
|
||||
Country::FRANCE,
|
||||
[Country::FRANCE],
|
||||
new WarningThresholdQuantity(100),
|
||||
new PrintRules(maxPerCustomer: 3)
|
||||
)
|
||||
|
|
@ -233,7 +232,7 @@ describe('campaign', function (): void {
|
|||
new CampaignCoupon(
|
||||
new ProcessingCode('PC1'),
|
||||
new CouponQuantity(100),
|
||||
Country::FRANCE,
|
||||
[Country::FRANCE],
|
||||
new WarningThresholdQuantity(100),
|
||||
new PrintRules(maxPerCustomer: 3)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ describe('coupon', function (): void {
|
|||
$this->createdBy = ActorId::generate();
|
||||
$this->products = [new IncoCode(35784), new IncoCode(15522)];
|
||||
$this->availabilityCalendar = new CouponAvailabilityCalendar([new Period(new DateTime('2026-08-25'), new DateTime('2026-10-31'))]);
|
||||
$this->gtin = new GS1('110101010', BarcodeFormat::EAN13);
|
||||
$this->gs1 = new GS1('110101010', BarcodeFormat::EAN13);
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
|
|
@ -88,7 +88,7 @@ describe('coupon', function (): void {
|
|||
$this->createdBy,
|
||||
$this->products,
|
||||
$this->availabilityCalendar,
|
||||
$this->gtin,
|
||||
$this->gs1,
|
||||
);
|
||||
|
||||
// THEN
|
||||
|
|
@ -106,7 +106,7 @@ describe('coupon', function (): void {
|
|||
->and($coupon->createdBy)->toBe($this->createdBy)
|
||||
->and($coupon->products)->toBe($this->products)
|
||||
->and($coupon->availabilityCalendar)->toBe($this->availabilityCalendar)
|
||||
->and($coupon->gtin)->toBe($this->gtin)
|
||||
->and($coupon->gs1)->toBe($this->gs1)
|
||||
->and($coupon->updatedAt)->toBeNull()
|
||||
->and($coupon->updatedAt())->toBeNull();
|
||||
});
|
||||
|
|
@ -136,7 +136,7 @@ describe('coupon', function (): void {
|
|||
// THEN
|
||||
expect($coupon->products)->toBe($this->products)
|
||||
->and($coupon->availabilityCalendar)->toBe($this->availabilityCalendar)
|
||||
->and($coupon->gtin)->toBeNull();
|
||||
->and($coupon->gs1)->toBeNull();
|
||||
});
|
||||
|
||||
it('hydrates a coupon without availability calendar', function (): void {
|
||||
|
|
@ -158,13 +158,13 @@ describe('coupon', function (): void {
|
|||
brandCode: $this->brandCode,
|
||||
createdBy: $this->createdBy,
|
||||
products: $this->products,
|
||||
gtin: $this->gtin
|
||||
gs1: $this->gs1
|
||||
);
|
||||
|
||||
// THEN
|
||||
expect($coupon->availabilityCalendar)->toBeNull()
|
||||
->and($coupon->products)->toBe($this->products)
|
||||
->and($coupon->gtin)->toBe($this->gtin);
|
||||
->and($coupon->gs1)->toBe($this->gs1);
|
||||
});
|
||||
|
||||
it('hydrates a coupon without products', function (): void {
|
||||
|
|
@ -186,13 +186,13 @@ describe('coupon', function (): void {
|
|||
brandCode: $this->brandCode,
|
||||
createdBy: $this->createdBy,
|
||||
availabilityCalendar: $this->availabilityCalendar,
|
||||
gtin: $this->gtin
|
||||
gs1: $this->gs1
|
||||
);
|
||||
|
||||
// THEN
|
||||
expect($coupon->products)->toBe([])
|
||||
->and($coupon->availabilityCalendar)->toBe($this->availabilityCalendar)
|
||||
->and($coupon->gtin)->toBe($this->gtin);
|
||||
->and($coupon->gs1)->toBe($this->gs1);
|
||||
});
|
||||
|
||||
it('hydrates a coupon with only required fields', function (): void {
|
||||
|
|
@ -219,7 +219,7 @@ describe('coupon', function (): void {
|
|||
expect($coupon->id())->toBe($id)
|
||||
->and($coupon->products)->toBe([])
|
||||
->and($coupon->availabilityCalendar)->toBeNull()
|
||||
->and($coupon->gtin)->toBeNull()
|
||||
->and($coupon->gs1)->toBeNull()
|
||||
->and($coupon->updatedAt)->toBeNull()
|
||||
->and($coupon->updatedAt())->toBeNull();
|
||||
});
|
||||
|
|
@ -244,7 +244,7 @@ describe('coupon', function (): void {
|
|||
$this->createdBy,
|
||||
$this->products,
|
||||
$this->availabilityCalendar,
|
||||
$this->gtin,
|
||||
$this->gs1,
|
||||
);
|
||||
|
||||
// THEN
|
||||
|
|
@ -272,7 +272,7 @@ describe('coupon', function (): void {
|
|||
$this->createdBy,
|
||||
$this->products,
|
||||
$this->availabilityCalendar,
|
||||
$this->gtin,
|
||||
$this->gs1,
|
||||
))->toThrow(\DomainException::class, 'Coupon validity information must be CouponValidityInformation instances.');
|
||||
});
|
||||
|
||||
|
|
@ -296,7 +296,7 @@ describe('coupon', function (): void {
|
|||
$this->createdBy,
|
||||
$this->products,
|
||||
$this->availabilityCalendar,
|
||||
$this->gtin,
|
||||
$this->gs1,
|
||||
))->toThrow(\DomainException::class, 'Coupon legal information must be CouponLegalInformation instances.');
|
||||
});
|
||||
|
||||
|
|
@ -321,7 +321,7 @@ describe('coupon', function (): void {
|
|||
$this->createdBy,
|
||||
$this->products,
|
||||
$this->availabilityCalendar,
|
||||
$this->gtin,
|
||||
$this->gs1,
|
||||
$updatedAt
|
||||
);
|
||||
|
||||
|
|
@ -346,7 +346,7 @@ describe('coupon', function (): void {
|
|||
$this->createdBy,
|
||||
$this->products,
|
||||
$this->availabilityCalendar,
|
||||
$this->gtin,
|
||||
$this->gs1,
|
||||
);
|
||||
|
||||
// THEN
|
||||
|
|
@ -364,7 +364,7 @@ describe('coupon', function (): void {
|
|||
->and($coupon->createdBy)->toBe($this->createdBy)
|
||||
->and($coupon->products)->toBe($this->products)
|
||||
->and($coupon->availabilityCalendar)->toBe($this->availabilityCalendar)
|
||||
->and($coupon->gtin)->toBe($this->gtin)
|
||||
->and($coupon->gs1)->toBe($this->gs1)
|
||||
->and($coupon->updatedAt)->toBeNull()
|
||||
->and($coupon->updatedAt())->toBeNull();
|
||||
});
|
||||
|
|
@ -385,7 +385,7 @@ describe('coupon', function (): void {
|
|||
$this->createdBy,
|
||||
$this->products,
|
||||
$this->availabilityCalendar,
|
||||
$this->gtin,
|
||||
$this->gs1,
|
||||
);
|
||||
$secondCoupon = Coupon::create(
|
||||
$this->code,
|
||||
|
|
@ -401,7 +401,7 @@ describe('coupon', function (): void {
|
|||
$this->createdBy,
|
||||
$this->products,
|
||||
$this->availabilityCalendar,
|
||||
$this->gtin,
|
||||
$this->gs1,
|
||||
);
|
||||
|
||||
// THEN
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ describe('campaign coupon', function (): void {
|
|||
// GIVEN
|
||||
$processingCode = new ProcessingCode('PC1');
|
||||
$quantity = new CouponQuantity(10);
|
||||
$availableIn = Country::FRANCE;
|
||||
$availableIn = [Country::FRANCE];
|
||||
$warningThresholdQuantity = new WarningThresholdQuantity(100);
|
||||
$printRules = new PrintRules(maxPerCustomer: 3);
|
||||
|
||||
|
|
@ -37,7 +37,7 @@ describe('campaign coupon', function (): void {
|
|||
// GIVEN
|
||||
$processingCode = new ProcessingCode('PC1');
|
||||
$quantity = new CouponQuantity(10);
|
||||
$availableIn = Country::FRANCE;
|
||||
$availableIn = [Country::FRANCE];
|
||||
$printRules = new PrintRules(maxPerCustomer: 3);
|
||||
|
||||
// WHEN
|
||||
|
|
@ -59,7 +59,7 @@ describe('campaign coupon', function (): void {
|
|||
// GIVEN
|
||||
$processingCode = new ProcessingCode('PC1');
|
||||
$quantity = new CouponQuantity(10);
|
||||
$availableIn = Country::FRANCE;
|
||||
$availableIn = [Country::FRANCE];
|
||||
$warningThresholdQuantity = new WarningThresholdQuantity(100);
|
||||
|
||||
// WHEN
|
||||
|
|
@ -81,7 +81,7 @@ describe('campaign coupon', function (): void {
|
|||
// GIVEN
|
||||
$processingCode = new ProcessingCode('PC1');
|
||||
$quantity = new CouponQuantity(10);
|
||||
$availableIn = Country::FRANCE;
|
||||
$availableIn = [Country::FRANCE];
|
||||
|
||||
// WHEN
|
||||
$campaignCoupon = new CampaignCoupon(processingCode: $processingCode, quantity: $quantity, availableIn: $availableIn);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue