Skip to content

Commit

Permalink
Merge pull request #6997 from michalmarcinkowski/scenarios-for-custom…
Browse files Browse the repository at this point in the history
…er-group-based-promotions

Behat scenarios for customer group based promotions
  • Loading branch information
pjedrzejewski committed Dec 5, 2016
2 parents 1ee1a00 + fa23625 commit f8ebe19
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 4 deletions.
@@ -0,0 +1,37 @@
@applying_promotion_rules
Feature: Receiving discount based on customer group
In order to apply discount only for selected customer group
As a Visitor
I want to have a discount applied when I belong to a specific customer group

Background:
Given the store operates on a single channel in "United States"
And the store has a product "PHP T-Shirt" priced at "$80.00"
And the store has a customer group "Wholesale"
And there is a promotion "Wholesale promotion"
And the promotion gives "10%" off the order for customers from "Wholesale" group

@ui
Scenario: Not logged in customer should not receive discount
When I add product "PHP T-Shirt" to the cart
Then my cart total should be "$80.00"
And there should be no discount

@ui
Scenario: Receiving discounts when belonging to a specific customer group
Given there is a customer account "wholesale@sylius.com"
And the customer belongs to group "Wholesale"
And I am logged in as "wholesale@sylius.com"
When I add product "PHP T-Shirt" to the cart
Then my cart total should be "$72.00"
And my discount should be "-$8.00"

@ui
Scenario: Not receiving discount when belonging to a different customer group that specified in the promotion
Given the store has a customer group "Retail"
And there is a customer account "retail@sylius.com"
And the customer belongs to group "retail"
And I am logged in as "retail@sylius.com"
When I add product "PHP T-Shirt" to the cart
Then my cart total should be "$80.00"
And there should be no discount
Expand Up @@ -39,3 +39,14 @@ Feature: Adding a new promotion with rule
And I add it
Then I should be notified that it has been successfully created
And the "PHP T-Shirt promotion" promotion should appear in the registry

@ui @javascript
Scenario: Adding a new group based promotion
Given the store has a customer group "Wholesale"
When I want to create a new promotion
And I specify its code as "WHOLESALES_PROMOTION"
And I name it "Wholesale promotion"
And I add the "Customer group" rule for "Wholesale" group
And I add it
Then I should be notified that it has been successfully created
And the "Wholesale promotion" promotion should appear in the registry
5 changes: 3 additions & 2 deletions src/Sylius/Behat/Context/Setup/CustomerContext.php
Expand Up @@ -121,10 +121,11 @@ public function thereIsDisabledCustomerAccountWithPassword($email, $password)
}

/**
* @Given there is enabled customer account :email with password :password
* @Given there is a customer account :email
* @Given there is a customer account :email identified by :password
* @Given there is enabled customer account :email with password :password
*/
public function theStoreHasEnabledCustomerAccountWithPassword($email, $password)
public function theStoreHasEnabledCustomerAccountWithPassword($email, $password = 'sylius')
{
$this->createCustomerWithUserAccount($email, $password, true);
}
Expand Down
25 changes: 23 additions & 2 deletions src/Sylius/Behat/Context/Setup/PromotionContext.php
Expand Up @@ -21,7 +21,9 @@
use Sylius\Component\Core\Model\PromotionCouponInterface;
use Sylius\Component\Core\Model\PromotionInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Core\Promotion\Checker\Rule\CustomerGroupRuleChecker;
use Sylius\Component\Core\Test\Factory\TestPromotionFactoryInterface;
use Sylius\Component\Customer\Model\CustomerGroupInterface;
use Sylius\Component\Promotion\Factory\PromotionCouponFactoryInterface;
use Sylius\Component\Promotion\Model\PromotionActionInterface;
use Sylius\Component\Promotion\Model\PromotionRuleInterface;
Expand Down Expand Up @@ -643,6 +645,21 @@ public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promo
$this->createUnitPercentagePromotion($promotion, $discount, $this->getProductsFilterConfiguration([$product->getCode()]));
}

/**
* @Given /^([^"]+) gives ("[^"]+%") off the order for customers from ("[^"]*" group)$/
*/
public function thePromotionGivesOffTheOrderForCustomersFromGroup(
PromotionInterface $promotion,
$discount,
CustomerGroupInterface $customerGroup
) {
$rule = $this->ruleFactory->createNew();
$rule->setType(CustomerGroupRuleChecker::TYPE);
$rule->setConfiguration(['group_code' => $customerGroup->getCode()]);

$this->createPercentagePromotion($promotion, $discount, [], $rule);
}

/**
* @param array $taxonCodes
*
Expand Down Expand Up @@ -740,8 +757,12 @@ private function createFixedPromotion(
* @param array $configuration
* @param PromotionRuleInterface $rule
*/
private function createPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null)
{
private function createPercentagePromotion(
PromotionInterface $promotion,
$discount,
array $configuration = [],
PromotionRuleInterface $rule = null
) {
$this->persistPromotion($promotion, $this->actionFactory->createPercentageDiscount($discount), $configuration, $rule);
}

Expand Down
Expand Up @@ -36,6 +36,7 @@ public function __construct(RepositoryInterface $customerGroupRepository)
/**
* @Transform :customerGroup
* @Transform /^group "([^"]+)"$/
* @Transform /^"([^"]+)" group$/
*/
public function getCustomerGroupByName($customerGroupName)
{
Expand Down
Expand Up @@ -266,6 +266,15 @@ public function iAddTheActionConfiguredWithAPercentageValue($actionType, $percen
$this->createPage->fillActionOption('Percentage', $percentage);
}

/**
* @When I add the "Customer group" rule for :customerGroupName group
*/
public function iAddTheCustomerGroupRuleConfiguredForGroup($customerGroupName)
{
$this->createPage->addRule('Customer group');
$this->createPage->selectRuleOption('Customer group', $customerGroupName);
}

/**
* @Then /^there should be (\d+) promotion(?:|s)$/
*/
Expand Down
Expand Up @@ -9,6 +9,7 @@ default:

- sylius.behat.context.transform.address
- sylius.behat.context.transform.customer
- sylius.behat.context.transform.customer_group
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.payment
- sylius.behat.context.transform.product
Expand All @@ -18,6 +19,8 @@ default:
- sylius.behat.context.transform.user

- sylius.behat.context.setup.channel
- sylius.behat.context.setup.customer
- sylius.behat.context.setup.customer_group
- sylius.behat.context.setup.order
- sylius.behat.context.setup.payment
- sylius.behat.context.setup.product
Expand Down
Expand Up @@ -18,6 +18,7 @@ default:
- sylius.behat.context.transform.shipping_method

- sylius.behat.context.setup.channel
- sylius.behat.context.setup.customer_group
- sylius.behat.context.setup.currency
- sylius.behat.context.setup.order
- sylius.behat.context.setup.payment
Expand Down
Expand Up @@ -23,6 +23,8 @@
*/
class CustomerGroupRuleChecker implements RuleCheckerInterface
{
const TYPE = 'customer_group';

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit f8ebe19

Please sign in to comment.