Skip to content

Commit

Permalink
Add api tags and behat implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed Nov 25, 2020
1 parent f25e2f9 commit 5c28552
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
39 changes: 39 additions & 0 deletions src/Sylius/Behat/Context/Api/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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$/
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 5c28552

Please sign in to comment.