From 538448d174cd0f735c9a3a9c79872923edb0182e Mon Sep 17 00:00:00 2001 From: venyii Date: Tue, 8 May 2018 14:25:46 +0200 Subject: [PATCH] Fix removal of cart payments in OrderPaymentProcessor The order does not accept a $state property to filter payments --- .../OrderProcessing/OrderPaymentProcessor.php | 6 +++++- .../OrderProcessing/OrderPaymentProcessorSpec.php | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Sylius/Component/Core/OrderProcessing/OrderPaymentProcessor.php b/src/Sylius/Component/Core/OrderProcessing/OrderPaymentProcessor.php index 637342f6982..500095478e2 100644 --- a/src/Sylius/Component/Core/OrderProcessing/OrderPaymentProcessor.php +++ b/src/Sylius/Component/Core/OrderProcessing/OrderPaymentProcessor.php @@ -59,7 +59,11 @@ public function process(BaseOrderInterface $order): void } if (0 === $order->getTotal()) { - foreach ($order->getPayments(OrderPaymentStates::STATE_CART) as $payment) { + $removablePayments = $order->getPayments()->filter(function (PaymentInterface $payment): bool { + return $payment->getState() === OrderPaymentStates::STATE_CART; + }); + + foreach ($removablePayments as $payment) { $order->removePayment($payment); } diff --git a/src/Sylius/Component/Core/spec/OrderProcessing/OrderPaymentProcessorSpec.php b/src/Sylius/Component/Core/spec/OrderProcessing/OrderPaymentProcessorSpec.php index 0aa9a7adc6e..faeb86bf415 100644 --- a/src/Sylius/Component/Core/spec/OrderProcessing/OrderPaymentProcessorSpec.php +++ b/src/Sylius/Component/Core/spec/OrderProcessing/OrderPaymentProcessorSpec.php @@ -14,6 +14,7 @@ namespace spec\Sylius\Component\Core\OrderProcessing; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Sylius\Component\Core\Model\OrderInterface; @@ -46,13 +47,21 @@ function it_throws_exception_if_passed_order_is_not_core_order(BaseOrderInterfac function it_removes_cart_payments_from_order_if_its_total_is_zero( OrderInterface $order, - PaymentInterface $cartPayment + PaymentInterface $cartPayment, + PaymentInterface $cancelledPayment, + Collection $payments ): void { $order->getState()->willReturn(OrderInterface::STATE_CART); $order->getTotal()->willReturn(0); - $order->getPayments(OrderPaymentStates::STATE_CART)->willReturn(new ArrayCollection([$cartPayment->getWrappedObject()])); - $order->removePayment($cartPayment)->shouldBeCalled(); + $cartPayment->getState()->willReturn(OrderPaymentStates::STATE_CART); + $cancelledPayment->getState()->willReturn(OrderPaymentStates::STATE_CANCELLED); + + $payments->filter(Argument::type(\Closure::class))->willReturn([$cartPayment]); + + $order->getPayments()->willReturn($payments); + $order->removePayment($cartPayment)->shouldBeCalledTimes(1); + $order->removePayment($cancelledPayment)->shouldNotBeCalled(); $this->process($order); }