From 6231b4c6ba3fa3bff8550415c2f1d5d29e251399 Mon Sep 17 00:00:00 2001 From: Hubert Filar Date: Thu, 25 Jul 2024 15:23:22 +0200 Subject: [PATCH] OP-291: Cover changes with phpspec --- .../AddSelectedProductsToCartHandlerSpec.php | 50 ++++++++----------- .../Wishlist/AddSelectedProductsToCart.php | 2 +- .../AddSelectedProductsToCartInterface.php | 19 +++++++ .../AddSelectedProductsToCartHandler.php | 4 +- 4 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 src/Command/Wishlist/AddSelectedProductsToCartInterface.php diff --git a/spec/CommandHandler/Wishlist/AddSelectedProductsToCartHandlerSpec.php b/spec/CommandHandler/Wishlist/AddSelectedProductsToCartHandlerSpec.php index adb47989..dad54c26 100644 --- a/spec/CommandHandler/Wishlist/AddSelectedProductsToCartHandlerSpec.php +++ b/spec/CommandHandler/Wishlist/AddSelectedProductsToCartHandlerSpec.php @@ -15,6 +15,7 @@ use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItem; use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItemInterface; use BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist\AddSelectedProductsToCartHandler; +use BitBag\SyliusWishlistPlugin\Exception\InvalidProductQuantityException; use Doctrine\Common\Collections\ArrayCollection; use PhpSpec\ObjectBehavior; use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface; @@ -25,25 +26,16 @@ 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, AvailabilityCheckerInterface $availabilityChecker, ): void { $this->beConstructedWith( - $requestStack, - $translator, $itemQuantityModifier, $orderModifier, $orderRepository, @@ -60,14 +52,9 @@ public function it_adds_selected_products_to_cart( WishlistItem $wishlistProduct, OrderModifierInterface $orderModifier, OrderRepositoryInterface $orderRepository, - OrderItemQuantityModifierInterface $itemQuantityModifier, OrderInterface $order, OrderItemInterface $orderItem, AddToCartCommandInterface $addToCartCommand, - RequestStack $requestStack, - Session $session, - FlashBagInterface $flashBag, - TranslatorInterface $translator, AvailabilityCheckerInterface $availabilityChecker, ProductVariantInterface $productVariant, ): void { @@ -86,13 +73,6 @@ public function it_adds_selected_products_to_cart( $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); } @@ -103,10 +83,6 @@ public function it_doesnt_add_selected_products_to_cart_if_product_cannot_be_pro OrderInterface $order, OrderItemInterface $orderItem, AddToCartCommandInterface $addToCartCommand, - RequestStack $requestStack, - Session $session, - FlashBagInterface $flashBag, - TranslatorInterface $translator, AvailabilityCheckerInterface $availabilityChecker, ProductVariantInterface $productVariant, ): void { @@ -123,12 +99,26 @@ public function it_doesnt_add_selected_products_to_cart_if_product_cannot_be_pro $orderModifier->addToOrder($order, $orderItem)->shouldNotBeCalled(); $orderRepository->add($order)->shouldNotBeCalled(); - $requestStack->getSession()->willReturn($session); - $session->getFlashBag()->willReturn($flashBag); + $this->shouldThrow(InvalidProductQuantityException::class)->during('__invoke', [$addSelectedProductsToCart]); + } + + public function it_throws_exception_when_quantity_is_not_positive( + AvailabilityCheckerInterface $availabilityChecker, + ProductVariantInterface $productVariant, + OrderItemInterface $orderItem, + WishlistItemInterface $wishlistProduct, + AddToCartCommandInterface $addToCartCommand, + ): void { + $collection = new ArrayCollection([$wishlistProduct->getWrappedObject()]); + $addSelectedProductsToCart = new AddSelectedProductsToCart($collection); + + $wishlistProduct->getCartItem()->willReturn($addToCartCommand); + $addToCartCommand->getCartItem()->willReturn($orderItem); + $orderItem->getVariant()->willReturn($productVariant); + $orderItem->getQuantity()->willReturn(0); - $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(); + $availabilityChecker->isStockSufficient($productVariant, 0)->willReturn(true); - $this->__invoke($addSelectedProductsToCart); + $this->shouldThrow(InvalidProductQuantityException::class)->during('__invoke', [$addSelectedProductsToCart]); } } diff --git a/src/Command/Wishlist/AddSelectedProductsToCart.php b/src/Command/Wishlist/AddSelectedProductsToCart.php index f1134351..b6d8f024 100644 --- a/src/Command/Wishlist/AddSelectedProductsToCart.php +++ b/src/Command/Wishlist/AddSelectedProductsToCart.php @@ -13,7 +13,7 @@ use Doctrine\Common\Collections\Collection; -final class AddSelectedProductsToCart implements WishlistSyncCommandInterface +final class AddSelectedProductsToCart implements AddSelectedProductsToCartInterface { public function __construct(private readonly Collection $wishlistProducts) { diff --git a/src/Command/Wishlist/AddSelectedProductsToCartInterface.php b/src/Command/Wishlist/AddSelectedProductsToCartInterface.php new file mode 100644 index 00000000..9d5fc3b7 --- /dev/null +++ b/src/Command/Wishlist/AddSelectedProductsToCartInterface.php @@ -0,0 +1,19 @@ +addSelectedProductsToCart($addSelectedProductsToCartCommand->getWishlistProducts()); }