Skip to content

Commit

Permalink
[CreditMemo] Refactor creating CreditMemo to use a factory
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed May 24, 2021
1 parent b7bb062 commit acf9cdd
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 157 deletions.
19 changes: 19 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@
1. `Sylius\RefundPlugin\Grid\Filter\ChannelFilter` and `Sylius\RefundPlugin\Form\Type\ChannelFilterType` services
have been removed and channel filter configuration in grid has been replaced by entity filter.

1. Constructor of `Sylius\RefundPlugin\Entity\CreditMemo` has been removed and now `CreditMemo` entity
is created by `Sylius\RefundPlugin\Factory\CreditMemoFactory`.

1. The constructor of `Sylius\RefundPlugin\Generator\CreditMemoGenerator` has been changed:

```diff
public function __construct(
LineItemsConverterInterface $lineItemsConverter,
LineItemsConverterInterface $shipmentLineItemsConverter,
TaxItemsGeneratorInterface $taxItemsGenerator,
- NumberGenerator $creditMemoNumberGenerator,
- CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider,
- CreditMemoIdentifierGeneratorInterface $uuidCreditMemoIdentifierGenerator
+ CreditMemoFactoryInterface $creditMemoFactory
) {
...
}
```

### UPGRADE FROM 1.0.0-RC.7 TO 1.0.0-RC.8

1. The `fully_refunded` state and the `refund` transition have been removed from `sylius_order` state machine.
Expand Down
64 changes: 23 additions & 41 deletions spec/Entity/CreditMemoSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,111 +9,93 @@
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
use Sylius\RefundPlugin\Entity\CustomerBillingData;
use Sylius\RefundPlugin\Entity\CustomerBillingDataInterface;
use Sylius\RefundPlugin\Entity\LineItemInterface;
use Sylius\RefundPlugin\Entity\ShopBillingData;
use Sylius\RefundPlugin\Entity\ShopBillingDataInterface;
use Sylius\RefundPlugin\Entity\TaxItemInterface;

final class CreditMemoSpec extends ObjectBehavior
{
function let(
OrderInterface $order,
ChannelInterface $channel,
LineItemInterface $lineItem,
TaxItemInterface $taxItem
): void {
$this->beConstructedWith(
'7903c83a-4c5e-4bcf-81d8-9dc304c6a353',
'2018/07/00003333',
$order,
1000,
'USD',
'en_US',
$channel,
[$lineItem->getWrappedObject()],
[$taxItem->getWrappedObject()],
'Comment',
new \DateTimeImmutable('01-01-2020 10:10:10'),
new CustomerBillingData('Rick', 'Sanchez', 'Main St. 3322', '90802', 'US', 'Curse Purge Plus!', 'Los Angeles', 'Baldwin Hills', '323'),
new ShopBillingData('Needful Things', '000222', 'US', 'Main St. 123', 'Los Angeles', '90001')
);
}

function it_implements_a_credit_memo_interface(): void
{
$this->shouldImplement(CreditMemoInterface::class);
}

function it_has_id(): void
function it_has_an_id(): void
{
$this->setId('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');
$this->getId()->shouldReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');
}

function it_has_number(): void
function it_has_a_number(): void
{
$this->setNumber('2018/07/00003333');
$this->getNumber()->shouldReturn('2018/07/00003333');
}

function it_has_an_order(OrderInterface $order): void
{
$this->setOrder($order);
$this->getOrder()->shouldReturn($order);
}

function it_has_total(): void
function it_has_a_total(): void
{
$this->setTotal(1000);
$this->getTotal()->shouldReturn(1000);
}

function it_has_a_currency_code(): void
{
$this->setCurrencyCode('USD');
$this->getCurrencyCode()->shouldReturn('USD');
}

function it_has_a_locale_code(): void
{
$this->setLocaleCode('en_US');
$this->getLocaleCode()->shouldReturn('en_US');
}

function it_has_a_channel(ChannelInterface $channel): void
{
$this->setChannel($channel);
$this->getChannel()->shouldReturn($channel);
}

function it_has_line_items(LineItemInterface $lineItem): void
{
$this->setLineItems(new ArrayCollection([$lineItem->getWrappedObject()]));
$this->getLineItems()->shouldBeLike(new ArrayCollection([$lineItem->getWrappedObject()]));
}

function it_has_tax_items(TaxItemInterface $taxItem): void
{
$this->setTaxItems(new ArrayCollection([$taxItem->getWrappedObject()]));
$this->getTaxItems()->shouldBeLike(new ArrayCollection([$taxItem->getWrappedObject()]));
}

function it_has_a_date_of_creation(): void
{
$this->getIssuedAt()->shouldBeLike(new \DateTime('01-01-2020 10:10:10'));
$this->setIssuedAt(new \DateTimeImmutable('01-01-2020 10:10:10'));
$this->getIssuedAt()->shouldBeLike(new \DateTimeImmutable('01-01-2020 10:10:10'));
}

function it_has_a_comment(): void
{
$this->setComment('Comment');
$this->getComment()->shouldReturn('Comment');
}

function it_has_a_from_address(): void
function it_has_a_from_address(CustomerBillingDataInterface $from): void
{
$this
->getFrom()
->shouldBeLike(
new CustomerBillingData('Rick', 'Sanchez', 'Main St. 3322', '90802', 'US', 'Curse Purge Plus!', 'Los Angeles', 'Baldwin Hills', '323')
)
;
$this->setFrom($from);
$this->getFrom()->shouldReturn($from);
}

function it_has_a_to_address(): void
function it_has_a_to_address(ShopBillingDataInterface $to): void
{
$this
->getTo()
->shouldBeLike(new ShopBillingData('Needful Things', '000222', 'US', 'Main St. 123', 'Los Angeles', '90001'))
;
$this->setTo($to);
$this->getTo()->shouldReturn($to);
}
}
93 changes: 93 additions & 0 deletions spec/Factory/CreditMemoFactorySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\RefundPlugin\Factory;

use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
use Sylius\RefundPlugin\Entity\CustomerBillingDataInterface;
use Sylius\RefundPlugin\Entity\LineItemInterface;
use Sylius\RefundPlugin\Entity\ShopBillingDataInterface;
use Sylius\RefundPlugin\Entity\TaxItemInterface;
use Sylius\RefundPlugin\Factory\CreditMemoFactoryInterface;
use Sylius\RefundPlugin\Generator\CreditMemoIdentifierGeneratorInterface;
use Sylius\RefundPlugin\Generator\NumberGenerator;
use Sylius\RefundPlugin\Provider\CurrentDateTimeImmutableProviderInterface;

final class CreditMemoFactorySpec extends ObjectBehavior
{
function let(
FactoryInterface $creditMemoFactory,
CreditMemoIdentifierGeneratorInterface $creditMemoIdentifierGenerator,
NumberGenerator $creditMemoNumberGenerator,
CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider
): void {
$this->beConstructedWith(
$creditMemoFactory,
$creditMemoIdentifierGenerator,
$creditMemoNumberGenerator,
$currentDateTimeImmutableProvider
);
}
function it_implements_a_credit_memo_factory_interface(): void
{
$this->shouldImplement(CreditMemoFactoryInterface::class);
}

function it_creates_a_new_credit_memo(
FactoryInterface $creditMemoFactory,
CreditMemoInterface $creditMemo
): void {
$creditMemoFactory->createNew()->willReturn($creditMemo);

$this->createNew()->shouldReturn($creditMemo);
}

function it_creates_a_new_credit_memo_with_data(
FactoryInterface $creditMemoFactory,
CreditMemoIdentifierGeneratorInterface $creditMemoIdentifierGenerator,
NumberGenerator $creditMemoNumberGenerator,
CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider,
CreditMemoInterface $creditMemo,
OrderInterface $order,
ChannelInterface $channel,
LineItemInterface $firstLineItem,
LineItemInterface $secondLineItem,
TaxItemInterface $taxItem,
CustomerBillingDataInterface $from,
ShopBillingDataInterface $to
): void {
$creditMemoIdentifierGenerator->generate()->willReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');
$creditMemoNumberGenerator->generate()->willReturn('2018/07/00001111');
$currentDateTimeImmutableProvider->now()->willReturn(new \DateTimeImmutable('01-01-2020 10:10:10'));

$order->getChannel()->willReturn($channel);
$order->getCurrencyCode()->willReturn('USD');
$order->getLocaleCode()->willReturn('en_US');

$creditMemoFactory->createNew()->willReturn($creditMemo);
$creditMemo->setId('7903c83a-4c5e-4bcf-81d8-9dc304c6a353')->shouldBeCalled();
$creditMemo->setNumber('2018/07/00001111')->shouldBeCalled();
$creditMemo->setOrder($order)->shouldBeCalled();
$creditMemo->setChannel($channel)->shouldBeCalled();
$creditMemo->setCurrencyCode('USD')->shouldBeCalled();
$creditMemo->setLocaleCode('en_US')->shouldBeCalled();
$creditMemo->setTotal(1400)->shouldBeCalled();
$creditMemo->setLineItems(new ArrayCollection([$firstLineItem->getWrappedObject(), $secondLineItem->getWrappedObject()]))->shouldBeCalled();
$creditMemo->setTaxItems(new ArrayCollection([$taxItem->getWrappedObject()]))->shouldBeCalled();
$creditMemo->setComment('Comment')->shouldBeCalled();
$creditMemo->setIssuedAt(new \DateTimeImmutable('01-01-2020 10:10:10'))->shouldBeCalled();
$creditMemo->setFrom($from)->shouldBeCalled();
$creditMemo->setTo($to)->shouldBeCalled();

$this
->createWithData($order, 1400, [$firstLineItem, $secondLineItem], [$taxItem], 'Comment', $from, $to)
->shouldBeLike($creditMemo)
;
}
}
60 changes: 21 additions & 39 deletions spec/Generator/CreditMemoGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
use Sylius\Component\Core\Model\ShopBillingDataInterface;
use Sylius\RefundPlugin\Converter\LineItemsConverterInterface;
use Sylius\RefundPlugin\Entity\CreditMemo;
use Sylius\RefundPlugin\Entity\CreditMemoInterface;
use Sylius\RefundPlugin\Entity\CustomerBillingData;
use Sylius\RefundPlugin\Entity\LineItemInterface;
use Sylius\RefundPlugin\Entity\ShopBillingData;
use Sylius\RefundPlugin\Entity\TaxItemInterface;
use Sylius\RefundPlugin\Factory\CreditMemoFactoryInterface;
use Sylius\RefundPlugin\Generator\CreditMemoGeneratorInterface;
use Sylius\RefundPlugin\Generator\CreditMemoIdentifierGeneratorInterface;
use Sylius\RefundPlugin\Generator\NumberGenerator;
Expand All @@ -29,17 +31,13 @@ function let(
LineItemsConverterInterface $lineItemsConverter,
LineItemsConverterInterface $shipmentLineItemsConverter,
TaxItemsGeneratorInterface $taxItemsGenerator,
NumberGenerator $creditMemoNumberGenerator,
CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider,
CreditMemoIdentifierGeneratorInterface $creditMemoIdentifierGenerator
CreditMemoFactoryInterface $creditMemoFactory
): void {
$this->beConstructedWith(
$lineItemsConverter,
$shipmentLineItemsConverter,
$taxItemsGenerator,
$creditMemoNumberGenerator,
$currentDateTimeImmutableProvider,
$creditMemoIdentifierGenerator
$creditMemoFactory
);
}

Expand All @@ -52,30 +50,21 @@ function it_generates_credit_memo_basing_on_event_data(
LineItemsConverterInterface $lineItemsConverter,
LineItemsConverterInterface $shipmentLineItemsConverter,
TaxItemsGeneratorInterface $taxItemsGenerator,
NumberGenerator $creditMemoNumberGenerator,
CurrentDateTimeImmutableProviderInterface $currentDateTimeImmutableProvider,
CreditMemoIdentifierGeneratorInterface $creditMemoIdentifierGenerator,
CreditMemoFactoryInterface $creditMemoFactory,
CreditMemoInterface $creditMemo,
OrderInterface $order,
ChannelInterface $channel,
ShopBillingDataInterface $shopBillingData,
AddressInterface $customerBillingAddress,
LineItemInterface $firstLineItem,
LineItemInterface $secondLineItem,
TaxItemInterface $taxItem,
\DateTimeImmutable $dateTime
TaxItemInterface $taxItem
): void {
$firstUnitRefund = new OrderItemUnitRefund(1, 500);
$secondUnitRefund = new OrderItemUnitRefund(3, 500);
$shipmentRefund = new ShipmentRefund(3, 400);

$order->getCurrencyCode()->willReturn('GBP');
$order->getLocaleCode()->willReturn('en_US');

$order->getChannel()->willReturn($channel);
$channel->getCode()->willReturn('WEB-US');
$channel->getName()->willReturn('United States');
$channel->getColor()->willReturn('Linen');

$channel->getShopBillingData()->willReturn($shopBillingData);
$shopBillingData->getCompany()->willReturn('Needful Things');
$shopBillingData->getTaxId()->willReturn('000222');
Expand All @@ -100,26 +89,19 @@ function it_generates_credit_memo_basing_on_event_data(

$taxItemsGenerator->generate([$firstLineItem, $secondLineItem])->willReturn([$taxItem]);

$creditMemoNumberGenerator->generate()->willReturn('2018/07/00001111');

$currentDateTimeImmutableProvider->now()->willReturn($dateTime);

$creditMemoIdentifierGenerator->generate()->willReturn('7903c83a-4c5e-4bcf-81d8-9dc304c6a353');

$this->generate($order, 1400, [$firstUnitRefund, $secondUnitRefund], [$shipmentRefund], 'Comment')->shouldBeLike(new CreditMemo(
'7903c83a-4c5e-4bcf-81d8-9dc304c6a353',
'2018/07/00001111',
$order->getWrappedObject(),
1400,
'GBP',
'en_US',
$channel->getWrappedObject(),
[$firstLineItem->getWrappedObject(), $secondLineItem->getWrappedObject()],
[$taxItem->getWrappedObject()],
'Comment',
$dateTime->getWrappedObject(),
new CustomerBillingData('Rick', 'Sanchez', 'Universe St. 444', '000333', 'US', 'Los Angeles', 'Curse Purge Plus!'),
new ShopBillingData('Needful Things', '000222', 'US', 'Main St. 123', 'New York', '90222')
));
$creditMemoFactory
->createWithData(
$order,
1400,
[$firstLineItem, $secondLineItem],
[$taxItem],
'Comment',
new CustomerBillingData('Rick', 'Sanchez', 'Universe St. 444', '000333', 'US', 'Los Angeles', 'Curse Purge Plus!'),
new ShopBillingData('Needful Things', '000222', 'US', 'Main St. 123', 'New York', '90222')
)
->willReturn($creditMemo)
;

$this->generate($order, 1400, [$firstUnitRefund, $secondUnitRefund], [$shipmentRefund], 'Comment')->shouldReturn($creditMemo);
}
}
Loading

0 comments on commit acf9cdd

Please sign in to comment.