fix: ♻️ Sprint 4 feedbacks - reorganize outputs, remove useless tests ...
This commit is contained in:
parent
53fb12f20f
commit
f80c95479c
36 changed files with 384 additions and 362 deletions
|
|
@ -4,6 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Application\UseCases\Brand\Create;
|
||||
|
||||
use Application\UseCases\Brand\Create\Outputs\BrandCreated;
|
||||
use Application\UseCases\Brand\Create\Outputs\BrandCreationFailed;
|
||||
use Application\UseCases\Brand\Create\Outputs\CreateBrandError;
|
||||
use Domain\Models\Entities\Brand;
|
||||
use Domain\Ports\Repositories\BrandRepository;
|
||||
use Domain\Ports\Services\PictureStorage;
|
||||
|
|
@ -12,7 +15,7 @@ use Shared\DDD\UseCases\UseCase;
|
|||
use Shared\DDD\UseCases\Transactional;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/** @implements UseCase<\Application\UseCases\Brand\Create\Input, \Application\UseCases\Brand\Create\Output> */
|
||||
/** @implements UseCase<\Application\UseCases\Brand\Create\Input, \Application\UseCases\Brand\Create\Outputs\BrandCreated|\Application\UseCases\Brand\Create\Outputs\BrandCreationFailed> */
|
||||
final readonly class CreateBrand implements UseCase, Transactional
|
||||
{
|
||||
public function __construct(
|
||||
|
|
@ -22,12 +25,12 @@ final readonly class CreateBrand implements UseCase, Transactional
|
|||
}
|
||||
|
||||
#[\Override]
|
||||
public function execute(BaseInput $input): Output
|
||||
public function execute(BaseInput $input): BrandCreated | BrandCreationFailed
|
||||
{
|
||||
Assert::isInstanceOf($input, Input::class);
|
||||
|
||||
if ($this->repository->existsByCode($input->code)) {
|
||||
throw new \DomainException(sprintf('Brand code "%s" already exists.', $input->code->value));
|
||||
return new BrandCreationFailed(CreateBrandError::BrandCodeAlreadyExists);
|
||||
}
|
||||
|
||||
$logoPath = $this->pictureStorage->store($input->logo);
|
||||
|
|
@ -42,7 +45,7 @@ final readonly class CreateBrand implements UseCase, Transactional
|
|||
|
||||
$this->repository->save($brand);
|
||||
|
||||
return new Output(
|
||||
return new BrandCreated(
|
||||
name: $brand->name,
|
||||
code: $brand->code,
|
||||
logo: $brand->logo,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Brand\Create;
|
||||
namespace Application\UseCases\Brand\Create\Outputs;
|
||||
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Brand\BrandName;
|
||||
|
|
@ -11,7 +11,7 @@ use Shared\ValueObjects\Email;
|
|||
use Domain\Models\ValueObjects\PicturePath;
|
||||
use Shared\ValueObjects\Rcs;
|
||||
|
||||
final readonly class Output implements UseCaseOutput
|
||||
final readonly class BrandCreated implements UseCaseOutput
|
||||
{
|
||||
public function __construct(
|
||||
public BrandName $name,
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Brand\Create\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
|
||||
final readonly class BrandCreationFailed implements UseCaseOutput
|
||||
{
|
||||
public function __construct(
|
||||
public CreateBrandError $error,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Brand\Create\Outputs;
|
||||
|
||||
enum CreateBrandError: string
|
||||
{
|
||||
case BrandCodeAlreadyExists = 'brand_code_already_exists';
|
||||
}
|
||||
|
|
@ -4,15 +4,18 @@ declare(strict_types=1);
|
|||
|
||||
namespace Application\UseCases\CampaignRequest\Create;
|
||||
|
||||
use Application\UseCases\CampaignRequest\Create\Outputs\CampaignRequestCreated;
|
||||
use Application\UseCases\CampaignRequest\Outputs\BrandNotFound;
|
||||
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\Output;
|
||||
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> */
|
||||
/** @implements UseCase<\Application\UseCases\CampaignRequest\Create\Input, \Shared\DDD\UseCases\Output> */
|
||||
final readonly class CreateCampaignRequest implements UseCase, Transactional
|
||||
{
|
||||
public function __construct(
|
||||
|
|
@ -27,7 +30,7 @@ final readonly class CreateCampaignRequest implements UseCase, Transactional
|
|||
Assert::isInstanceOf($input, Input::class);
|
||||
|
||||
if (! $this->brandRepository->existsByCode($input->brandCode)) {
|
||||
throw new \DomainException(sprintf('Brand with code "%s" was not found.', $input->brandCode->value));
|
||||
return new BrandNotFound();
|
||||
}
|
||||
|
||||
$campaignRequest = CampaignRequest::create(
|
||||
|
|
@ -40,7 +43,7 @@ final readonly class CreateCampaignRequest implements UseCase, Transactional
|
|||
|
||||
$this->campaignRequestRepository->save($campaignRequest);
|
||||
|
||||
return new Output(
|
||||
return new CampaignRequestCreated(
|
||||
id: $campaignRequest->id(),
|
||||
brandCode: $campaignRequest->brandCode,
|
||||
name: $campaignRequest->name,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\CampaignRequest\Create;
|
||||
namespace Application\UseCases\CampaignRequest\Create\Outputs;
|
||||
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignName;
|
||||
|
|
@ -13,7 +13,7 @@ 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
|
||||
final readonly class CampaignRequestCreated implements UseCaseOutput
|
||||
{
|
||||
public function __construct(
|
||||
public CampaignRequestId $id,
|
||||
|
|
@ -6,7 +6,7 @@ namespace Application\UseCases\CampaignRequest\Find;
|
|||
|
||||
use Application\UseCases\CampaignRequest\Find\Output\CampaignRequestFound;
|
||||
use Application\UseCases\CampaignRequest\Find\Output\CampaignRequestNotFound;
|
||||
use Application\UseCases\CampaignRequest\Find\Output\Output;
|
||||
use Application\UseCases\CampaignRequest\Outputs\BrandNotFound;
|
||||
use Domain\Models\Entities\Brand;
|
||||
use Domain\Models\Entities\CampaignRequest;
|
||||
use Domain\Ports\Repositories\BrandRepository;
|
||||
|
|
@ -25,7 +25,7 @@ final readonly class FindCampaignRequest implements UseCase
|
|||
}
|
||||
|
||||
#[\Override]
|
||||
public function execute(BaseInput $input): Output
|
||||
public function execute(BaseInput $input): CampaignRequestNotFound | CampaignRequestFound | BrandNotFound
|
||||
{
|
||||
Assert::isInstanceOf($input, Input::class);
|
||||
|
||||
|
|
@ -38,7 +38,7 @@ final readonly class FindCampaignRequest implements UseCase
|
|||
$brand = $this->brandRepository->findByCode($campaignRequest->brandCode);
|
||||
|
||||
if (! $brand instanceof Brand) {
|
||||
throw new \DomainException(sprintf('Brand with code "%s" was not found.', $campaignRequest->brandCode->value));
|
||||
return new BrandNotFound();
|
||||
}
|
||||
|
||||
return new CampaignRequestFound(
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\CampaignRequest\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output;
|
||||
|
||||
final readonly class BrandNotFound implements Output
|
||||
{
|
||||
}
|
||||
|
|
@ -4,15 +4,19 @@ declare(strict_types=1);
|
|||
|
||||
namespace Application\UseCases\Coupon\Create;
|
||||
|
||||
use Application\UseCases\Coupon\Create\Outputs\CouponCreated;
|
||||
use Application\UseCases\Coupon\Outputs\BrandNotFound;
|
||||
use Application\UseCases\Coupon\Create\Outputs\CouponCodeAlreadyExists;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Ports\Repositories\BrandRepository;
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
use Shared\DDD\UseCases\Input as BaseInput;
|
||||
use Shared\DDD\UseCases\Output;
|
||||
use Shared\DDD\UseCases\UseCase;
|
||||
use Shared\DDD\UseCases\Transactional;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/** @implements UseCase<\Application\UseCases\Coupon\Create\Input, \Application\UseCases\Coupon\Create\Output> */
|
||||
/** @implements UseCase<\Application\UseCases\Coupon\Create\Input, \Shared\DDD\UseCases\Output> */
|
||||
final readonly class CreateCoupon implements UseCase, Transactional
|
||||
{
|
||||
public function __construct(
|
||||
|
|
@ -27,11 +31,11 @@ final readonly class CreateCoupon implements UseCase, Transactional
|
|||
Assert::isInstanceOf($input, Input::class);
|
||||
|
||||
if (! $this->brandRepository->existsByCode($input->brandCode)) {
|
||||
throw new \DomainException(sprintf('Brand code "%s" was not found.', $input->brandCode->value));
|
||||
return new BrandNotFound();
|
||||
}
|
||||
|
||||
if ($this->couponRepository->existsByCode($input->code)) {
|
||||
throw new \DomainException(sprintf('Coupon code "%s" already exists.', $input->code->value));
|
||||
return new CouponCodeAlreadyExists();
|
||||
}
|
||||
|
||||
$coupon = Coupon::create(
|
||||
|
|
@ -52,7 +56,7 @@ final readonly class CreateCoupon implements UseCase, Transactional
|
|||
|
||||
$this->couponRepository->save($coupon);
|
||||
|
||||
return new Output(
|
||||
return new CouponCreated(
|
||||
id: $coupon->id,
|
||||
code: $coupon->code,
|
||||
name: $coupon->name,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Coupon\Create\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output;
|
||||
|
||||
final readonly class CouponCodeAlreadyExists implements Output
|
||||
{
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Coupon\Create;
|
||||
namespace Application\UseCases\Coupon\Create\Outputs;
|
||||
|
||||
use Domain\Models\ValueObjects\Actor\ActorId;
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
|
|
@ -20,7 +20,7 @@ use Domain\Models\ValueObjects\MoneyAmount;
|
|||
use Domain\Models\ValueObjects\ProcessingCodeArray;
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
|
||||
final readonly class Output implements UseCaseOutput
|
||||
final readonly class CouponCreated implements UseCaseOutput
|
||||
{
|
||||
public function __construct(
|
||||
public CouponId $id,
|
||||
|
|
@ -7,6 +7,7 @@ namespace Application\UseCases\Coupon\Find;
|
|||
use Application\UseCases\Coupon\Find\Output\Output;
|
||||
use Application\UseCases\Coupon\Find\Output\CouponFound;
|
||||
use Application\UseCases\Coupon\Find\Output\CouponNotFound;
|
||||
use Application\UseCases\Coupon\Outputs\BrandNotFound;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Ports\Repositories\BrandRepository;
|
||||
use Domain\Ports\Repositories\CampaignRepository;
|
||||
|
|
@ -26,7 +27,7 @@ final readonly class FindCoupon implements UseCase
|
|||
}
|
||||
|
||||
#[\Override]
|
||||
public function execute(BaseInput $input): Output
|
||||
public function execute(BaseInput $input): CouponNotFound | BrandNotFound | Output
|
||||
{
|
||||
Assert::isInstanceOf($input, Input::class);
|
||||
|
||||
|
|
@ -36,7 +37,7 @@ final readonly class FindCoupon implements UseCase
|
|||
return new CouponNotFound();
|
||||
}
|
||||
if (! $this->brandRepository->existsByCode($coupon->brandCode)) {
|
||||
throw new \DomainException(sprintf('Brand code "%s" was not found.', $coupon->brandCode->value));
|
||||
return new BrandNotFound();
|
||||
}
|
||||
|
||||
$campaigns = $this->campaignRepository->findByCouponCode($coupon->code);
|
||||
|
|
|
|||
11
src/Application/UseCases/Coupon/Outputs/BrandNotFound.php
Normal file
11
src/Application/UseCases/Coupon/Outputs/BrandNotFound.php
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\Coupon\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output;
|
||||
|
||||
final readonly class BrandNotFound implements Output
|
||||
{
|
||||
}
|
||||
|
|
@ -4,27 +4,33 @@ declare(strict_types=1);
|
|||
|
||||
namespace Application\UseCases\PrintableCoupon\Create;
|
||||
|
||||
use Application\UseCases\PrintableCoupon\Create\Outputs\CampaignNotFound;
|
||||
use Application\UseCases\PrintableCoupon\Create\Outputs\CampaignNotPublished;
|
||||
use Application\UseCases\PrintableCoupon\Create\Outputs\CouponNotAssociated;
|
||||
use Application\UseCases\PrintableCoupon\Outputs\CouponNotFound;
|
||||
use Application\UseCases\PrintableCoupon\Create\Outputs\PrintableCouponCreated;
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Models\Entities\PrintableCoupon;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCoupon;
|
||||
use Domain\Models\ValueObjects\CrmHashcode;
|
||||
use Domain\Models\ValueObjects\PrintRules;
|
||||
use Domain\Ports\Repositories\CampaignRepository;
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
use Domain\Ports\Repositories\PrintableCouponRepository;
|
||||
use Domain\Services\PrintableCoupon\PrintEligibilityChecker;
|
||||
use Shared\DDD\UseCases\Input as BaseInput;
|
||||
use Shared\DDD\UseCases\Output;
|
||||
use Shared\DDD\UseCases\UseCase;
|
||||
use Shared\DDD\UseCases\Transactional;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/** @implements UseCase<\Application\UseCases\PrintableCoupon\Create\Input, \Application\UseCases\PrintableCoupon\Create\Output> */
|
||||
/** @implements UseCase<\Application\UseCases\PrintableCoupon\Create\Input, \Shared\DDD\UseCases\Output> */
|
||||
final readonly class CreatePrintableCoupon implements UseCase, Transactional
|
||||
{
|
||||
public function __construct(
|
||||
private PrintableCouponRepository $printableCouponRepository,
|
||||
private CouponRepository $couponRepository,
|
||||
private CampaignRepository $campaignRepository,
|
||||
private PrintEligibilityChecker $printEligibilityChecker
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
@ -35,25 +41,30 @@ final readonly class CreatePrintableCoupon implements UseCase, Transactional
|
|||
|
||||
$campaign = $this->campaignRepository->findByCode($input->campaignCode);
|
||||
if (! $campaign instanceof Campaign) {
|
||||
throw new \DomainException(sprintf('Campaign from code "%s" not found.', $input->campaignCode->value));
|
||||
return new CampaignNotFound();
|
||||
}
|
||||
if (! $campaign->publication) {
|
||||
throw new \DomainException('Campaign must be published.');
|
||||
return new CampaignNotPublished();
|
||||
}
|
||||
|
||||
$coupon = $this->couponRepository->findByCode($input->couponCode);
|
||||
if (! $coupon instanceof Coupon) {
|
||||
throw new \DomainException(sprintf('Coupon code "%s" not found.', $input->couponCode->value));
|
||||
return new CouponNotFound();
|
||||
}
|
||||
|
||||
$campaignCoupon = $this->campaignRepository->findCampaignCoupon($campaign->code, $coupon->code);
|
||||
if (! $campaignCoupon instanceof CampaignCoupon) {
|
||||
throw new \DomainException('Coupon must be associated to the associated campaign coupon.');
|
||||
return new CouponNotAssociated();
|
||||
}
|
||||
|
||||
$period = $coupon->getActivePeriod();
|
||||
|
||||
$this->checkPrintRulesFromCampaignCoupon($campaign, $campaignCoupon, $coupon, $input->crmCustomerHashcode);
|
||||
$this->printEligibilityChecker->checkPrintRulesFromCampaignCoupon(
|
||||
$campaign,
|
||||
$campaignCoupon,
|
||||
$coupon,
|
||||
$input->crmCustomerHashcode
|
||||
);
|
||||
|
||||
$printableCoupon = PrintableCoupon::create(
|
||||
campaignCode: $campaign->code,
|
||||
|
|
@ -65,7 +76,7 @@ final readonly class CreatePrintableCoupon implements UseCase, Transactional
|
|||
|
||||
$this->printableCouponRepository->save($printableCoupon);
|
||||
|
||||
return new Output(
|
||||
return new PrintableCouponCreated(
|
||||
campaignCode: $printableCoupon->campaignCode,
|
||||
couponCode: $printableCoupon->couponCode,
|
||||
couponName: $printableCoupon->couponName,
|
||||
|
|
@ -73,28 +84,4 @@ final readonly class CreatePrintableCoupon implements UseCase, Transactional
|
|||
distributableUntil: $printableCoupon->distributableUntil,
|
||||
);
|
||||
}
|
||||
|
||||
public function checkPrintRulesFromCampaignCoupon(
|
||||
Campaign $campaign,
|
||||
CampaignCoupon $campaignCoupon,
|
||||
Coupon $coupon,
|
||||
CrmHashcode $crmCustomerHashcode,
|
||||
): void {
|
||||
if ($campaignCoupon->printRules instanceof PrintRules) {
|
||||
$countPrintedCoupon = $this->printableCouponRepository->countPrintedCouponForCustomer(
|
||||
$campaign->code,
|
||||
$coupon->code,
|
||||
$crmCustomerHashcode,
|
||||
);
|
||||
$campaignCoupon->printRules->hasPrintLimitNotReached($countPrintedCoupon);
|
||||
|
||||
$lastPrintedCoupon = $this->printableCouponRepository->findLastPrintedCouponForCustomer(
|
||||
$campaign->code,
|
||||
$coupon->code,
|
||||
$crmCustomerHashcode,
|
||||
);
|
||||
$diff = $lastPrintedCoupon->printedAt()->diff(new \DateTime());
|
||||
$campaignCoupon->printRules->hasReprintPeriodExpired($diff->days);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\PrintableCoupon\Create\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
|
||||
final readonly class CampaignNotFound implements UseCaseOutput
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\PrintableCoupon\Create\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
|
||||
final readonly class CampaignNotPublished implements UseCaseOutput
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\PrintableCoupon\Create\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
|
||||
final readonly class CouponNotAssociated implements UseCaseOutput
|
||||
{
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\PrintableCoupon\Create;
|
||||
namespace Application\UseCases\PrintableCoupon\Create\Outputs;
|
||||
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCode;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponCode;
|
||||
|
|
@ -10,7 +10,7 @@ use Domain\Models\ValueObjects\Coupon\CouponName;
|
|||
use Domain\Models\ValueObjects\CrmHashcode;
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
|
||||
final readonly class Output implements UseCaseOutput
|
||||
final readonly class PrintableCouponCreated implements UseCaseOutput
|
||||
{
|
||||
public function __construct(
|
||||
public CampaignCode $campaignCode,
|
||||
|
|
@ -4,17 +4,20 @@ declare(strict_types=1);
|
|||
|
||||
namespace Application\UseCases\PrintableCoupon\List;
|
||||
|
||||
use Application\UseCases\PrintableCoupon\Outputs\CouponNotFound;
|
||||
use Application\UseCases\PrintableCoupon\List\Outputs\PrintableCouponListed;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Models\Entities\PrintableCoupon;
|
||||
use Domain\Models\ValueObjects\Coupon\CouponPicture;
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
use Domain\Ports\Repositories\PrintableCouponRepository;
|
||||
use Shared\DDD\UseCases\Input as BaseInput;
|
||||
use Shared\DDD\UseCases\Output;
|
||||
use Shared\DDD\UseCases\UseCase;
|
||||
use Shared\ValueObjects\Pagination\Paginated;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/** @implements UseCase<\Application\UseCases\PrintableCoupon\List\Input, \Application\UseCases\PrintableCoupon\List\Output> */
|
||||
/** @implements UseCase<\Application\UseCases\PrintableCoupon\List\Input, \Shared\DDD\UseCases\Output> */
|
||||
final readonly class ListPrintableCoupons implements UseCase
|
||||
{
|
||||
public function __construct(
|
||||
|
|
@ -49,7 +52,7 @@ final readonly class ListPrintableCoupons implements UseCase
|
|||
|
||||
$coupon = $this->couponRepository->findByCode($couponCode);
|
||||
if (!$coupon instanceof Coupon) {
|
||||
throw new \LogicException(sprintf('Coupon "%s" referenced by a printable coupon does not exist.', $couponCode->value));
|
||||
return new CouponNotFound();
|
||||
}
|
||||
|
||||
$picturesByCouponCode[$couponCode->value] = $coupon->picture;
|
||||
|
|
@ -66,7 +69,7 @@ final readonly class ListPrintableCoupons implements UseCase
|
|||
$printableCoupons,
|
||||
);
|
||||
|
||||
return new Output(
|
||||
return new PrintableCouponListed(
|
||||
printableCoupons: new Paginated(
|
||||
pagination: $result->getPagination(),
|
||||
items: $items,
|
||||
|
|
|
|||
|
|
@ -2,12 +2,13 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\PrintableCoupon\List;
|
||||
namespace Application\UseCases\PrintableCoupon\List\Outputs;
|
||||
|
||||
use Application\UseCases\PrintableCoupon\List\PrintableCouponListItem;
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
use Shared\ValueObjects\Pagination\Paginated;
|
||||
|
||||
final readonly class Output implements UseCaseOutput
|
||||
final readonly class PrintableCouponListed implements UseCaseOutput
|
||||
{
|
||||
/** @param Paginated<PrintableCouponListItem> $printableCoupons */
|
||||
public function __construct(
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Application\UseCases\PrintableCoupon\Outputs;
|
||||
|
||||
use Shared\DDD\UseCases\Output as UseCaseOutput;
|
||||
|
||||
final readonly class CouponNotFound implements UseCaseOutput
|
||||
{
|
||||
}
|
||||
|
|
@ -26,32 +26,4 @@ final readonly class CouponPicture
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function create(
|
||||
PicturePath $decoration,
|
||||
PicturePath $thumbnail,
|
||||
PicturePath $mobileThumbnail,
|
||||
): self {
|
||||
$picture = new self($decoration, $thumbnail, $mobileThumbnail);
|
||||
|
||||
$size = is_file($decoration->value) ? getimagesize($decoration->value) : false;
|
||||
|
||||
if ($size === false) {
|
||||
throw new \DomainException(sprintf('Unable to read the decoration picture "%s".', $decoration));
|
||||
}
|
||||
|
||||
[$width, $height] = $size;
|
||||
|
||||
if ($width !== self::DECORATION_WIDTH || $height !== self::DECORATION_HEIGHT) {
|
||||
throw new \DomainException(sprintf(
|
||||
'The decoration picture must be %d x %d px, %d x %d px given.',
|
||||
self::DECORATION_WIDTH,
|
||||
self::DECORATION_HEIGHT,
|
||||
$width,
|
||||
$height,
|
||||
));
|
||||
}
|
||||
|
||||
return $picture;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domain\Services\PrintableCoupon;
|
||||
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Models\ValueObjects\Campaign\CampaignCoupon;
|
||||
use Domain\Models\ValueObjects\CrmHashcode;
|
||||
use Domain\Models\ValueObjects\PrintRules;
|
||||
use Domain\Ports\Repositories\PrintableCouponRepository;
|
||||
|
||||
final readonly class PrintEligibilityChecker
|
||||
{
|
||||
public function __construct(
|
||||
private PrintableCouponRepository $printableCouponRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
public function checkPrintRulesFromCampaignCoupon(
|
||||
Campaign $campaign,
|
||||
CampaignCoupon $campaignCoupon,
|
||||
Coupon $coupon,
|
||||
CrmHashcode $crmCustomerHashcode,
|
||||
): void {
|
||||
if ($campaignCoupon->printRules instanceof PrintRules) {
|
||||
$countPrintedCoupon = $this->printableCouponRepository->countPrintedCouponForCustomer(
|
||||
$campaign->code,
|
||||
$coupon->code,
|
||||
$crmCustomerHashcode,
|
||||
);
|
||||
$campaignCoupon->printRules->hasPrintLimitNotReached($countPrintedCoupon);
|
||||
|
||||
$lastPrintedCoupon = $this->printableCouponRepository->findLastPrintedCouponForCustomer(
|
||||
$campaign->code,
|
||||
$coupon->code,
|
||||
$crmCustomerHashcode,
|
||||
);
|
||||
$diff = $lastPrintedCoupon->printedAt()->diff(new \DateTime());
|
||||
$campaignCoupon->printRules->hasReprintPeriodExpired($diff->days);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,21 +4,22 @@ declare(strict_types=1);
|
|||
|
||||
namespace Tests\Unit\Application\UseCases\Brand\Create;
|
||||
|
||||
use Application\UseCases\Brand\Create\Outputs\BrandCreated;
|
||||
use Application\UseCases\Brand\Create\Outputs\BrandCreationFailed;
|
||||
use Application\UseCases\Brand\Create\Outputs\CreateBrandError;
|
||||
use Application\UseCases\Brand\Create\CreateBrand;
|
||||
use Application\UseCases\Brand\Create\Input;
|
||||
use Application\UseCases\Brand\Create\Output;
|
||||
use Domain\Models\Entities\Brand;
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
use Domain\Models\ValueObjects\Brand\BrandName;
|
||||
use Domain\Ports\Repositories\BrandRepository;
|
||||
use Domain\Ports\Services\PictureStorage;
|
||||
use Shared\DDD\UseCases\Input as BaseInput;
|
||||
use Shared\ValueObjects\Email;
|
||||
use Domain\Models\ValueObjects\PicturePath;
|
||||
use Domain\Models\ValueObjects\RawPicturePath;
|
||||
use Shared\ValueObjects\Rcs;
|
||||
|
||||
covers(CreateBrand::class, Input::class, Output::class);
|
||||
covers(CreateBrand::class, Input::class, BrandCreated::class, BrandCreationFailed::class);
|
||||
|
||||
describe('create brand use case', function (): void {
|
||||
it('creates a brand when the code is unique and valid', function (): void {
|
||||
|
|
@ -47,7 +48,7 @@ describe('create brand use case', function (): void {
|
|||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(Output::class)
|
||||
expect($result)->toBeInstanceOf(BrandCreated::class)
|
||||
->and($result->code)->toBeInstanceOf(BrandCode::class);
|
||||
});
|
||||
});
|
||||
|
|
@ -73,27 +74,10 @@ describe('create brand use case validation rules', function (): void {
|
|||
$useCase = new CreateBrand($repository, $pictureStorage);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('create brand use case assertions', function (): void {
|
||||
it('rejects an input of the wrong type', function (): void {
|
||||
// GIVEN
|
||||
$repository = \Mockery::mock(BrandRepository::class);
|
||||
$pictureStorage = \Mockery::mock(PictureStorage::class);
|
||||
$useCase = new CreateBrand($repository, $pictureStorage);
|
||||
$invalidInput = new class () implements BaseInput
|
||||
{
|
||||
};
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($invalidInput);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\InvalidArgumentException::class);
|
||||
expect($result)->toBeInstanceOf(BrandCreationFailed::class)
|
||||
->and($result->error)->toBeInstanceOf(CreateBrandError::class);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ 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 Application\UseCases\CampaignRequest\Create\Outputs\CampaignRequestCreated;
|
||||
use Application\UseCases\CampaignRequest\Outputs\BrandNotFound;
|
||||
use DateTime;
|
||||
use Domain\Models\Entities\CampaignRequest;
|
||||
use Domain\Models\ValueObjects\Brand\BrandCode;
|
||||
|
|
@ -37,7 +38,7 @@ use Shared\Enums\Currency;
|
|||
use Shared\ValueObjects\Email;
|
||||
use Shared\ValueObjects\Money;
|
||||
|
||||
covers(CreateCampaignRequest::class, Input::class, Output::class);
|
||||
covers(CreateCampaignRequest::class, Input::class, CampaignRequestCreated::class);
|
||||
|
||||
describe('create campaign request use case', function (): void {
|
||||
$makeInput = fn (): Input => new Input(
|
||||
|
|
@ -88,7 +89,7 @@ describe('create campaign request use case', function (): void {
|
|||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(Output::class)
|
||||
expect($result)->toBeInstanceOf(CampaignRequestCreated::class)
|
||||
->and($result->status)->toBe(CampaignRequestStatus::CREATED)
|
||||
->and($result->brandCode)->toBe($input->brandCode);
|
||||
});
|
||||
|
|
@ -106,9 +107,9 @@ describe('create campaign request use case', function (): void {
|
|||
$useCase = new CreateCampaignRequest($campaignRequestRepository, $brandRepository);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class);
|
||||
expect($result)->toBeInstanceOf(BrandNotFound::class);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use Application\UseCases\CampaignRequest\Find\Input;
|
|||
use Application\UseCases\CampaignRequest\Find\Output\CampaignRequestFound;
|
||||
use Application\UseCases\CampaignRequest\Find\Output\CampaignRequestNotFound;
|
||||
use Application\UseCases\CampaignRequest\Find\Output\Output;
|
||||
use Application\UseCases\CampaignRequest\Outputs\BrandNotFound;
|
||||
use DateTime;
|
||||
use Domain\Models\Entities\Brand;
|
||||
use Domain\Models\Entities\CampaignRequest;
|
||||
|
|
@ -203,10 +204,10 @@ describe('find campaign request use case', function (): void {
|
|||
$useCase = new FindCampaignRequest($repository, $brandRepository);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute(new Input(id: $id));
|
||||
$result = $useCase->execute(new Input(id: $id));
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class);
|
||||
expect($result)->toBeInstanceOf(BrandNotFound::class);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,9 @@ namespace Tests\Unit\Application\UseCases\Coupon\Create;
|
|||
|
||||
use Application\UseCases\Coupon\Create\CreateCoupon;
|
||||
use Application\UseCases\Coupon\Create\Input;
|
||||
use Application\UseCases\Coupon\Create\Output;
|
||||
use Application\UseCases\Coupon\Create\Outputs\CouponCreated;
|
||||
use Application\UseCases\Coupon\Outputs\BrandNotFound;
|
||||
use Application\UseCases\Coupon\Create\Outputs\CouponCodeAlreadyExists;
|
||||
use DateTime;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Models\ValueObjects\Actor\ActorId;
|
||||
|
|
@ -36,7 +38,7 @@ use Shared\Enums\Country;
|
|||
use Shared\Enums\Currency;
|
||||
use Shared\ValueObjects\Money;
|
||||
|
||||
covers(CreateCoupon::class, Input::class, Output::class);
|
||||
covers(CreateCoupon::class, Input::class, CouponCreated::class, CouponCodeAlreadyExists::class);
|
||||
|
||||
describe('create coupon use case', function (): void {
|
||||
$makeInput = fn (array $overrides = []): Input => new Input(...array_merge([
|
||||
|
|
@ -86,7 +88,7 @@ describe('create coupon use case', function (): void {
|
|||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(Output::class)
|
||||
expect($result)->toBeInstanceOf(CouponCreated::class)
|
||||
->and($result->id)->toBeInstanceOf(CouponId::class)
|
||||
->and($result->code)->toEqual($input->code)
|
||||
->and($result->name)->toEqual($input->name)
|
||||
|
|
@ -117,10 +119,10 @@ describe('create coupon use case', function (): void {
|
|||
$useCase = new CreateCoupon($couponRepository, $brandRepository);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Brand code "TST89" was not found.');
|
||||
expect($result)->toBeInstanceOf(BrandNotFound::class);
|
||||
});
|
||||
|
||||
it('throws when the coupon code already exists', function () use ($makeInput): void {
|
||||
|
|
@ -137,10 +139,10 @@ describe('create coupon use case', function (): void {
|
|||
$useCase = new CreateCoupon($couponRepository, $brandRepository);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Coupon code "COU01" already exists.');
|
||||
expect($result)->toBeInstanceOf(CouponCodeAlreadyExists::class);
|
||||
});
|
||||
|
||||
it('creates a coupon with an availability calendar', function () use ($makeInput): void {
|
||||
|
|
@ -248,7 +250,7 @@ describe('create coupon use case', function (): void {
|
|||
$useCase = new CreateCoupon($couponRepository, $brandRepository);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$action = fn () => $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'A coupon requires legal information for BENELUX.');
|
||||
|
|
@ -277,7 +279,7 @@ describe('create coupon use case', function (): void {
|
|||
$useCase = new CreateCoupon($couponRepository, $brandRepository);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$action = fn () => $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'A coupon requires validity information for BENELUX.');
|
||||
|
|
@ -335,7 +337,7 @@ describe('create coupon use case assertions', function (): void {
|
|||
};
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($invalidInput);
|
||||
$action = fn () => $useCase->execute($invalidInput);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\InvalidArgumentException::class);
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use Application\UseCases\Coupon\Find\FindCoupon;
|
|||
use Application\UseCases\Coupon\Find\Input;
|
||||
use Application\UseCases\Coupon\Find\Output\CouponFound;
|
||||
use Application\UseCases\Coupon\Find\Output\CouponNotFound;
|
||||
use Application\UseCases\Coupon\Find\Output\Output;
|
||||
use Application\UseCases\Coupon\Outputs\BrandNotFound;
|
||||
use DateTime;
|
||||
use Domain\Models\Entities\Brand;
|
||||
use Domain\Models\Entities\Campaign;
|
||||
|
|
@ -181,8 +181,8 @@ describe('find coupon use case', function (): void {
|
|||
$useCase = new FindCoupon($couponRepository, $brandRepository, $campaignRepository);
|
||||
|
||||
// WHEN / THEN
|
||||
expect(fn (): Output => $useCase->execute(new Input(code: $coupon->code)))
|
||||
->toThrow(\DomainException::class);
|
||||
expect($useCase->execute(new Input(code: $coupon->code)))
|
||||
->toBeInstanceOf(BrandNotFound::class);
|
||||
});
|
||||
|
||||
it('finds a coupon not attached to any campaign', function () use ($coupon): void {
|
||||
|
|
|
|||
|
|
@ -5,8 +5,12 @@ declare(strict_types=1);
|
|||
namespace Tests\Unit\Application\UseCases\PrintableCoupon\Create;
|
||||
|
||||
use Application\UseCases\PrintableCoupon\Create\Input;
|
||||
use Application\UseCases\PrintableCoupon\Create\Output;
|
||||
use Application\UseCases\PrintableCoupon\Create\CreatePrintableCoupon;
|
||||
use Application\UseCases\PrintableCoupon\Create\Outputs\CampaignNotFound;
|
||||
use Application\UseCases\PrintableCoupon\Create\Outputs\CampaignNotPublished;
|
||||
use Application\UseCases\PrintableCoupon\Create\Outputs\CouponNotAssociated;
|
||||
use Application\UseCases\PrintableCoupon\Create\Outputs\PrintableCouponCreated;
|
||||
use Application\UseCases\PrintableCoupon\Outputs\CouponNotFound;
|
||||
use DateTime;
|
||||
use Domain\Models\Entities\Campaign;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
|
|
@ -44,13 +48,14 @@ use Domain\Models\ValueObjects\ProcessingCodeArray;
|
|||
use Domain\Ports\Repositories\CampaignRepository;
|
||||
use Domain\Ports\Repositories\CouponRepository;
|
||||
use Domain\Ports\Repositories\PrintableCouponRepository;
|
||||
use Domain\Services\PrintableCoupon\PrintEligibilityChecker;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Shared\Enums\Country;
|
||||
use Shared\Enums\Currency;
|
||||
use Shared\Helpers\DatesHelper;
|
||||
use Shared\ValueObjects\Money;
|
||||
|
||||
covers(CreatePrintableCoupon::class, Input::class, Output::class);
|
||||
covers(CreatePrintableCoupon::class, Input::class, PrintableCouponCreated::class);
|
||||
|
||||
describe('create coupon use case', function (): void {
|
||||
$crmCustomerHashcode = new CrmHashcode(Uuid::uuid4());
|
||||
|
|
@ -93,7 +98,7 @@ describe('create coupon use case', function (): void {
|
|||
brandCode: $brandCode,
|
||||
createdBy: ActorId::generate(),
|
||||
products: new CouponProductArray([
|
||||
new IncoCode(35784) ,
|
||||
new IncoCode(35784),
|
||||
new IncoCode(15522),
|
||||
]),
|
||||
movements: new CouponMovementArray([
|
||||
|
|
@ -152,13 +157,22 @@ describe('create coupon use case', function (): void {
|
|||
$printableCouponRepository = \Mockery::mock(PrintableCouponRepository::class);
|
||||
$printableCouponRepository->shouldReceive('save')->once()->with(\Mockery::type(PrintableCoupon::class));
|
||||
|
||||
$useCase = new CreatePrintableCoupon($printableCouponRepository, $couponRepository, $campaignRepository);
|
||||
$printEligibilityChecker = new PrintEligibilityChecker(
|
||||
$printableCouponRepository,
|
||||
);
|
||||
|
||||
$useCase = new CreatePrintableCoupon(
|
||||
$printableCouponRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
$printEligibilityChecker,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(Output::class)
|
||||
expect($result)->toBeInstanceOf(PrintableCouponCreated::class)
|
||||
->and($result->campaignCode)->toEqual($campaign->code)
|
||||
->and($result->couponCode)->toEqual($coupon->code)
|
||||
->and($result->crmCustomerHashcode)->toEqual($crmCustomerHashcode)
|
||||
|
|
@ -177,13 +191,22 @@ describe('create coupon use case', function (): void {
|
|||
|
||||
$printableCouponRepository = \Mockery::mock(PrintableCouponRepository::class);
|
||||
|
||||
$useCase = new CreatePrintableCoupon($printableCouponRepository, $couponRepository, $campaignRepository);
|
||||
$printEligibilityChecker = new PrintEligibilityChecker(
|
||||
$printableCouponRepository,
|
||||
);
|
||||
|
||||
$useCase = new CreatePrintableCoupon(
|
||||
$printableCouponRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
$printEligibilityChecker,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, sprintf('Campaign from code "%s" not found.', $input->campaignCode->value));
|
||||
expect($result)->toBeInstanceOf(CampaignNotFound::class);
|
||||
});
|
||||
|
||||
it('rejects a printable coupon when campaign is not published', function () use ($makeInput, $makeCampaign): void {
|
||||
|
|
@ -198,13 +221,23 @@ describe('create coupon use case', function (): void {
|
|||
|
||||
$printableCouponRepository = \Mockery::mock(PrintableCouponRepository::class);
|
||||
|
||||
$useCase = new CreatePrintableCoupon($printableCouponRepository, $couponRepository, $campaignRepository);
|
||||
$printEligibilityChecker = new PrintEligibilityChecker(
|
||||
$printableCouponRepository,
|
||||
);
|
||||
|
||||
$useCase = new CreatePrintableCoupon(
|
||||
$printableCouponRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
$printEligibilityChecker,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Campaign must be published.');
|
||||
expect($result)->toBeInstanceOf(CampaignNotPublished::class);
|
||||
});
|
||||
|
||||
it('rejects a printable coupon when coupon is not found', function () use ($makeInput, $makeCampaign, $makeCoupon): void {
|
||||
|
|
@ -221,13 +254,23 @@ describe('create coupon use case', function (): void {
|
|||
|
||||
$printableCouponRepository = \Mockery::mock(PrintableCouponRepository::class);
|
||||
|
||||
$useCase = new CreatePrintableCoupon($printableCouponRepository, $couponRepository, $campaignRepository);
|
||||
$printEligibilityChecker = new PrintEligibilityChecker(
|
||||
$printableCouponRepository,
|
||||
);
|
||||
|
||||
$useCase = new CreatePrintableCoupon(
|
||||
$printableCouponRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
$printEligibilityChecker,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, sprintf('Coupon code "%s" not found.', $input->couponCode->value));
|
||||
expect($result)->toBeInstanceOf(CouponNotFound::class);
|
||||
});
|
||||
|
||||
it('rejects a printable coupon when campaign coupon is not found', function () use ($makeInput, $makeCampaign, $makeCoupon): void {
|
||||
|
|
@ -246,13 +289,23 @@ describe('create coupon use case', function (): void {
|
|||
|
||||
$printableCouponRepository = \Mockery::mock(PrintableCouponRepository::class);
|
||||
|
||||
$useCase = new CreatePrintableCoupon($printableCouponRepository, $couponRepository, $campaignRepository);
|
||||
$printEligibilityChecker = new PrintEligibilityChecker(
|
||||
$printableCouponRepository,
|
||||
);
|
||||
|
||||
$useCase = new CreatePrintableCoupon(
|
||||
$printableCouponRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
$printEligibilityChecker,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Coupon must be associated to the associated campaign coupon.');
|
||||
expect($result)->toBeInstanceOf(CouponNotAssociated::class);
|
||||
});
|
||||
|
||||
it('rejects a printable coupon when outside of diffusion start', function () use ($makeInput, $makeCampaign, $makeCoupon, $campaignCoupon): void {
|
||||
|
|
@ -271,10 +324,19 @@ describe('create coupon use case', function (): void {
|
|||
|
||||
$printableCouponRepository = \Mockery::mock(PrintableCouponRepository::class);
|
||||
|
||||
$useCase = new CreatePrintableCoupon($printableCouponRepository, $couponRepository, $campaignRepository);
|
||||
$printEligibilityChecker = new PrintEligibilityChecker(
|
||||
$printableCouponRepository,
|
||||
);
|
||||
|
||||
$useCase = new CreatePrintableCoupon(
|
||||
$printableCouponRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
$printEligibilityChecker,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$action = fn () => $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'The printing period has not yet begun.');
|
||||
|
|
@ -296,10 +358,19 @@ describe('create coupon use case', function (): void {
|
|||
|
||||
$printableCouponRepository = \Mockery::mock(PrintableCouponRepository::class);
|
||||
|
||||
$useCase = new CreatePrintableCoupon($printableCouponRepository, $couponRepository, $campaignRepository);
|
||||
$printEligibilityChecker = new PrintEligibilityChecker(
|
||||
$printableCouponRepository,
|
||||
);
|
||||
|
||||
$useCase = new CreatePrintableCoupon(
|
||||
$printableCouponRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
$printEligibilityChecker,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$action = fn () => $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'The printing period has expired.');
|
||||
|
|
@ -337,13 +408,22 @@ describe('create coupon use case', function (): void {
|
|||
$printableCouponRepository = \Mockery::mock(PrintableCouponRepository::class);
|
||||
$printableCouponRepository->shouldReceive('save')->once()->with(\Mockery::type(PrintableCoupon::class));
|
||||
|
||||
$useCase = new CreatePrintableCoupon($printableCouponRepository, $couponRepository, $campaignRepository);
|
||||
$printEligibilityChecker = new PrintEligibilityChecker(
|
||||
$printableCouponRepository,
|
||||
);
|
||||
|
||||
$useCase = new CreatePrintableCoupon(
|
||||
$printableCouponRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
$printEligibilityChecker,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(Output::class)
|
||||
expect($result)->toBeInstanceOf(PrintableCouponCreated::class)
|
||||
->and($result->campaignCode)->toEqual($campaign->code)
|
||||
->and($result->crmCustomerHashcode)->toEqual($crmCustomerHashcode)
|
||||
->and($result->distributableUntil)->toEqual($calendarEnd);
|
||||
|
|
@ -376,10 +456,19 @@ describe('create coupon use case', function (): void {
|
|||
$crmCustomerHashcode,
|
||||
)->andReturn(5);
|
||||
|
||||
$useCase = new CreatePrintableCoupon($printableCouponRepository, $couponRepository, $campaignRepository);
|
||||
$printEligibilityChecker = new PrintEligibilityChecker(
|
||||
$printableCouponRepository,
|
||||
);
|
||||
|
||||
$useCase = new CreatePrintableCoupon(
|
||||
$printableCouponRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
$printEligibilityChecker,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$action = fn () => $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Maximum number of prints reached.');
|
||||
|
|
@ -418,10 +507,19 @@ describe('create coupon use case', function (): void {
|
|||
$crmCustomerHashcode,
|
||||
)->andReturn($printedCoupon);
|
||||
|
||||
$useCase = new CreatePrintableCoupon($printableCouponRepository, $couponRepository, $campaignRepository);
|
||||
$printEligibilityChecker = new PrintEligibilityChecker(
|
||||
$printableCouponRepository,
|
||||
);
|
||||
|
||||
$useCase = new CreatePrintableCoupon(
|
||||
$printableCouponRepository,
|
||||
$couponRepository,
|
||||
$campaignRepository,
|
||||
$printEligibilityChecker,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$action = fn () => $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Reprint period has not expired.');
|
||||
|
|
|
|||
|
|
@ -6,8 +6,9 @@ namespace Tests\Unit\Application\UseCases\PrintableCoupon\List;
|
|||
|
||||
use Application\UseCases\PrintableCoupon\List\Input;
|
||||
use Application\UseCases\PrintableCoupon\List\ListPrintableCoupons;
|
||||
use Application\UseCases\PrintableCoupon\List\Output;
|
||||
use Application\UseCases\PrintableCoupon\List\Outputs\PrintableCouponListed;
|
||||
use Application\UseCases\PrintableCoupon\List\PrintableCouponListItem;
|
||||
use Application\UseCases\PrintableCoupon\Outputs\CouponNotFound;
|
||||
use DateTime;
|
||||
use Domain\Models\Entities\Coupon;
|
||||
use Domain\Models\Entities\PrintableCoupon;
|
||||
|
|
@ -44,7 +45,7 @@ use Shared\ValueObjects\Money;
|
|||
use Shared\ValueObjects\Pagination\Paginated;
|
||||
use Shared\ValueObjects\Pagination\Pagination;
|
||||
|
||||
covers(ListPrintableCoupons::class, Input::class, Output::class, PrintableCouponListItem::class);
|
||||
covers(ListPrintableCoupons::class, Input::class, PrintableCouponListed::class, PrintableCouponListItem::class);
|
||||
|
||||
describe('list printable coupons use case', function (): void {
|
||||
$createPicture = fn (string $name): CouponPicture => new CouponPicture(
|
||||
|
|
@ -269,9 +270,9 @@ describe('list printable coupons use case', function (): void {
|
|||
$useCase = new ListPrintableCoupons($repository, $couponRepository);
|
||||
|
||||
// WHEN
|
||||
$action = fn (): Output => $useCase->execute($input);
|
||||
$result = $useCase->execute($input);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\LogicException::class);
|
||||
expect($result)->toBeInstanceOf(CouponNotFound::class);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -41,45 +41,4 @@ describe('coupon picture', function (): void {
|
|||
'thumbnail' => ['files/decoration.jpg', 'files/thumbnail.webp', 'files/mobile_thumbnail.jpg', 'files/thumbnail.webp'],
|
||||
'mobile thumbnail' => ['files/decoration.jpg', 'files/thumbnail.jpg', 'files/mobile_thumbnail.png', 'files/mobile_thumbnail.png'],
|
||||
]);
|
||||
|
||||
it('creates a picture when the decoration is 280 x 260', function (): void {
|
||||
// GIVEN
|
||||
$decoration = new PicturePath('tests/Fixtures/Coupon/decoration.jpg');
|
||||
$thumbnail = new PicturePath('files/thumbnail.jpg');
|
||||
$mobileThumbnail = new PicturePath('files/mobile_thumbnail.jpg');
|
||||
|
||||
// WHEN
|
||||
$picture = CouponPicture::create($decoration, $thumbnail, $mobileThumbnail);
|
||||
|
||||
// THEN
|
||||
expect($picture->decoration)->toBe($decoration)
|
||||
->and($picture->thumbnail)->toBe($thumbnail)
|
||||
->and($picture->mobileThumbnail)->toBe($mobileThumbnail);
|
||||
});
|
||||
|
||||
it('refuses a decoration with the wrong size', function (): void {
|
||||
// GIVEN
|
||||
$decoration = new PicturePath('tests/Fixtures/Coupon/invalid_decoration.jpg');
|
||||
$thumbnail = new PicturePath('files/thumbnail.jpg');
|
||||
$mobileThumbnail = new PicturePath('files/mobile_thumbnail.jpg');
|
||||
|
||||
// WHEN
|
||||
$create = fn (): CouponPicture => CouponPicture::create($decoration, $thumbnail, $mobileThumbnail);
|
||||
|
||||
// THEN
|
||||
expect($create)->toThrow(\DomainException::class, 'The decoration picture must be 280 x 260 px');
|
||||
});
|
||||
|
||||
it('refuses a decoration file that does not exist', function (): void {
|
||||
// GIVEN
|
||||
$decoration = new PicturePath('files/missing.jpg');
|
||||
$thumbnail = new PicturePath('files/thumbnail.jpg');
|
||||
$mobileThumbnail = new PicturePath('files/mobile_thumbnail.jpg');
|
||||
|
||||
// WHEN
|
||||
$create = fn (): CouponPicture => CouponPicture::create($decoration, $thumbnail, $mobileThumbnail);
|
||||
|
||||
// THEN
|
||||
expect($create)->toThrow(\DomainException::class, 'Unable to read the decoration picture "files/missing.jpg".');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -54,14 +54,6 @@ describe('money amount', function (): void {
|
|||
expect($amount->value)->toBe($minimumMoney);
|
||||
});
|
||||
|
||||
it('accepts the maximum amount of 3.00€', function () use ($money): void {
|
||||
// GIVEN / WHEN
|
||||
$amount = new MoneyAmount($money);
|
||||
|
||||
// THEN
|
||||
expect($amount->value)->toBe($money);
|
||||
});
|
||||
|
||||
it('throws when the amount is zero', function () use ($zeroMoney): void {
|
||||
// GIVEN / WHEN / THEN
|
||||
expect(fn (): MoneyAmount => new MoneyAmount($zeroMoney))
|
||||
|
|
|
|||
|
|
@ -22,19 +22,14 @@ describe('quantity', function (): void {
|
|||
->and($quantity->value)->toBe($value);
|
||||
});
|
||||
|
||||
it('rejects a quantity of zero', function () use ($zeroValue): void {
|
||||
it('rejects a quantity of zero or negative', function (int $value): void {
|
||||
// GIVEN / WHEN
|
||||
$action = fn (): RequestedCouponQuantity => new RequestedCouponQuantity($zeroValue);
|
||||
$action = fn (): RequestedCouponQuantity => new RequestedCouponQuantity($value);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Quantity should be more than 0.');
|
||||
});
|
||||
|
||||
it('rejects a negative quantity', function () use ($negativeValue): void {
|
||||
// GIVEN / WHEN
|
||||
$action = fn (): RequestedCouponQuantity => new RequestedCouponQuantity($negativeValue);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Quantity should be more than 0.');
|
||||
});
|
||||
})->with([
|
||||
$zeroValue,
|
||||
$negativeValue,
|
||||
]);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -175,40 +175,6 @@ describe('requested coupon', function (): void {
|
|||
->and($coupon->couponProducts)->toBe($couponProducts);
|
||||
});
|
||||
|
||||
it('accepts a france only coupon', function () use (
|
||||
$couponName,
|
||||
$quantity,
|
||||
$diffusion,
|
||||
$moneyAmount,
|
||||
$couponPicture,
|
||||
$franceProcessingCode,
|
||||
$franceValidityInformation,
|
||||
$franceLegalInformation,
|
||||
$couponProducts,
|
||||
): void {
|
||||
// GIVEN / WHEN
|
||||
$coupon = new RequestedCoupon(
|
||||
couponName: $couponName,
|
||||
quantity: $quantity,
|
||||
diffusion: $diffusion,
|
||||
moneyAmount: $moneyAmount,
|
||||
couponPicture: $couponPicture,
|
||||
couponProcessingCodes: new ProcessingCodeArray([
|
||||
$franceProcessingCode,
|
||||
]),
|
||||
couponValidityInformations: new CouponValidityInformationArray([
|
||||
$franceValidityInformation,
|
||||
]),
|
||||
couponLegalInformations: new CouponLegalInformationArray([
|
||||
$franceLegalInformation,
|
||||
]),
|
||||
couponProducts: $couponProducts,
|
||||
);
|
||||
|
||||
// THEN
|
||||
expect($coupon)->toBeInstanceOf(RequestedCoupon::class);
|
||||
});
|
||||
|
||||
it('throws when there is no processing code for france', function () use (
|
||||
$couponName,
|
||||
$quantity,
|
||||
|
|
@ -235,58 +201,4 @@ describe('requested coupon', function (): void {
|
|||
couponProducts: $couponProducts,
|
||||
))->toThrow(\DomainException::class, 'ProcessingCodes requires a ProcessingCode for France.');
|
||||
});
|
||||
|
||||
it('throws when a benelux processing code has no benelux validity information', function () use (
|
||||
$couponName,
|
||||
$quantity,
|
||||
$diffusion,
|
||||
$moneyAmount,
|
||||
$couponPicture,
|
||||
$couponProcessingCodes,
|
||||
$franceValidityInformation,
|
||||
$couponLegalInformations,
|
||||
$couponProducts,
|
||||
): void {
|
||||
// GIVEN / WHEN / THEN
|
||||
expect(fn (): RequestedCoupon => new RequestedCoupon(
|
||||
couponName: $couponName,
|
||||
quantity: $quantity,
|
||||
diffusion: $diffusion,
|
||||
moneyAmount: $moneyAmount,
|
||||
couponPicture: $couponPicture,
|
||||
couponProcessingCodes: $couponProcessingCodes,
|
||||
couponValidityInformations: new CouponValidityInformationArray([
|
||||
$franceValidityInformation,
|
||||
]),
|
||||
couponLegalInformations: $couponLegalInformations,
|
||||
couponProducts: $couponProducts,
|
||||
))->toThrow(\DomainException::class, 'A coupon requires validity information for BENELUX.');
|
||||
});
|
||||
|
||||
it('throws when a benelux processing code has no benelux legal information', function () use (
|
||||
$couponName,
|
||||
$quantity,
|
||||
$diffusion,
|
||||
$moneyAmount,
|
||||
$couponPicture,
|
||||
$couponProcessingCodes,
|
||||
$couponValidityInformations,
|
||||
$franceLegalInformation,
|
||||
$couponProducts,
|
||||
): void {
|
||||
// GIVEN / WHEN / THEN
|
||||
expect(fn (): RequestedCoupon => new RequestedCoupon(
|
||||
couponName: $couponName,
|
||||
quantity: $quantity,
|
||||
diffusion: $diffusion,
|
||||
moneyAmount: $moneyAmount,
|
||||
couponPicture: $couponPicture,
|
||||
couponProcessingCodes: $couponProcessingCodes,
|
||||
couponValidityInformations: $couponValidityInformations,
|
||||
couponLegalInformations: new CouponLegalInformationArray([
|
||||
$franceLegalInformation,
|
||||
]),
|
||||
couponProducts: $couponProducts,
|
||||
))->toThrow(\DomainException::class, 'A coupon requires legal information for BENELUX.');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -51,33 +51,21 @@ describe('stock adjustment', function (): void {
|
|||
expect($adjustment->createdAt)->toBe($createdAt);
|
||||
});
|
||||
|
||||
it('rejects a quantity of zero', function () use ($zeroQuantity, $reason): void {
|
||||
it('rejects a quantity of zero or negative', function (int $value) use ($reason): void {
|
||||
// GIVEN
|
||||
$actorId = ActorId::generate();
|
||||
|
||||
// WHEN
|
||||
$action = fn (): StockAdjustment => new StockAdjustment(
|
||||
$zeroQuantity,
|
||||
$value,
|
||||
$reason,
|
||||
$actorId,
|
||||
);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Stock Amount should be more than 0.');
|
||||
});
|
||||
|
||||
it('rejects a negative quantity', function () use ($negativeQuantity, $reason): void {
|
||||
// GIVEN
|
||||
$actorId = ActorId::generate();
|
||||
|
||||
// WHEN
|
||||
$action = fn (): StockAdjustment => new StockAdjustment(
|
||||
$negativeQuantity,
|
||||
$reason,
|
||||
$actorId,
|
||||
);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class, 'Stock Amount should be more than 0.');
|
||||
});
|
||||
})->with([
|
||||
$zeroQuantity,
|
||||
$negativeQuantity,
|
||||
]);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Shared\Enums;
|
||||
|
||||
use Shared\DDD\Exceptions\UnknownCountryIsoCodeException;
|
||||
use Shared\Enums\Country;
|
||||
|
||||
covers(Country::class, UnknownCountryIsoCodeException::class);
|
||||
|
||||
describe('country', function (): void {
|
||||
it('has the expected cases with their backing values', function (): void {
|
||||
// THEN
|
||||
expect(Country::FRANCE->value)->toBe('france')
|
||||
->and(Country::BENELUX->value)->toBe('benelux');
|
||||
});
|
||||
|
||||
it('exposes its case name', function (): void {
|
||||
// THEN
|
||||
expect(Country::FRANCE->name)->toBe('FRANCE');
|
||||
});
|
||||
|
||||
it('is a domain exception', function (): void {
|
||||
// GIVEN / WHEN
|
||||
$exception = UnknownCountryIsoCodeException::fromIsoCode('XX');
|
||||
|
||||
// THEN
|
||||
expect($exception)->toBeInstanceOf(\DomainException::class);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue