Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Feature: Changing quantity of a product in cart
And the store has a product "T-shirt banana" priced at "$12.54"
And I add this product to the cart

@ui
@ui @api
Scenario: Increasing quantity of an item in cart
Given I see the summary of my cart
When I change "T-shirt banana" quantity to 2
When I change product "T-shirt banana" quantity to 2 in my cart
Then I should see "T-shirt banana" with quantity 2 in my cart

@ui
@ui @api
Scenario: Increasing quantity of an item in cart beyond the threshold
Given I see the summary of my cart
When I change "T-shirt banana" quantity to 20000
When I change product "T-shirt banana" quantity to 20000 in my cart
Then I should see "T-shirt banana" with quantity 9999 in my cart
51 changes: 40 additions & 11 deletions src/Sylius/Behat/Context/Api/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,22 @@ public function iAddOfThemToMyCart(int $quantity, ProductInterface $product, str
$this->putProductToCart($product, $tokenValue, $quantity);
}

/**
* @When /^I change (product "[^"]+") quantity to (\d+) in my (cart)$/
*/
public function iChangeQuantityToInMyCart(ProductInterface $product, int $quantity, string $tokenValue): void
{
$itemId = $this->geOrderItemIdForProductInCart($product, $tokenValue);
$this->changeQuantityOfOrderItem($itemId, $quantity, $tokenValue);
}

/**
* @When /^I remove (product "[^"]+") from the (cart)$/
*/
public function iRemoveProductFromTheCart(ProductInterface $product, string $tokenValue): void
{
$items = $this->responseChecker->getValue($this->cartsClient->show($tokenValue), 'items');

foreach ($items as $item) {
$pathElements = explode('/', $item['variant']['product']);

$productCode = $pathElements[array_key_last($pathElements)];

if ($product->getCode() === $productCode) {
$this->removeOrderItemFromCart((string) $item['id'], $tokenValue);
}
}
$itemId = $this->geOrderItemIdForProductInCart($product, $tokenValue);
$this->removeOrderItemFromCart($itemId, $tokenValue);
}

/**
Expand Down Expand Up @@ -244,4 +244,33 @@ private function getProductForItem(array $item): Response

return $this->productsClient->show(StringInflector::nameToSlug($productCode));
}

private function getOrderItemProductCode(array $item): string
{
$pathElements = explode('/', $item['variant']['product']);

return $pathElements[array_key_last($pathElements)];
}

private function geOrderItemIdForProductInCart(ProductInterface $product, string $tokenValue): ?string
{
$items = $this->responseChecker->getValue($this->cartsClient->show($tokenValue), 'items');

foreach ($items as $item) {
if ($product->getCode() === $this->getOrderItemProductCode($item)) {
return (string) $item['id'];
}
}

return null;
}

private function changeQuantityOfOrderItem(string $orderItemId, int $quantity, string $tokenValue): void
{
$request = Request::customItemAction('orders', $tokenValue, HttpRequest::METHOD_PATCH, 'change-quantity');

$request->updateContent(['orderItemId' => $orderItemId, 'newQuantity' => $quantity]);

$this->cartsClient->executeCustomRequest($request);
}
}
1 change: 1 addition & 0 deletions src/Sylius/Behat/Context/Ui/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function iRemoveProductFromTheCart(string $productName): void

/**
* @Given I change :productName quantity to :quantity
* @Given I change product :productName quantity to :quantity in my cart
*/
public function iChangeQuantityTo($productName, $quantity)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Command\Cart;

use Sylius\Bundle\ApiBundle\Command\OrderTokenValueAwareInterface;

final class ChangeItemQuantityInCart implements OrderTokenValueAwareInterface
{
/** @var string|null */
public $orderTokenValue;

/**
* @var string
* @psalm-immutable
*/
public $orderItemId;

/**
* @var int
* @psalm-immutable
*/
public $newQuantity;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public $newQuantity;
public $quantity;

Should be enough

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i chose newQuantity because this name is visible in the request body, and IMO newQuantity is more descriptive than just quantity


public function __construct(string $orderItemId, int $newQuantity)
{
$this->orderItemId = $orderItemId;
$this->newQuantity = $newQuantity;
}

public static function createFromData(string $tokenValue, string $orderItemId, int $newQuantity): self
{
$command = new self($orderItemId, $newQuantity);

$command->orderTokenValue = $tokenValue;

return $command;
}

public function getOrderTokenValue(): ?string
{
return $this->orderTokenValue;
}

public function setOrderTokenValue(?string $orderTokenValue): void
{
$this->orderTokenValue = $orderTokenValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\CommandHandler\Cart;

use Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
use Sylius\Component\Order\Repository\OrderItemRepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Webmozart\Assert\Assert;

final class ChangeItemQuantityInCartHandler implements MessageHandlerInterface
{
/** @var OrderItemRepositoryInterface */
private $orderItemRepository;

/** @var OrderItemQuantityModifierInterface */
private $orderItemQuantityModifier;

/** @var OrderProcessorInterface */
private $orderProcessor;

public function __construct(
OrderItemRepositoryInterface $orderItemRepository,
OrderItemQuantityModifierInterface $orderItemQuantityModifier,
OrderProcessorInterface $orderProcessor
) {
$this->orderItemRepository = $orderItemRepository;
$this->orderItemQuantityModifier = $orderItemQuantityModifier;
$this->orderProcessor = $orderProcessor;
}

public function __invoke(ChangeItemQuantityInCart $command): OrderInterface
{
/** @var OrderItemInterface|null $orderItem */
$orderItem = $this->orderItemRepository->findOneByIdAndCartTokenValue(
$command->orderItemId,
$command->orderTokenValue
);

Assert::notNull($orderItem);

/** @var OrderInterface $cart */
$cart = $orderItem->getOrder();

Assert::same($cart->getTokenValue(), $command->orderTokenValue);

$this->orderItemQuantityModifier->modify($orderItem, $command->newQuantity);
$this->orderProcessor->process($cart);

return $cart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@
<attribute name="groups">cart:remove_item</attribute>
</attribute>
</itemOperation>

<itemOperation name="change_quantity">
<attribute name="security">is_granted('IS_AUTHENTICATED_ANONYMOUSLY')</attribute>
<attribute name="openapi_context">
<attribute name="summary">Change quantity of order item</attribute>
</attribute>
<attribute name="method">PATCH</attribute>
<attribute name="path">/orders/{id}/change-quantity</attribute>
<attribute name="messenger">input</attribute>
<attribute name="input">Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart</attribute>
<attribute name="denormalization_context">
<attribute name="groups">cart:change_quantity</attribute>
</attribute>
</itemOperation>
</itemOperations>

<property name="id" identifier="false" writable="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" ?>

<!--

This file is part of the Sylius package.

(c) Paweł Jędrzejewski

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

-->

<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart">
<attribute name="orderItemId">
<group>cart:change_quantity</group>
</attribute>

<attribute name="newQuantity">
<group>cart:change_quantity</group>
</attribute>
</class>
</serializer>
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,12 @@
<argument type="service" id="sm.factory" />
<tag name="messenger.message_handler" />
</service>

<service id="Sylius\Bundle\ApiBundle\CommandHandler\Cart\ChangeItemQuantityInCartHandler">
<argument type="service" id="sylius.repository.order_item" />
<argument type="service" id="sylius.order_item_quantity_modifier" />
<argument type="service" id="sylius.order_processing.order_processor" />
<tag name="messenger.message_handler" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\ApiBundle\CommandHandler\Cart;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart;
use Sylius\Bundle\OrderBundle\Doctrine\ORM\OrderItemRepository;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
use Sylius\Component\Order\Repository\OrderItemRepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class ChangeItemQuantityInCartHandlerSpec extends ObjectBehavior
{
function let(
OrderItemRepositoryInterface $orderItemRepository,
OrderItemQuantityModifierInterface $orderItemQuantityModifier,
OrderProcessorInterface $orderProcessor
) {
$this->beConstructedWith($orderItemRepository, $orderItemQuantityModifier, $orderProcessor);
}

function it_is_a_message_handler(): void
{
$this->shouldImplement(MessageHandlerInterface::class);
}

function it_changes_order_item_quantity(
OrderItemRepositoryInterface $orderItemRepository,
OrderItemQuantityModifierInterface $orderItemQuantityModifier,
OrderProcessorInterface $orderProcessor,
OrderInterface $cart,
OrderItemInterface $cartItem
): void {
$orderItemRepository->findOneByIdAndCartTokenValue(
'ORDER_ITEM_ID',
'TOKEN_VALUE'
)->willReturn($cartItem);

$cartItem->getOrder()->willReturn($cart);

$cart->getTokenValue()->willReturn('TOKEN_VALUE');

$orderItemQuantityModifier->modify($cartItem, 5)->shouldBeCalled();
Comment thread
AdamKasp marked this conversation as resolved.
$orderProcessor->process($cart)->shouldBeCalled();

$this(ChangeItemQuantityInCart::createFromData( 'TOKEN_VALUE', 'ORDER_ITEM_ID', 5));
}
}