fix: US2.2 - Review feedbacks - Remove useless tests, rename UpdateCampaign, rewrite failed outputs, rewrite auto unpublish, move campaign availability checker
This commit is contained in:
parent
4ff1a6c48a
commit
689368109b
18 changed files with 698 additions and 233 deletions
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Campaign\Update;
|
||||
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignNotFound;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignStatusUpdated;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignUnpublicationSkipped;
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Ports\Repositories\CampaignRepository;
|
||||
use Domain\Services\Campaign\CampaignAvailabilityChecker;
|
||||
use Shared\DDD\UseCases\Input as BaseInput;
|
||||
use Shared\DDD\UseCases\Output;
|
||||
use Shared\DDD\UseCases\Transactional;
|
||||
use Shared\DDD\UseCases\UseCase;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/** @implements UseCase<\Application\UseCases\Campaign\Update\AutoUnpublishCampaignInput, \Shared\DDD\UseCases\Output> */
|
||||
final readonly class AutoUnpublishCampaign implements UseCase, Transactional
|
||||
{
|
||||
public function __construct(
|
||||
private CampaignRepository $repository,
|
||||
private CampaignAvailabilityChecker $availabilityChecker
|
||||
) {
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function execute(BaseInput $input): Output
|
||||
{
|
||||
Assert::isInstanceOf($input, AutoUnpublishCampaignInput::class);
|
||||
|
||||
$campaign = $this->repository->findByCode($input->code);
|
||||
|
||||
if (! $campaign instanceof Campaign) {
|
||||
return new CampaignNotFound();
|
||||
}
|
||||
|
||||
if (! $campaign->publication || $this->availabilityChecker->hasActiveCoupon($campaign)) {
|
||||
return new CampaignUnpublicationSkipped();
|
||||
}
|
||||
|
||||
$campaign = $campaign->unpublish();
|
||||
$this->repository->save($campaign);
|
||||
|
||||
return new CampaignStatusUpdated(
|
||||
name: $campaign->name,
|
||||
code: $campaign->code,
|
||||
publication: $campaign->publication,
|
||||
printAllAtOnce: $campaign->printAllAtOnce,
|
||||
coupons: $campaign->coupons,
|
||||
brandCode: $campaign->brandCode,
|
||||
actorId: $campaign->actorId
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Campaign\Update;
|
||||
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCode;
|
||||
use Shared\DDD\UseCases\Input as UseCaseInput;
|
||||
|
||||
final readonly class AutoUnpublishCampaignInput implements UseCaseInput
|
||||
{
|
||||
public function __construct(
|
||||
public CampaignCode $code,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ use Domain\Models\ValueObjects\Campaign\CampaignCouponArray;
|
|||
use Domain\Models\ValueObjects\Campaign\CampaignName;
|
||||
use Shared\DDD\UseCases\Output;
|
||||
|
||||
final readonly class CampaignUpdated implements Output
|
||||
final readonly class CampaignStatusUpdated implements Output
|
||||
{
|
||||
public function __construct(
|
||||
public CampaignCode $code,
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Campaign\Update\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output;
|
||||
|
||||
final readonly class CampaignUnpublicationSkipped implements Output
|
||||
{
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Campaign\Update\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
|
||||
final readonly class CampaignUpdateFailed implements UseCaseOutput
|
||||
{
|
||||
public function __construct(
|
||||
public UpdateCampaignError $error,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Campaign\Update\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output;
|
||||
|
||||
final readonly class NotEnoughCouponAvailable implements Output
|
||||
{
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Campaign\Update\Outputs;
|
||||
|
||||
enum UpdateCampaignError: string
|
||||
{
|
||||
case CampaignAlreadyPublished = 'campaign_already_published';
|
||||
case CampaignAlreadyUnpublished = 'campaign_already_unpublished';
|
||||
}
|
||||
|
|
@ -5,12 +5,11 @@ declare(strict_types=1);
|
|||
namespace Application\UseCases\Campaign\Update;
|
||||
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignNotFound;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignUpdated;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignUpdateFailed;
|
||||
use Application\UseCases\Campaign\Update\Outputs\UpdateCampaignError;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignStatusUpdated;
|
||||
use Application\UseCases\Campaign\Update\Outputs\NotEnoughCouponAvailable;
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Ports\Repositories\CampaignRepository;
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
use Domain\Services\Campaign\CampaignAvailabilityChecker;
|
||||
use Shared\DDD\UseCases\Input as BaseInput;
|
||||
use Shared\DDD\UseCases\Output;
|
||||
use Shared\DDD\UseCases\Transactional;
|
||||
|
|
@ -18,11 +17,11 @@ use Shared\DDD\UseCases\UseCase;
|
|||
use Webmozart\Assert\Assert;
|
||||
|
||||
/** @implements UseCase<\Application\UseCases\Campaign\Update\Input, \Shared\DDD\UseCases\Output> */
|
||||
final readonly class UpdateCampaign implements UseCase, Transactional
|
||||
final readonly class PublishCampaign implements UseCase, Transactional
|
||||
{
|
||||
public function __construct(
|
||||
private CampaignRepository $repository,
|
||||
private CouponRepository $couponRepository
|
||||
private CampaignAvailabilityChecker $availabilityChecker
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -37,21 +36,19 @@ final readonly class UpdateCampaign implements UseCase, Transactional
|
|||
return new CampaignNotFound();
|
||||
}
|
||||
|
||||
if ($campaign->publication && $input->publication) {
|
||||
return new CampaignUpdateFailed(UpdateCampaignError::CampaignAlreadyPublished);
|
||||
}
|
||||
if ($input->publication) {
|
||||
if (! $this->availabilityChecker->hasActiveCoupon($campaign)) {
|
||||
return new NotEnoughCouponAvailable();
|
||||
}
|
||||
|
||||
if (! $campaign->publication && ! $input->publication) {
|
||||
return new CampaignUpdateFailed(UpdateCampaignError::CampaignAlreadyUnpublished);
|
||||
$campaign = $campaign->publish();
|
||||
} else {
|
||||
$campaign = $campaign->unpublish();
|
||||
}
|
||||
|
||||
$campaign = $input->publication ?
|
||||
$campaign->publish($this->couponRepository->findByCodes($campaign->coupons->getCouponCodes())) :
|
||||
$campaign->unpublish();
|
||||
|
||||
$this->repository->save($campaign);
|
||||
|
||||
return new CampaignUpdated(
|
||||
return new CampaignStatusUpdated(
|
||||
name: $campaign->name,
|
||||
code: $campaign->code,
|
||||
publication: $campaign->publication,
|
||||
|
|
@ -10,6 +10,7 @@ use Domain\Models\ValueObjects\Campaign\CampaignCode;
|
|||
use Domain\Models\ValueObjects\Campaign\CampaignCouponArray;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignId;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignName;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponCode;
|
||||
use Shared\DDD\Models\AggregateRoot;
|
||||
use Shared\DDD\Models\HasTimestampIdentifier;
|
||||
use Shared\Helpers\DatesHelper;
|
||||
|
|
@ -89,35 +90,13 @@ final readonly class Campaign implements AggregateRoot, HasTimestampIdentifier
|
|||
);
|
||||
}
|
||||
|
||||
/** @param array<Coupon> $coupons Coupons available in the Campaign (with CampaignCoupon) */
|
||||
public function publish(array $coupons): self
|
||||
public function publish(): self
|
||||
{
|
||||
if ($this->publication) {
|
||||
throw new \DomainException('Campaign is already published.');
|
||||
}
|
||||
|
||||
$hasAvailableCoupon = true;
|
||||
foreach ($coupons as $coupon) {
|
||||
try {
|
||||
$coupon->getActivePeriod();
|
||||
} catch (\DomainException) {
|
||||
$hasAvailableCoupon = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (! $hasAvailableCoupon) {
|
||||
throw new \DomainException('At least 1 available coupon should be provided to activate a campaign.');
|
||||
}
|
||||
|
||||
return $this->changePublication(true);
|
||||
}
|
||||
|
||||
public function unpublish(): self
|
||||
{
|
||||
if (! $this->publication) {
|
||||
throw new \DomainException('Campaign is already unpublished.');
|
||||
}
|
||||
|
||||
return $this->changePublication(false);
|
||||
}
|
||||
|
||||
|
|
@ -154,4 +133,10 @@ final readonly class Campaign implements AggregateRoot, HasTimestampIdentifier
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** @return array<CouponCode> */
|
||||
public function couponCodes(): array
|
||||
{
|
||||
return $this->coupons->getCouponCodes();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -215,4 +215,15 @@ final readonly class Coupon implements AggregateRoot, HasTimestampIdentifier
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
try {
|
||||
$this->getActivePeriod();
|
||||
|
||||
return true;
|
||||
} catch (\DomainException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Domain\Models\ValueObjects\Campaign;
|
||||
|
||||
use Domain\Models\ValueObjects\Coupon\CouponCode;
|
||||
|
||||
final readonly class CampaignCouponArray
|
||||
{
|
||||
public function __construct(
|
||||
|
|
|
|||
24
src/Domain/Services/Campaign/CampaignAvailabilityChecker.php
Normal file
24
src/Domain/Services/Campaign/CampaignAvailabilityChecker.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Services\Campaign;
|
||||
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
|
||||
final readonly class CampaignAvailabilityChecker
|
||||
{
|
||||
public function __construct(private CouponRepository $couponRepository)
|
||||
{
|
||||
}
|
||||
|
||||
public function hasActiveCoupon(Campaign $campaign): bool
|
||||
{
|
||||
return array_any(
|
||||
$this->couponRepository->findByCodes($campaign->couponCodes()),
|
||||
static fn (Coupon $coupon): bool => $coupon->isActive(),
|
||||
);
|
||||
}
|
||||
}
|
||||
103
tests/Fixtures/Campaign/CampaignFactory.php
Normal file
103
tests/Fixtures/Campaign/CampaignFactory.php
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Fixtures\Campaign;
|
||||
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Models\ValueObjects\Actor\ActorId;
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCoupon;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCouponArray;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignId;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignName;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponCode;
|
||||
use Domain\Models\ValueObjects\PrintRules;
|
||||
|
||||
final class CampaignFactory
|
||||
{
|
||||
/**
|
||||
* Campagne "neuve" (id, code générés, updatedAt = null), publiée par défaut.
|
||||
*
|
||||
* @param list<string|CouponCode>|null $couponCodes ignoré si $coupons est fourni
|
||||
* @param list<CampaignCoupon>|null $coupons prioritaire sur $couponCodes
|
||||
*/
|
||||
public static function make(
|
||||
string $name = 'Campaign name',
|
||||
bool $printAllAtOnce = false,
|
||||
?array $couponCodes = null,
|
||||
?array $coupons = null,
|
||||
?PrintRules $printRules = null,
|
||||
string $brandCode = 'AAA01',
|
||||
?ActorId $actorId = null,
|
||||
): Campaign {
|
||||
return Campaign::create(
|
||||
name: new CampaignName($name),
|
||||
printAllAtOnce: $printAllAtOnce,
|
||||
coupons: self::coupons($coupons, $couponCodes, $printRules),
|
||||
brandCode: new BrandCode($brandCode),
|
||||
actorId: $actorId ?? ActorId::generate(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Campagne reconstruite comme depuis la base : permet de fixer id, code et updatedAt.
|
||||
* Nécessite que hydrate() accepte $updatedAt en dernier paramètre.
|
||||
*
|
||||
* @param list<string|CouponCode>|null $couponCodes
|
||||
* @param list<CampaignCoupon>|null $coupons
|
||||
*/
|
||||
public static function hydrated(
|
||||
?CampaignId $id = null,
|
||||
?CampaignCode $code = null,
|
||||
bool $published = true,
|
||||
string $name = 'Campaign name',
|
||||
bool $printAllAtOnce = false,
|
||||
?array $couponCodes = null,
|
||||
?array $coupons = null,
|
||||
?PrintRules $printRules = null,
|
||||
string $brandCode = 'AAA01',
|
||||
?ActorId $actorId = null,
|
||||
): Campaign {
|
||||
return Campaign::hydrate(
|
||||
id: $id ?? CampaignId::generate(),
|
||||
code: $code ?? CampaignCode::generate(),
|
||||
name: new CampaignName($name),
|
||||
publication: $published,
|
||||
printAllAtOnce: $printAllAtOnce,
|
||||
coupons: self::coupons($coupons, $couponCodes, $printRules),
|
||||
brandCode: new BrandCode($brandCode),
|
||||
actorId: $actorId ?? ActorId::generate(),
|
||||
);
|
||||
}
|
||||
|
||||
public static function published(string ...$couponCodes): Campaign
|
||||
{
|
||||
return self::hydrated(published: true, couponCodes: $couponCodes ?: null);
|
||||
}
|
||||
|
||||
public static function unpublished(string ...$couponCodes): Campaign
|
||||
{
|
||||
return self::hydrated(published: false, couponCodes: $couponCodes ?: null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<CampaignCoupon>|null $coupons
|
||||
* @param list<string|CouponCode>|null $couponCodes
|
||||
*/
|
||||
private static function coupons(?array $coupons, ?array $couponCodes, ?PrintRules $printRules): CampaignCouponArray
|
||||
{
|
||||
if ($coupons !== null) {
|
||||
return new CampaignCouponArray($coupons);
|
||||
}
|
||||
|
||||
return new CampaignCouponArray(array_map(
|
||||
static fn (string | CouponCode $code): CampaignCoupon => new CampaignCoupon(
|
||||
$code instanceof CouponCode ? $code : new CouponCode($code),
|
||||
$printRules,
|
||||
),
|
||||
$couponCodes ?? ['COU01'],
|
||||
));
|
||||
}
|
||||
}
|
||||
96
tests/Fixtures/Coupon/CouponFactory.php
Normal file
96
tests/Fixtures/Coupon/CouponFactory.php
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Fixtures\Coupon;
|
||||
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Models\ValueObjects\Actor\ActorId;
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
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\CouponLegalInformationArray;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponMovement;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponMovementArray;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponName;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponPicture;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponProductArray;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponValidityInformation;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponValidityInformationArray;
|
||||
use Domain\Models\ValueObjects\IncoCode;
|
||||
use Domain\Models\ValueObjects\MoneyAmount;
|
||||
use Domain\Models\ValueObjects\Period;
|
||||
use Domain\Models\ValueObjects\PicturePath;
|
||||
use Domain\Models\ValueObjects\ProcessingCode;
|
||||
use Domain\Models\ValueObjects\ProcessingCodeArray;
|
||||
use Shared\Enums\Country;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\ValueObjects\Money;
|
||||
|
||||
final class CouponFactory
|
||||
{
|
||||
public static function make(
|
||||
string | CouponCode $code = 'COU01',
|
||||
?int $amount = 100,
|
||||
?\DateTime $diffusionStart = null,
|
||||
?\DateTime $diffusionEnd = null,
|
||||
?CouponAvailabilityCalendar $availabilityCalendar = null,
|
||||
string $brandCode = 'AAA01',
|
||||
): Coupon {
|
||||
return Coupon::create(
|
||||
code: $code instanceof CouponCode ? $code : new CouponCode($code),
|
||||
name: new CouponName('Coupon name'),
|
||||
amount: new MoneyAmount(new Money(Currency::EUR, $amount)),
|
||||
picture: new CouponPicture(
|
||||
new PicturePath('files/product.jpg'),
|
||||
new PicturePath('files/thumbnail.jpg'),
|
||||
new PicturePath('files/mobile_thumbnail.jpg'),
|
||||
),
|
||||
diffusion: new CouponDiffusion(
|
||||
new Period(
|
||||
start: $diffusionStart ?? new \DateTime()->modify('-1 month'),
|
||||
end: $diffusionEnd ?? new \DateTime()->modify('+1 month'),
|
||||
),
|
||||
),
|
||||
validityInformations: new CouponValidityInformationArray([
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE),
|
||||
new CouponValidityInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX)
|
||||
]),
|
||||
legalInformations: new CouponLegalInformationArray([
|
||||
new CouponLegalInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::FRANCE),
|
||||
new CouponLegalInformation('Lorem ipsum dolor sit amet consectetur adipiscing elit.', Country::BENELUX)
|
||||
]),
|
||||
processingCode: new ProcessingCodeArray([
|
||||
new ProcessingCode('5425003944130', Country::FRANCE),
|
||||
new ProcessingCode('4006381333931', Country::BENELUX),
|
||||
]),
|
||||
brandCode: new BrandCode($brandCode),
|
||||
createdBy: ActorId::generate(),
|
||||
products: new CouponProductArray([new IncoCode(35784), new IncoCode(15522)]),
|
||||
movements: new CouponMovementArray([new CouponMovement(1000)]),
|
||||
availabilityCalendar: $availabilityCalendar,
|
||||
);
|
||||
}
|
||||
|
||||
/** Coupon dont la diffusion est terminée. */
|
||||
public static function expired(string | CouponCode $code = 'COU01'): Coupon
|
||||
{
|
||||
return self::make(
|
||||
code: $code,
|
||||
diffusionStart: new \DateTime()->modify('-2 months'),
|
||||
diffusionEnd: new \DateTime()->modify('-1 day'),
|
||||
);
|
||||
}
|
||||
|
||||
/** Coupon dont la diffusion n'a pas commencé. */
|
||||
public static function upcoming(string | CouponCode $code = 'COU01'): Coupon
|
||||
{
|
||||
return self::make(
|
||||
code: $code,
|
||||
diffusionStart: new \DateTime()->modify('+1 day'),
|
||||
diffusionEnd: new \DateTime()->modify('+2 months'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,166 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Application\UseCases\Campaign\Update;
|
||||
|
||||
use Application\UseCases\Campaign\Update\AutoUnpublishCampaign;
|
||||
use Application\UseCases\Campaign\Update\AutoUnpublishCampaignInput;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignNotFound;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignStatusUpdated;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignUnpublicationSkipped;
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCode;
|
||||
use Domain\Ports\Repositories\CampaignRepository;
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
use Domain\Services\Campaign\CampaignAvailabilityChecker;
|
||||
use Tests\Fixtures\Campaign\CampaignFactory;
|
||||
use Tests\Fixtures\Coupon\CouponFactory;
|
||||
|
||||
covers(
|
||||
AutoUnpublishCampaign::class,
|
||||
AutoUnpublishCampaignInput::class,
|
||||
CampaignStatusUpdated::class,
|
||||
CampaignUnpublicationSkipped::class,
|
||||
CampaignNotFound::class,
|
||||
);
|
||||
|
||||
describe('auto unpublish campaign use case', function (): void {
|
||||
it('returns CampaignNotFound when the campaign does not exist', function (): void {
|
||||
// GIVEN
|
||||
$code = CampaignCode::generate();
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($code)->andReturn(null);
|
||||
$repository->shouldNotReceive('save');
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldNotReceive('findByCodes');
|
||||
|
||||
$useCase = new AutoUnpublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute(new AutoUnpublishCampaignInput($code));
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignNotFound::class);
|
||||
});
|
||||
|
||||
it('skips an already unpublished campaign without querying coupons', function (): void {
|
||||
// GIVEN
|
||||
$campaign = CampaignFactory::hydrated(published: false);
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($campaign->code)->andReturn($campaign);
|
||||
$repository->shouldNotReceive('save');
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldNotReceive('findByCodes');
|
||||
|
||||
$useCase = new AutoUnpublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute(new AutoUnpublishCampaignInput($campaign->code));
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignUnpublicationSkipped::class);
|
||||
});
|
||||
|
||||
it('skips a published campaign that still has an active coupon', function (): void {
|
||||
// GIVEN
|
||||
$campaign = CampaignFactory::hydrated(published: true, couponCodes: ['COU01']);
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($campaign->code)->andReturn($campaign);
|
||||
$repository->shouldNotReceive('save');
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldReceive('findByCodes')->once()->andReturn([CouponFactory::make('COU01')]);
|
||||
|
||||
$useCase = new AutoUnpublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute(new AutoUnpublishCampaignInput($campaign->code));
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignUnpublicationSkipped::class);
|
||||
});
|
||||
|
||||
it('skips a published campaign with one active and one expired coupon', function (): void {
|
||||
// GIVEN
|
||||
$campaign = CampaignFactory::hydrated(published: true, couponCodes: ['COU01', 'COU02']);
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($campaign->code)->andReturn($campaign);
|
||||
$repository->shouldNotReceive('save');
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldReceive('findByCodes')->once()->andReturn([
|
||||
CouponFactory::expired('COU01'),
|
||||
CouponFactory::make('COU02'),
|
||||
]);
|
||||
|
||||
$useCase = new AutoUnpublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute(new AutoUnpublishCampaignInput($campaign->code));
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignUnpublicationSkipped::class);
|
||||
});
|
||||
|
||||
it('unpublishes a published campaign when every coupon is not available', function (): void {
|
||||
// GIVEN
|
||||
$campaign = CampaignFactory::hydrated(published: true, couponCodes: ['COU01', 'COU02']);
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($campaign->code)->andReturn($campaign);
|
||||
$repository->shouldReceive('save')->once()->with(\Mockery::on(
|
||||
static fn (Campaign $saved): bool => $saved->publication === false
|
||||
&& $saved->code === $campaign->code
|
||||
&& $saved->updatedAt() instanceof \DateTimeInterface,
|
||||
));
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldReceive('findByCodes')->once()->andReturn([
|
||||
CouponFactory::expired('COU01'),
|
||||
CouponFactory::upcoming('COU02'),
|
||||
]);
|
||||
|
||||
$useCase = new AutoUnpublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute(new AutoUnpublishCampaignInput($campaign->code));
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignStatusUpdated::class)
|
||||
->and($result->publication)->toBeFalse()
|
||||
->and($result->code)->toBe($campaign->code)
|
||||
->and($result->name)->toBe($campaign->name)
|
||||
->and($result->brandCode)->toBe($campaign->brandCode)
|
||||
->and($result->coupons)->toBe($campaign->coupons);
|
||||
});
|
||||
|
||||
it('unpublishes a published campaign when no coupon is found', function (): void {
|
||||
// GIVEN
|
||||
$campaign = CampaignFactory::hydrated(published: true, couponCodes: ['COU01']);
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($campaign->code)->andReturn($campaign);
|
||||
$repository->shouldReceive('save')->once()->with(\Mockery::on(
|
||||
static fn (Campaign $saved): bool => $saved->publication === false,
|
||||
));
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldReceive('findByCodes')->once()->andReturn([]);
|
||||
|
||||
$useCase = new AutoUnpublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute(new AutoUnpublishCampaignInput($campaign->code));
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignStatusUpdated::class)
|
||||
->and($result->publication)->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
|
@ -6,10 +6,9 @@ namespace Tests\Unit\Application\UseCases\Campaign\Update;
|
|||
|
||||
use Application\UseCases\Campaign\Update\Input;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignNotFound;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignUpdated;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignUpdateFailed;
|
||||
use Application\UseCases\Campaign\Update\Outputs\UpdateCampaignError;
|
||||
use Application\UseCases\Campaign\Update\UpdateCampaign;
|
||||
use Application\UseCases\Campaign\Update\Outputs\CampaignStatusUpdated;
|
||||
use Application\UseCases\Campaign\Update\Outputs\NotEnoughCouponAvailable;
|
||||
use Application\UseCases\Campaign\Update\PublishCampaign;
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Models\ValueObjects\Actor\ActorId;
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
|
|
@ -22,10 +21,14 @@ use Domain\Models\ValueObjects\Coupon\CouponCode;
|
|||
use Domain\Models\ValueObjects\PrintRules;
|
||||
use Domain\Ports\Repositories\CampaignRepository;
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
use Domain\Services\Campaign\CampaignAvailabilityChecker;
|
||||
use Tests\Fixtures\Coupon\CouponFactory;
|
||||
|
||||
covers(UpdateCampaign::class, Input::class, CampaignUpdateFailed::class, CampaignUpdated::class, CampaignNotFound::class);
|
||||
covers(PublishCampaign::class, Input::class, CampaignStatusUpdated::class, CampaignNotFound::class);
|
||||
|
||||
describe('update campaign publication use case', function (): void {
|
||||
$campaignCoupon1 = new CampaignCoupon(new CouponCode('COU01'), new PrintRules(maxPerCustomer: 1));
|
||||
$campaignCoupon2 = new CampaignCoupon(new CouponCode('COU02'), new PrintRules(maxPerCustomer: 1));
|
||||
$existingCampaign = static fn (CampaignCode $code, bool $basePublicationStatus): Campaign => Campaign::hydrate(
|
||||
CampaignId::generate(),
|
||||
$code,
|
||||
|
|
@ -33,8 +36,8 @@ describe('update campaign publication use case', function (): void {
|
|||
$basePublicationStatus,
|
||||
false,
|
||||
new CampaignCouponArray([
|
||||
new CampaignCoupon(new CouponCode('AAA01'), new PrintRules(maxPerCustomer: 1)),
|
||||
new CampaignCoupon(new CouponCode('AAA02'), new PrintRules(maxPerCustomer: 1))
|
||||
$campaignCoupon1,
|
||||
$campaignCoupon2
|
||||
]),
|
||||
new BrandCode('AAA01'),
|
||||
ActorId::generate()
|
||||
|
|
@ -57,45 +60,27 @@ describe('update campaign publication use case', function (): void {
|
|||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
|
||||
$useCase = new UpdateCampaign($repository, $couponRepository);
|
||||
$useCase = new PublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignUpdated::class)
|
||||
expect($result)->toBeInstanceOf(CampaignStatusUpdated::class)
|
||||
->and($result->publication)->toBeFalse();
|
||||
});
|
||||
|
||||
it('throws when unpublish an already unpublished campaign', function () use ($existingCampaign): void {
|
||||
// GIVEN
|
||||
$code = CampaignCode::generate();
|
||||
$campaign = $existingCampaign($code, false);
|
||||
$publication = false;
|
||||
$input = new Input(code: $campaign->code, publication: $publication);
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($code)->andReturn($campaign);
|
||||
$repository->shouldNotReceive('save');
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
|
||||
$useCase = new UpdateCampaign($repository, $couponRepository);
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignUpdateFailed::class)
|
||||
->and($result->error)->toBe(UpdateCampaignError::CampaignAlreadyUnpublished);
|
||||
});
|
||||
|
||||
it('publish an unpublished campaign', function () use ($existingCampaign): void {
|
||||
// GIVEN
|
||||
$code = CampaignCode::generate();
|
||||
$campaign = $existingCampaign($code, false);
|
||||
$publication = true;
|
||||
$input = new Input(code: $campaign->code, publication: $publication);
|
||||
$code = CampaignCode::generate();
|
||||
$campaign = $existingCampaign($code, false);
|
||||
$publication = true;
|
||||
$input = new Input(code: $campaign->code, publication: $publication);
|
||||
$expectedCoupons = [
|
||||
CouponFactory::make('COU01'),
|
||||
CouponFactory::make('COU02'),
|
||||
CouponFactory::expired('COU03'),
|
||||
];
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($code)->andReturn($campaign);
|
||||
|
|
@ -106,42 +91,19 @@ describe('update campaign publication use case', function (): void {
|
|||
));
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldReceive('findByCodes')->once()->with($campaign->coupons->getCouponCodes());
|
||||
$couponRepository->shouldReceive('findByCodes')->once()->with($campaign->coupons->getCouponCodes())->andReturn($expectedCoupons);
|
||||
|
||||
$useCase = new UpdateCampaign($repository, $couponRepository);
|
||||
$useCase = new PublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignUpdated::class)
|
||||
expect($result)->toBeInstanceOf(CampaignStatusUpdated::class)
|
||||
->and($result->publication)->toBeTrue();
|
||||
});
|
||||
|
||||
it('throws when publish an already published campaign', function () use ($existingCampaign): void {
|
||||
// GIVEN
|
||||
$code = CampaignCode::generate();
|
||||
$campaign = $existingCampaign($code, true);
|
||||
$publication = true;
|
||||
$input = new Input(code: $campaign->code, publication: $publication);
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($code)->andReturn($campaign);
|
||||
$repository->shouldNotReceive('save');
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
|
||||
$useCase = new UpdateCampaign($repository, $couponRepository);
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignUpdateFailed::class)
|
||||
->and($result->error)->toBe(UpdateCampaignError::CampaignAlreadyPublished);
|
||||
});
|
||||
|
||||
it('throws when the campaign is not found', function (): void {
|
||||
it('returns a CampaignNotFound output when the campaign is not found', function (): void {
|
||||
// GIVEN
|
||||
$code = CampaignCode::generate();
|
||||
$publication = true;
|
||||
|
|
@ -152,7 +114,7 @@ describe('update campaign publication use case', function (): void {
|
|||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
|
||||
$useCase = new UpdateCampaign($repository, $couponRepository);
|
||||
$useCase = new PublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
|
@ -160,4 +122,55 @@ describe('update campaign publication use case', function (): void {
|
|||
// THEN
|
||||
expect($result)->toBeInstanceOf(CampaignNotFound::class);
|
||||
});
|
||||
|
||||
it('returns a specific output when there is no available coupons in the campaign', function () use ($existingCampaign): void {
|
||||
// GIVEN
|
||||
$code = CampaignCode::generate();
|
||||
$campaign = $existingCampaign($code, false);
|
||||
$publication = true;
|
||||
$input = new Input(code: $campaign->code, publication: $publication);
|
||||
$expectedCoupons = [
|
||||
CouponFactory::expired('COU01'),
|
||||
CouponFactory::upcoming('COU02'),
|
||||
];
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($code)->andReturn($campaign);
|
||||
$repository->shouldNotReceive('save');
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldReceive('findByCodes')->once()->with($campaign->coupons->getCouponCodes())->andReturn($expectedCoupons);
|
||||
|
||||
$useCase = new PublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(NotEnoughCouponAvailable::class);
|
||||
});
|
||||
|
||||
it('returns a NotEnoughCouponAvailable output when there is no available coupons in the campaign', function () use ($existingCampaign): void {
|
||||
// GIVEN
|
||||
$code = CampaignCode::generate();
|
||||
$campaign = $existingCampaign($code, false);
|
||||
$publication = true;
|
||||
$input = new Input(code: $campaign->code, publication: $publication);
|
||||
$expectedCoupons = [];
|
||||
|
||||
$repository = \Mockery::mock(CampaignRepository::class);
|
||||
$repository->shouldReceive('findByCode')->once()->with($code)->andReturn($campaign);
|
||||
$repository->shouldNotReceive('save');
|
||||
|
||||
$couponRepository = \Mockery::mock(CouponRepository::class);
|
||||
$couponRepository->shouldReceive('findByCodes')->once()->with($campaign->coupons->getCouponCodes())->andReturn($expectedCoupons);
|
||||
|
||||
$useCase = new PublishCampaign($repository, new CampaignAvailabilityChecker($couponRepository));
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(NotEnoughCouponAvailable::class);
|
||||
});
|
||||
});
|
||||
|
|
@ -33,16 +33,17 @@ use Domain\Models\ValueObjects\PicturePath;
|
|||
use Domain\Models\ValueObjects\PrintRules;
|
||||
use Domain\Models\ValueObjects\ProcessingCode;
|
||||
use Domain\Models\ValueObjects\ProcessingCodeArray;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\Enums\Country;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\Helpers\DatesHelper;
|
||||
use Shared\ValueObjects\Money;
|
||||
use Tests\Fixtures\Campaign\CampaignFactory;
|
||||
|
||||
covers(Campaign::class);
|
||||
|
||||
describe('campaign', function (): void {
|
||||
$code = CampaignCode::generate();
|
||||
$name = new CampaignName('Name');
|
||||
$publication = \boolval(1);
|
||||
$printAllAtOnce = \boolval(1);
|
||||
$coupons = new CampaignCouponArray([
|
||||
new CampaignCoupon(
|
||||
|
|
@ -238,36 +239,6 @@ describe('campaign', function (): void {
|
|||
->and($campaign->updatedAt()->format('Y-m-d H:i:s'))->toBe('2026-06-15 16:45:00');
|
||||
});
|
||||
|
||||
it('throws when unpublish an already unpublished campaign', function () use (
|
||||
$code,
|
||||
$name,
|
||||
$printAllAtOnce,
|
||||
$coupons,
|
||||
$brandCode,
|
||||
$actorId
|
||||
): void {
|
||||
// GIVEN
|
||||
CarbonImmutable::setTestNow('2026-06-15 16:45:00 UTC');
|
||||
$id = CampaignId::generate();
|
||||
$oldPublicationStatus = false;
|
||||
$campaign = Campaign::hydrate(
|
||||
$id,
|
||||
$code,
|
||||
$name,
|
||||
$oldPublicationStatus,
|
||||
$printAllAtOnce,
|
||||
$coupons,
|
||||
$brandCode,
|
||||
$actorId
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn () => $campaign = $campaign->unpublish();
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Campaign is already unpublished.');
|
||||
});
|
||||
|
||||
it('publishes a campaign and sets updatedAt timestamp', function () use (
|
||||
$code,
|
||||
$name,
|
||||
|
|
@ -275,11 +246,9 @@ describe('campaign', function (): void {
|
|||
$coupons,
|
||||
$brandCode,
|
||||
$actorId,
|
||||
$makeCoupon
|
||||
): void {
|
||||
// GIVEN
|
||||
CarbonImmutable::setTestNow('2026-06-15 16:45:00 UTC');
|
||||
$now = DatesHelper::now();
|
||||
$id = CampaignId::generate();
|
||||
$oldPublicationStatus = false;
|
||||
$newPublicationStatus = true;
|
||||
|
|
@ -294,14 +263,8 @@ describe('campaign', function (): void {
|
|||
$actorId
|
||||
);
|
||||
|
||||
// Coupons valides
|
||||
$printableCoupon = $makeCoupon([
|
||||
'diffusionStart' => $now->modify('-1 month')->toDateTime(),
|
||||
'diffusionEnd' => $now->modify('+1 month')->toDateTime(),
|
||||
]);
|
||||
|
||||
// WHEN
|
||||
$campaign = $campaign->publish([$printableCoupon]);
|
||||
$campaign = $campaign->publish();
|
||||
|
||||
// THEN
|
||||
expect($campaign->id())->toBe($id)
|
||||
|
|
@ -315,68 +278,6 @@ describe('campaign', function (): void {
|
|||
->and($campaign->updatedAt()->format('Y-m-d H:i:s'))->toBe('2026-06-15 16:45:00');
|
||||
});
|
||||
|
||||
it('throws when publish an already published campaign', function () use ($code, $name, $printAllAtOnce, $coupons, $brandCode, $actorId): void {
|
||||
// GIVEN
|
||||
CarbonImmutable::setTestNow('2026-06-15 16:45:00 UTC');
|
||||
$id = CampaignId::generate();
|
||||
$oldPublicationStatus = true;
|
||||
$campaign = Campaign::hydrate(
|
||||
$id,
|
||||
$code,
|
||||
$name,
|
||||
$oldPublicationStatus,
|
||||
$printAllAtOnce,
|
||||
$coupons,
|
||||
$brandCode,
|
||||
$actorId
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn () => $campaign = $campaign->publish([]);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Campaign is already published.');
|
||||
});
|
||||
|
||||
it('throws when publish a campaign with no active coupons', function () use (
|
||||
$code,
|
||||
$name,
|
||||
$printAllAtOnce,
|
||||
$brandCode,
|
||||
$actorId,
|
||||
$makeCoupon
|
||||
): void {
|
||||
// GIVEN
|
||||
CarbonImmutable::setTestNow('2026-06-15 16:45:00 UTC');
|
||||
$now = DatesHelper::now();
|
||||
$id = CampaignId::generate();
|
||||
$oldPublicationStatus = false;
|
||||
|
||||
$printableCoupon = $makeCoupon([
|
||||
'diffusionStart' => $now->modify('+1 month')->toDateTime(),
|
||||
'diffusionEnd' => $now->modify('+2 month')->toDateTime(),
|
||||
]);
|
||||
$coupons = new CampaignCouponArray([
|
||||
new CampaignCoupon($printableCoupon->code)
|
||||
]);
|
||||
$campaign = Campaign::hydrate(
|
||||
$id,
|
||||
$code,
|
||||
$name,
|
||||
$oldPublicationStatus,
|
||||
$printAllAtOnce,
|
||||
$coupons,
|
||||
$brandCode,
|
||||
$actorId
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn () => $campaign = $campaign->publish([$printableCoupon]);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'At least 1 available coupon should be provided to activate a campaign.');
|
||||
});
|
||||
|
||||
it('unpublishes a campaign when there are no available coupons in it', function () use (
|
||||
$code,
|
||||
$name,
|
||||
|
|
@ -464,4 +365,16 @@ describe('campaign', function (): void {
|
|||
->and($campaign->createdAt())->toBeInstanceOf(DateTimeInterface::class)
|
||||
->and($campaign->updatedAt())->toBeInstanceOf(DateTimeInterface::class);
|
||||
});
|
||||
|
||||
it('gets the codes of the associated coupons', function (): void {
|
||||
// GIVEN
|
||||
$expectedCoupons = ['COU01', 'COU02'];
|
||||
$campaign = CampaignFactory::hydrated(couponCodes: $expectedCoupons);
|
||||
|
||||
// WHEN
|
||||
$coupons = array_map(static fn (CouponCode $c): string => $c->value, $campaign->couponCodes());
|
||||
|
||||
// THEN
|
||||
expect($coupons)->toBe($expectedCoupons);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Application\UseCases\Campaign\Update;
|
||||
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
use Domain\Services\Campaign\CampaignAvailabilityChecker;
|
||||
use Tests\Fixtures\Campaign\CampaignFactory;
|
||||
use Tests\Fixtures\Coupon\CouponFactory;
|
||||
|
||||
covers(CampaignAvailabilityChecker::class);
|
||||
|
||||
describe('campaign availability checker', function (): void {
|
||||
$makeRepository = static function (array $campaignCodes, array $coupons): CouponRepository {
|
||||
$repository = \Mockery::mock(CouponRepository::class);
|
||||
$repository->shouldReceive('findByCodes')
|
||||
->once()
|
||||
->with(\Mockery::on(
|
||||
static fn (array $codes): bool => array_map(
|
||||
static fn ($code): string => $code->value,
|
||||
$codes,
|
||||
) === $campaignCodes,
|
||||
))
|
||||
->andReturn($coupons);
|
||||
|
||||
return $repository;
|
||||
};
|
||||
|
||||
it('is available with an active coupon', function () use ($makeRepository): void {
|
||||
// GIVEN
|
||||
$campaign = CampaignFactory::hydrated(couponCodes: ['COU01']);
|
||||
$repository = $makeRepository(['COU01'], [CouponFactory::make('COU01')]);
|
||||
$service = new CampaignAvailabilityChecker($repository);
|
||||
|
||||
// WHEN
|
||||
$result = $service->hasActiveCoupon($campaign);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeTrue();
|
||||
});
|
||||
|
||||
it('is available with one active and one expired coupon', function () use ($makeRepository): void {
|
||||
// GIVEN
|
||||
$campaign = CampaignFactory::hydrated(couponCodes: ['COU01', 'COU02']);
|
||||
$repository = $makeRepository(['COU01', 'COU02'], [
|
||||
CouponFactory::expired('COU01'),
|
||||
CouponFactory::make('COU02'),
|
||||
]);
|
||||
$service = new CampaignAvailabilityChecker($repository);
|
||||
|
||||
// WHEN
|
||||
$result = $service->hasActiveCoupon($campaign);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeTrue();
|
||||
});
|
||||
|
||||
it('is not available when every coupon is expired or upcoming', function () use ($makeRepository): void {
|
||||
// GIVEN
|
||||
$campaign = CampaignFactory::hydrated(couponCodes: ['COU01', 'COU02']);
|
||||
$repository = $makeRepository(['COU01', 'COU02'], [
|
||||
CouponFactory::expired('COU01'),
|
||||
CouponFactory::upcoming('COU02'),
|
||||
]);
|
||||
$service = new CampaignAvailabilityChecker($repository);
|
||||
|
||||
// WHEN
|
||||
$result = $service->hasActiveCoupon($campaign);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeFalse();
|
||||
});
|
||||
|
||||
it('is not available when no coupon is found', function () use ($makeRepository): void {
|
||||
// GIVEN
|
||||
$campaign = CampaignFactory::hydrated(couponCodes: ['COU01']);
|
||||
$repository = $makeRepository(['COU01'], []);
|
||||
$service = new CampaignAvailabilityChecker($repository);
|
||||
|
||||
// WHEN
|
||||
$result = $service->hasActiveCoupon($campaign);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeFalse();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue