Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set cartItem unit price before giftcard creation #194

Merged
merged 4 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Form/Extension/AddToCartTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Webmozart\Assert\Assert;

final class AddToCartTypeExtension extends AbstractTypeExtension
{
Expand Down Expand Up @@ -75,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 @@ -85,12 +87,20 @@ public function populateCartItem(FormEvent $event): void
return;
}

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

if ($product->isGiftCardAmountConfigurable()) {
$cartItem->setUnitPrice($giftCardInformation->getAmount());
$cartItem->setImmutable(true);
} else {
$channel = $data->getCart()->getChannel();
Assert::notNull($channel);
$variant = $data->getCartItem()->getVariant();
Assert::notNull($variant);
$channelPricing = $variant->getChannelPricingForChannel($channel);
Assert::notNull($channelPricing);
$price = $channelPricing->getPrice();
Assert::notNull($price);
$cartItem->setUnitPrice($price);
}

$cart = $data->getCart();
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());
}
}