Skip to content

Commit

Permalink
Incorporate changes from Sylius#12703 into the checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 authored and Tomanhez committed Jun 24, 2021
1 parent a2b7694 commit 8dee4eb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ public function __construct(
$this->promotionCouponChecker = $promotionCouponChecker;
}

public function isEligible(PromotionCouponInterface $promotionCoupon, OrderInterface $cart): bool
public function isEligible(?PromotionCouponInterface $promotionCoupon, OrderInterface $cart): bool
{
if (null === $promotionCoupon) {
return false;
}

/** @var PromotionInterface $promotion */
$promotion = $promotionCoupon->getPromotion();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

interface AppliedCouponEligibilityCheckerInterface
{
public function isEligible(PromotionCouponInterface $promotionCoupon, OrderInterface $cart): bool;
public function isEligible(?PromotionCouponInterface $promotionCoupon, OrderInterface $cart): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ public function validate($value, Constraint $constraint): void

$cart->setPromotionCoupon($promotionCoupon);

if (
$promotionCoupon === null ||
!$this->appliedCouponEligibilityChecker->isEligible($promotionCoupon, $cart)
) {
if (!$this->appliedCouponEligibilityChecker->isEligible($promotionCoupon, $cart)) {
$this->context
->buildViolation('sylius.promotion_coupon.is_invalid')
->atPath('couponCode')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ function it_implements_promotion_coupon_eligibility_checker_interface(): void
$this->shouldImplement(AppliedCouponEligibilityCheckerInterface::class);
}

function it_returns_false_if_promotion_coupon_is_null(
PromotionEligibilityCheckerInterface $promotionChecker,
PromotionCouponEligibilityCheckerInterface $promotionCouponChecker,
PromotionCouponInterface $promotionCoupon,
PromotionInterface $promotion,
OrderInterface $cart
): void {
$promotionCoupon->getPromotion()->shouldNotBeCalled();
$promotion->getChannels()->shouldNotBeCalled();
$promotionChecker->isEligible(Argument::any())->shouldNotBeCalled();
$promotionCouponChecker->isEligible(Argument::any())->shouldNotBeCalled();

$this->isEligible(null, $cart)->shouldReturn(false);
}

function it_returns_false_if_cart_channel_is_not_one_of_promotion_channels(
PromotionEligibilityCheckerInterface $promotionChecker,
PromotionCouponEligibilityCheckerInterface $promotionCouponChecker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,4 @@ function it_adds_violation_if_promotion_coupon_is_not_eligible(

$this->validate($value, $constraint);
}

function it_does_add_violation_if_promotion_code_does_not_exist(
PromotionCouponRepositoryInterface $promotionCouponRepository,
OrderRepositoryInterface $orderRepository,
OrderInterface $cart,
ExecutionContextInterface $executionContext,
ConstraintViolationBuilderInterface $constraintViolationBuilder
): void {
$this->initialize($executionContext);
$constraint = new PromotionCouponEligibility();

$value = new ApplyCouponToCart('couponCode');
$value->setOrderTokenValue('token');

$promotionCouponRepository->findOneBy(['code' => 'couponCode'])->willReturn(null);

$orderRepository->findCartByTokenValue('token')->willReturn($cart);

$cart->setPromotionCoupon(null)->shouldBeCalled();

$executionContext->buildViolation('sylius.promotion_coupon.is_invalid')->willReturn($constraintViolationBuilder);
$constraintViolationBuilder->atPath('couponCode')->willReturn($constraintViolationBuilder);
$constraintViolationBuilder->addViolation()->shouldBeCalled();

$this->validate($value, $constraint);
}
}

0 comments on commit 8dee4eb

Please sign in to comment.