mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 09:02:12 +00:00
Merge pull request #6744 from Zales0123/unify-taxon-rule-checkers
[Core][Promotion] Unify taxon rule checkers
This commit is contained in:
commit
747322a1fa
18 changed files with 28 additions and 478 deletions
|
|
@ -52,7 +52,6 @@ The types of rules that are configured in **Sylius** by default are:
|
|||
* **Item Total** - checks if items in the cart cost a given amount of money,
|
||||
* **Taxon** - checks if there is at least one item from given taxons in the cart,
|
||||
* **Items From Taxon Total** - checks in the cart if items from a given taxon cost a given amount of money,
|
||||
* **Contains Taxon** - checks if there is a given amount of items from a given taxon in the cart,
|
||||
* **Nth Order** - checks if this is for example the second order made by the customer,
|
||||
* **Shipping Country** - checks if the order's shipping address is in a given country.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,55 +0,0 @@
|
|||
@applying_promotion_rules
|
||||
Feature: Receiving a discount based on the number of products from a specific taxon
|
||||
In order to pay less while buying required number of goods from a promoted taxon
|
||||
As a Customer
|
||||
I want to receive a discount for my purchase
|
||||
|
||||
Background:
|
||||
Given the store operates on a single channel in "United States"
|
||||
And the store classifies its products as "T-Shirts" and "Mugs"
|
||||
And the store has a product "PHP T-Shirt" priced at "$100.00"
|
||||
And this product belongs to "T-Shirts"
|
||||
And the store has a product "Symfony T-Shirt" priced at "$150.00"
|
||||
And this product belongs to "T-Shirts"
|
||||
And the store has a product "PHP Mug" priced at "$20.00"
|
||||
And this product belongs to "Mugs"
|
||||
And there is a promotion "T-Shirts promotion"
|
||||
|
||||
@ui
|
||||
Scenario: Receiving a discount on order while buying the required number of products from a promoted taxon
|
||||
Given the promotion gives "$50.00" off if order contains 2 products classified as "T-Shirts"
|
||||
When I add 3 products "PHP T-Shirt" to the cart
|
||||
Then my cart total should be "$250.00"
|
||||
And my discount should be "-$50.00"
|
||||
|
||||
@ui
|
||||
Scenario: Receiving a discount on order while buying exactly the required number of products from a promoted taxon
|
||||
Given the promotion gives "$50.00" off if order contains 2 products classified as "T-Shirts"
|
||||
When I add 2 products "PHP T-Shirt" to the cart
|
||||
Then my cart total should be "$150.00"
|
||||
And my discount should be "-$50.00"
|
||||
|
||||
@ui
|
||||
Scenario: Receiving no discount on order while buying less than required number of products from a promoted taxon
|
||||
Given the promotion gives "$50.00" off if order contains 2 products classified as "T-Shirts"
|
||||
When I add product "PHP T-Shirt" to the cart
|
||||
Then my cart total should be "$100.00"
|
||||
And there should be no discount
|
||||
|
||||
@ui
|
||||
Scenario: Receiving a discount on order while buying multiple items with products from a promoted taxon which fit number criteria
|
||||
Given the promotion gives "$100.00" off if order contains 4 products classified as "T-Shirts"
|
||||
When I add 3 products "PHP T-Shirt" to the cart
|
||||
And I add 2 products "Symfony T-Shirt" to the cart
|
||||
Then my cart total should be "$500.00"
|
||||
And my discount should be "-$100.00"
|
||||
|
||||
@ui
|
||||
Scenario: Receiving different discounts on different promotions checking number of products from the specific taxon
|
||||
Given the promotion gives "$50.00" off if order contains 2 products classified as "T-Shirts"
|
||||
And there is a promotion "Mugs promotion"
|
||||
And it gives "$10.00" off if order contains 3 products classified as "Mugs"
|
||||
When I add 3 products "PHP T-Shirt" to the cart
|
||||
And I add 3 products "PHP Mug" to the cart
|
||||
Then my cart total should be "$300.00"
|
||||
And my discount should be "-$60.00"
|
||||
|
|
@ -25,15 +25,6 @@ Feature: Receiving a discount based on a configured promotion
|
|||
Then my cart total should be "$40.00"
|
||||
And my discount should be "-$10.00"
|
||||
|
||||
@ui
|
||||
Scenario: Receiving a discount on products from a specific taxon if an order contains products from an another taxon
|
||||
Given there is a promotion "Jacket-trousers pack"
|
||||
And it gives "10%" off on every product classified as "Jackets" if an order contains any product classified as "Trousers"
|
||||
When I add product "Iron Maiden trousers" to the cart
|
||||
And I add product "Black Sabbath jacket" to the cart
|
||||
Then product "Black Sabbath jacket" price should be decreased by "$10.00"
|
||||
And my cart total should be "$170.00"
|
||||
|
||||
@ui
|
||||
Scenario: Receiving a discount on items and shipping from one promotion based on items total
|
||||
Given the store has "DHL" shipping method with "$10.00" fee
|
||||
|
|
|
|||
|
|
@ -9,22 +9,12 @@ Feature: Adding a new promotion with rule
|
|||
And the store classifies its products as "T-Shirts" and "Mugs"
|
||||
And I am logged in as an administrator
|
||||
|
||||
@ui @javascript
|
||||
Scenario: Adding a new promotion with contains items from taxon rule
|
||||
Given I want to create a new promotion
|
||||
When I specify its code as "T_SHIRTS_PROMOTION"
|
||||
And I name it "T-Shirts promotion"
|
||||
And I add the "Contains number of items from taxon" rule configured with 4 "T-Shirts"
|
||||
And I add it
|
||||
Then I should be notified that it has been successfully created
|
||||
And the "T-Shirts promotion" promotion should appear in the registry
|
||||
|
||||
@ui @javascript
|
||||
Scenario: Adding a new promotion with taxon rule
|
||||
Given I want to create a new promotion
|
||||
When I specify its code as "HOLIDAY_SALE"
|
||||
And I name it "Holiday sale"
|
||||
And I add the "Taxon" rule configured with "T-Shirts" and "Mugs"
|
||||
And I add the "Has at least one from taxons" rule configured with "T-Shirts" and "Mugs"
|
||||
And I add it
|
||||
Then I should be notified that it has been successfully created
|
||||
And the "Holiday sale" promotion should appear in the registry
|
||||
|
|
|
|||
|
|
@ -399,7 +399,7 @@ final class PromotionContext implements Context
|
|||
$discount,
|
||||
TaxonInterface $taxon
|
||||
) {
|
||||
$rule = $this->ruleFactory->createTaxon([$taxon->getCode()]);
|
||||
$rule = $this->ruleFactory->createHasTaxon([$taxon->getCode()]);
|
||||
|
||||
$this->createFixedPromotion($promotion, $discount, [], $rule);
|
||||
}
|
||||
|
|
@ -412,7 +412,7 @@ final class PromotionContext implements Context
|
|||
$discount,
|
||||
array $taxons
|
||||
) {
|
||||
$rule = $this->ruleFactory->createTaxon([$taxons[0]->getCode(), $taxons[1]->getCode()]);
|
||||
$rule = $this->ruleFactory->createHasTaxon([$taxons[0]->getCode(), $taxons[1]->getCode()]);
|
||||
|
||||
$this->createFixedPromotion($promotion, $discount, [], $rule);
|
||||
}
|
||||
|
|
@ -431,20 +431,6 @@ final class PromotionContext implements Context
|
|||
$this->createFixedPromotion($promotion, $discount, [], $rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (\d+) products (classified as "[^"]+")$/
|
||||
*/
|
||||
public function thePromotionGivesOffIfOrderContainsNumberOfProductsClassifiedAs(
|
||||
PromotionInterface $promotion,
|
||||
$discount,
|
||||
$count,
|
||||
TaxonInterface $taxon
|
||||
) {
|
||||
$rule = $this->ruleFactory->createContainsTaxon($taxon->getCode(), $count);
|
||||
|
||||
$this->createFixedPromotion($promotion, $discount, [], $rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/
|
||||
*/
|
||||
|
|
@ -465,20 +451,6 @@ final class PromotionContext implements Context
|
|||
$this->createPercentagePromotion($promotion, $discount, [], $rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if an order contains any product (classified as "[^"]+")$/
|
||||
*/
|
||||
public function itGivesPercentageOffOnEveryProductClassifiedAsIfAnOrderContainsAnyProductClassifiedAs(
|
||||
PromotionInterface $promotion,
|
||||
$discount,
|
||||
TaxonInterface $discountTaxon,
|
||||
TaxonInterface $targetTaxon
|
||||
) {
|
||||
$rule = $this->ruleFactory->createContainsTaxon($targetTaxon->getCode(), 1);
|
||||
|
||||
$this->createUnitPercentagePromotion($promotion, $discount, $this->getTaxonFilterConfiguration([$discountTaxon->getCode()]), $rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/
|
||||
*/
|
||||
|
|
@ -544,7 +516,7 @@ final class PromotionContext implements Context
|
|||
$discountTaxonsCodes = [$discountTaxons[0]->getCode(), $discountTaxons[1]->getCode()];
|
||||
$targetTaxonsCodes = [$targetTaxons[0]->getCode(), $targetTaxons[1]->getCode()];
|
||||
|
||||
$rule = $this->ruleFactory->createTaxon($targetTaxonsCodes);
|
||||
$rule = $this->ruleFactory->createHasTaxon($targetTaxonsCodes);
|
||||
|
||||
$this->createUnitPercentagePromotion(
|
||||
$promotion,
|
||||
|
|
|
|||
|
|
@ -143,22 +143,12 @@ final class ManagingPromotionsContext implements Context
|
|||
}
|
||||
|
||||
/**
|
||||
* @Given I add the "Contains number of items from taxon" rule configured with :count :taxonName
|
||||
* @Given I add the "Has at least one from taxons" rule configured with :firstTaxon
|
||||
* @Given I add the "Has at least one from taxons" rule configured with :firstTaxon and :secondTaxon
|
||||
*/
|
||||
public function iAddTheContainsTaxonRuleConfiguredWith($count, $taxonName)
|
||||
public function iAddTheHasTaxonRuleConfiguredWith(...$taxons)
|
||||
{
|
||||
$this->createPage->addRule('Contains number of items from taxon');
|
||||
$this->createPage->selectRuleOption('Taxon', $taxonName);
|
||||
$this->createPage->fillRuleOption('Count', $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Given I add the "Taxon" rule configured with :firstTaxon
|
||||
* @Given I add the "Taxon" rule configured with :firstTaxon and :secondTaxon
|
||||
*/
|
||||
public function iAddTheTaxonRuleConfiguredWith(...$taxons)
|
||||
{
|
||||
$this->createPage->addRule('Taxon');
|
||||
$this->createPage->addRule('Has at least one from taxons');
|
||||
|
||||
foreach ($taxons as $taxon) {
|
||||
$this->createPage->selectRuleOption('Taxons', $taxon, true);
|
||||
|
|
|
|||
|
|
@ -1,63 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\CoreBundle\Form\Type\Promotion\Rule;
|
||||
|
||||
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
/**
|
||||
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
|
||||
*/
|
||||
class ContainsTaxonConfigurationType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @var TaxonRepositoryInterface
|
||||
*/
|
||||
private $taxonRepository;
|
||||
|
||||
/**
|
||||
* @param TaxonRepositoryInterface $taxonRepository
|
||||
*/
|
||||
public function __construct(TaxonRepositoryInterface $taxonRepository)
|
||||
{
|
||||
$this->taxonRepository = $taxonRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('taxon', 'sylius_taxon_from_identifier', [
|
||||
'label' => 'sylius.form.promotion_rule.contains_taxon.taxon',
|
||||
'class' => $this->taxonRepository->getClassName(),
|
||||
'query_builder' => function () {
|
||||
return $this->taxonRepository->getFormQueryBuilder();
|
||||
},
|
||||
'identifier' => 'code',
|
||||
])
|
||||
->add('count', 'integer', [
|
||||
'label' => 'sylius.form.promotion_rule.contains_taxon.count',
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'sylius_promotion_rule_contains_taxon_configuration';
|
||||
}
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ use Symfony\Component\Form\FormBuilderInterface;
|
|||
/**
|
||||
* @author Saša Stamenković <umpirsky@gmail.com>
|
||||
*/
|
||||
class TaxonConfigurationType extends AbstractType
|
||||
class HasTaxonConfigurationType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* @var DataTransformerInterface
|
||||
|
|
@ -53,6 +53,6 @@ class TaxonConfigurationType extends AbstractType
|
|||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'sylius_promotion_rule_taxon_configuration';
|
||||
return 'sylius_promotion_rule_has_taxon_configuration';
|
||||
}
|
||||
}
|
||||
|
|
@ -86,13 +86,9 @@
|
|||
<service id="sylius.form.type.promotion_rule.shipping_country_configuration" class="Sylius\Bundle\CoreBundle\Form\Type\Promotion\Rule\ShippingCountryConfigurationType">
|
||||
<tag name="form.type" alias="sylius_promotion_rule_shipping_country_configuration" />
|
||||
</service>
|
||||
<service id="sylius.form.type.promotion_rule.taxon_configuration" class="Sylius\Bundle\CoreBundle\Form\Type\Promotion\Rule\TaxonConfigurationType">
|
||||
<service id="sylius.form.type.promotion_rule.has_taxon_configuration" class="Sylius\Bundle\CoreBundle\Form\Type\Promotion\Rule\HasTaxonConfigurationType">
|
||||
<argument type="service" id="sylius.form.type.data_transformer.taxons_to_codes" />
|
||||
<tag name="form.type" alias="sylius_promotion_rule_taxon_configuration" />
|
||||
</service>
|
||||
<service id="sylius.form.type.promotion_rule.contains_taxon_configuration" class="Sylius\Bundle\CoreBundle\Form\Type\Promotion\Rule\ContainsTaxonConfigurationType">
|
||||
<argument type="service" id="sylius.repository.taxon" />
|
||||
<tag name="form.type" alias="sylius_promotion_rule_contains_taxon_configuration" />
|
||||
<tag name="form.type" alias="sylius_promotion_rule_has_taxon_configuration" />
|
||||
</service>
|
||||
<service id="sylius.form.type.promotion_rule.total_of_items_from_taxon_configuration" class="Sylius\Bundle\CoreBundle\Form\Type\Promotion\Rule\TotalOfItemsFromTaxonConfigurationType">
|
||||
<argument type="service" id="sylius.repository.taxon" />
|
||||
|
|
|
|||
|
|
@ -30,12 +30,8 @@
|
|||
<argument type="service" id="sylius.repository.country" />
|
||||
<tag name="sylius.promotion_rule_checker" type="shipping_country" label="Shipping country" />
|
||||
</service>
|
||||
<service id="sylius.promotion_rule_checker.taxon" class="Sylius\Component\Core\Promotion\Checker\Rule\TaxonRuleChecker">
|
||||
<tag name="sylius.promotion_rule_checker" type="taxon" label="Taxon" />
|
||||
</service>
|
||||
<service id="sylius.promotion_rule_checker.contains_taxon" class="Sylius\Component\Core\Promotion\Checker\Rule\ContainsTaxonRuleChecker">
|
||||
<argument type="service" id="sylius.repository.taxon" />
|
||||
<tag name="sylius.promotion_rule_checker" type="contains_taxon" label="Contains number of items from taxon" />
|
||||
<service id="sylius.promotion_rule_checker.has_taxon" class="Sylius\Component\Core\Promotion\Checker\Rule\HasTaxonRuleChecker">
|
||||
<tag name="sylius.promotion_rule_checker" type="has_taxon" label="Has at least one from taxons" />
|
||||
</service>
|
||||
<service id="sylius.promotion_rule_checker.total_of_items_from_taxon" class="Sylius\Component\Core\Promotion\Checker\Rule\TotalOfItemsFromTaxonRuleChecker">
|
||||
<argument type="service" id="sylius.repository.taxon" />
|
||||
|
|
|
|||
|
|
@ -120,9 +120,6 @@ sylius:
|
|||
product_association_type:
|
||||
name: Name
|
||||
promotion_rule:
|
||||
contains_taxon:
|
||||
taxon: Taxon
|
||||
count: Count
|
||||
taxon:
|
||||
taxons: Taxons
|
||||
product:
|
||||
|
|
|
|||
|
|
@ -12,9 +12,8 @@
|
|||
namespace Sylius\Component\Core\Factory;
|
||||
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\ContainsProductRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\ContainsTaxonRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\NthOrderRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\TaxonRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\HasTaxonRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\TotalOfItemsFromTaxonRuleChecker;
|
||||
use Sylius\Component\Promotion\Checker\Rule\CartQuantityRuleChecker;
|
||||
use Sylius\Component\Promotion\Checker\Rule\ItemTotalRuleChecker;
|
||||
|
|
@ -76,11 +75,11 @@ class PromotionRuleFactory implements PromotionRuleFactoryInterface
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createTaxon(array $taxons)
|
||||
public function createHasTaxon(array $taxons)
|
||||
{
|
||||
/** @var PromotionRuleInterface $rule */
|
||||
$rule = $this->createNew();
|
||||
$rule->setType(TaxonRuleChecker::TYPE);
|
||||
$rule->setType(HasTaxonRuleChecker::TYPE);
|
||||
$rule->setConfiguration(['taxons' => $taxons]);
|
||||
|
||||
return $rule;
|
||||
|
|
@ -99,19 +98,6 @@ class PromotionRuleFactory implements PromotionRuleFactoryInterface
|
|||
return $rule;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createContainsTaxon($taxonCode, $count)
|
||||
{
|
||||
/** @var PromotionRuleInterface $rule */
|
||||
$rule = $this->createNew();
|
||||
$rule->setType(ContainsTaxonRuleChecker::TYPE);
|
||||
$rule->setConfiguration(['taxon' => $taxonCode, 'count' => $count]);
|
||||
|
||||
return $rule;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ interface PromotionRuleFactoryInterface extends FactoryInterface
|
|||
*
|
||||
* @return PromotionRuleInterface
|
||||
*/
|
||||
public function createTaxon(array $taxons);
|
||||
public function createHasTaxon(array $taxons);
|
||||
|
||||
/**
|
||||
* @param string $taxonCode
|
||||
|
|
@ -48,14 +48,6 @@ interface PromotionRuleFactoryInterface extends FactoryInterface
|
|||
*/
|
||||
public function createItemsFromTaxonTotal($taxonCode, $amount);
|
||||
|
||||
/**
|
||||
* @param string $taxonCode
|
||||
* @param int $count
|
||||
*
|
||||
* @return PromotionRuleInterface
|
||||
*/
|
||||
public function createContainsTaxon($taxonCode, $count);
|
||||
|
||||
/**
|
||||
* @param int $nth
|
||||
*
|
||||
|
|
|
|||
|
|
@ -1,77 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Component\Core\Promotion\Checker\Rule;
|
||||
|
||||
use Sylius\Component\Core\Model\OrderInterface;
|
||||
use Sylius\Component\Promotion\Checker\Rule\RuleCheckerInterface;
|
||||
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
|
||||
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
|
||||
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
|
||||
|
||||
/**
|
||||
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
|
||||
*/
|
||||
final class ContainsTaxonRuleChecker implements RuleCheckerInterface
|
||||
{
|
||||
const TYPE = 'contains_taxon';
|
||||
|
||||
/**
|
||||
* @var TaxonRepositoryInterface
|
||||
*/
|
||||
private $taxonRepository;
|
||||
|
||||
/**
|
||||
* @param TaxonRepositoryInterface $taxonRepository
|
||||
*/
|
||||
public function __construct(TaxonRepositoryInterface $taxonRepository)
|
||||
{
|
||||
$this->taxonRepository = $taxonRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isEligible(PromotionSubjectInterface $subject, array $configuration)
|
||||
{
|
||||
if (!$subject instanceof OrderInterface) {
|
||||
throw new UnexpectedTypeException($subject, OrderInterface::class);
|
||||
}
|
||||
|
||||
if (!isset($configuration['taxon']) || !isset($configuration['count'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$targetTaxon = $this->taxonRepository->findOneBy(['code' => $configuration['taxon']]);
|
||||
if (null === $targetTaxon) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$validProducts = 0;
|
||||
foreach ($subject->getItems() as $item) {
|
||||
if (!$item->getProduct()->hasTaxon($targetTaxon)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$validProducts += $item->getQuantity();
|
||||
}
|
||||
|
||||
return $validProducts >= $configuration['count'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getConfigurationFormType()
|
||||
{
|
||||
return 'sylius_promotion_rule_contains_taxon_configuration';
|
||||
}
|
||||
}
|
||||
|
|
@ -20,9 +20,9 @@ use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
|
|||
/**
|
||||
* @author Saša Stamenković <umpirsky@gmail.com>
|
||||
*/
|
||||
final class TaxonRuleChecker implements RuleCheckerInterface
|
||||
final class HasTaxonRuleChecker implements RuleCheckerInterface
|
||||
{
|
||||
const TYPE = 'taxon';
|
||||
const TYPE = 'has_taxon';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
|
@ -54,6 +54,6 @@ final class TaxonRuleChecker implements RuleCheckerInterface
|
|||
*/
|
||||
public function getConfigurationFormType()
|
||||
{
|
||||
return 'sylius_promotion_rule_taxon_configuration';
|
||||
return 'sylius_promotion_rule_has_taxon_configuration';
|
||||
}
|
||||
}
|
||||
|
|
@ -15,9 +15,8 @@ use PhpSpec\ObjectBehavior;
|
|||
use Sylius\Component\Core\Factory\PromotionRuleFactory;
|
||||
use Sylius\Component\Core\Factory\PromotionRuleFactoryInterface;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\ContainsProductRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\ContainsTaxonRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\NthOrderRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\TaxonRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\HasTaxonRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\TotalOfItemsFromTaxonRuleChecker;
|
||||
use Sylius\Component\Promotion\Checker\Rule\CartQuantityRuleChecker;
|
||||
use Sylius\Component\Promotion\Checker\Rule\ItemTotalRuleChecker;
|
||||
|
|
@ -71,13 +70,13 @@ final class PromotionRuleFactorySpec extends ObjectBehavior
|
|||
$this->createItemTotal(1000)->shouldReturn($rule);
|
||||
}
|
||||
|
||||
function it_creates_a_taxon_rule(FactoryInterface $decoratedFactory, PromotionRuleInterface $rule)
|
||||
function it_creates_a_has_taxon_rule(FactoryInterface $decoratedFactory, PromotionRuleInterface $rule)
|
||||
{
|
||||
$decoratedFactory->createNew()->willReturn($rule);
|
||||
$rule->setType(TaxonRuleChecker::TYPE)->shouldBeCalled();
|
||||
$rule->setType(HasTaxonRuleChecker::TYPE)->shouldBeCalled();
|
||||
$rule->setConfiguration(['taxons' => [1, 6]])->shouldBeCalled();
|
||||
|
||||
$this->createTaxon([1, 6])->shouldReturn($rule);
|
||||
$this->createHasTaxon([1, 6])->shouldReturn($rule);
|
||||
}
|
||||
|
||||
function it_creates_a_total_of_items_from_taxon_rule(
|
||||
|
|
@ -91,15 +90,6 @@ final class PromotionRuleFactorySpec extends ObjectBehavior
|
|||
$this->createItemsFromTaxonTotal('spears', 1000)->shouldReturn($rule);
|
||||
}
|
||||
|
||||
function it_creates_a_contains_taxon_rule(FactoryInterface $decoratedFactory, PromotionRuleInterface $rule)
|
||||
{
|
||||
$decoratedFactory->createNew()->willReturn($rule);
|
||||
$rule->setType(ContainsTaxonRuleChecker::TYPE)->shouldBeCalled();
|
||||
$rule->setConfiguration(['taxon' => 'bows', 'count' => 10])->shouldBeCalled();
|
||||
|
||||
$this->createContainsTaxon('bows', 10)->shouldReturn($rule);
|
||||
}
|
||||
|
||||
function it_creates_a_nth_order_rule(FactoryInterface $decoratedFactory, PromotionRuleInterface $rule)
|
||||
{
|
||||
$decoratedFactory->createNew()->willReturn($rule);
|
||||
|
|
|
|||
|
|
@ -1,154 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace spec\Sylius\Component\Core\Promotion\Checker\Rule;
|
||||
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Sylius\Component\Core\Model\OrderInterface;
|
||||
use Sylius\Component\Core\Model\OrderItemInterface;
|
||||
use Sylius\Component\Core\Model\ProductInterface;
|
||||
use Sylius\Component\Core\Model\TaxonInterface;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\ContainsTaxonRuleChecker;
|
||||
use Sylius\Component\Promotion\Checker\Rule\RuleCheckerInterface;
|
||||
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
|
||||
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
|
||||
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
|
||||
|
||||
/**
|
||||
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
|
||||
*/
|
||||
final class ContainsTaxonRuleCheckerSpec extends ObjectBehavior
|
||||
{
|
||||
function let(TaxonRepositoryInterface $taxonRepository)
|
||||
{
|
||||
$this->beConstructedWith($taxonRepository);
|
||||
}
|
||||
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType(ContainsTaxonRuleChecker::class);
|
||||
}
|
||||
|
||||
function it_implements_a_rule_checker_interface()
|
||||
{
|
||||
$this->shouldImplement(RuleCheckerInterface::class);
|
||||
}
|
||||
|
||||
function it_returns_true_if_order_has_required_number_of_products_from_configured_taxon(
|
||||
OrderInterface $order,
|
||||
OrderItemInterface $compositeBowItem,
|
||||
OrderItemInterface $longbowItem,
|
||||
ProductInterface $compositeBow,
|
||||
ProductInterface $longbow,
|
||||
TaxonInterface $bows,
|
||||
TaxonRepositoryInterface $taxonRepository
|
||||
) {
|
||||
$order
|
||||
->getItems()
|
||||
->willReturn(new \ArrayIterator([$compositeBowItem->getWrappedObject(), $longbowItem->getWrappedObject()]))
|
||||
;
|
||||
|
||||
$taxonRepository->findOneBy(['code' => 'bows'])->willReturn($bows);
|
||||
|
||||
$compositeBowItem->getProduct()->willReturn($compositeBow);
|
||||
$compositeBow->hasTaxon($bows)->willReturn(true);
|
||||
$compositeBowItem->getQuantity()->willReturn(4);
|
||||
|
||||
$longbowItem->getProduct()->willReturn($longbow);
|
||||
$longbow->hasTaxon($bows)->willReturn(true);
|
||||
$longbowItem->getQuantity()->willReturn(5);
|
||||
|
||||
$this->isEligible($order, ['taxon' => 'bows', 'count' => 5])->shouldReturn(true);
|
||||
}
|
||||
|
||||
function it_returns_false_if_an_order_does_not_have_the_required_number_of_products_from_a_configured_taxon(
|
||||
OrderInterface $order,
|
||||
OrderItemInterface $compositeBowItem,
|
||||
OrderItemInterface $longbowItem,
|
||||
ProductInterface $compositeBow,
|
||||
ProductInterface $longbow,
|
||||
TaxonInterface $bows,
|
||||
TaxonRepositoryInterface $taxonRepository
|
||||
) {
|
||||
$order
|
||||
->getItems()
|
||||
->willReturn(new \ArrayIterator([$compositeBowItem->getWrappedObject(), $longbowItem->getWrappedObject()]))
|
||||
;
|
||||
|
||||
$taxonRepository->findOneBy(['code' => 'bows'])->willReturn($bows);
|
||||
|
||||
$compositeBowItem->getProduct()->willReturn($compositeBow);
|
||||
$compositeBow->hasTaxon($bows)->willReturn(true);
|
||||
$compositeBowItem->getQuantity()->willReturn(4);
|
||||
|
||||
$longbowItem->getProduct()->willReturn($longbow);
|
||||
$longbow->hasTaxon($bows)->willReturn(true);
|
||||
$longbowItem->getQuantity()->willReturn(5);
|
||||
|
||||
$this->isEligible($order, ['taxon' => 'bows', 'count' => 15])->shouldReturn(false);
|
||||
}
|
||||
|
||||
function it_does_not_check_an_item_if_its_product_has_no_required_taxon(
|
||||
OrderInterface $order,
|
||||
OrderItemInterface $compositeBowItem,
|
||||
OrderItemInterface $longswordItem,
|
||||
ProductInterface $compositeBow,
|
||||
ProductInterface $longsword,
|
||||
TaxonInterface $bows,
|
||||
TaxonRepositoryInterface $taxonRepository
|
||||
) {
|
||||
$order
|
||||
->getItems()
|
||||
->willReturn(new \ArrayIterator([$compositeBowItem->getWrappedObject(), $longswordItem->getWrappedObject()]))
|
||||
;
|
||||
|
||||
$taxonRepository->findOneBy(['code' => 'bows'])->willReturn($bows);
|
||||
|
||||
$compositeBowItem->getProduct()->willReturn($compositeBow);
|
||||
$compositeBow->hasTaxon($bows)->willReturn(true);
|
||||
$compositeBowItem->getQuantity()->willReturn(4);
|
||||
|
||||
$longswordItem->getProduct()->willReturn($longsword);
|
||||
$longsword->hasTaxon($bows)->willReturn(false);
|
||||
$longswordItem->getQuantity()->shouldNotBeCalled();
|
||||
|
||||
$this->isEligible($order, ['taxon' => 'bows', 'count' => 5])->shouldReturn(false);
|
||||
}
|
||||
|
||||
function it_returns_false_if_configuration_is_invalid(OrderInterface $order)
|
||||
{
|
||||
$this->isEligible($order, ['taxon' => 'bows'])->shouldReturn(false);
|
||||
$this->isEligible($order, ['count' => 10])->shouldReturn(false);
|
||||
$this->isEligible($order, [])->shouldReturn(false);
|
||||
}
|
||||
|
||||
function it_returns_false_if_taxon_with_configured_code_does_not_exist(
|
||||
OrderInterface $order,
|
||||
TaxonRepositoryInterface $taxonRepository
|
||||
) {
|
||||
$taxonRepository->findOneBy(['code' => 'bows'])->willReturn(null);
|
||||
|
||||
$this->isEligible($order, ['taxon' => 'bows', 'count' => 10])->shouldReturn(false);
|
||||
}
|
||||
|
||||
function it_throws_an_exception_if_the_promotion_subject_is_not_an_order(PromotionSubjectInterface $subject)
|
||||
{
|
||||
$this
|
||||
->shouldThrow(new UnexpectedTypeException($subject->getWrappedObject(), OrderInterface::class))
|
||||
->during('isEligible', [$subject, []])
|
||||
;
|
||||
}
|
||||
|
||||
function it_has_a_configuration_type()
|
||||
{
|
||||
$this->getConfigurationFormType()->shouldReturn('sylius_promotion_rule_contains_taxon_configuration');
|
||||
}
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@ use Sylius\Component\Core\Model\OrderInterface;
|
|||
use Sylius\Component\Core\Model\OrderItemInterface;
|
||||
use Sylius\Component\Core\Model\ProductInterface;
|
||||
use Sylius\Component\Core\Model\TaxonInterface;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\TaxonRuleChecker;
|
||||
use Sylius\Component\Core\Promotion\Checker\Rule\HasTaxonRuleChecker;
|
||||
use Sylius\Component\Promotion\Checker\Rule\RuleCheckerInterface;
|
||||
use Sylius\Component\Promotion\Exception\UnsupportedTypeException;
|
||||
use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
|
||||
|
|
@ -26,11 +26,11 @@ use Sylius\Component\Promotion\Model\PromotionSubjectInterface;
|
|||
* @author Joseph Bielawski <stloyd@gmail.com>
|
||||
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
|
||||
*/
|
||||
final class TaxonRuleCheckerSpec extends ObjectBehavior
|
||||
final class HasTaxonRuleCheckerSpec extends ObjectBehavior
|
||||
{
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType(TaxonRuleChecker::class);
|
||||
$this->shouldHaveType(HasTaxonRuleChecker::class);
|
||||
}
|
||||
|
||||
function it_is_a_rule_checker()
|
||||
Loading…
Add table
Reference in a new issue