From 9418cef5a4850b307e83a237154a1746989ac99f Mon Sep 17 00:00:00 2001 From: Hubert Filar Date: Fri, 6 Sep 2024 14:26:17 +0200 Subject: [PATCH] OP-289: Cover adding product bundle to cart with API --- .../having_bundled_product_in_store.feature | 9 ++ .../Context/Api/ProductBundleContext.php | 83 +++++++++++++++++++ tests/Behat/Resources/services.yml | 8 ++ tests/Behat/Resources/suites.yml | 42 +++++++++- 4 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 tests/Behat/Context/Api/ProductBundleContext.php diff --git a/features/having_bundled_product_in_store.feature b/features/having_bundled_product_in_store.feature index 9b2ee00d..bc75045f 100644 --- a/features/having_bundled_product_in_store.feature +++ b/features/having_bundled_product_in_store.feature @@ -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 diff --git a/tests/Behat/Context/Api/ProductBundleContext.php b/tests/Behat/Context/Api/ProductBundleContext.php new file mode 100644 index 00000000..fc9182c9 --- /dev/null +++ b/tests/Behat/Context/Api/ProductBundleContext.php @@ -0,0 +1,83 @@ +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'); + } +} diff --git a/tests/Behat/Resources/services.yml b/tests/Behat/Resources/services.yml index 5aad58db..db9940d3 100644 --- a/tests/Behat/Resources/services.yml +++ b/tests/Behat/Resources/services.yml @@ -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' diff --git a/tests/Behat/Resources/suites.yml b/tests/Behat/Resources/suites.yml index 1e5e44b7..1b818d96 100644 --- a/tests/Behat/Resources/suites.yml +++ b/tests/Behat/Resources/suites.yml @@ -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: @@ -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"