diff --git a/src/Sylius/Bundle/ApiBundle/Checker/AppliedCouponEligibilityChecker.php b/src/Sylius/Bundle/ApiBundle/Checker/AppliedCouponEligibilityChecker.php index affaaf3bb62..5ada9101ac6 100644 --- a/src/Sylius/Bundle/ApiBundle/Checker/AppliedCouponEligibilityChecker.php +++ b/src/Sylius/Bundle/ApiBundle/Checker/AppliedCouponEligibilityChecker.php @@ -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(); diff --git a/src/Sylius/Bundle/ApiBundle/Checker/AppliedCouponEligibilityCheckerInterface.php b/src/Sylius/Bundle/ApiBundle/Checker/AppliedCouponEligibilityCheckerInterface.php index df98e7d3384..96428d886e1 100644 --- a/src/Sylius/Bundle/ApiBundle/Checker/AppliedCouponEligibilityCheckerInterface.php +++ b/src/Sylius/Bundle/ApiBundle/Checker/AppliedCouponEligibilityCheckerInterface.php @@ -9,5 +9,5 @@ interface AppliedCouponEligibilityCheckerInterface { - public function isEligible(PromotionCouponInterface $promotionCoupon, OrderInterface $cart): bool; + public function isEligible(?PromotionCouponInterface $promotionCoupon, OrderInterface $cart): bool; } diff --git a/src/Sylius/Bundle/ApiBundle/Validator/Constraints/PromotionCouponEligibilityValidator.php b/src/Sylius/Bundle/ApiBundle/Validator/Constraints/PromotionCouponEligibilityValidator.php index 7a77f47969e..7f201a19ba8 100644 --- a/src/Sylius/Bundle/ApiBundle/Validator/Constraints/PromotionCouponEligibilityValidator.php +++ b/src/Sylius/Bundle/ApiBundle/Validator/Constraints/PromotionCouponEligibilityValidator.php @@ -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') diff --git a/src/Sylius/Bundle/ApiBundle/spec/Checker/AppliedCouponEligibilityCheckerSpec.php b/src/Sylius/Bundle/ApiBundle/spec/Checker/AppliedCouponEligibilityCheckerSpec.php index b0399ff0649..520e72f6e8c 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/Checker/AppliedCouponEligibilityCheckerSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/Checker/AppliedCouponEligibilityCheckerSpec.php @@ -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, diff --git a/src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/PromotionCouponEligibilityValidatorSpec.php b/src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/PromotionCouponEligibilityValidatorSpec.php index ea3e8487c1b..263cce93ca7 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/PromotionCouponEligibilityValidatorSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/Validator/Constraints/PromotionCouponEligibilityValidatorSpec.php @@ -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); - } }