From 2c18e148b691d87080f177a306ceda7149c4abd7 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Thu, 9 Jan 2025 16:52:44 +0100 Subject: [PATCH] Remove Winzou State Machine usages --- .../Ui/Admin/ManagingOrdersContext.php | 48 +++++++++++-------- tests/Behat/Resources/services/context.xml | 2 +- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/tests/Behat/Context/Ui/Admin/ManagingOrdersContext.php b/tests/Behat/Context/Ui/Admin/ManagingOrdersContext.php index 5fa5424..00c8a87 100644 --- a/tests/Behat/Context/Ui/Admin/ManagingOrdersContext.php +++ b/tests/Behat/Context/Ui/Admin/ManagingOrdersContext.php @@ -6,25 +6,22 @@ use Behat\Behat\Context\Context; use Doctrine\Persistence\ObjectManager; -use SM\Factory\FactoryInterface; use Stripe\Checkout\Session; use Stripe\PaymentIntent; +use Sylius\Abstraction\StateMachine\StateMachineInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\PaymentInterface; use Sylius\Component\Payment\PaymentTransitions; -use Sylius\Component\Resource\StateMachine\StateMachineInterface; use Tests\FluxSE\SyliusPayumStripePlugin\Behat\Mocker\StripeCheckoutSessionMocker; +use Webmozart\Assert\Assert; class ManagingOrdersContext implements Context { - private readonly FactoryInterface $stateMachineFactory; - public function __construct( - FactoryInterface $stateMachineFactory, + private readonly StateMachineInterface $stateMachine, private readonly ObjectManager $objectManager, private readonly StripeCheckoutSessionMocker $stripeCheckoutSessionMocker, ) { - $this->stateMachineFactory = $stateMachineFactory; } /** @@ -43,9 +40,7 @@ public function thisOrderIsAlreadyPaid(OrderInterface $order, string $stripePaym ]; $payment->setDetails($details); - /** @var StateMachineInterface $stateMachine */ - $stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH); - $stateMachine->apply(PaymentTransitions::TRANSITION_COMPLETE); + $this->applyTransitionToState($payment, PaymentTransitions::TRANSITION_COMPLETE); $this->objectManager->flush(); } @@ -66,9 +61,7 @@ public function thisOrderIsAlreadyAuthorized(OrderInterface $order, string $stri ]; $payment->setDetails($details); - /** @var StateMachineInterface $stateMachine */ - $stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH); - $stateMachine->apply(PaymentTransitions::TRANSITION_AUTHORIZE); + $this->applyTransitionToState($payment, PaymentTransitions::TRANSITION_AUTHORIZE); $this->objectManager->flush(); } @@ -81,13 +74,12 @@ public function thisOrderIsNotYetPaidStripeCheckoutSession(OrderInterface $order /** @var PaymentInterface $payment */ $payment = $order->getPayments()->first(); - $details = [ + $payment->setDetails([ 'object' => Session::OBJECT_NAME, 'id' => $stripeCheckoutSessionId, 'status' => Session::STATUS_OPEN, 'payment_status' => Session::PAYMENT_STATUS_UNPAID, - ]; - $payment->setDetails($details); + ]); $this->objectManager->flush(); } @@ -100,12 +92,11 @@ public function thisOrderIsNotYetPaidStripeJs(OrderInterface $order, string $str /** @var PaymentInterface $payment */ $payment = $order->getPayments()->first(); - $details = [ + $payment->setDetails([ 'object' => PaymentIntent::OBJECT_NAME, 'id' => $stripePaymentIntentId, 'status' => PaymentIntent::STATUS_REQUIRES_PAYMENT_METHOD, - ]; - $payment->setDetails($details); + ]); $this->objectManager->flush(); } @@ -118,9 +109,7 @@ public function thisOrderPaymentHasBeenCancelled(OrderInterface $order): void /** @var PaymentInterface $payment */ $payment = $order->getPayments()->first(); - /** @var StateMachineInterface $stateMachine */ - $stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH); - $stateMachine->apply(PaymentTransitions::TRANSITION_CANCEL); + $this->applyTransitionToState($payment, PaymentTransitions::TRANSITION_CANCEL); $this->objectManager->flush(); } @@ -181,4 +170,21 @@ public function iAmPreparedToCaptureAuthorizationOfThisOrder(OrderInterface $ord $this->stripeCheckoutSessionMocker->mockCaptureAuthorization($status, $captureMethod); } + + private function applyTransitionToState(PaymentInterface $payment, string $state): void + { + $transition = $this->stateMachine->getTransitionToState( + $payment, + PaymentTransitions::GRAPH, + $state, + ); + + Assert::notNull($transition, 'Transition cannot be null at this point.'); + + $this->stateMachine->apply( + $payment, + PaymentTransitions::GRAPH, + $transition, + ); + } } diff --git a/tests/Behat/Resources/services/context.xml b/tests/Behat/Resources/services/context.xml index 60226a7..a6fd915 100644 --- a/tests/Behat/Resources/services/context.xml +++ b/tests/Behat/Resources/services/context.xml @@ -19,7 +19,7 @@ - +