Skip to content

Commit

Permalink
Use the new factory in line items converters
Browse files Browse the repository at this point in the history
  • Loading branch information
NoResponseMate committed Feb 12, 2024
1 parent 69594b7 commit 0942bc9
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 38 deletions.
149 changes: 132 additions & 17 deletions spec/Converter/LineItem/OrderItemUnitLineItemsConverterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\RefundPlugin\Converter\LineItem\LineItemsConverterInterface;
use Sylius\RefundPlugin\Entity\LineItem;
use Sylius\RefundPlugin\Entity\LineItemInterface;
use Sylius\RefundPlugin\Factory\LineItemFactoryInterface;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Sylius\RefundPlugin\Provider\TaxRateProviderInterface;

final class OrderItemUnitLineItemsConverterSpec extends ObjectBehavior
{
function let(RepositoryInterface $orderItemUnitRepository, TaxRateProviderInterface $taxRateProvider): void
{
$this->beConstructedWith($orderItemUnitRepository, $taxRateProvider);
function let(
RepositoryInterface $orderItemUnitRepository,
TaxRateProviderInterface $taxRateProvider,
LineItemFactoryInterface $lineItemFactory,
): void {
$this->beConstructedWith($orderItemUnitRepository, $taxRateProvider, $lineItemFactory);
}

function it_implements_line_items_converter_interface(): void
Expand All @@ -38,8 +43,10 @@ function it_implements_line_items_converter_interface(): void
function it_converts_unit_refunds_to_line_items(
RepositoryInterface $orderItemUnitRepository,
TaxRateProviderInterface $taxRateProvider,
LineItemFactoryInterface $lineItemFactory,
OrderItemUnitInterface $orderItemUnit,
OrderItemInterface $orderItem,
LineItemInterface $lineItem,
): void {
$unitRefund = new OrderItemUnitRefund(1, 500);

Expand All @@ -53,6 +60,40 @@ function it_converts_unit_refunds_to_line_items(

$orderItem->getProductName()->willReturn('Portal gun');

$lineItemFactory->createWithData(
'Portal gun',
1,
400,
500,
400,
500,
100,
'25%',
)->willReturn($lineItem);

$this->convert([$unitRefund])->shouldBeLike([$lineItem]);
}

function it_converts_unit_refunds_to_line_items_without_using_factory(
RepositoryInterface $orderItemUnitRepository,
TaxRateProviderInterface $taxRateProvider,
OrderItemUnitInterface $orderItemUnit,
OrderItemInterface $orderItem,
): void {
$this->beConstructedWith($orderItemUnitRepository, $taxRateProvider, null);

$unitRefund = new OrderItemUnitRefund(1, 500);

$orderItemUnitRepository->find(1)->willReturn($orderItemUnit);

$orderItemUnit->getOrderItem()->willReturn($orderItem);
$orderItemUnit->getTotal()->willReturn(1500);
$orderItemUnit->getTaxTotal()->willReturn(300);

$taxRateProvider->provide($orderItemUnit)->willReturn('25%');

$orderItem->getProductName()->willReturn('Portal gun');

$this->convert([$unitRefund])->shouldBeLike([new LineItem(
'Portal gun',
1,
Expand All @@ -65,25 +106,91 @@ function it_converts_unit_refunds_to_line_items(
)]);
}

function it_throws_an_error_if_one_of_units_is_not_order_item_unit_refund(): void
{
$unitRefund = new OrderItemUnitRefund(1, 500);
$shipmentRefund = new ShipmentRefund(3, 1500);
function it_groups_the_same_line_items_during_converting(
RepositoryInterface $orderItemUnitRepository,
TaxRateProviderInterface $taxRateProvider,
LineItemFactoryInterface $lineItemFactory,
OrderItemUnitInterface $firstOrderItemUnit,
OrderItemUnitInterface $secondOrderItemUnit,
OrderItemInterface $firstOrderItem,
OrderItemInterface $secondOrderItem,
LineItemInterface $firstLineItem,
LineItemInterface $secondLineItem,
LineItemInterface $thirdLineItem,
): void {
$firstUnitRefund = new OrderItemUnitRefund(1, 500);
$secondUnitRefund = new OrderItemUnitRefund(2, 960);
$thirdUnitRefund = new OrderItemUnitRefund(2, 960);

$orderItemUnitRepository->find(1)->willReturn($firstOrderItemUnit);

$firstOrderItemUnit->getOrderItem()->willReturn($firstOrderItem);
$firstOrderItemUnit->getTotal()->willReturn(1500);
$firstOrderItemUnit->getTaxTotal()->willReturn(300);

$taxRateProvider->provide($firstOrderItemUnit)->willReturn('25%');

$firstOrderItem->getProductName()->willReturn('Portal gun');

$orderItemUnitRepository->find(2)->willReturn($secondOrderItemUnit);

$secondOrderItemUnit->getOrderItem()->willReturn($secondOrderItem);
$secondOrderItemUnit->getTotal()->willReturn(960);
$secondOrderItemUnit->getTaxTotal()->willReturn(160);

$taxRateProvider->provide($secondOrderItemUnit)->willReturn('20%');

$secondOrderItem->getProductName()->willReturn('Space gun');

$lineItemFactory->createWithData(
'Portal gun',
1,
400,
500,
400,
500,
100,
'25%',
)->willReturn($firstLineItem);

$lineItemFactory->createWithData(
'Space gun',
1,
800,
960,
800,
960,
160,
'20%',
)->willReturn($secondLineItem, $thirdLineItem);

$firstLineItem
->compare($secondLineItem)
->willReturn(false);
$firstLineItem
->compare($thirdLineItem)
->willReturn(false);

$secondLineItem
->compare($thirdLineItem)
->willReturn(true);
$secondLineItem->merge($thirdLineItem)->shouldBeCalled();

$this
->shouldThrow(\InvalidArgumentException::class)
->during('convert', [[$unitRefund, $shipmentRefund]])
;
->convert([$firstUnitRefund, $secondUnitRefund, $thirdUnitRefund])
->shouldBeLike([$firstLineItem, $secondLineItem]);
}

function it_groups_the_same_line_items_during_converting(
function it_groups_the_same_line_items_during_converting_without_using_factory(
RepositoryInterface $orderItemUnitRepository,
TaxRateProviderInterface $taxRateProvider,
OrderItemUnitInterface $firstOrderItemUnit,
OrderItemUnitInterface $secondOrderItemUnit,
OrderItemInterface $firstOrderItem,
OrderItemInterface $secondOrderItem,
): void {
$this->beConstructedWith($orderItemUnitRepository, $taxRateProvider, null);

$firstUnitRefund = new OrderItemUnitRefund(1, 500);
$secondUnitRefund = new OrderItemUnitRefund(2, 960);
$thirdUnitRefund = new OrderItemUnitRefund(2, 960);
Expand Down Expand Up @@ -132,7 +239,17 @@ function it_groups_the_same_line_items_during_converting(
]);
}

function it_throws_an_exception_if_there_is_no_shipping_adjustment_with_given_id(
function it_throws_an_error_if_one_of_units_is_not_order_item_unit_refund(): void
{
$unitRefund = new OrderItemUnitRefund(1, 500);
$shipmentRefund = new ShipmentRefund(3, 1500);

$this
->shouldThrow(\InvalidArgumentException::class)
->during('convert', [[$unitRefund, $shipmentRefund]]);
}

function it_throws_an_exception_if_there_is_no_order_item_unit_with_given_id(
RepositoryInterface $orderItemUnitRepository,
): void {
$unitRefund = new OrderItemUnitRefund(1, 500);
Expand All @@ -141,11 +258,10 @@ function it_throws_an_exception_if_there_is_no_shipping_adjustment_with_given_id

$this
->shouldThrow(\InvalidArgumentException::class)
->during('convert', [[$unitRefund]])
;
->during('convert', [[$unitRefund]]);
}

function it_throws_an_exception_if_refund_amount_is_higher_than_shipping_amount(
function it_throws_an_exception_if_refund_amount_is_higher_than_order_item_unit_total(
RepositoryInterface $orderItemUnitRepository,
OrderItemUnitInterface $orderItemUnit,
): void {
Expand All @@ -156,7 +272,6 @@ function it_throws_an_exception_if_refund_amount_is_higher_than_shipping_amount(

$this
->shouldThrow(\InvalidArgumentException::class)
->during('convert', [[$unitRefund]])
;
->during('convert', [[$unitRefund]]);
}
}
54 changes: 51 additions & 3 deletions spec/Converter/LineItem/ShipmentLineItemsConverterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\RefundPlugin\Converter\LineItem\LineItemsConverterInterface;
use Sylius\RefundPlugin\Entity\LineItem;
use Sylius\RefundPlugin\Entity\LineItemInterface;
use Sylius\RefundPlugin\Exception\MoreThanOneTaxAdjustment;
use Sylius\RefundPlugin\Factory\LineItemFactoryInterface;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Model\ShipmentRefund;
use Sylius\RefundPlugin\Provider\TaxRateProviderInterface;

final class ShipmentLineItemsConverterSpec extends ObjectBehavior
{
function let(RepositoryInterface $adjustmentRepository, TaxRateProviderInterface $taxRateProvider): void
{
$this->beConstructedWith($adjustmentRepository, $taxRateProvider);
function let(
RepositoryInterface $adjustmentRepository,
TaxRateProviderInterface $taxRateProvider,
LineItemFactoryInterface $lineItemFactory,
): void {
$this->beConstructedWith($adjustmentRepository, $taxRateProvider, $lineItemFactory);
}

function it_implements_line_items_converter_interface(): void
Expand All @@ -38,12 +43,55 @@ function it_implements_line_items_converter_interface(): void
}

function it_converts_shipment_unit_refunds_to_line_items(
RepositoryInterface $adjustmentRepository,
TaxRateProviderInterface $taxRateProvider,
LineItemFactoryInterface $lineItemFactory,
AdjustmentInterface $shippingAdjustment,
AdjustmentInterface $taxAdjustment,
ShipmentInterface $shipment,
LineItemInterface $lineItem,
): void {
$shipmentRefund = new ShipmentRefund(1, 575);

$adjustmentRepository
->findOneBy(['id' => 1, 'type' => AdjustmentInterface::SHIPPING_ADJUSTMENT])
->willReturn($shippingAdjustment)
;
$shippingAdjustment->getLabel()->willReturn('Galaxy post');
$shippingAdjustment->getShipment()->willReturn($shipment);

$shipment->getAdjustmentsTotal()->willReturn(1150);
$shipment
->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT)
->willReturn(new ArrayCollection([$taxAdjustment->getWrappedObject()]))
;

$taxAdjustment->getAmount()->willReturn(150);
$taxRateProvider->provide($shipment)->willReturn('15%');

$lineItemFactory->createWithData(
'Galaxy post',
1,
500,
575,
500,
575,
75,
'15%',
)->willReturn($lineItem);

$this->convert([$shipmentRefund])->shouldBeLike([$lineItem]);
}

function it_converts_shipment_unit_refunds_to_line_items_without_using_factory(
RepositoryInterface $adjustmentRepository,
TaxRateProviderInterface $taxRateProvider,
AdjustmentInterface $shippingAdjustment,
AdjustmentInterface $taxAdjustment,
ShipmentInterface $shipment,
): void {
$this->beConstructedWith($adjustmentRepository, $taxRateProvider, null);

$shipmentRefund = new ShipmentRefund(1, 575);

$adjustmentRepository
Expand Down
41 changes: 32 additions & 9 deletions src/Converter/LineItem/OrderItemUnitLineItemsConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\RefundPlugin\Entity\LineItem;
use Sylius\RefundPlugin\Entity\LineItemInterface;
use Sylius\RefundPlugin\Factory\LineItemFactoryInterface;
use Sylius\RefundPlugin\Model\OrderItemUnitRefund;
use Sylius\RefundPlugin\Provider\TaxRateProviderInterface;
use Webmozart\Assert\Assert;
Expand All @@ -27,7 +28,16 @@ final class OrderItemUnitLineItemsConverter implements LineItemsConverterUnitRef
public function __construct(
private RepositoryInterface $orderItemUnitRepository,
private TaxRateProviderInterface $taxRateProvider,
private ?LineItemFactoryInterface $lineItemFactory,
) {
if (null === $this->lineItemFactory) {
trigger_deprecation(
'sylius/refund-plugin',
'1.5',
'Not passing a line item factory to "%s" is deprecated and will be removed in 2.0.',
self::class,
);
}
}

public function convert(array $units): array
Expand Down Expand Up @@ -67,15 +77,28 @@ private function convertUnitRefundToLineItem(OrderItemUnitRefund $unitRefund): L
$productName = $orderItem->getProductName();
Assert::notNull($productName);

return new LineItem(
$productName,
1,
$netValue,
$grossValue,
$netValue,
$grossValue,
$taxAmount,
$this->taxRateProvider->provide($orderItemUnit),
if (null === $this->lineItemFactory) {
return new LineItem(
$productName,
1,
$netValue,
$grossValue,
$netValue,
$grossValue,
$taxAmount,
$this->taxRateProvider->provide($orderItemUnit),
);
}

return $this->lineItemFactory->createWithData(
name: $productName,
quantity: 1,
unitNetPrice: $netValue,
unitGrossPrice: $grossValue,
netValue: $netValue,
grossValue: $grossValue,
taxAmount: $taxAmount,
taxRate: $this->taxRateProvider->provide($orderItemUnit),
);
}

Expand Down
Loading

0 comments on commit 0942bc9

Please sign in to comment.