Skip to content

Commit

Permalink
OP-291: Cover changes with phpspec
Browse files Browse the repository at this point in the history
  • Loading branch information
hmfilar committed Jul 25, 2024
1 parent 4d14428 commit 6231b4c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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);
}

Expand All @@ -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 {
Expand All @@ -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]);
}
}
2 changes: 1 addition & 1 deletion src/Command/Wishlist/AddSelectedProductsToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
19 changes: 19 additions & 0 deletions src/Command/Wishlist/AddSelectedProductsToCartInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\Command\Wishlist;

use Doctrine\Common\Collections\Collection;

interface AddSelectedProductsToCartInterface extends WishlistSyncCommandInterface
{
public function getWishlistProducts(): Collection;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;

use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddSelectedProductsToCart;
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddSelectedProductsToCartInterface;
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItemInterface;
use BitBag\SyliusWishlistPlugin\Exception\InsufficientProductStockException;
use BitBag\SyliusWishlistPlugin\Exception\InvalidProductQuantityException;
Expand All @@ -38,7 +38,7 @@ public function __construct(
) {
}

public function __invoke(AddSelectedProductsToCart $addSelectedProductsToCartCommand): void
public function __invoke(AddSelectedProductsToCartInterface $addSelectedProductsToCartCommand): void
{
$this->addSelectedProductsToCart($addSelectedProductsToCartCommand->getWishlistProducts());
}
Expand Down

0 comments on commit 6231b4c

Please sign in to comment.