From 9a1f73efaf8e6f1adae6862f914d4afb1ab0bc33 Mon Sep 17 00:00:00 2001 From: Hubert Filar Date: Wed, 11 Sep 2024 16:04:54 +0200 Subject: [PATCH] OP-289: Adjust unit tests to handle unpacked bundles logic --- .../AddProductBundleItemToCartCommand.php | 2 +- ...roductBundleItemToCartCommandInterface.php | 26 ++++++++++ src/Command/AddProductBundleToCartCommand.php | 6 +-- ...dProductBundleItemToCartCommandFactory.php | 3 +- ...undleItemToCartCommandFactoryInterface.php | 4 +- .../AddProductBundleToCartCommandFactory.php | 4 +- ...uctBundleToCartCommandFactoryInterface.php | 4 +- .../AddProductBundleToCartDtoFactory.php | 4 +- src/Factory/ProductBundleOrderItemFactory.php | 7 +-- ...ProductBundleOrderItemFactoryInterface.php | 6 ++- .../Type/AddProductBundleItemToCartType.php | 3 +- .../CartProcessor.php | 4 +- .../CartProcessorInterface.php | 2 +- ...ProductBundleItemToCartCommandProvider.php | 6 +-- ...ndleItemToCartCommandProviderInterface.php | 4 +- ...ductBundleItemToCartCommandFactoryTest.php | 4 +- ...dProductBundleToCartCommandFactoryTest.php | 8 +++- .../CartProcessorTest.php | 48 ++++++++----------- .../AddProductBundleToCartHandlerTest.php | 20 +++++--- ...ddProductBundleItemToCartCommandMother.php | 3 +- 20 files changed, 102 insertions(+), 66 deletions(-) create mode 100644 src/Command/AddProductBundleItemToCartCommandInterface.php diff --git a/src/Command/AddProductBundleItemToCartCommand.php b/src/Command/AddProductBundleItemToCartCommand.php index 369ab358..79d1c60f 100644 --- a/src/Command/AddProductBundleItemToCartCommand.php +++ b/src/Command/AddProductBundleItemToCartCommand.php @@ -14,7 +14,7 @@ use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; use Sylius\Component\Core\Model\ProductVariantInterface; -final class AddProductBundleItemToCartCommand +final class AddProductBundleItemToCartCommand implements AddProductBundleItemToCartCommandInterface { public function __construct( private ProductBundleItemInterface $productBundleItem, diff --git a/src/Command/AddProductBundleItemToCartCommandInterface.php b/src/Command/AddProductBundleItemToCartCommandInterface.php new file mode 100644 index 00000000..9862a3f3 --- /dev/null +++ b/src/Command/AddProductBundleItemToCartCommandInterface.php @@ -0,0 +1,26 @@ + */ + /** @var Collection */ private Collection $productBundleItems; public function __construct( @@ -42,13 +42,13 @@ public function getQuantity(): int return $this->quantity; } - /** @return Collection */ + /** @return Collection */ public function getProductBundleItems(): Collection { return $this->productBundleItems; } - /** @param Collection $productBundleItems */ + /** @param Collection $productBundleItems */ public function setProductBundleItems(Collection $productBundleItems): void { $this->productBundleItems = $productBundleItems; diff --git a/src/Factory/AddProductBundleItemToCartCommandFactory.php b/src/Factory/AddProductBundleItemToCartCommandFactory.php index 7c2f8bd1..03682f53 100644 --- a/src/Factory/AddProductBundleItemToCartCommandFactory.php +++ b/src/Factory/AddProductBundleItemToCartCommandFactory.php @@ -12,11 +12,12 @@ namespace BitBag\SyliusProductBundlePlugin\Factory; use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; final class AddProductBundleItemToCartCommandFactory implements AddProductBundleItemToCartCommandFactoryInterface { - public function createNew(ProductBundleItemInterface $bundleItem): AddProductBundleItemToCartCommand + public function createNew(ProductBundleItemInterface $bundleItem): AddProductBundleItemToCartCommandInterface { return new AddProductBundleItemToCartCommand($bundleItem); } diff --git a/src/Factory/AddProductBundleItemToCartCommandFactoryInterface.php b/src/Factory/AddProductBundleItemToCartCommandFactoryInterface.php index cc881b78..b36dac4a 100644 --- a/src/Factory/AddProductBundleItemToCartCommandFactoryInterface.php +++ b/src/Factory/AddProductBundleItemToCartCommandFactoryInterface.php @@ -11,10 +11,10 @@ namespace BitBag\SyliusProductBundlePlugin\Factory; -use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; interface AddProductBundleItemToCartCommandFactoryInterface { - public function createNew(ProductBundleItemInterface $bundleItem): AddProductBundleItemToCartCommand; + public function createNew(ProductBundleItemInterface $bundleItem): AddProductBundleItemToCartCommandInterface; } diff --git a/src/Factory/AddProductBundleToCartCommandFactory.php b/src/Factory/AddProductBundleToCartCommandFactory.php index dd66796b..e0df023b 100644 --- a/src/Factory/AddProductBundleToCartCommandFactory.php +++ b/src/Factory/AddProductBundleToCartCommandFactory.php @@ -11,14 +11,14 @@ namespace BitBag\SyliusProductBundlePlugin\Factory; -use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleToCartCommand; use BitBag\SyliusProductBundlePlugin\Dto\AddProductBundleToCartDtoInterface; use Doctrine\Common\Collections\Collection; final class AddProductBundleToCartCommandFactory implements AddProductBundleToCartCommandFactoryInterface { - /** @param Collection $productBundleItems */ + /** @param Collection $productBundleItems */ public function createNew( int $orderId, string $productCode, diff --git a/src/Factory/AddProductBundleToCartCommandFactoryInterface.php b/src/Factory/AddProductBundleToCartCommandFactoryInterface.php index 24cba89a..01150104 100644 --- a/src/Factory/AddProductBundleToCartCommandFactoryInterface.php +++ b/src/Factory/AddProductBundleToCartCommandFactoryInterface.php @@ -11,14 +11,14 @@ namespace BitBag\SyliusProductBundlePlugin\Factory; -use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleToCartCommand; use BitBag\SyliusProductBundlePlugin\Dto\AddProductBundleToCartDtoInterface; use Doctrine\Common\Collections\Collection; interface AddProductBundleToCartCommandFactoryInterface { - /** @param Collection $productBundleItems */ + /** @param Collection $productBundleItems */ public function createNew( int $orderId, string $productCode, diff --git a/src/Factory/AddProductBundleToCartDtoFactory.php b/src/Factory/AddProductBundleToCartDtoFactory.php index b98af5d6..b30cd4c0 100644 --- a/src/Factory/AddProductBundleToCartDtoFactory.php +++ b/src/Factory/AddProductBundleToCartDtoFactory.php @@ -11,7 +11,7 @@ namespace BitBag\SyliusProductBundlePlugin\Factory; -use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Dto\AddProductBundleToCartDto; use BitBag\SyliusProductBundlePlugin\Dto\AddProductBundleToCartDtoInterface; use BitBag\SyliusProductBundlePlugin\Entity\OrderItemInterface; @@ -39,7 +39,7 @@ public function createNew( } /** - * @return AddProductBundleItemToCartCommand[] + * @return AddProductBundleItemToCartCommandInterface[] */ private function getProcessedProductBundleItems(ProductBundleInterface $productBundle): array { diff --git a/src/Factory/ProductBundleOrderItemFactory.php b/src/Factory/ProductBundleOrderItemFactory.php index 5d00e8c5..808220e0 100644 --- a/src/Factory/ProductBundleOrderItemFactory.php +++ b/src/Factory/ProductBundleOrderItemFactory.php @@ -11,7 +11,7 @@ namespace BitBag\SyliusProductBundlePlugin\Factory; -use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleOrderItemInterface; use Sylius\Component\Resource\Factory\FactoryInterface; @@ -43,8 +43,9 @@ public function createFromProductBundleItem(ProductBundleItemInterface $bundleIt return $productBundleOrderItem; } - public function createFromAddProductBundleItemToCartCommand(AddProductBundleItemToCartCommand $addItemToCartCommand): ProductBundleOrderItemInterface - { + public function createFromAddProductBundleItemToCartCommand( + AddProductBundleItemToCartCommandInterface $addItemToCartCommand, + ): ProductBundleOrderItemInterface { /** @var ProductBundleOrderItemInterface $productBundleOrderItem */ $productBundleOrderItem = $this->decoratedFactory->createNew(); diff --git a/src/Factory/ProductBundleOrderItemFactoryInterface.php b/src/Factory/ProductBundleOrderItemFactoryInterface.php index 44b7d9f5..3c32cb63 100644 --- a/src/Factory/ProductBundleOrderItemFactoryInterface.php +++ b/src/Factory/ProductBundleOrderItemFactoryInterface.php @@ -11,7 +11,7 @@ namespace BitBag\SyliusProductBundlePlugin\Factory; -use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleOrderItemInterface; use Sylius\Component\Resource\Factory\FactoryInterface; @@ -20,5 +20,7 @@ interface ProductBundleOrderItemFactoryInterface extends FactoryInterface { public function createFromProductBundleItem(ProductBundleItemInterface $bundleItem): ProductBundleOrderItemInterface; - public function createFromAddProductBundleItemToCartCommand(AddProductBundleItemToCartCommand $addItemToCartCommand): ProductBundleOrderItemInterface; + public function createFromAddProductBundleItemToCartCommand( + AddProductBundleItemToCartCommandInterface $addItemToCartCommand, + ): ProductBundleOrderItemInterface; } diff --git a/src/Form/Type/AddProductBundleItemToCartType.php b/src/Form/Type/AddProductBundleItemToCartType.php index f4e0f926..099da8cf 100644 --- a/src/Form/Type/AddProductBundleItemToCartType.php +++ b/src/Form/Type/AddProductBundleItemToCartType.php @@ -12,6 +12,7 @@ namespace BitBag\SyliusProductBundlePlugin\Form\Type; use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface; use Sylius\Bundle\ProductBundle\Form\Type\ProductVariantChoiceType; use Sylius\Bundle\ProductBundle\Form\Type\ProductVariantMatchType; @@ -35,7 +36,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void } $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void { - /** @var AddProductBundleItemToCartCommand $data */ + /** @var AddProductBundleItemToCartCommandInterface $data */ $data = $event->getData(); $form = $event->getForm(); diff --git a/src/Handler/AddProductBundleToCartHandler/CartProcessor.php b/src/Handler/AddProductBundleToCartHandler/CartProcessor.php index 5260b63d..faf794d4 100644 --- a/src/Handler/AddProductBundleToCartHandler/CartProcessor.php +++ b/src/Handler/AddProductBundleToCartHandler/CartProcessor.php @@ -35,7 +35,7 @@ public function process( OrderInterface $cart, ProductBundleInterface $productBundle, int $quantity, - Collection $productBundleOrderItems, + Collection $addBundleItemToCartCommands, ): void { Assert::greaterThan($quantity, 0); @@ -49,7 +49,7 @@ public function process( $cartItem = $this->cartItemFactory->createWithVariant($productVariant); $this->orderItemQuantityModifier->modify($cartItem, $quantity); - foreach ($productBundleOrderItems as $addBundleItemToCartCommand) { + foreach ($addBundleItemToCartCommands as $addBundleItemToCartCommand) { $productBundleOrderItem = $this->productBundleOrderItemFactory->createFromAddProductBundleItemToCartCommand($addBundleItemToCartCommand); $cartItem->addProductBundleOrderItem($productBundleOrderItem); } diff --git a/src/Handler/AddProductBundleToCartHandler/CartProcessorInterface.php b/src/Handler/AddProductBundleToCartHandler/CartProcessorInterface.php index 54e2a43e..aacf225b 100644 --- a/src/Handler/AddProductBundleToCartHandler/CartProcessorInterface.php +++ b/src/Handler/AddProductBundleToCartHandler/CartProcessorInterface.php @@ -21,6 +21,6 @@ public function process( OrderInterface $cart, ProductBundleInterface $productBundle, int $quantity, - Collection $productBundleOrderItems, + Collection $addBundleItemToCartCommands, ): void; } diff --git a/src/Provider/AddProductBundleItemToCartCommandProvider.php b/src/Provider/AddProductBundleItemToCartCommandProvider.php index 27fa1542..11a0ebd2 100644 --- a/src/Provider/AddProductBundleItemToCartCommandProvider.php +++ b/src/Provider/AddProductBundleItemToCartCommandProvider.php @@ -11,7 +11,7 @@ namespace BitBag\SyliusProductBundlePlugin\Provider; -use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; use BitBag\SyliusProductBundlePlugin\Factory\AddProductBundleItemToCartCommandFactoryInterface; use BitBag\SyliusProductBundlePlugin\Repository\ProductBundleItemRepositoryInterface; @@ -32,7 +32,7 @@ public function __construct( } /** - * @return Collection + * @return Collection * * @throws \Exception */ @@ -58,7 +58,7 @@ public function provide(string $bundleCode, array $overwrittenVariants): Collect } private function overwriteVariant( - AddProductBundleItemToCartCommand $command, + AddProductBundleItemToCartCommandInterface $command, ProductBundleItemInterface $bundleItem, array $overwrittenVariants, ): void { diff --git a/src/Provider/AddProductBundleItemToCartCommandProviderInterface.php b/src/Provider/AddProductBundleItemToCartCommandProviderInterface.php index 1949b599..fc355dbf 100644 --- a/src/Provider/AddProductBundleItemToCartCommandProviderInterface.php +++ b/src/Provider/AddProductBundleItemToCartCommandProviderInterface.php @@ -11,11 +11,11 @@ namespace BitBag\SyliusProductBundlePlugin\Provider; -use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use Doctrine\Common\Collections\Collection; interface AddProductBundleItemToCartCommandProviderInterface { - /** @return Collection */ + /** @return Collection */ public function provide(string $bundleCode, array $overwrittenVariants): Collection; } diff --git a/tests/Unit/Factory/AddProductBundleItemToCartCommandFactoryTest.php b/tests/Unit/Factory/AddProductBundleItemToCartCommandFactoryTest.php index 2a3f0764..e77b6425 100644 --- a/tests/Unit/Factory/AddProductBundleItemToCartCommandFactoryTest.php +++ b/tests/Unit/Factory/AddProductBundleItemToCartCommandFactoryTest.php @@ -11,7 +11,7 @@ namespace Tests\BitBag\SyliusProductBundlePlugin\Unit\Factory; -use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Factory\AddProductBundleItemToCartCommandFactory; use PHPUnit\Framework\TestCase; use Tests\BitBag\SyliusProductBundlePlugin\Unit\MotherObject\ProductBundleItemMother; @@ -25,6 +25,6 @@ public function testCreateAddProductBundleItemToCartCommand(): void $factory = new AddProductBundleItemToCartCommandFactory(); $command = $factory->createNew($productBundleItem); - self::assertInstanceOf(AddProductBundleItemToCartCommand::class, $command); + self::assertInstanceOf(AddProductBundleItemToCartCommandInterface::class, $command); } } diff --git a/tests/Unit/Factory/AddProductBundleToCartCommandFactoryTest.php b/tests/Unit/Factory/AddProductBundleToCartCommandFactoryTest.php index 4df89fd4..7e4c0bb3 100644 --- a/tests/Unit/Factory/AddProductBundleToCartCommandFactoryTest.php +++ b/tests/Unit/Factory/AddProductBundleToCartCommandFactoryTest.php @@ -11,8 +11,10 @@ namespace Tests\BitBag\SyliusProductBundlePlugin\Unit\Factory; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleToCartCommand; use BitBag\SyliusProductBundlePlugin\Factory\AddProductBundleToCartCommandFactory; +use Doctrine\Common\Collections\ArrayCollection; use PHPUnit\Framework\TestCase; use Tests\BitBag\SyliusProductBundlePlugin\Unit\MotherObject\AddProductBundleToCartDtoMother; @@ -26,13 +28,17 @@ final class AddProductBundleToCartCommandFactoryTest extends TestCase public function testCreateAddProductBundleToCartCommandObject(): void { + $addProductBundleItemToCartCommand = $this->createMock(AddProductBundleItemToCartCommandInterface::class); + $commands = new ArrayCollection([$addProductBundleItemToCartCommand]); + $factory = new AddProductBundleToCartCommandFactory(); - $command = $factory->createNew(self::ORDER_ID, self::PRODUCT_CODE, self::QUANTITY); + $command = $factory->createNew(self::ORDER_ID, self::PRODUCT_CODE, self::QUANTITY, $commands); self::assertInstanceOf(AddProductBundleToCartCommand::class, $command); self::assertEquals(self::ORDER_ID, $command->getOrderId()); self::assertEquals(self::PRODUCT_CODE, $command->getProductCode()); self::assertEquals(self::QUANTITY, $command->getQuantity()); + self::assertEquals($commands, $command->getProductBundleItems()); } public function testCreateAddProductBundleToCartCommandObjectFromDto(): void diff --git a/tests/Unit/Handler/AddProductBundleToCartHandler/CartProcessorTest.php b/tests/Unit/Handler/AddProductBundleToCartHandler/CartProcessorTest.php index 3a492db4..64bea086 100644 --- a/tests/Unit/Handler/AddProductBundleToCartHandler/CartProcessorTest.php +++ b/tests/Unit/Handler/AddProductBundleToCartHandler/CartProcessorTest.php @@ -11,11 +11,10 @@ namespace Tests\BitBag\SyliusProductBundlePlugin\Unit\Handler\AddProductBundleToCartHandler; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Entity\OrderItemInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundle; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleInterface; -use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItem; -use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleOrderItem; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleOrderItemInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface; @@ -23,6 +22,7 @@ use BitBag\SyliusProductBundlePlugin\Factory\ProductBundleOrderItemFactoryInterface; use BitBag\SyliusProductBundlePlugin\Handler\AddProductBundleToCartHandler\CartProcessor; use BitBag\SyliusProductBundlePlugin\Handler\AddProductBundleToCartHandler\CartProcessorInterface; +use Doctrine\Common\Collections\ArrayCollection; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Sylius\Component\Core\Model\ProductVariant; @@ -37,17 +37,15 @@ final class CartProcessorTest extends TestCase { - /** @var mixed|MockObject|OrderItemQuantityModifierInterface */ - private $orderItemQuantityModifier; + private OrderItemQuantityModifierInterface|MockObject $orderItemQuantityModifier; - /** @var ProductBundleOrderItemFactoryInterface|mixed|MockObject */ - private $productBundleOrderItemFactory; + private ProductBundleOrderItemFactoryInterface|MockObject $productBundleOrderItemFactory; - /** @var mixed|MockObject|OrderModifierInterface */ - private $orderModifier; + private OrderModifierInterface|MockObject $orderModifier; - /** @var OrderItemFactoryInterface|mixed|MockObject */ - private $cartItemFactory; + private OrderItemFactoryInterface|MockObject $cartItemFactory; + + private AddProductBundleItemToCartCommandInterface|MockObject $addProductBundleItemToCartCommand; protected function setUp(): void { @@ -55,6 +53,7 @@ protected function setUp(): void $this->productBundleOrderItemFactory = $this->createMock(ProductBundleOrderItemFactoryInterface::class); $this->orderModifier = $this->createMock(OrderModifierInterface::class); $this->cartItemFactory = $this->createMock(OrderItemFactoryInterface::class); + $this->addProductBundleItemToCartCommand = $this->createMock(AddProductBundleItemToCartCommandInterface::class); } public function testThrowExceptionIfQuantityNotGreaterThanZero(): void @@ -65,7 +64,7 @@ public function testThrowExceptionIfQuantityNotGreaterThanZero(): void $productBundle = $this->createProductBundle(); $processor = $this->createProcessor(); - $processor->process($cart, $productBundle, 0); + $processor->process($cart, $productBundle, 0, new ArrayCollection()); } public function testThrowExceptionIfProductIsNull(): void @@ -76,7 +75,7 @@ public function testThrowExceptionIfProductIsNull(): void $productBundle = $this->createProductBundle(); $processor = $this->createProcessor(); - $processor->process($cart, $productBundle, 1); + $processor->process($cart, $productBundle, 1, new ArrayCollection()); } public function testThrowExceptionIfProductHasNoVariant(): void @@ -87,7 +86,7 @@ public function testThrowExceptionIfProductHasNoVariant(): void $productBundle = $this->createProductBundleWithProduct(); $processor = $this->createProcessor(); - $processor->process($cart, $productBundle, 1); + $processor->process($cart, $productBundle, 1, new ArrayCollection()); } public function testCreateCartItem(): void @@ -103,7 +102,7 @@ public function testCreateCartItem(): void ; $processor = $this->createProcessor(); - $processor->process($cart, $productBundle, 2); + $processor->process($cart, $productBundle, 2, new ArrayCollection([$this->addProductBundleItemToCartCommand])); } public function testModifyCartItemQuantity(): void @@ -124,13 +123,13 @@ public function testModifyCartItemQuantity(): void ; $processor = $this->createProcessor(); - $processor->process($cart, $productBundle, 2); + $processor->process($cart, $productBundle, 2, new ArrayCollection([$this->addProductBundleItemToCartCommand])); } public function testCreateBundleOrderItemsFromBundleItems(): void { - $bundleItem1 = $this->createProductBundleItem(); - $bundleItem2 = $this->createProductBundleItem(); + $addBundleItemToCartCommand1 = $this->createMock(AddProductBundleItemToCartCommandInterface::class); + $addBundleItemToCartCommand2 = $this->createMock(AddProductBundleItemToCartCommandInterface::class); $productBundleOrderItem1 = $this->createProductBundleOrderItem(); $productBundleOrderItem2 = $this->createProductBundleOrderItem(); @@ -138,8 +137,6 @@ public function testCreateBundleOrderItemsFromBundleItems(): void $cart = $this->createCart(); $product = $this->createProductWithVariant(); $productBundle = $this->createProductBundleWithProduct($product); - $productBundle->addProductBundleItem($bundleItem1); - $productBundle->addProductBundleItem($bundleItem2); $cartItem = $this->createMock(OrderItemInterface::class); $cartItem->expects(self::exactly(2)) @@ -152,13 +149,13 @@ public function testCreateBundleOrderItemsFromBundleItems(): void ->willReturn($cartItem) ; $this->productBundleOrderItemFactory->expects(self::exactly(2)) - ->method('createFromProductBundleItem') - ->withConsecutive([$bundleItem1], [$bundleItem2]) + ->method('createFromAddProductBundleItemToCartCommand') + ->withConsecutive([$addBundleItemToCartCommand1], [$addBundleItemToCartCommand2]) ->willReturn($productBundleOrderItem1, $productBundleOrderItem2) ; $processor = $this->createProcessor(); - $processor->process($cart, $productBundle, 1); + $processor->process($cart, $productBundle, 1, new ArrayCollection([$addBundleItemToCartCommand1, $addBundleItemToCartCommand2])); } public function testAddCartItemToOrder(): void @@ -178,7 +175,7 @@ public function testAddCartItemToOrder(): void ; $processor = $this->createProcessor(); - $processor->process($cart, $productBundle, 1); + $processor->process($cart, $productBundle, 1, new ArrayCollection([$this->addProductBundleItemToCartCommand])); } private function createProcessor(): CartProcessorInterface @@ -230,11 +227,6 @@ private function createProductBundleWithProduct(?ProductInterface $product = nul return $productBundle; } - private function createProductBundleItem(): ProductBundleItemInterface - { - return new ProductBundleItem(); - } - private function createProductBundleOrderItem(): ProductBundleOrderItemInterface { return new ProductBundleOrderItem(); diff --git a/tests/Unit/Handler/AddProductBundleToCartHandlerTest.php b/tests/Unit/Handler/AddProductBundleToCartHandlerTest.php index 922efa27..4df1d108 100644 --- a/tests/Unit/Handler/AddProductBundleToCartHandlerTest.php +++ b/tests/Unit/Handler/AddProductBundleToCartHandlerTest.php @@ -11,10 +11,12 @@ namespace Tests\BitBag\SyliusProductBundlePlugin\Unit\Handler; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleToCartCommand; use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface; use BitBag\SyliusProductBundlePlugin\Handler\AddProductBundleToCartHandler; use BitBag\SyliusProductBundlePlugin\Handler\AddProductBundleToCartHandler\CartProcessorInterface; +use Doctrine\Common\Collections\ArrayCollection; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Sylius\Component\Core\Model\OrderInterface; @@ -28,20 +30,20 @@ final class AddProductBundleToCartHandlerTest extends TestCase { - /** @var mixed|MockObject|OrderRepositoryInterface */ - private $orderRepository; + private OrderRepositoryInterface|MockObject $orderRepository; - /** @var mixed|MockObject|ProductRepositoryInterface */ - private $productRepository; + private ProductRepositoryInterface|MockObject $productRepository; - /** @var CartProcessorInterface|mixed|MockObject */ - private $cartProcessor; + private CartProcessorInterface|MockObject $cartProcessor; + + private AddProductBundleItemToCartCommandInterface|MockObject $addProductBundleItemToCartCommand; protected function setUp(): void { $this->orderRepository = $this->createMock(OrderRepositoryInterface::class); $this->productRepository = $this->createMock(ProductRepositoryInterface::class); $this->cartProcessor = $this->createMock(CartProcessorInterface::class); + $this->addProductBundleItemToCartCommand = $this->createMock(AddProductBundleItemToCartCommandInterface::class); } public function testThrowExceptionIfCartDoesntExist(): void @@ -122,10 +124,12 @@ public function testProcessCart(): void $this->cartProcessor->expects(self::once()) ->method('process') - ->with($cart, $productBundle, 2) + ->with($cart, $productBundle, 2, new ArrayCollection([$this->addProductBundleItemToCartCommand])) ; $command = new AddProductBundleToCartCommand(1, '', 2); + $command->setProductBundleItems(new ArrayCollection([$this->addProductBundleItemToCartCommand])); + $handler = $this->createHandler(); $handler($command); } @@ -149,6 +153,8 @@ public function testAddCartToRepository(): void ; $command = new AddProductBundleToCartCommand(1, '', 1); + $command->setProductBundleItems(new ArrayCollection([$this->addProductBundleItemToCartCommand])); + $handler = $this->createHandler(); $handler($command); } diff --git a/tests/Unit/MotherObject/AddProductBundleItemToCartCommandMother.php b/tests/Unit/MotherObject/AddProductBundleItemToCartCommandMother.php index 6912c547..0d296c5d 100644 --- a/tests/Unit/MotherObject/AddProductBundleItemToCartCommandMother.php +++ b/tests/Unit/MotherObject/AddProductBundleItemToCartCommandMother.php @@ -12,11 +12,12 @@ namespace Tests\BitBag\SyliusProductBundlePlugin\Unit\MotherObject; use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; +use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommandInterface; use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; final class AddProductBundleItemToCartCommandMother { - public static function create(ProductBundleItemInterface $bundleItem): AddProductBundleItemToCartCommand + public static function create(ProductBundleItemInterface $bundleItem): AddProductBundleItemToCartCommandInterface { return new AddProductBundleItemToCartCommand($bundleItem); }