[Behat][Promotion] Implemented missing promotion steps

This commit is contained in:
Mateusz Zalewski 2016-02-22 15:47:28 +01:00
parent 55c204fb90
commit 8711b8c695
6 changed files with 87 additions and 14 deletions

View file

@ -96,6 +96,7 @@
<service id="sylius.behat.context.setup.promotion" class="%sylius.behat.context.setup.promotion.class%" scope="scenario">
<argument type="service" id="sylius.behat.shared_storage" container="symfony" />
<argument type="service" id="sylius.factory.promotion_action" container="symfony" />
<argument type="service" id="sylius.factory.promotion_rule" container="symfony" />
<argument type="service" id="sylius.test.factory.promotion" container="symfony" />
<argument type="service" id="sylius.repository.promotion" container="symfony" />
<argument type="service" id="doctrine.orm.entity_manager" container="symfony" />

View file

@ -9,32 +9,32 @@ Feature: Receiving discount based on cart quantity
And the store has a product "PHP T-Shirt" priced at "100.00"
And there is a promotion "Holiday promotion"
@todo
@ui
Scenario: Receiving discount when buying more than required quantity
Given the promotion gives "10.00" fixed discount to every cart with quantity at least 3
Given the promotion gives "10.00" fixed discount to every order with quantity at least 3
When I add 4 products "PHP T-Shirt" to the cart
Then my cart total should be "390.00"
And my discount should be "-10.00"
@todo
@ui
Scenario: Receiving discount when buying the required quantity
Given the promotion gives "10.00" fixed discount to every cart with quantity at least 3
When I add 3 products "PHP T-Shirt" to the cart
Then my cart total should be "290.00"
Given the promotion gives "10.00" fixed discount to every order with quantity at least 4
When I add 4 products "PHP T-Shirt" to the cart
Then my cart total should be "390.00"
And my discount should be "-10.00"
@todo
@ui
Scenario: Not receiving discount when buying less than required quantity
Given the promotion gives "10.00" fixed discount to every cart with quantity at least 3
Given the promotion gives "10.00" fixed discount to every order with quantity at least 5
When I add 2 products "PHP T-Shirt" to the cart
Then my cart total should be "200.00"
And my discount should be "0.00"
And there should be no discount
@todo
@ui
Scenario: Receiving discount when buying different products with the required quantity
Given the store has a product "Symfony T-Shirt" priced at "50.00"
And the promotion gives "10.00" fixed discount to every cart with quantity at least 3
And the promotion gives "10.00" fixed discount to every order with quantity at least 3
When I add 2 products "PHP T-Shirt" to the cart
And I add product "Symfony T-Shirt" to the cart
Then my cart total should be "240.00"
And I add 2 products "Symfony T-Shirt" to the cart
Then my cart total should be "290.00"
And my discount should be "-10.00"

View file

@ -13,6 +13,7 @@ namespace Sylius\Behat\Context\Setup;
use Behat\Behat\Context\Context;
use Doctrine\Common\Persistence\ObjectManager;
use Sylius\Component\Core\Factory\RuleFactoryInterface;
use Sylius\Component\Core\Test\Factory\TestPromotionFactoryInterface;
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
use Sylius\Component\Promotion\Factory\ActionFactoryInterface;
@ -33,6 +34,11 @@ final class PromotionContext implements Context
*/
private $actionFactory;
/**
* @var RuleFactoryInterface
*/
private $ruleFactory;
/**
* @var TestPromotionFactoryInterface
*/
@ -51,6 +57,7 @@ final class PromotionContext implements Context
/**
* @param SharedStorageInterface $sharedStorage
* @param ActionFactoryInterface $actionFactory
* @param RuleFactoryInterface $ruleFactory
* @param TestPromotionFactoryInterface $testPromotionFactory
* @param PromotionRepositoryInterface $promotionRepository
* @param ObjectManager $objectManager
@ -58,12 +65,14 @@ final class PromotionContext implements Context
public function __construct(
SharedStorageInterface $sharedStorage,
ActionFactoryInterface $actionFactory,
RuleFactoryInterface $ruleFactory,
TestPromotionFactoryInterface $testPromotionFactory,
PromotionRepositoryInterface $promotionRepository,
ObjectManager $objectManager
) {
$this->sharedStorage = $sharedStorage;
$this->actionFactory = $actionFactory;
$this->ruleFactory = $ruleFactory;
$this->testPromotionFactory = $testPromotionFactory;
$this->promotionRepository = $promotionRepository;
$this->objectManager = $objectManager;
@ -96,7 +105,7 @@ final class PromotionContext implements Context
}
/**
* @Given /^it gives "([^"]+)%" percentage discount to every order$/
* @Given /^(?:it|the promotion) gives "([^"]+)%" percentage discount to every order$/
*/
public function itGivesPercentageDiscountToEveryOrder($discount)
{
@ -108,6 +117,22 @@ final class PromotionContext implements Context
$this->objectManager->flush();
}
/**
* @Given /^(?:it|the promotion) gives "(?:€|£|\$)([^"]+)" fixed discount to every order with quantity at least ([^"]+)$/
*/
public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast($amount, $quantity)
{
$currentPromotion = $this->sharedStorage->get('promotion');
$action = $this->actionFactory->createFixedDiscount($this->getPriceFromString($amount));
$currentPromotion->addAction($action);
$rule = $this->ruleFactory->createCartQuantity((int) $quantity);
$currentPromotion->addRule($rule);
$this->objectManager->flush();
}
/**
* @param string $price
*

View file

@ -13,6 +13,7 @@ namespace Sylius\Behat\Context\Ui;
use Behat\Behat\Context\Context;
use Sylius\Behat\Page\Cart\CartSummaryPage;
use Sylius\Behat\Page\ElementNotFoundException;
use Sylius\Behat\Page\Product\ProductShowPage;
use Sylius\Component\Core\Model\ProductInterface;
@ -124,6 +125,7 @@ final class CartContext implements Context
}
/**
* @Then my cart promotions should be :promotionsTotal
* @Then my discount should be :promotionsTotal
*/
public function myDiscountShouldBe($promotionsTotal)
@ -132,4 +134,14 @@ final class CartContext implements Context
expect($this->cartSummaryPage->getPromotionTotal())->toBe($promotionsTotal);
}
/**
* @Given /^there should be no discount$/
*/
public function thereShouldBeNoDiscount()
{
$this->cartSummaryPage->open();
expect($this->cartSummaryPage)->toThrow(ElementNotFoundException::class)->during('getPromotionTotal', []);
}
}

View file

@ -14,12 +14,14 @@ namespace spec\Sylius\Behat\Context\Setup;
use Behat\Behat\Context\Context;
use Doctrine\Common\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Factory\RuleFactoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\PromotionInterface;
use Sylius\Component\Core\Test\Factory\TestPromotionFactoryInterface;
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
use Sylius\Component\Promotion\Factory\ActionFactoryInterface;
use Sylius\Component\Promotion\Model\ActionInterface;
use Sylius\Component\Promotion\Model\RuleInterface;
use Sylius\Component\Promotion\Repository\PromotionRepositoryInterface;
/**
@ -30,6 +32,7 @@ class PromotionContextSpec extends ObjectBehavior
function let(
SharedStorageInterface $sharedStorage,
ActionFactoryInterface $actionFactory,
RuleFactoryInterface $ruleFactory,
TestPromotionFactoryInterface $testPromotionFactory,
PromotionRepositoryInterface $promotionRepository,
ObjectManager $objectManager
@ -37,6 +40,7 @@ class PromotionContextSpec extends ObjectBehavior
$this->beConstructedWith(
$sharedStorage,
$actionFactory,
$ruleFactory,
$testPromotionFactory,
$promotionRepository,
$objectManager
@ -103,4 +107,26 @@ class PromotionContextSpec extends ObjectBehavior
$this->itGivesPercentageDiscountToEveryOrder('10');
}
function it_creates_fixed_discount_promotion_for_cart_with_specified_quantity(
$sharedStorage,
$actionFactory,
$ruleFactory,
$objectManager,
ActionInterface $action,
RuleInterface $rule,
PromotionInterface $promotion
) {
$sharedStorage->get('promotion')->willReturn($promotion);
$actionFactory->createFixedDiscount(1000)->willReturn($action);
$promotion->addAction($action)->shouldBeCalled();
$ruleFactory->createCartQuantity(5)->willReturn($rule);
$promotion->addRule($rule)->shouldBeCalled();
$objectManager->flush()->shouldBeCalled();
$this->itGivesFixedDiscountToEveryOrderWithQuantityAtLeast('10.00', '5');
}
}

View file

@ -16,6 +16,7 @@ use PhpSpec\Exception\Example\NotEqualException;
use PhpSpec\ObjectBehavior;
use Sylius\Behat\Context\Ui\CartContext;
use Sylius\Behat\Page\Cart\CartSummaryPage;
use Sylius\Behat\Page\ElementNotFoundException;
use Sylius\Behat\Page\Product\ProductShowPage;
/**
@ -87,4 +88,12 @@ class CartContextSpec extends ObjectBehavior
$this->shouldThrow(NotEqualException::class)->during('myDiscountShouldBe', ['$50.00']);
}
function it_ensures_there_is_no_discount_info_on_the_page(CartSummaryPage $cartSummaryPage)
{
$cartSummaryPage->open()->shouldBeCalled();
$cartSummaryPage->getPromotionTotal()->willThrow(new ElementNotFoundException('"promotion total" element is not present on the page'));
$this->thereShouldBeNoDiscount();
}
}