diff --git a/src/Checker/OrderPaymentEditionChecker.php b/src/Checker/OrderPaymentEditionChecker.php index e964ece..dc64ecc 100644 --- a/src/Checker/OrderPaymentEditionChecker.php +++ b/src/Checker/OrderPaymentEditionChecker.php @@ -4,7 +4,6 @@ namespace Setono\SyliusOrderEditPlugin\Checker; -use Sylius\Component\Core\OrderPaymentStates; use Sylius\Component\Order\Model\OrderInterface; use Symfony\Component\HttpFoundation\RequestStack; use Webmozart\Assert\Assert; @@ -24,10 +23,6 @@ public function shouldOrderPaymentBeEdited(OrderInterface $order): bool return true; } - if ('setono_sylius_order_edit_admin_update' !== $request->attributes->get('_route')) { - return true; - } - - return $order->getPaymentState() === OrderPaymentStates::STATE_AWAITING_PAYMENT; + return 'setono_sylius_order_edit_admin_update' !== $request->attributes->get('_route'); } } diff --git a/src/Checker/PostCheckoutOrderPaymentEditionChecker.php b/src/Checker/PostCheckoutOrderPaymentEditionChecker.php new file mode 100644 index 0000000..8d02bcb --- /dev/null +++ b/src/Checker/PostCheckoutOrderPaymentEditionChecker.php @@ -0,0 +1,33 @@ +requestStack->getCurrentRequest(); + if (null === $request) { + return true; + } + + if ('setono_sylius_order_edit_admin_update' !== $request->attributes->get('_route')) { + return true; + } + + return $order->getPaymentState() === OrderPaymentStates::STATE_AWAITING_PAYMENT; + } +} diff --git a/src/Resources/config/services/order_processing.xml b/src/Resources/config/services/order_processing.xml index 05bc6eb..a2ef9ef 100644 --- a/src/Resources/config/services/order_processing.xml +++ b/src/Resources/config/services/order_processing.xml @@ -17,7 +17,7 @@ decorates="sylius.order_processing.order_payment_processor.after_checkout" decoration-priority="64" > - + + + + + prophesize(RequestStack::class); $request = new Request([], [], ['_route' => 'setono_sylius_order_edit_admin_update']); $requestStack->getCurrentRequest()->willReturn($request); $checker = new OrderPaymentEditionChecker($requestStack->reveal()); + $order = new Order(); - $order = $this->prophesize(OrderInterface::class); - $order->getPaymentState()->willReturn(OrderPaymentStates::STATE_AUTHORIZED); - - self::assertFalse($checker->shouldOrderPaymentBeEdited($order->reveal())); + self::assertFalse($checker->shouldOrderPaymentBeEdited($order)); } - public function testItSaysOrderPaymentShouldBeEditedItTheRouteIsOrderEditButOrderIsAwaitingPayment(): void - { - $requestStack = $this->prophesize(RequestStack::class); - $request = new Request([], [], ['_route' => 'setono_sylius_order_edit_admin_update']); - $requestStack->getCurrentRequest()->willReturn($request); - - $checker = new OrderPaymentEditionChecker($requestStack->reveal()); - - $order = $this->prophesize(OrderInterface::class); - $order->getPaymentState()->willReturn(OrderPaymentStates::STATE_AWAITING_PAYMENT); - - self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal())); - } - - public function testItSaysOrderPaymentShouldBeEditedIfItsNotOrderEditRoute(): void + public function testItSaysOrderPaymentCanBeEditedIfItsNotOrderEditRoute(): void { $requestStack = $this->prophesize(RequestStack::class); $request = new Request([], [], ['_route' => 'sylius_admin_order_any_other_route']); $requestStack->getCurrentRequest()->willReturn($request); $checker = new OrderPaymentEditionChecker($requestStack->reveal()); + $order = new Order(); - $order = $this->prophesize(OrderInterface::class); - - self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal())); + self::assertTrue($checker->shouldOrderPaymentBeEdited($order)); } - public function testItSaysOrderPaymentShouldBeEditedIfThereIsNoCurrentRequest(): void + public function testItSaysOrderPaymentCanBeEditedIfThereIsNoCurrentRequest(): void { $requestStack = $this->prophesize(RequestStack::class); $requestStack->getCurrentRequest()->willReturn(null); $checker = new OrderPaymentEditionChecker($requestStack->reveal()); + $order = new Order(); - $order = $this->prophesize(OrderInterface::class); - - self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal())); + self::assertTrue($checker->shouldOrderPaymentBeEdited($order)); } } diff --git a/tests/Unit/Checker/PostCheckoutOrderPaymentEditionCheckerTest.php b/tests/Unit/Checker/PostCheckoutOrderPaymentEditionCheckerTest.php new file mode 100644 index 0000000..edf7f00 --- /dev/null +++ b/tests/Unit/Checker/PostCheckoutOrderPaymentEditionCheckerTest.php @@ -0,0 +1,71 @@ +prophesize(RequestStack::class); + $request = new Request([], [], ['_route' => 'setono_sylius_order_edit_admin_update']); + $requestStack->getCurrentRequest()->willReturn($request); + + $checker = new PostCheckoutOrderPaymentEditionChecker($requestStack->reveal()); + + $order = $this->prophesize(OrderInterface::class); + $order->getPaymentState()->willReturn(OrderPaymentStates::STATE_AUTHORIZED); + + self::assertFalse($checker->shouldOrderPaymentBeEdited($order->reveal())); + } + + public function testItSaysOrderPaymentShouldBeEditedItTheRouteIsOrderEditButOrderIsAwaitingPayment(): void + { + $requestStack = $this->prophesize(RequestStack::class); + $request = new Request([], [], ['_route' => 'setono_sylius_order_edit_admin_update']); + $requestStack->getCurrentRequest()->willReturn($request); + + $checker = new PostCheckoutOrderPaymentEditionChecker($requestStack->reveal()); + + $order = $this->prophesize(OrderInterface::class); + $order->getPaymentState()->willReturn(OrderPaymentStates::STATE_AWAITING_PAYMENT); + + self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal())); + } + + public function testItSaysOrderPaymentShouldBeEditedIfItsNotOrderEditRoute(): void + { + $requestStack = $this->prophesize(RequestStack::class); + $request = new Request([], [], ['_route' => 'sylius_admin_order_any_other_route']); + $requestStack->getCurrentRequest()->willReturn($request); + + $checker = new PostCheckoutOrderPaymentEditionChecker($requestStack->reveal()); + + $order = $this->prophesize(OrderInterface::class); + + self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal())); + } + + public function testItSaysOrderPaymentShouldBeEditedIfThereIsNoCurrentRequest(): void + { + $requestStack = $this->prophesize(RequestStack::class); + $requestStack->getCurrentRequest()->willReturn(null); + + $checker = new PostCheckoutOrderPaymentEditionChecker($requestStack->reveal()); + + $order = $this->prophesize(OrderInterface::class); + + self::assertTrue($checker->shouldOrderPaymentBeEdited($order->reveal())); + } +}