Skip to content

Commit

Permalink
Unify invalid coupon/promotion validation message
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Jun 9, 2021
1 parent bcc3354 commit 70b2666
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,3 @@ Feature: Applying promotion coupon with an expiration date
Then I should be notified that the coupon is invalid
And my cart total should be "$100.00"
And there should be no discount

@ui
Scenario: Receiving no discount from valid coupon from expired promotion
Given this promotion has already expired
When I add product "PHP T-Shirt" to the cart
And I use coupon with code "SANTA2016"
Then I should be notified that the coupon is invalid
And my cart total should be "$100.00"
And there should be no discount
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,42 @@ Feature: Receiving no discount if coupon promotion is not eligible
Given this promotion is not available in any channel
When I add 2 products "PHP T-Shirt" to the cart
And I use coupon with code "SANTA2016"
Then I should be notified that the promotion is invalid
Then I should be notified that the coupon is invalid
And my cart total should be "$200.00"
And there should be no discount

@api
@ui @api
Scenario: Receiving no discount if promotion for the applied coupon has not started yet
Given this promotion starts tomorrow
When I add 2 products "PHP T-Shirt" to the cart
And I use coupon with code "SANTA2016"
Then I should be notified that the promotion is invalid
Then I should be notified that the coupon is invalid
And my cart total should be "$200.00"
And there should be no discount

@api
@ui @api
Scenario: Receiving no discount if promotion for the applied coupon has already expired
Given this promotion has already expired
When I add 2 products "PHP T-Shirt" to the cart
And I use coupon with code "SANTA2016"
Then I should be notified that the promotion is invalid
Then I should be notified that the coupon is invalid
And my cart total should be "$200.00"
And there should be no discount

@api
@ui @api
Scenario: Receiving no discount if promotion's usage for the applied coupon is already exceeded
Given this promotion has usage limit equal to 100
And this promotion usage limit is already reached
When I add 2 products "PHP T-Shirt" to the cart
And I use coupon with code "SANTA2016"
Then I should be notified that the promotion is invalid
Then I should be notified that the coupon is invalid
And my cart total should be "$200.00"
And there should be no discount

@api
@ui @api
Scenario: Receiving no discount if promotion's rules for the applied coupon are not fulfilled
When I add product "PHP T-Shirt" to the cart
And I use coupon with code "SANTA2016"
Then I should be notified that the promotion is invalid
Then I should be notified that the coupon is invalid
And my cart total should be "$100.00"
And there should be no discount
11 changes: 0 additions & 11 deletions src/Sylius/Behat/Context/Api/Shop/PromotionContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ public function iShouldBeNotifiedThatCouponIsInvalid(): void
Assert::same($this->responseChecker->getError($response), 'couponCode: Coupon code is invalid.');
}

/**
* @Then I should be notified that the promotion is invalid
*/
public function iShouldBeNotifiedThatPromotionIsInvalid(): void
{
$response = $this->cartsClient->getLastResponse();

Assert::same($response->getStatusCode(), 422);
Assert::same($this->responseChecker->getError($response), 'couponCode: Promotion is invalid.');
}

private function getCartTokenValue(): ?string
{
if ($this->sharedStorage->has('cart_token')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,11 @@ public function validate($value, Constraint $constraint): void

if (
$promotionCoupon === null ||
!$this->promotionCouponChecker->isEligible($cart, $promotionCoupon)
) {
$this->addViolation('sylius.promotion_coupon.is_invalid', 'couponCode');
}

if (
!$this->promotionCouponChecker->isEligible($cart, $promotionCoupon) ||
!$this->promotionChecker->isEligible($cart, $promotionCoupon->getPromotion()) ||
!$promotion->getChannels()->contains($cart->getChannel())
) {
$this->addViolation('sylius.promotion.is_invalid', 'couponCode');
$this->addViolation('sylius.promotion_coupon.is_invalid', 'couponCode');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,12 @@ function it_does_not_add_violation_if_promotion_coupon_is_eligible(

$promotionCouponChecker->isEligible($cart, $promotionCoupon)->willReturn(true);

$executionContext->buildViolation('sylius.promotion_coupon.is_invalid')->shouldNotBeCalled();

$promotionCoupon->getPromotion()->willReturn($promotion);
$promotion->getChannels()->willReturn(new ArrayCollection([$firstChannel->getWrappedObject(), $secondChannel->getWrappedObject()]));

$promotionChecker->isEligible($cart, $promotion)->willReturn(true);

$executionContext->buildViolation('sylius.promotion.is_invalid')->shouldNotBeCalled();
$executionContext->buildViolation('sylius.promotion_coupon.is_invalid')->shouldNotBeCalled();

$this->validate($value, $constraint);
}
Expand Down Expand Up @@ -161,7 +159,7 @@ function it_adds_violation_if_promotion_is_not_available_in_cart_channel(

$promotionChecker->isEligible($cart, $promotion)->willReturn(false);

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

Expand Down Expand Up @@ -193,14 +191,10 @@ function it_adds_violation_if_promotion_is_not_eligible(

$promotionCouponChecker->isEligible($cart, $promotionCoupon)->willReturn(true);

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

$promotionCoupon->getPromotion()->willReturn($promotion);
$promotionChecker->isEligible($cart, $promotion)->willReturn(false);

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

Expand Down

0 comments on commit 70b2666

Please sign in to comment.