Skip to content

Commit

Permalink
OP-289: Cover adding product bundle to cart with API
Browse files Browse the repository at this point in the history
  • Loading branch information
hmfilar committed Sep 6, 2024
1 parent a088750 commit 9418cef
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 2 deletions.
9 changes: 9 additions & 0 deletions features/having_bundled_product_in_store.feature
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,12 @@ Feature: Having a product in store which is a bundle of other products
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
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');
}
}
8 changes: 8 additions & 0 deletions tests/Behat/Resources/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ services:
- '@sylius.repository.product'
- '@bitbag_sylius_product_bundle_plugin.behat.page.summary_page'
- '@bitbag_sylius_product_bundle_plugin.behat.page.account.order_show_page'

bitbag_sylius_product_bundle_plugin.behat.context.api.product_bundle:
class: Tests\BitBag\SyliusProductBundlePlugin\Behat\Context\Api\ProductBundleContext
arguments:
- '@sylius.behat.shared_storage'
- '@sylius.behat.api_platform_client.shop'
- '@sylius.behat.request_factory'
- '@Sylius\Behat\Client\ResponseCheckerInterface'
42 changes: 40 additions & 2 deletions tests/Behat/Resources/suites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ default:
- bitbag_sylius_product_bundle_plugin.behat.context.setup.product_bundle

filters:
tags: "@bundled_product&&~@shop"
tags: "@bundled_product&&@ui&&~@shop"

shop_bundled_product:
contexts:
Expand Down Expand Up @@ -69,4 +69,42 @@ default:
- bitbag_sylius_product_bundle_plugin.behat.context.setup.product_bundle

filters:
tags: "@bundled_product&&@shop"
tags: "@bundled_product&&@ui&&@shop"

api_bundled_product:
contexts:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.setup.admin_security
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.currency
- sylius.behat.context.setup.customer
- sylius.behat.context.setup.locale
- sylius.behat.context.setup.payment
- sylius.behat.context.setup.product
- sylius.behat.context.setup.promotion
- sylius.behat.context.setup.taxation
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.shop_security

- sylius.behat.context.transform.address
- sylius.behat.context.transform.channel
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.locale
- sylius.behat.context.transform.payment
- sylius.behat.context.transform.product
- sylius.behat.context.transform.promotion
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.shipping_method
- sylius.behat.context.transform.tax_category
- sylius.behat.context.transform.zone

- sylius.behat.context.api.shop.cart
- sylius.behat.context.api.shop.checkout
- sylius.behat.context.api.shop.checkout.complete

- bitbag_sylius_product_bundle_plugin.behat.context.api.product_bundle
- bitbag_sylius_product_bundle_plugin.behat.context.setup.product_bundle

filters:
tags: "@bundled_product&&@api"

0 comments on commit 9418cef

Please sign in to comment.