[Core] Remove ContainsTaxonRuleChecker

This commit is contained in:
Mateusz Zalewski 2016-11-16 10:11:20 +01:00
parent e83bc0f4b8
commit f83c30435d
No known key found for this signature in database
GPG key ID: CFC1E4176165876B
15 changed files with 0 additions and 449 deletions

View file

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

View file

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

View file

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

View file

@ -9,16 +9,6 @@ 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

View file

@ -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$/
*/

View file

@ -142,16 +142,6 @@ final class ManagingPromotionsContext implements Context
$this->createPage->create();
}
/**
* @Given I add the "Contains number of items from taxon" rule configured with :count :taxonName
*/
public function iAddTheContainsTaxonRuleConfiguredWith($count, $taxonName)
{
$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

View file

@ -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';
}
}

View file

@ -90,10 +90,6 @@
<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" />
</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" />
<tag name="form.type" alias="sylius_promotion_rule_total_of_items_from_taxon_configuration" />

View file

@ -33,10 +33,6 @@
<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>
<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" />
<tag name="sylius.promotion_rule_checker" type="total_of_items_from_taxon" label="Total price of items from taxon" />

View file

@ -120,9 +120,6 @@ sylius:
product_association_type:
name: Name
promotion_rule:
contains_taxon:
taxon: Taxon
count: Count
taxon:
taxons: Taxons
product:

View file

@ -12,7 +12,6 @@
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\TotalOfItemsFromTaxonRuleChecker;
@ -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}
*/

View file

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

View file

@ -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';
}
}

View file

@ -91,15 +91,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);

View file

@ -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');
}
}