[Unit] Add promotion coupon applying tests

This commit is contained in:
Rafikooo 2024-09-05 07:49:34 +02:00
parent bddbfd8a7d
commit b7c30eb2f0
No known key found for this signature in database
GPG key ID: 4A26D8327BC2442B
5 changed files with 203 additions and 3 deletions

View file

@ -3,7 +3,17 @@ Sylius\Component\Core\Model\Product:
code: 'CAP' code: 'CAP'
channels: ['@channel_web'] channels: ['@channel_web']
currentLocale: 'en_US' currentLocale: 'en_US'
product_socks:
code: 'SOCKS' Sylius\Component\Core\Model\ProductVariant:
channels: ['@channel_web'] product_variant_cap_blue:
code: 'CAP_BLUE'
product: '@product_cap'
currentLocale: 'en_US' 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

View file

@ -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: <dateTimeBetween('-2 month', '-1 month')>

View file

@ -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: <dateTimeBetween('-1 month', '-1 day')>
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: <dateTimeBetween('+1 month', '+2 month')>

View file

@ -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

View file

@ -24,6 +24,7 @@ final class CartTest extends JsonApiTestCase
protected function setUp(): void protected function setUp(): void
{ {
$this->setUpOrderPlacer(); $this->setUpOrderPlacer();
$this->setUpDefaultPutHeaders();
parent::setUp(); 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 */ /** @test */
public function it_deletes_cart(): void public function it_deletes_cart(): void
{ {