Skip to content

Commit 9418cef

Browse files
committed
OP-289: Cover adding product bundle to cart with API
1 parent a088750 commit 9418cef

File tree

4 files changed

+140
-2
lines changed

4 files changed

+140
-2
lines changed

features/having_bundled_product_in_store.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,12 @@ Feature: Having a product in store which is a bundle of other products
5555
And I proceed with "Free" shipping method and "Offline" payment
5656
Then my cart total should be "$41.00"
5757
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: 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/Resources/services.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ services:
4444
- '@sylius.repository.product'
4545
- '@bitbag_sylius_product_bundle_plugin.behat.page.summary_page'
4646
- '@bitbag_sylius_product_bundle_plugin.behat.page.account.order_show_page'
47+
48+
bitbag_sylius_product_bundle_plugin.behat.context.api.product_bundle:
49+
class: Tests\BitBag\SyliusProductBundlePlugin\Behat\Context\Api\ProductBundleContext
50+
arguments:
51+
- '@sylius.behat.shared_storage'
52+
- '@sylius.behat.api_platform_client.shop'
53+
- '@sylius.behat.request_factory'
54+
- '@Sylius\Behat\Client\ResponseCheckerInterface'

tests/Behat/Resources/suites.yml

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ default:
3939
- bitbag_sylius_product_bundle_plugin.behat.context.setup.product_bundle
4040

4141
filters:
42-
tags: "@bundled_product&&~@shop"
42+
tags: "@bundled_product&&@ui&&~@shop"
4343

4444
shop_bundled_product:
4545
contexts:
@@ -69,4 +69,42 @@ default:
6969
- bitbag_sylius_product_bundle_plugin.behat.context.setup.product_bundle
7070

7171
filters:
72-
tags: "@bundled_product&&@shop"
72+
tags: "@bundled_product&&@ui&&@shop"
73+
74+
api_bundled_product:
75+
contexts:
76+
- sylius.behat.context.hook.doctrine_orm
77+
78+
- sylius.behat.context.setup.admin_security
79+
- sylius.behat.context.setup.channel
80+
- sylius.behat.context.setup.currency
81+
- sylius.behat.context.setup.customer
82+
- sylius.behat.context.setup.locale
83+
- sylius.behat.context.setup.payment
84+
- sylius.behat.context.setup.product
85+
- sylius.behat.context.setup.promotion
86+
- sylius.behat.context.setup.taxation
87+
- sylius.behat.context.setup.shipping
88+
- sylius.behat.context.setup.shop_security
89+
90+
- sylius.behat.context.transform.address
91+
- sylius.behat.context.transform.channel
92+
- sylius.behat.context.transform.lexical
93+
- sylius.behat.context.transform.locale
94+
- sylius.behat.context.transform.payment
95+
- sylius.behat.context.transform.product
96+
- sylius.behat.context.transform.promotion
97+
- sylius.behat.context.transform.shared_storage
98+
- sylius.behat.context.transform.shipping_method
99+
- sylius.behat.context.transform.tax_category
100+
- sylius.behat.context.transform.zone
101+
102+
- sylius.behat.context.api.shop.cart
103+
- sylius.behat.context.api.shop.checkout
104+
- sylius.behat.context.api.shop.checkout.complete
105+
106+
- bitbag_sylius_product_bundle_plugin.behat.context.api.product_bundle
107+
- bitbag_sylius_product_bundle_plugin.behat.context.setup.product_bundle
108+
109+
filters:
110+
tags: "@bundled_product&&@api"

0 commit comments

Comments
 (0)