Skip to content

Commit

Permalink
Remove Winzou State Machine usages
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed Jan 9, 2025
1 parent 6e21f3f commit 2c18e14
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
48 changes: 27 additions & 21 deletions tests/Behat/Context/Ui/Admin/ManagingOrdersContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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,
);
}
}
2 changes: 1 addition & 1 deletion tests/Behat/Resources/services/context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<service id="tests.flux_se.sylius_payum_stripe_plugin.behat.context.ui.admin.managing_orders"
class="Tests\FluxSE\SyliusPayumStripePlugin\Behat\Context\Ui\Admin\ManagingOrdersContext">
<argument type="service" id="sm.factory" />
<argument type="service" id="sylius_abstraction.state_machine" />
<argument type="service" id="sylius.manager.order" />
<argument type="service" id="tests.flux_se.sylius_payum_stripe_plugin.behat.mocker.stripe_checkout_session_mocker" />
</service>
Expand Down

0 comments on commit 2c18e14

Please sign in to comment.