|
| 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 | +} |
0 commit comments