Skip to content

Commit

Permalink
Add tests and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed Jun 9, 2021
1 parent 01623fd commit 0364306
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 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,14 +59,23 @@ 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 ($promotionCoupon === null) {
$this->context->buildViolation('sylius.promotion_coupon.not_exist')
->atPath('couponCode')
->addViolation()
;

return;
}

if (!$this->promotionCouponChecker->isEligible($cart, $promotionCoupon)) {
$this->context->buildViolation('sylius.promotion_coupon.is_invalid')
->atPath('couponCode')
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.not_exist')->willReturn($constraintViolationBuilder);
$constraintViolationBuilder->atPath('couponCode')->willReturn($constraintViolationBuilder);
$constraintViolationBuilder->addViolation()->shouldBeCalled();

$this->validate($value, $constraint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sylius:
regex: Coupon code can only be comprised of letters, numbers, dashes and underscores.
unique: This coupon already exists.
is_invalid: Coupon code is invalid.
not_exist: Coupon code not exist.
usage_limit:
min: Coupon usage limit must be at least {{ limit }}.
promotion_coupon_generator_instruction:
Expand Down

0 comments on commit 0364306

Please sign in to comment.