From a24fb0e3749c4c123563fc0bf2bbe6cbcdc7ed3c Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Wed, 26 Jun 2024 14:08:58 +0200 Subject: [PATCH] Do not use minimum price distributor --- .../config/services/order_processing.xml | 2 +- src/Setter/OrderDiscountAdjustmentSetter.php | 9 +++------ .../OrderDiscountAdjustmentSetterTest.php | 19 ++++++++----------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/Resources/config/services/order_processing.xml b/src/Resources/config/services/order_processing.xml index 20964b8..9c8a21d 100644 --- a/src/Resources/config/services/order_processing.xml +++ b/src/Resources/config/services/order_processing.xml @@ -70,7 +70,7 @@ id="setono_sylius_order_edit.setter.order_discount_adjustment" class="Setono\SyliusOrderEditPlugin\Setter\OrderDiscountAdjustmentSetter" > - + diff --git a/src/Setter/OrderDiscountAdjustmentSetter.php b/src/Setter/OrderDiscountAdjustmentSetter.php index b61edd4..d0f5396 100644 --- a/src/Setter/OrderDiscountAdjustmentSetter.php +++ b/src/Setter/OrderDiscountAdjustmentSetter.php @@ -6,28 +6,25 @@ use Setono\SyliusOrderEditPlugin\Adder\DiscountAdjustmentsAdderInterface; use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes; -use Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface; -use Sylius\Component\Core\Model\ChannelInterface; +use Sylius\Component\Core\Distributor\IntegerDistributorInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\OrderItemInterface; final class OrderDiscountAdjustmentSetter implements OrderDiscountAdjustmentSetterInterface { public function __construct( - private readonly MinimumPriceDistributorInterface $minimumPriceDistributor, + private readonly IntegerDistributorInterface $integerDistributor, private readonly DiscountAdjustmentsAdderInterface $orderItemDiscountAdjustmentAdder, ) { } public function set(OrderInterface $order, int $discount): void { - /** @var ChannelInterface $channel */ - $channel = $order->getChannel(); $items = $order->getItems(); /** @var int $orderId */ $orderId = $order->getId(); - $distributedPrices = $this->minimumPriceDistributor->distribute($items->toArray(), $discount, $channel, true); + $distributedPrices = $this->integerDistributor->distribute($discount, $items->count()); /** @var int $distribution */ foreach ($distributedPrices as $i => $distribution) { diff --git a/tests/Unit/Setter/OrderDiscountAdjustmentSetterTest.php b/tests/Unit/Setter/OrderDiscountAdjustmentSetterTest.php index fe63b5f..74deb92 100644 --- a/tests/Unit/Setter/OrderDiscountAdjustmentSetterTest.php +++ b/tests/Unit/Setter/OrderDiscountAdjustmentSetterTest.php @@ -11,8 +11,7 @@ use Setono\SyliusOrderEditPlugin\Entity\EditableOrderInterface; use Setono\SyliusOrderEditPlugin\Model\AdjustmentTypes; use Setono\SyliusOrderEditPlugin\Setter\OrderDiscountAdjustmentSetter; -use Sylius\Component\Core\Distributor\MinimumPriceDistributorInterface; -use Sylius\Component\Core\Model\ChannelInterface; +use Sylius\Component\Core\Distributor\IntegerDistributorInterface; use Sylius\Component\Core\Model\OrderItemInterface; final class OrderDiscountAdjustmentSetterTest extends TestCase @@ -21,34 +20,32 @@ final class OrderDiscountAdjustmentSetterTest extends TestCase public function testItDistributesDiscountsOnOrderItemUnits(): void { - $minimumPriceDistributor = $this->prophesize(MinimumPriceDistributorInterface::class); + $integerDistributor = $this->prophesize(IntegerDistributorInterface::class); $orderItemDiscountAdjustmentAdder = $this->prophesize(DiscountAdjustmentsAdderInterface::class); $setter = new OrderDiscountAdjustmentSetter( - $minimumPriceDistributor->reveal(), + $integerDistributor->reveal(), $orderItemDiscountAdjustmentAdder->reveal(), ); $order = $this->prophesize(EditableOrderInterface::class); $firstItem = $this->prophesize(OrderItemInterface::class); $secondItem = $this->prophesize(OrderItemInterface::class); - $channel = $this->prophesize(ChannelInterface::class); $order->getItems()->willReturn(new ArrayCollection([$firstItem->reveal(), $secondItem->reveal()])); - $order->getChannel()->willReturn($channel->reveal()); $order->getId()->willReturn(100); - $minimumPriceDistributor - ->distribute([$firstItem->reveal(), $secondItem->reveal()], 1000, $channel->reveal(), true) - ->willReturn([400, 600]) + $integerDistributor + ->distribute(1000, 2) + ->willReturn([500, 500]) ; $orderItemDiscountAdjustmentAdder - ->add($firstItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT . '_100', 'Custom order discount', -400) + ->add($firstItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT . '_100', 'Custom order discount', -500) ->shouldBeCalled() ; $orderItemDiscountAdjustmentAdder - ->add($secondItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT . '_100', 'Custom order discount', -600) + ->add($secondItem->reveal(), AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT, AdjustmentTypes::SETONO_ADMIN_ORDER_DISCOUNT . '_100', 'Custom order discount', -500) ->shouldBeCalled() ;