Skip to content

Commit

Permalink
Merge pull request #264 from SzymonKostrubiec/op-282
Browse files Browse the repository at this point in the history
OP-282 - Add fix for bulk actions
  • Loading branch information
senghe authored Jul 9, 2024
2 parents 46abe3a + 3a5a38f commit b9938c5
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 6,709 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,43 @@

namespace spec\BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;

use BitBag\SyliusWishlistPlugin\Checker\ProductProcessingCheckerInterface;
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddSelectedProductsToCart;
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItem;
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItemInterface;
use BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist\AddSelectedProductsToCartHandler;
use BitBag\SyliusWishlistPlugin\Exception\ProductCantBeAddedToCartException;
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Sylius\Component\Order\Modifier\OrderModifierInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Translation\Translator;
use Symfony\Contracts\Translation\TranslatorInterface;

final class AddSelectedProductsToCartHandlerSpec extends ObjectBehavior
{
public function let(
RequestStack $requestStack,
Translator $translator,
OrderItemQuantityModifierInterface $itemQuantityModifier,
OrderModifierInterface $orderModifier,
OrderRepositoryInterface $orderRepository,
ProductProcessingCheckerInterface $productProcessingChecker,
AvailabilityCheckerInterface $availabilityChecker,
): void {
$this->beConstructedWith(
$requestStack,
$translator,
$itemQuantityModifier,
$orderModifier,
$orderRepository,
$productProcessingChecker,
$availabilityChecker,
);
}

Expand All @@ -55,23 +64,34 @@ public function it_adds_selected_products_to_cart(
OrderInterface $order,
OrderItemInterface $orderItem,
AddToCartCommandInterface $addToCartCommand,
ProductProcessingCheckerInterface $productProcessingChecker,
RequestStack $requestStack,
Session $session,
FlashBagInterface $flashBag,
TranslatorInterface $translator,
AvailabilityCheckerInterface $availabilityChecker,
ProductVariantInterface $productVariant,
): void {
$collection = new ArrayCollection([$wishlistProduct->getWrappedObject()]);
$productProcessingChecker->canBeProcessed($wishlistProduct)->willReturn(true);
$addSelectedProductsToCart = new AddSelectedProductsToCart($collection);

$wishlistProduct->getCartItem()->willReturn($addToCartCommand);

$addToCartCommand->getCart()->willReturn($order);
$addToCartCommand->getCartItem()->willReturn($orderItem);

$orderItem->getQuantity()->willReturn(0);
$itemQuantityModifier->modify($orderItem, 1)->shouldBeCalled();
$orderItem->getVariant()->willReturn($productVariant);
$orderItem->getQuantity()->willReturn(1);
$addToCartCommand->getCart()->willReturn($order);

$orderModifier->addToOrder($order, $orderItem)->shouldBeCalled();
$orderRepository->add($order)->shouldBeCalled();

$addSelectedProductsToCart = new AddSelectedProductsToCart($collection);
$availabilityChecker->isStockSufficient($productVariant, 1)->willReturn(true);

$requestStack->getSession()->willReturn($session);
$session->getFlashBag()->willReturn($flashBag);
$flashBag->has('success')->willReturn(false);

$translator->trans('bitbag_sylius_wishlist_plugin.ui.added_to_cart')->willReturn('Test translation');
$flashBag->add('success', 'Test translation')->shouldBeCalled();

$this->__invoke($addSelectedProductsToCart);
}
Expand All @@ -80,32 +100,35 @@ public function it_doesnt_add_selected_products_to_cart_if_product_cannot_be_pro
WishlistItemInterface $wishlistProduct,
OrderModifierInterface $orderModifier,
OrderRepositoryInterface $orderRepository,
OrderItemQuantityModifierInterface $itemQuantityModifier,
OrderInterface $order,
OrderItemInterface $orderItem,
AddToCartCommandInterface $addToCartCommand,
ProductProcessingCheckerInterface $productProcessingChecker,
RequestStack $requestStack,
Session $session,
FlashBagInterface $flashBag,
TranslatorInterface $translator,
AvailabilityCheckerInterface $availabilityChecker,
ProductVariantInterface $productVariant,
): void {
$collection = new ArrayCollection([$wishlistProduct->getWrappedObject()]);
$addSelectedProductsToCart = new AddSelectedProductsToCart($collection);

$productProcessingChecker->canBeProcessed($wishlistProduct)->willReturn(false);

$wishlistProduct->getCartItem()->shouldNotBeCalled();

$addToCartCommand->getCart()->shouldNotBeCalled();
$addToCartCommand->getCartItem()->shouldNotBeCalled();

$wishlistProduct->getCartItem()->willReturn($addToCartCommand);
$addToCartCommand->getCartItem()->willReturn($orderItem);
$orderItem->getVariant()->willReturn($productVariant);
$orderItem->getQuantity()->willReturn(0);
$itemQuantityModifier->modify($orderItem, 1)->shouldNotBeCalled();
$addToCartCommand->getCart()->willReturn($order);
$availabilityChecker->isStockSufficient($productVariant, 0)->willReturn(true);

$orderModifier->addToOrder($order, $orderItem)->shouldNotBeCalled();
$orderRepository->add($order)->shouldNotBeCalled();

$addSelectedProductsToCart = new AddSelectedProductsToCart($collection);
$requestStack->getSession()->willReturn($session);
$session->getFlashBag()->willReturn($flashBag);

$this
->shouldThrow(ProductCantBeAddedToCartException::class)
->during('__invoke', [$addSelectedProductsToCart])
;
$translator->trans('bitbag_sylius_wishlist_plugin.ui.increase_quantity')->willReturn('Increase the quantity of at least one item.');
$flashBag->add('error', 'Increase the quantity of at least one item.')->shouldBeCalled();

$this->__invoke($addSelectedProductsToCart);
}
}
176 changes: 0 additions & 176 deletions spec/Validator/InStockWishlistValidatorSpec.php

This file was deleted.

Loading

0 comments on commit b9938c5

Please sign in to comment.