diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 648bcafff..b6378e339 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -1,3 +1,7 @@ +# UPGRADE FROM 2.1.0 TO 2.1.1 + +1. `OrderPartiallyRefundedStateResolver` now checks whether the `partially_refund` transition is applicable via the state machine instead of comparing the payment state directly, so the transition (and its side effects) is applied again for every subsequent partial refund on an already partially refunded order. + # UPGRADE FROM 2.0 TO 2.1 1. The minimum supported Symfony version has been raised to `6.4` and `7.4` — earlier 7.x releases (`7.1`, `7.2`, `7.3`) are no longer supported. diff --git a/src/StateResolver/OrderPartiallyRefundedStateResolver.php b/src/StateResolver/OrderPartiallyRefundedStateResolver.php index f11175eae..96b818f95 100644 --- a/src/StateResolver/OrderPartiallyRefundedStateResolver.php +++ b/src/StateResolver/OrderPartiallyRefundedStateResolver.php @@ -16,7 +16,6 @@ use Doctrine\ORM\EntityManagerInterface; use Sylius\Abstraction\StateMachine\StateMachineInterface; use Sylius\Component\Core\Model\OrderInterface; -use Sylius\Component\Core\OrderPaymentStates; use Sylius\Component\Core\OrderPaymentTransitions; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\RefundPlugin\Exception\OrderNotFound; @@ -39,7 +38,7 @@ public function resolve(string $orderNumber): void throw OrderNotFound::withNumber($orderNumber); } - if ($order->getPaymentState() === OrderPaymentStates::STATE_PARTIALLY_REFUNDED) { + if (!$this->stateMachineFactory->can($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND)) { return; } diff --git a/tests/TestApplication/config/config.yaml b/tests/TestApplication/config/config.yaml index 18c891fe6..4dfae8a97 100644 --- a/tests/TestApplication/config/config.yaml +++ b/tests/TestApplication/config/config.yaml @@ -5,3 +5,9 @@ imports: twig: paths: '%kernel.project_dir%/../../../tests/TestApplication/templates': ~ + +knp_snappy: + pdf: + enabled: true + binary: '%env(resolve:WKHTMLTOPDF_PATH)%' + options: [] \ No newline at end of file diff --git a/tests/Unit/StateResolver/OrderPartiallyRefundedStateResolverTest.php b/tests/Unit/StateResolver/OrderPartiallyRefundedStateResolverTest.php index 84758e4e5..7739e95e3 100644 --- a/tests/Unit/StateResolver/OrderPartiallyRefundedStateResolverTest.php +++ b/tests/Unit/StateResolver/OrderPartiallyRefundedStateResolverTest.php @@ -19,7 +19,6 @@ use PHPUnit\Framework\TestCase; use Sylius\Abstraction\StateMachine\StateMachineInterface; use Sylius\Component\Core\Model\OrderInterface; -use Sylius\Component\Core\OrderPaymentStates; use Sylius\Component\Core\OrderPaymentTransitions; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\RefundPlugin\Exception\OrderNotFound; @@ -62,10 +61,11 @@ public function it_marks_order_as_partially_refunded(): void ->with('000777') ->willReturn($order); - $order + $stateMachine ->expects(self::once()) - ->method('getPaymentState') - ->willReturn(OrderPaymentStates::STATE_PAID); + ->method('can') + ->with($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND) + ->willReturn(true); $stateMachine ->expects(self::once()) @@ -80,7 +80,7 @@ public function it_marks_order_as_partially_refunded(): void } #[Test] - public function it_does_nothing_if_order_is_already_marked_as_partially_refunded(): void + public function it_marks_order_as_partially_refunded_again_if_it_is_already_partially_refunded(): void { $orderRepository = $this->createMock(OrderRepositoryInterface::class); $stateMachine = $this->createMock(StateMachineInterface::class); @@ -95,15 +95,54 @@ public function it_does_nothing_if_order_is_already_marked_as_partially_refunded ->with('000777') ->willReturn($order); - $order + $stateMachine ->expects(self::once()) - ->method('getPaymentState') - ->willReturn(OrderPaymentStates::STATE_PARTIALLY_REFUNDED); + ->method('can') + ->with($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND) + ->willReturn(true); + + $stateMachine + ->expects(self::once()) + ->method('apply') + ->with($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND); + + $orderManager + ->expects(self::once()) + ->method('flush'); + + $resolver->resolve('000777'); + } + + #[Test] + public function it_does_nothing_if_partially_refund_transition_is_not_applicable(): void + { + $orderRepository = $this->createMock(OrderRepositoryInterface::class); + $stateMachine = $this->createMock(StateMachineInterface::class); + $orderManager = $this->createMock(EntityManagerInterface::class); + $order = $this->createMock(OrderInterface::class); + + $resolver = new OrderPartiallyRefundedStateResolver($orderRepository, $stateMachine, $orderManager); + + $orderRepository + ->expects(self::once()) + ->method('findOneByNumber') + ->with('000777') + ->willReturn($order); + + $stateMachine + ->expects(self::once()) + ->method('can') + ->with($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND) + ->willReturn(false); $stateMachine ->expects($this->never()) ->method('apply'); + $orderManager + ->expects($this->never()) + ->method('flush'); + $resolver->resolve('000777'); } @@ -139,10 +178,11 @@ public function it_uses_winzou_state_machine_if_abstraction_not_passed_to_mark_o ->with('000777') ->willReturn($order); - $order + $this->stateMachineFactory ->expects(self::once()) - ->method('getPaymentState') - ->willReturn(OrderPaymentStates::STATE_PAID); + ->method('can') + ->with($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND) + ->willReturn(true); $this->stateMachineFactory ->expects(self::once())