Skip to content

Commit 44bf4a0

Browse files
committed
OP-289: Add more behat scenarios
1 parent 9ac1c75 commit 44bf4a0

12 files changed

+549
-91
lines changed
Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
11
@bundled_product
22
Feature: Creating a product in store which is a bundle of other products
3-
I want to be able to add bundled product to cart
3+
As an Administrator
4+
I want to be able to add bundled product to cart
45

5-
Background:
6-
Given the store operates on a single channel in "United States"
7-
And I am logged in as an administrator
8-
And the store has a product "Jack Daniels Gentleman" priced at "$10.00"
9-
And the store has a product "Johny Walker Black" priced at "$10.00"
10-
And the store has a product "Jim Beam Double Oak" priced at "$10.00"
6+
Background:
7+
Given the store operates on a single channel in "United States"
8+
And I am logged in as an administrator
9+
And the store has a product "Jack Daniels Gentleman" priced at "$10.00"
10+
And the store has a product "Johny Walker Black" priced at "$10.00"
11+
And the store has a product "Jim Beam Double Oak" priced at "$10.00"
1112

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

24-
@ui @javascript
25-
Scenario: Creating a bundled product with more products
26-
When I want to create a new bundled product
27-
And I specify its code as "WHISKEY_BIG_PACK"
28-
And I name it "Whiskey triple pack" in "English (United States)"
29-
And I set its slug to "whiskey-triple-pack" in "English (United States)"
30-
And I set its price to "$10.00" for "United States" channel
31-
And I set its original price to "$20.00" for "United States" channel
32-
And I add product "Johny Walker Black" and "Jack Daniels Gentleman" and "Jim Beam Double Oak" to the bundle
33-
And I add it
34-
Then I should be notified that it has been successfully created
25+
@ui @javascript
26+
Scenario: Creating a bundled product with more products
27+
When I want to create a new bundled product
28+
And I specify its code as "WHISKEY_BIG_PACK"
29+
And I name it "Whiskey triple pack" in "English (United States)"
30+
And I set its slug to "whiskey-triple-pack" in "English (United States)"
31+
And I set its price to "$10.00" for "United States" channel
32+
And I set its original price to "$20.00" for "United States" channel
33+
And I add product "Johny Walker Black" and "Jack Daniels Gentleman" and "Jim Beam Double Oak" to the bundle
34+
And I add it
35+
Then I should be notified that it has been successfully created
36+
And there should be a "WHISKEY_BIG_PACK" bundle containing "Johny Walker Black" with quantity 1
37+
And there should be a "WHISKEY_BIG_PACK" bundle containing "Jack Daniels Gentleman" with quantity 1
38+
And there should be a "WHISKEY_BIG_PACK" bundle containing "Jim Beam Double Oak" with quantity 1
39+
40+
@ui @javascript
41+
Scenario: Creating a bundled product with higher quantity
42+
When I want to create a new bundled product
43+
And I specify its code as "WHISKEY_BIG_PACK"
44+
And I name it "Whiskey triple pack" in "English (United States)"
45+
And I set its slug to "whiskey-triple-pack" in "English (United States)"
46+
And I set its price to "$10.00" for "United States" channel
47+
And I set its original price to "$20.00" for "United States" channel
48+
And I add product "Jim Beam Double Oak" with quantity 5 and "Jack Daniels Gentleman" with quantity 2 to the bundle
49+
And I add it
50+
Then I should be notified that it has been successfully created
51+
And there should be a "WHISKEY_BIG_PACK" bundle containing "Jim Beam Double Oak" with quantity 5
52+
And there should be a "WHISKEY_BIG_PACK" bundle containing "Jack Daniels Gentleman" with quantity 2
Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,66 @@
11
@bundled_product
22
Feature: Having a product in store which is a bundle of other products
3-
I want to be able to add bundled product to cart
3+
As a Customer
4+
I want to be able to order bundled products
45

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

11-
@ui
12-
Scenario: Adding a product bundle to the cart
13-
Given the store has a product "Jack Daniels Gentleman" priced at "$10.00"
14-
And the store has a product "Johny Walker Black" priced at "$10.00"
15-
And the store has bundled product "Whiskey double pack" priced at "$18.00" which contains "Jack Daniels Gentleman" and "Johny Walker Black"
16-
And all store products appear under a main taxonomy
17-
Then I added product "Whiskey double pack" to the cart
18-
And I should be on my cart summary page
19-
And there should be one item in my cart
20+
@ui
21+
Scenario: Adding product bundles to cart
22+
When I added product "Jim Beam double pack" to the cart
23+
And I change product "Jim Beam double pack" quantity to 5 in my cart
24+
Then I should see "Jim Beam double pack" with quantity 5 in my cart
25+
And my cart total should be "$90.00"
2026

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

31-
@ui
32-
Scenario: Placing an order for a bundled product
33-
Given the store has a product "Jim Beam" priced at "$10.00"
34-
And the store has a product "Jim Beam Double Oak" priced at "$10.00"
35-
And the store has bundled product "Jim Beam double pack" priced at "$18.00" which contains "Jim Beam" and "Jim Beam Double Oak"
36-
Given I have product "Jim Beam double pack" in the cart
37-
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
38-
And I proceed with "Free" shipping method and "Offline" payment
39-
And I confirm my order
40-
Then I should see the thank you page
37+
@ui
38+
Scenario: Placing an order for bundled products with promotion applied
39+
Given there is a promotion "Holiday promotion"
40+
And this promotion gives "$1.00" off on every product with minimum price at "$15.00"
41+
And I have product "Jim Beam double pack" in the cart
42+
And I have product "Jim Beam&Coke" in the cart
43+
And my cart total should be "$29.00"
44+
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
45+
And I proceed with "Free" shipping method and "Offline" payment
46+
When I confirm my order
47+
Then I should see the thank you page
48+
49+
@ui
50+
Scenario: Placing an order for bundled products with tax applied
51+
Given I have product "Jim Beam double pack" in the cart
52+
And I have product "Jim Beam&Coke" in the cart
53+
And I have product "Coca-Cola" in the cart
54+
When I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
55+
And I proceed with "Free" shipping method and "Offline" payment
56+
Then my cart total should be "$41.00"
57+
And my cart taxes should be "$6.00"
58+
59+
@api
60+
Scenario: Adding product bundles to cart with API
61+
When I pick up my cart
62+
And I add bundle "Jim Beam&Coke" with quantity 5 to my cart
63+
And I add bundle "Jim Beam&Coke" with quantity 5 to my cart
64+
Then I should have bundle "Jim Beam&Coke" with quantity 10 in my cart
65+
And I should have product "Jim Beam" in bundled items
66+
And I should have product "Coca-Cola" in bundled items
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@bundled_product
2+
Feature: Reviewing products from ordered bundle
3+
As a Customer
4+
I want to be able to see products in the bundle I ordered
5+
6+
Background:
7+
Given the store operates on a single channel in "United States"
8+
And the store ships everywhere for Free
9+
And the store allows paying Offline
10+
And the store has a product "Jim Beam" priced at "$10.00"
11+
And the store has a product "Coca-Cola" priced at "$5.00"
12+
And the store has bundled product "Jim Beam&Coke" priced at "$12.00" which contains "Jim Beam" and "Coca-Cola"
13+
And all store products appear under a main taxonomy
14+
And I am a logged in customer with name "Bundle Customer"
15+
16+
@ui @shop
17+
Scenario: Viewing bundled products in cart
18+
Given I have product "Jim Beam&Coke" in the cart
19+
When I see the summary of my cart
20+
Then there should be one item in my cart
21+
And this item should have name "Jim Beam&Coke"
22+
And there should be bundled products listed
23+
And the list should contain "Jim Beam"
24+
And the list should contain "Coca-Cola"
25+
26+
@ui @shop
27+
Scenario: Viewing bundled products in order history
28+
Given there is a customer "sylius@example.com" that placed an order "#1" later
29+
And the customer bought a single bundle "Jim Beam&Coke"
30+
And I addressed it to "Ankh Morpork", "Frost Alley", "90210" "Los Angeles" in the "United States"
31+
And for the billing address of "Ankh Morpork" in the "Frost Alley", "90210" "Los Angeles", "United States"
32+
And I chose "Free" shipping method with "Offline" payment
33+
When I view the summary of my order "#1"
34+
Then it should have the number "#1"
35+
And there should be bundled products listed in order details
36+
And the list should contain "Jim Beam" in order details
37+
And the list should contain "Coca-Cola" in order details
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Tests\BitBag\SyliusProductBundlePlugin\Behat\Context\Api;
13+
14+
use Behat\Behat\Context\Context;
15+
use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
16+
use Sylius\Behat\Client\ApiClientInterface;
17+
use Sylius\Behat\Client\RequestFactoryInterface;
18+
use Sylius\Behat\Client\ResponseCheckerInterface;
19+
use Sylius\Behat\Context\Api\Resources;
20+
use Sylius\Behat\Service\SharedStorageInterface;
21+
use Symfony\Component\HttpFoundation\Request as HttpRequest;
22+
use Webmozart\Assert\Assert;
23+
24+
final class ProductBundleContext implements Context
25+
{
26+
public function __construct(
27+
private readonly SharedStorageInterface $sharedStorage,
28+
private readonly ApiClientInterface $client,
29+
private readonly RequestFactoryInterface $requestFactory,
30+
private readonly ResponseCheckerInterface $responseChecker,
31+
) {
32+
}
33+
34+
/**
35+
* @When I add bundle :product to my cart
36+
* @When I add bundle :product with quantity :quantity to my cart
37+
*/
38+
public function iAddProductBundleToMyCart(ProductInterface $product, int $quantity = 1): void
39+
{
40+
$request = $this->requestFactory->customItemAction(
41+
'shop',
42+
Resources::ORDERS,
43+
$this->sharedStorage->get('cart_token'),
44+
HttpRequest::METHOD_PATCH,
45+
'product-bundle',
46+
);
47+
$request->updateContent([
48+
'productCode' => $product->getCode(),
49+
'quantity' => $quantity,
50+
]);
51+
52+
$this->client->executeCustomRequest($request);
53+
}
54+
55+
/**
56+
* @When I should have bundle :product with quantity :quantity in my cart
57+
*/
58+
public function iShouldHaveBundleWithQuantityInMyCart(ProductInterface $product, int $quantity): void
59+
{
60+
$response = $this->client->show(Resources::ORDERS, $this->sharedStorage->get('cart_token'));
61+
62+
$item = $this->responseChecker->getValue($response, 'items')[0];
63+
Assert::eq($item['productName'], $product->getName());
64+
Assert::eq($item['quantity'], $quantity);
65+
}
66+
67+
/**
68+
* @When I should have product :product in bundled items
69+
*/
70+
public function iShouldHaveProductInBundledItems(ProductInterface $product): void
71+
{
72+
$response = $this->client->show(Resources::ORDERS, $this->sharedStorage->get('cart_token'));
73+
74+
$productBundleOrderItems = $this->responseChecker->getValue($response, 'items')[0]['productBundleOrderItems'];
75+
foreach ($productBundleOrderItems as $item) {
76+
if ($item['productVariant']['code'] === $product->getCode()) {
77+
return;
78+
}
79+
}
80+
81+
throw new \InvalidArgumentException('Product not found in bundled items');
82+
}
83+
}

tests/Behat/Context/Setup/ProductBundleContext.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,43 @@
1414
use Behat\Behat\Context\Context;
1515
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface;
1616
use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
17+
use BitBag\SyliusProductBundlePlugin\Factory\OrderItemFactoryInterface;
18+
use BitBag\SyliusProductBundlePlugin\Factory\ProductBundleOrderItemFactoryInterface;
1719
use BitBag\SyliusProductBundlePlugin\Factory\ProductFactory;
1820
use Doctrine\ORM\EntityManagerInterface;
1921
use Sylius\Behat\Service\SharedStorageInterface;
2022
use Sylius\Component\Core\Formatter\StringInflector;
2123
use Sylius\Component\Core\Model\ChannelInterface;
2224
use Sylius\Component\Core\Model\ChannelPricingInterface;
25+
use Sylius\Component\Core\Model\OrderInterface;
2326
use Sylius\Component\Core\Model\ProductTaxonInterface;
2427
use Sylius\Component\Core\Model\ProductVariantInterface;
2528
use Sylius\Component\Core\Model\TaxonInterface;
2629
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
30+
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
31+
use Sylius\Component\Order\Modifier\OrderModifierInterface;
2732
use Sylius\Component\Product\Generator\SlugGeneratorInterface;
2833
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
2934
use Sylius\Component\Resource\Factory\FactoryInterface;
3035

3136
final class ProductBundleContext implements Context
3237
{
3338
public function __construct(
34-
private SharedStorageInterface $sharedStorage,
35-
private FactoryInterface $taxonFactory,
36-
private ProductRepositoryInterface $productRepository,
37-
private FactoryInterface $productTaxonFactory,
38-
private EntityManagerInterface $productTaxonManager,
39-
private ProductFactory $productFactory,
40-
private FactoryInterface $productBundleItemFactory,
41-
private FactoryInterface $channelPricingFactory,
42-
private ProductVariantResolverInterface $productVariantResolver,
43-
private SlugGeneratorInterface $slugGenerator,
39+
private readonly SharedStorageInterface $sharedStorage,
40+
private readonly FactoryInterface $taxonFactory,
41+
private readonly ProductRepositoryInterface $productRepository,
42+
private readonly FactoryInterface $productTaxonFactory,
43+
private readonly EntityManagerInterface $productTaxonManager,
44+
private readonly ProductFactory $productFactory,
45+
private readonly FactoryInterface $productBundleItemFactory,
46+
private readonly FactoryInterface $channelPricingFactory,
47+
private readonly ProductVariantResolverInterface $productVariantResolver,
48+
private readonly SlugGeneratorInterface $slugGenerator,
49+
private readonly EntityManagerInterface $objectManager,
50+
private readonly OrderItemQuantityModifierInterface $orderItemQuantityModifier,
51+
private readonly ProductBundleOrderItemFactoryInterface $productBundleOrderItemFactory,
52+
private readonly OrderModifierInterface $orderModifier,
53+
private readonly OrderItemFactoryInterface $cartItemFactory,
4454
) {
4555
}
4656

@@ -149,4 +159,27 @@ private function createProduct(
149159

150160
return $product;
151161
}
162+
163+
/**
164+
* @When the customer bought a single bundle :product
165+
*/
166+
public function theCustomerBoughtBundle(ProductInterface $product): void
167+
{
168+
/** @var OrderInterface|null $cart */
169+
$cart = $this->sharedStorage->get('order');
170+
/** @var ProductVariantInterface|null $variant */
171+
$variant = $product->getVariants()->first();
172+
173+
$cartItem = $this->cartItemFactory->createWithVariant($variant);
174+
$this->orderItemQuantityModifier->modify($cartItem, 1);
175+
176+
foreach ($product->getProductBundle()->getProductBundleItems() as $bundleItem) {
177+
$productBundleOrderItem = $this->productBundleOrderItemFactory->createFromProductBundleItem($bundleItem);
178+
$cartItem->addProductBundleOrderItem($productBundleOrderItem);
179+
}
180+
181+
$this->orderModifier->addToOrder($cart, $cartItem);
182+
183+
$this->objectManager->flush();
184+
}
152185
}

0 commit comments

Comments
 (0)