Skip to content

Commit

Permalink
Add Unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Roshyo committed Mar 29, 2022
1 parent 1e187fe commit 13401d9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Form/Extension/AddToCartTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ public function populateCartItem(FormEvent $event): void
return;
}

$cartItem = $data->getCartItem();
/** @var ProductInterface|null $product */
$product = $data->getCartItem()->getProduct();
$product = $cartItem->getProduct();
if (null === $product) {
return;
}
Expand All @@ -86,9 +87,7 @@ public function populateCartItem(FormEvent $event): void
return;
}

$cartItem = $data->getCartItem();
$giftCardInformation = $data->getGiftCardInformation();

if ($product->isGiftCardAmountConfigurable()) {
$cartItem->setUnitPrice($giftCardInformation->getAmount());
$cartItem->setImmutable(true);
Expand Down
110 changes: 110 additions & 0 deletions tests/Unit/Form/Extension/AddToCartTypeExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace Tests\Setono\SyliusGiftCardPlugin\Unit\Form\Extension;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusGiftCardPlugin\Factory\GiftCardFactoryInterface;
use Setono\SyliusGiftCardPlugin\Form\Extension\AddToCartTypeExtension;
use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Setono\SyliusGiftCardPlugin\Order\AddToCartCommand;
use Setono\SyliusGiftCardPlugin\Order\GiftCardInformationInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Symfony\Component\Form\FormEvent;
use Tests\Setono\SyliusGiftCardPlugin\Application\Model\Order;
use Tests\Setono\SyliusGiftCardPlugin\Application\Model\OrderItem;
use Tests\Setono\SyliusGiftCardPlugin\Application\Model\OrderItemUnit;
use Tests\Setono\SyliusGiftCardPlugin\Application\Model\Product;

final class AddToCartTypeExtensionTest extends TestCase
{
use ProphecyTrait;

/**
* @test
*/
public function it_populates_cart_item_for_configurable_gift_card(): void
{
$cart = $this->prophesize(Order::class);
$orderItem = $this->prophesize(OrderItem::class);
$giftCardInformation = $this->prophesize(GiftCardInformationInterface::class);
$orderItemUnit = $this->prophesize(OrderItemUnit::class);

$orderItem->getUnits()->willReturn(new ArrayCollection([$orderItemUnit->reveal()]));

$product = $this->prophesize(Product::class);
$product->isGiftCard()->willReturn(true);
$product->isGiftCardAmountConfigurable()->willReturn(true);
$orderItem->getProduct()->willReturn($product);

$giftCardInformation->getAmount()->willReturn(100);
$giftCardInformation->getCustomMessage()->willReturn('custom message');

$giftCardFactory = $this->prophesize(GiftCardFactoryInterface::class);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$formEvent = $this->prophesize(FormEvent::class);
$formEvent->getData()->willReturn(new AddToCartCommand(
$cart->reveal(),
$orderItem->reveal(),
$giftCardInformation->reveal()
));

$giftCard = $this->prophesize(GiftCardInterface::class);
$giftCardFactory->createFromOrderItemUnitAndCart($orderItemUnit, $cart)->willReturn($giftCard);
$giftCard->setCustomMessage('custom message')->shouldBeCalled();

$orderItem->setUnitPrice(100)->shouldBeCalled();
$orderItem->setImmutable(true)->shouldBeCalled();

$extension = new AddToCartTypeExtension($giftCardFactory->reveal(), $entityManager->reveal());
$extension->populateCartItem($formEvent->reveal());
}

/**
* @test
*/
public function it_populates_cart_item_for_not_configurable_gift_card(): void
{
$cart = $this->prophesize(Order::class);
$orderItem = $this->prophesize(OrderItem::class);
$giftCardInformation = $this->prophesize(GiftCardInformationInterface::class);
$orderItemUnit = $this->prophesize(OrderItemUnit::class);

$channel = $this->prophesize(ChannelInterface::class);
$variant = $this->prophesize(ProductVariantInterface::class);
$channelPricing = $this->prophesize(ChannelPricingInterface::class);

$orderItem->getUnits()->willReturn(new ArrayCollection([$orderItemUnit->reveal()]));

$product = $this->prophesize(Product::class);
$product->isGiftCard()->willReturn(true);
$product->isGiftCardAmountConfigurable()->willReturn(false);
$orderItem->getProduct()->willReturn($product);
$cart->getChannel()->willReturn($channel);
$orderItem->getVariant()->willReturn($variant);
$variant->getChannelPricingForChannel($channel)->willReturn($channelPricing);
$channelPricing->getPrice()->willReturn(100);

$giftCardFactory = $this->prophesize(GiftCardFactoryInterface::class);
$entityManager = $this->prophesize(EntityManagerInterface::class);
$formEvent = $this->prophesize(FormEvent::class);
$formEvent->getData()->willReturn(new AddToCartCommand(
$cart->reveal(),
$orderItem->reveal(),
$giftCardInformation->reveal()
));

$giftCard = $this->prophesize(GiftCardInterface::class);
$giftCardFactory->createFromOrderItemUnitAndCart($orderItemUnit, $cart)->willReturn($giftCard);
$orderItem->setUnitPrice(100)->shouldBeCalled();

$extension = new AddToCartTypeExtension($giftCardFactory->reveal(), $entityManager->reveal());
$extension->populateCartItem($formEvent->reveal());
}
}

0 comments on commit 13401d9

Please sign in to comment.