From 5c28552c5fa14446ed9f2efce5ce7e2b278dc64a Mon Sep 17 00:00:00 2001 From: Tomanhez Date: Tue, 24 Nov 2020 10:54:57 +0100 Subject: [PATCH] Add api tags and behat implementation --- ...om_promotion_with_multiple_actions.feature | 4 +- .../Behat/Context/Api/Shop/CartContext.php | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/features/promotion/receiving_discount/receiving_discount_from_promotion_with_multiple_actions.feature b/features/promotion/receiving_discount/receiving_discount_from_promotion_with_multiple_actions.feature index 32de40247f51..44ae52648c2b 100644 --- a/features/promotion/receiving_discount/receiving_discount_from_promotion_with_multiple_actions.feature +++ b/features/promotion/receiving_discount/receiving_discount_from_promotion_with_multiple_actions.feature @@ -13,7 +13,7 @@ Feature: Receiving discount from a promotion with multiple actions And it belongs to "Mugs" And there is a promotion "Christmas promotion" - @ui + @ui @api Scenario: Receiving fixed discounts only on items that fit action filters Given this promotion gives "$10.00" off on every product with minimum price at "$50.00" And this promotion gives another "$5.00" off on every product classified as "T-Shirts" @@ -23,7 +23,7 @@ Feature: Receiving discount from a promotion with multiple actions And product "PHP Mug" price should not be decreased And my cart total should be "$105.00" - @ui + @ui @api Scenario: Receiving percentage discounts only on items that fit action filters Given this promotion gives "20%" off on every product priced between "$30.00" and "$150.00" And this promotion gives another "10%" off every product classified as "T-Shirts" diff --git a/src/Sylius/Behat/Context/Api/Shop/CartContext.php b/src/Sylius/Behat/Context/Api/Shop/CartContext.php index 10ca0e8d3338..6440f4461c13 100644 --- a/src/Sylius/Behat/Context/Api/Shop/CartContext.php +++ b/src/Sylius/Behat/Context/Api/Shop/CartContext.php @@ -19,6 +19,7 @@ use Sylius\Behat\Client\ResponseCheckerInterface; use Sylius\Behat\Service\SharedStorageInterface; use Sylius\Behat\Service\SprintfResponseEscaper; +use Sylius\Component\Core\Model\ChannelPricingInterface; use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\ProductVariantInterface; use Sylius\Component\Product\Resolver\ProductVariantResolverInterface; @@ -311,6 +312,26 @@ public function thisItemShouldHaveCode(array $item, string $variantCode): void ); } + /** + * @Then /^(product "[^"]+") price should be decreased by ("[^"]+")$/ + */ + public function itsPriceShouldBeDecreasedBy(ProductInterface $product, int $amount): void + { + $this->compareItemSubtotal($product, function (ChannelPricingInterface $channelPricing) use ($amount) { + return $channelPricing->getPrice() - $amount; + }); + } + + /** + * @Then product :product price should not be decreased + */ + public function productPriceShouldNotBeDecreased(ProductInterface $product): void + { + $this->compareItemSubtotal($product, function (ChannelPricingInterface $channelPricing) { + return $channelPricing->getPrice(); + }); + } + /** * @Then I should see :productName with quantity :quantity in my cart * @Then /^the (?:customer|visitor) should see product "([^"]+)" with quantity (\d+) in his cart$/ @@ -541,4 +562,22 @@ private function checkProductQuantity( } } } + + private function compareItemSubtotal(ProductInterface $product, \Closure $price): void + { + /** @var ChannelPricingInterface $pricing */ + $pricing = $product->getVariants()->get(0)->getChannelPricingForChannel($this->sharedStorage->get('channel')); + + $items = $this->responseChecker->getValue($this->cartsClient->getLastResponse(), 'items'); + + foreach ($items as $item) { + if ($item['productName'] === $product->getName()) { + Assert::same($price($pricing), $item['subtotal']); + } + + return; + } + + throw new \InvalidArgumentException('Expected product does not exist'); + } }