Skip to content

Commit

Permalink
bug Sylius#12703 [Api] Denying usage of unexisting promotion coupon (…
Browse files Browse the repository at this point in the history
…Tomanhez)

This PR was merged into the 1.11-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | master
| Bug fix?        | no
| New feature?    | yes
| BC breaks?      | no
| Deprecations?   | no

| License         | MIT

<!--
 - Bug fixes must be submitted against the 1.9 or 1.10 branch (the lowest possible)
 - Features and deprecations must be submitted against the master branch
 - Make sure that the correct base branch is set

 To be sure you are not breaking any Backward Compatibilities, check the documentation:
 https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html
-->


Commits
-------

0e73cad Add tests and implementation
  • Loading branch information
Zales0123 committed Jun 9, 2021
2 parents 01a36f6 + 0e73cad commit 91e4507
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Feature: Denying usage of unexisting promotion coupon
And the store has promotion "Christmas sale" with coupon "SANTA2016"
And this promotion gives "$10.00" discount to every order

@ui
@ui @api
Scenario: Receiving no discount from unexisting coupon
When I add product "PHP T-Shirt" to the cart
And I use coupon with code "SANTA2011"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public function validate($value, Constraint $constraint): void
return;
}

/** @var PromotionCouponInterface $promotionCoupon */
/** @var PromotionCouponInterface|null $promotionCoupon */
$promotionCoupon = $this->promotionCouponRepository->findOneBy(['code' => $value->couponCode]);

/** @var OrderInterface $cart */
$cart = $this->orderRepository->findCartByTokenValue($value->getOrderTokenValue());

$cart->setPromotionCoupon($promotionCoupon);

if (!$this->promotionCouponChecker->isEligible($cart, $promotionCoupon)) {
if ($promotionCoupon === null || !$this->promotionCouponChecker->isEligible($cart, $promotionCoupon)) {
$this->context->buildViolation('sylius.promotion_coupon.is_invalid')
->atPath('couponCode')
->addViolation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,30 @@ function it_does_add_violation_if_promotion_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 91e4507

Please sign in to comment.