From b7c30eb2f0e4f8055bca38d1935752044ae04fba Mon Sep 17 00:00:00 2001 From: Rafikooo Date: Thu, 5 Sep 2024 07:49:34 +0200 Subject: [PATCH] [Unit] Add promotion coupon applying tests --- .../DataFixtures/ORM/promotion/product.yaml | 16 +++- .../promotion/promotion_coupon_expired.yaml | 29 ++++++ .../promotion_ended_with_valid_coupon.yaml | 30 +++++++ .../ORM/promotion/promotion_ineligible.yaml | 43 +++++++++ tests/Api/Shop/Checkout/CartTest.php | 88 +++++++++++++++++++ 5 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 tests/Api/DataFixtures/ORM/promotion/promotion_coupon_expired.yaml create mode 100644 tests/Api/DataFixtures/ORM/promotion/promotion_ended_with_valid_coupon.yaml create mode 100644 tests/Api/DataFixtures/ORM/promotion/promotion_ineligible.yaml diff --git a/tests/Api/DataFixtures/ORM/promotion/product.yaml b/tests/Api/DataFixtures/ORM/promotion/product.yaml index a98984a661..cd674f1111 100644 --- a/tests/Api/DataFixtures/ORM/promotion/product.yaml +++ b/tests/Api/DataFixtures/ORM/promotion/product.yaml @@ -3,7 +3,17 @@ Sylius\Component\Core\Model\Product: code: 'CAP' channels: ['@channel_web'] currentLocale: 'en_US' - product_socks: - code: 'SOCKS' - channels: ['@channel_web'] + +Sylius\Component\Core\Model\ProductVariant: + product_variant_cap_blue: + code: 'CAP_BLUE' + product: '@product_cap' currentLocale: 'en_US' + channelPricings: + WEB: '@channel_pricing_cap_blue_web' + +Sylius\Component\Core\Model\ChannelPricing: + channel_pricing_cap_blue_web: + channelCode: 'WEB' + price: 2000 + originalPrice: 3000 diff --git a/tests/Api/DataFixtures/ORM/promotion/promotion_coupon_expired.yaml b/tests/Api/DataFixtures/ORM/promotion/promotion_coupon_expired.yaml new file mode 100644 index 0000000000..702c5e79ec --- /dev/null +++ b/tests/Api/DataFixtures/ORM/promotion/promotion_coupon_expired.yaml @@ -0,0 +1,29 @@ +Sylius\Component\Core\Model\Promotion: + promotion_active: + code: 'promotion_active' + name: 'Active promotion with expired coupon' + description: 'This promotion is active but the coupon has already expired' + channels: ['@channel_web'] + priority: 1 + exclusive: true + appliesToDiscounted: false + usageLimit: 1 + used: 0 + couponBased: true + translations: + - '@promotion_active_en' + +Sylius\Component\Promotion\Model\PromotionTranslation: + promotion_active_en: + locale: 'en_US' + label: 'Active promotion with expired coupon' + translatable: '@promotion_active' + +Sylius\Component\Core\Model\PromotionCoupon: + promotion_coupon_expired: + code: 'PROMOTION_COUPON_EXPIRED' + usageLimit: null + perCustomerUsageLimit: null + reusableFromCancelledOrders: false + promotion: '@promotion_active' + expiresAt: diff --git a/tests/Api/DataFixtures/ORM/promotion/promotion_ended_with_valid_coupon.yaml b/tests/Api/DataFixtures/ORM/promotion/promotion_ended_with_valid_coupon.yaml new file mode 100644 index 0000000000..b39ea8d980 --- /dev/null +++ b/tests/Api/DataFixtures/ORM/promotion/promotion_ended_with_valid_coupon.yaml @@ -0,0 +1,30 @@ +Sylius\Component\Core\Model\Promotion: + promotion_ended: + code: 'promotion_ended' + name: 'Ended promotion with valid coupon' + description: 'This promotion has already ended but the coupon is still valid' + channels: ['@channel_web'] + priority: 1 + exclusive: true + appliesToDiscounted: false + usageLimit: 1 + used: 0 + couponBased: true + endsAt: + translations: + - '@promotion_ended_en' + +Sylius\Component\Promotion\Model\PromotionTranslation: + promotion_ended_en: + locale: 'en_US' + label: 'Ended promotion with valid coupon' + translatable: '@promotion_ended' + +Sylius\Component\Core\Model\PromotionCoupon: + promotion_coupon_valid: + code: 'PROMOTION_COUPON_VALID' + usageLimit: null + perCustomerUsageLimit: null + reusableFromCancelledOrders: false + promotion: '@promotion_ended' + expiresAt: diff --git a/tests/Api/DataFixtures/ORM/promotion/promotion_ineligible.yaml b/tests/Api/DataFixtures/ORM/promotion/promotion_ineligible.yaml new file mode 100644 index 0000000000..4d26c4166a --- /dev/null +++ b/tests/Api/DataFixtures/ORM/promotion/promotion_ineligible.yaml @@ -0,0 +1,43 @@ +Sylius\Component\Core\Model\Promotion: + promotion_ineligible: + code: 'promotion_ineligible' + name: 'Ineligible promotion' + description: 'This promotion is not eligible for the cart that contains more than 10 items' + channels: ['@channel_web'] + priority: 1 + exclusive: true + appliesToDiscounted: false + usageLimit: 1 + used: 0 + couponBased: true + translations: + - '@promotion_ineligible_en' + +Sylius\Component\Promotion\Model\PromotionTranslation: + promotion_ineligible_en: + locale: 'en_US' + label: 'Ineligible' + translatable: '@promotion_ineligible' + +Sylius\Component\Core\Model\PromotionCoupon: + promotion_coupon_ineligible: + code: 'ELIGIBLE_COUPON_WITH_INELIGIBLE_PROMOTION' + usageLimit: null + perCustomerUsageLimit: null + reusableFromCancelledOrders: false + promotion: '@promotion_ineligible' + +Sylius\Component\Promotion\Model\PromotionRule: + promotion_rule_cart_quantity_more_than_10: + type: "cart_quantity" + promotion: "@promotion_ineligible" + configuration: + count: 5 + +Sylius\Component\Promotion\Model\PromotionAction: + promotion_action_unit_fixed_discount_100: + type: "unit_fixed_discount" + promotion: "@promotion_ineligible" + configuration: + WEB: + amount: 100 diff --git a/tests/Api/Shop/Checkout/CartTest.php b/tests/Api/Shop/Checkout/CartTest.php index 10ecba1327..cec85fd062 100644 --- a/tests/Api/Shop/Checkout/CartTest.php +++ b/tests/Api/Shop/Checkout/CartTest.php @@ -24,6 +24,7 @@ final class CartTest extends JsonApiTestCase protected function setUp(): void { $this->setUpOrderPlacer(); + $this->setUpDefaultPutHeaders(); parent::setUp(); } @@ -598,6 +599,93 @@ final class CartTest extends JsonApiTestCase ); } + /** @test */ + public function it_does_not_apply_invalid_coupon_code(): void + { + $this->loadFixturesFromFiles([ + 'promotion/channel.yaml', + 'promotion/product.yaml', + ]); + + $tokenValue = $this->pickUpCart(); + $this->addItemToCart('CAP_BLUE', 1, $tokenValue); + + $this->requestPut( + uri: sprintf('/api/v2/shop/orders/%s', $tokenValue), + body: ['couponCode' => 'INVALID'], + ); + + $this->assertResponseViolations($this->client->getResponse(), [ + ['propertyPath' => 'couponCode', 'message' => 'Coupon code is invalid.'], + ]); + } + + /** @test */ + public function it_does_not_apply_expired_coupon_code(): void + { + $this->loadFixturesFromFiles([ + 'promotion/channel.yaml', + 'promotion/product.yaml', + 'promotion/promotion_coupon_expired.yaml', + ]); + + $tokenValue = $this->pickUpCart(); + $this->addItemToCart('CAP_BLUE', 1, $tokenValue); + + $this->requestPut( + uri: sprintf('/api/v2/shop/orders/%s', $tokenValue), + body: ['couponCode' => 'PROMOTION_COUPON_EXPIRED'], + ); + + $this->assertResponseViolations($this->client->getResponse(), [ + ['propertyPath' => 'couponCode', 'message' => 'Coupon code has expired.'], + ]); + } + + /** @test */ + public function it_does_not_apply_a_valid_coupon_code_of_ended_promotion(): void + { + $this->loadFixturesFromFiles([ + 'promotion/channel.yaml', + 'promotion/promotion_ended_with_valid_coupon.yaml', + 'promotion/product.yaml', + ]); + + $tokenValue = $this->pickUpCart(); + $this->addItemToCart('CAP_BLUE', 1, $tokenValue); + + $this->requestPut( + uri: sprintf('/api/v2/shop/orders/%s', $tokenValue), + body: ['couponCode' => 'PROMOTION_COUPON_VALID'], + ); + + $this->assertResponseViolations($this->client->getResponse(), [ + ['propertyPath' => 'couponCode', 'message' => 'Coupon code is not valid for this order.'], + ]); + } + + /** @test */ + public function it_does_not_apply_valid_coupon_with_ineligible_promotion(): void + { + $this->loadFixturesFromFiles([ + 'promotion/channel.yaml', + 'promotion/promotion_ineligible.yaml', + 'promotion/product.yaml', + ]); + + $tokenValue = $this->pickUpCart(); + $this->addItemToCart('CAP_BLUE', 1, $tokenValue); + + $this->requestPut( + uri: sprintf('/api/v2/shop/orders/%s', $tokenValue), + body: ['couponCode' => 'ELIGIBLE_COUPON_WITH_INELIGIBLE_PROMOTION'], + ); + + $this->assertResponseViolations($this->client->getResponse(), [ + ['propertyPath' => 'couponCode', 'message' => 'Coupon code is not valid for this order.'], + ]); + } + /** @test */ public function it_deletes_cart(): void {