Skip to content

Commit

Permalink
OP-289: Add more behat scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
hmfilar committed Sep 6, 2024
1 parent 9ac1c75 commit 44bf4a0
Show file tree
Hide file tree
Showing 12 changed files with 549 additions and 91 deletions.
76 changes: 47 additions & 29 deletions features/creating_bundled_product.feature
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
@bundled_product
Feature: Creating a product in store which is a bundle of other products
I want to be able to add bundled product to cart
As an Administrator
I want to be able to add bundled product to cart

Background:
Given the store operates on a single channel in "United States"
And I am logged in as an administrator
And the store has a product "Jack Daniels Gentleman" priced at "$10.00"
And the store has a product "Johny Walker Black" priced at "$10.00"
And the store has a product "Jim Beam Double Oak" priced at "$10.00"
Background:
Given the store operates on a single channel in "United States"
And I am logged in as an administrator
And the store has a product "Jack Daniels Gentleman" priced at "$10.00"
And the store has a product "Johny Walker Black" priced at "$10.00"
And the store has a product "Jim Beam Double Oak" priced at "$10.00"

@ui @javascript
Scenario: Creating a bundled product
When I want to create a new bundled product
And I specify its code as "WHISKEY_PACK"
And I name it "Whiskey double pack" in "English (United States)"
And I set its slug to "whiskey-double-pack" in "English (United States)"
And I set its price to "$10.00" for "United States" channel
And I set its original price to "$20.00" for "United States" channel
And I add product "Johny Walker Black" and "Jack Daniels Gentleman" to the bundle
And I add it
Then I should be notified that it has been successfully created
@ui @javascript
Scenario: Creating a bundled product
When I want to create a new bundled product
And I specify its code as "WHISKEY_PACK"
And I name it "Whiskey double pack" in "English (United States)"
And I set its slug to "whiskey-double-pack" in "English (United States)"
And I set its price to "$10.00" for "United States" channel
And I set its original price to "$20.00" for "United States" channel
And I add product "Johny Walker Black" and "Jack Daniels Gentleman" to the bundle
And I add it
Then I should be notified that it has been successfully created

@ui @javascript
Scenario: Creating a bundled product with more products
When I want to create a new bundled product
And I specify its code as "WHISKEY_BIG_PACK"
And I name it "Whiskey triple pack" in "English (United States)"
And I set its slug to "whiskey-triple-pack" in "English (United States)"
And I set its price to "$10.00" for "United States" channel
And I set its original price to "$20.00" for "United States" channel
And I add product "Johny Walker Black" and "Jack Daniels Gentleman" and "Jim Beam Double Oak" to the bundle
And I add it
Then I should be notified that it has been successfully created
@ui @javascript
Scenario: Creating a bundled product with more products
When I want to create a new bundled product
And I specify its code as "WHISKEY_BIG_PACK"
And I name it "Whiskey triple pack" in "English (United States)"
And I set its slug to "whiskey-triple-pack" in "English (United States)"
And I set its price to "$10.00" for "United States" channel
And I set its original price to "$20.00" for "United States" channel
And I add product "Johny Walker Black" and "Jack Daniels Gentleman" and "Jim Beam Double Oak" to the bundle
And I add it
Then I should be notified that it has been successfully created
And there should be a "WHISKEY_BIG_PACK" bundle containing "Johny Walker Black" with quantity 1
And there should be a "WHISKEY_BIG_PACK" bundle containing "Jack Daniels Gentleman" with quantity 1
And there should be a "WHISKEY_BIG_PACK" bundle containing "Jim Beam Double Oak" with quantity 1

@ui @javascript
Scenario: Creating a bundled product with higher quantity
When I want to create a new bundled product
And I specify its code as "WHISKEY_BIG_PACK"
And I name it "Whiskey triple pack" in "English (United States)"
And I set its slug to "whiskey-triple-pack" in "English (United States)"
And I set its price to "$10.00" for "United States" channel
And I set its original price to "$20.00" for "United States" channel
And I add product "Jim Beam Double Oak" with quantity 5 and "Jack Daniels Gentleman" with quantity 2 to the bundle
And I add it
Then I should be notified that it has been successfully created
And there should be a "WHISKEY_BIG_PACK" bundle containing "Jim Beam Double Oak" with quantity 5
And there should be a "WHISKEY_BIG_PACK" bundle containing "Jack Daniels Gentleman" with quantity 2
94 changes: 60 additions & 34 deletions features/having_bundled_product_in_store.feature
Original file line number Diff line number Diff line change
@@ -1,40 +1,66 @@
@bundled_product
Feature: Having a product in store which is a bundle of other products
I want to be able to add bundled product to cart
As a Customer
I want to be able to order bundled products

Background:
Given the store operates on a single channel in "United States"
And I am a logged in customer
And the store ships everywhere for Free
And the store allows paying Offline
Background:
Given the store operates on a single channel in "United States"
And the store has "VAT" tax rate of 50% for "Coke" within the "US" zone
And I am a logged in customer
And the store ships everywhere for Free
And the store allows paying Offline
And the store has a product "Jim Beam" priced at "$10.00"
And the store has a product "Jim Beam Double Oak" priced at "$10.00"
And the store has a product "Coca-Cola" priced at "$5.00"
And the store has bundled product "Jim Beam double pack" priced at "$18.00" which contains "Jim Beam" and "Jim Beam Double Oak"
And the store has bundled product "Jim Beam&Coke" priced at "$12.00" which contains "Jim Beam" and "Coca-Cola"
And it belongs to "Coke" tax category
And all store products appear under a main taxonomy

@ui
Scenario: Adding a product bundle to the cart
Given the store has a product "Jack Daniels Gentleman" priced at "$10.00"
And the store has a product "Johny Walker Black" priced at "$10.00"
And the store has bundled product "Whiskey double pack" priced at "$18.00" which contains "Jack Daniels Gentleman" and "Johny Walker Black"
And all store products appear under a main taxonomy
Then I added product "Whiskey double pack" to the cart
And I should be on my cart summary page
And there should be one item in my cart
@ui
Scenario: Adding product bundles to cart
When I added product "Jim Beam double pack" to the cart
And I change product "Jim Beam double pack" quantity to 5 in my cart
Then I should see "Jim Beam double pack" with quantity 5 in my cart
And my cart total should be "$90.00"

@ui
Scenario: Adding a few product bundles to the cart
Given the store has a product "Jim Beam" priced at "$10.00"
And the store has a product "Jim Beam Double Oak" priced at "$10.00"
And the store has bundled product "Jim Beam double pack" priced at "$18.00" which contains "Jim Beam" and "Jim Beam Double Oak"
And all store products appear under a main taxonomy
Then I added product "Jim Beam double pack" to the cart
And I change product "Jim Beam double pack" quantity to 5 in my cart
And I should see "Jim Beam double pack" with quantity 5 in my cart
@ui
Scenario: Placing an order for bundled products
Given I have product "Jim Beam double pack" in the cart
And I have product "Jim Beam&Coke" in the cart
And my cart total should be "$30.00"
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceed with "Free" shipping method and "Offline" payment
When I confirm my order
Then I should see the thank you page

@ui
Scenario: Placing an order for a bundled product
Given the store has a product "Jim Beam" priced at "$10.00"
And the store has a product "Jim Beam Double Oak" priced at "$10.00"
And the store has bundled product "Jim Beam double pack" priced at "$18.00" which contains "Jim Beam" and "Jim Beam Double Oak"
Given I have product "Jim Beam double pack" in the cart
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceed with "Free" shipping method and "Offline" payment
And I confirm my order
Then I should see the thank you page
@ui
Scenario: Placing an order for bundled products with promotion applied
Given there is a promotion "Holiday promotion"
And this promotion gives "$1.00" off on every product with minimum price at "$15.00"
And I have product "Jim Beam double pack" in the cart
And I have product "Jim Beam&Coke" in the cart
And my cart total should be "$29.00"
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceed with "Free" shipping method and "Offline" payment
When I confirm my order
Then I should see the thank you page

@ui
Scenario: Placing an order for bundled products with tax applied
Given I have product "Jim Beam double pack" in the cart
And I have product "Jim Beam&Coke" in the cart
And I have product "Coca-Cola" in the cart
When I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceed with "Free" shipping method and "Offline" payment
Then my cart total should be "$41.00"
And my cart taxes should be "$6.00"

@api
Scenario: Adding product bundles to cart with API
When I pick up my cart
And I add bundle "Jim Beam&Coke" with quantity 5 to my cart
And I add bundle "Jim Beam&Coke" with quantity 5 to my cart
Then I should have bundle "Jim Beam&Coke" with quantity 10 in my cart
And I should have product "Jim Beam" in bundled items
And I should have product "Coca-Cola" in bundled items
37 changes: 37 additions & 0 deletions features/viewing_products_from_ordered_bundle.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@bundled_product
Feature: Reviewing products from ordered bundle
As a Customer
I want to be able to see products in the bundle I ordered

Background:
Given the store operates on a single channel in "United States"
And the store ships everywhere for Free
And the store allows paying Offline
And the store has a product "Jim Beam" priced at "$10.00"
And the store has a product "Coca-Cola" priced at "$5.00"
And the store has bundled product "Jim Beam&Coke" priced at "$12.00" which contains "Jim Beam" and "Coca-Cola"
And all store products appear under a main taxonomy
And I am a logged in customer with name "Bundle Customer"

@ui @shop
Scenario: Viewing bundled products in cart
Given I have product "Jim Beam&Coke" in the cart
When I see the summary of my cart
Then there should be one item in my cart
And this item should have name "Jim Beam&Coke"
And there should be bundled products listed
And the list should contain "Jim Beam"
And the list should contain "Coca-Cola"

@ui @shop
Scenario: Viewing bundled products in order history
Given there is a customer "[email protected]" that placed an order "#1" later
And the customer bought a single bundle "Jim Beam&Coke"
And I addressed it to "Ankh Morpork", "Frost Alley", "90210" "Los Angeles" in the "United States"
And for the billing address of "Ankh Morpork" in the "Frost Alley", "90210" "Los Angeles", "United States"
And I chose "Free" shipping method with "Offline" payment
When I view the summary of my order "#1"
Then it should have the number "#1"
And there should be bundled products listed in order details
And the list should contain "Jim Beam" in order details
And the list should contain "Coca-Cola" in order details
83 changes: 83 additions & 0 deletions tests/Behat/Context/Api/ProductBundleContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace Tests\BitBag\SyliusProductBundlePlugin\Behat\Context\Api;

use Behat\Behat\Context\Context;
use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\RequestFactoryInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Api\Resources;
use Sylius\Behat\Service\SharedStorageInterface;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Webmozart\Assert\Assert;

final class ProductBundleContext implements Context
{
public function __construct(
private readonly SharedStorageInterface $sharedStorage,
private readonly ApiClientInterface $client,
private readonly RequestFactoryInterface $requestFactory,
private readonly ResponseCheckerInterface $responseChecker,
) {
}

/**
* @When I add bundle :product to my cart
* @When I add bundle :product with quantity :quantity to my cart
*/
public function iAddProductBundleToMyCart(ProductInterface $product, int $quantity = 1): void
{
$request = $this->requestFactory->customItemAction(
'shop',
Resources::ORDERS,
$this->sharedStorage->get('cart_token'),
HttpRequest::METHOD_PATCH,
'product-bundle',
);
$request->updateContent([
'productCode' => $product->getCode(),
'quantity' => $quantity,
]);

$this->client->executeCustomRequest($request);
}

/**
* @When I should have bundle :product with quantity :quantity in my cart
*/
public function iShouldHaveBundleWithQuantityInMyCart(ProductInterface $product, int $quantity): void
{
$response = $this->client->show(Resources::ORDERS, $this->sharedStorage->get('cart_token'));

$item = $this->responseChecker->getValue($response, 'items')[0];
Assert::eq($item['productName'], $product->getName());
Assert::eq($item['quantity'], $quantity);
}

/**
* @When I should have product :product in bundled items
*/
public function iShouldHaveProductInBundledItems(ProductInterface $product): void
{
$response = $this->client->show(Resources::ORDERS, $this->sharedStorage->get('cart_token'));

$productBundleOrderItems = $this->responseChecker->getValue($response, 'items')[0]['productBundleOrderItems'];
foreach ($productBundleOrderItems as $item) {
if ($item['productVariant']['code'] === $product->getCode()) {
return;
}
}

throw new \InvalidArgumentException('Product not found in bundled items');
}
}
53 changes: 43 additions & 10 deletions tests/Behat/Context/Setup/ProductBundleContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,43 @@
use Behat\Behat\Context\Context;
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface;
use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
use BitBag\SyliusProductBundlePlugin\Factory\OrderItemFactoryInterface;
use BitBag\SyliusProductBundlePlugin\Factory\ProductBundleOrderItemFactoryInterface;
use BitBag\SyliusProductBundlePlugin\Factory\ProductFactory;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ProductTaxonInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Sylius\Component\Order\Modifier\OrderModifierInterface;
use Sylius\Component\Product\Generator\SlugGeneratorInterface;
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class ProductBundleContext implements Context
{
public function __construct(
private SharedStorageInterface $sharedStorage,
private FactoryInterface $taxonFactory,
private ProductRepositoryInterface $productRepository,
private FactoryInterface $productTaxonFactory,
private EntityManagerInterface $productTaxonManager,
private ProductFactory $productFactory,
private FactoryInterface $productBundleItemFactory,
private FactoryInterface $channelPricingFactory,
private ProductVariantResolverInterface $productVariantResolver,
private SlugGeneratorInterface $slugGenerator,
private readonly SharedStorageInterface $sharedStorage,
private readonly FactoryInterface $taxonFactory,
private readonly ProductRepositoryInterface $productRepository,
private readonly FactoryInterface $productTaxonFactory,
private readonly EntityManagerInterface $productTaxonManager,
private readonly ProductFactory $productFactory,
private readonly FactoryInterface $productBundleItemFactory,
private readonly FactoryInterface $channelPricingFactory,
private readonly ProductVariantResolverInterface $productVariantResolver,
private readonly SlugGeneratorInterface $slugGenerator,
private readonly EntityManagerInterface $objectManager,
private readonly OrderItemQuantityModifierInterface $orderItemQuantityModifier,
private readonly ProductBundleOrderItemFactoryInterface $productBundleOrderItemFactory,
private readonly OrderModifierInterface $orderModifier,
private readonly OrderItemFactoryInterface $cartItemFactory,
) {
}

Expand Down Expand Up @@ -149,4 +159,27 @@ private function createProduct(

return $product;
}

/**
* @When the customer bought a single bundle :product
*/
public function theCustomerBoughtBundle(ProductInterface $product): void
{
/** @var OrderInterface|null $cart */
$cart = $this->sharedStorage->get('order');
/** @var ProductVariantInterface|null $variant */
$variant = $product->getVariants()->first();

$cartItem = $this->cartItemFactory->createWithVariant($variant);
$this->orderItemQuantityModifier->modify($cartItem, 1);

foreach ($product->getProductBundle()->getProductBundleItems() as $bundleItem) {
$productBundleOrderItem = $this->productBundleOrderItemFactory->createFromProductBundleItem($bundleItem);
$cartItem->addProductBundleOrderItem($productBundleOrderItem);
}

$this->orderModifier->addToOrder($cart, $cartItem);

$this->objectManager->flush();
}
}
Loading

0 comments on commit 44bf4a0

Please sign in to comment.