Skip to content

Commit

Permalink
OP-289: Adjust unit tests to handle unpacked bundles logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hmfilar committed Sep 11, 2024
1 parent e36d8a0 commit 9a1f73e
Show file tree
Hide file tree
Showing 20 changed files with 102 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/Command/AddProductBundleItemToCartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions src/Command/AddProductBundleItemToCartCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\SyliusProductBundlePlugin\Command;

use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;

interface AddProductBundleItemToCartCommandInterface
{
public function getProductBundleItem(): ProductBundleItemInterface;

public function getProductVariant(): ?ProductVariantInterface;

public function setProductVariant(ProductVariantInterface $productVariant): void;

public function getQuantity(): ?int;
}
6 changes: 3 additions & 3 deletions src/Command/AddProductBundleToCartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

final class AddProductBundleToCartCommand implements OrderIdentityAwareInterface, ProductCodeAwareInterface
{
/** @var Collection<int, AddProductBundleItemToCartCommand> */
/** @var Collection<int, AddProductBundleItemToCartCommandInterface> */
private Collection $productBundleItems;

public function __construct(
Expand All @@ -42,13 +42,13 @@ public function getQuantity(): int
return $this->quantity;
}

/** @return Collection<int, AddProductBundleItemToCartCommand> */
/** @return Collection<int, AddProductBundleItemToCartCommandInterface> */
public function getProductBundleItems(): Collection
{
return $this->productBundleItems;
}

/** @param Collection<int, AddProductBundleItemToCartCommand> $productBundleItems */
/** @param Collection<int, AddProductBundleItemToCartCommandInterface> $productBundleItems */
public function setProductBundleItems(Collection $productBundleItems): void
{
$this->productBundleItems = $productBundleItems;
Expand Down
3 changes: 2 additions & 1 deletion src/Factory/AddProductBundleItemToCartCommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions src/Factory/AddProductBundleToCartCommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, AddProductBundleItemToCartCommand> $productBundleItems */
/** @param Collection<int, AddProductBundleItemToCartCommandInterface> $productBundleItems */
public function createNew(
int $orderId,
string $productCode,
Expand Down
4 changes: 2 additions & 2 deletions src/Factory/AddProductBundleToCartCommandFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, AddProductBundleItemToCartCommand> $productBundleItems */
/** @param Collection<int, AddProductBundleItemToCartCommandInterface> $productBundleItems */
public function createNew(
int $orderId,
string $productCode,
Expand Down
4 changes: 2 additions & 2 deletions src/Factory/AddProductBundleToCartDtoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,7 +39,7 @@ public function createNew(
}

/**
* @return AddProductBundleItemToCartCommand[]
* @return AddProductBundleItemToCartCommandInterface[]
*/
private function getProcessedProductBundleItems(ProductBundleInterface $productBundle): array
{
Expand Down
7 changes: 4 additions & 3 deletions src/Factory/ProductBundleOrderItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
6 changes: 4 additions & 2 deletions src/Factory/ProductBundleOrderItemFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
3 changes: 2 additions & 1 deletion src/Form/Type/AddProductBundleItemToCartType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/Handler/AddProductBundleToCartHandler/CartProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function process(
OrderInterface $cart,
ProductBundleInterface $productBundle,
int $quantity,
Collection $productBundleOrderItems,
Collection $addBundleItemToCartCommands,
): void {
Assert::greaterThan($quantity, 0);

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public function process(
OrderInterface $cart,
ProductBundleInterface $productBundle,
int $quantity,
Collection $productBundleOrderItems,
Collection $addBundleItemToCartCommands,
): void;
}
6 changes: 3 additions & 3 deletions src/Provider/AddProductBundleItemToCartCommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +32,7 @@ public function __construct(
}

/**
* @return Collection<int, AddProductBundleItemToCartCommand>
* @return Collection<int, AddProductBundleItemToCartCommandInterface>
*
* @throws \Exception
*/
Expand All @@ -58,7 +58,7 @@ public function provide(string $bundleCode, array $overwrittenVariants): Collect
}

private function overwriteVariant(
AddProductBundleItemToCartCommand $command,
AddProductBundleItemToCartCommandInterface $command,
ProductBundleItemInterface $bundleItem,
array $overwrittenVariants,
): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, AddProductBundleItemToCartCommand> */
/** @return Collection<int, AddProductBundleItemToCartCommandInterface> */
public function provide(string $bundleCode, array $overwrittenVariants): Collection;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Loading

0 comments on commit 9a1f73e

Please sign in to comment.