Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use StateMachine Abstraction instead of hardcoded Winzou #439

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions UPGRADE-1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@
1. Support for Sylius 1.12 has been dropped, upgrade your application to [Sylius 1.13](https://github.com/Sylius/Sylius/blob/1.13/UPGRADE-1.13.md).
or to [Sylius 1.14](https://github.com/Sylius/Sylius/blob/1.14/UPGRADE-1.14.md).

1. Passing an instance of `SM\Factory\FactoryInterface` as a constructor argument to the below classes has been deprecated
and will not be possible in RefundPlugin 2.0. Pass an instance of `Sylius\Abstraction\StateMachine\StateMachineInterface`.

Applies to classes:
- `Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolver`
- `Sylius\RefundPlugin\StateResolver\OrderPartiallyRefundedStateResolver`
- `Sylius\RefundPlugin\StateResolver\RefundPaymentCompletedStateApplier`
73 changes: 57 additions & 16 deletions spec/StateResolver/OrderFullyRefundedStateResolverSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

use Doctrine\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
use SM\StateMachine\StateMachineInterface as WinzouStateMachineInterface;
use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\OrderPaymentTransitions;
Expand All @@ -41,61 +41,102 @@ function let(
}

function it_applies_refund_transition_on_order(
OrderRepositoryInterface $orderRepository,
OrderFullyRefundedTotalCheckerInterface $orderFullyRefundedTotalChecker,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine,
ObjectManager $orderManager,
OrderFullyRefundedTotalCheckerInterface $orderFullyRefundedTotalChecker,
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
StateMachineInterface $stateMachine,
): void {
$this->beConstructedWith($stateMachine, $orderManager, $orderFullyRefundedTotalChecker, $orderRepository);

$orderRepository->findOneByNumber('000222')->willReturn($order);
$orderFullyRefundedTotalChecker->isOrderFullyRefunded($order)->willReturn(true);
$order->getPaymentState()->willReturn(OrderPaymentStates::STATE_PAID);

$stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->apply(OrderPaymentTransitions::TRANSITION_REFUND)->shouldBeCalled();
$stateMachine
->apply($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_REFUND)
->shouldBeCalled()
;

$orderManager->flush()->shouldBeCalled();

$this->resolve('000222');
}

function it_does_nothing_if_order_state_is_fully_refunded(
OrderRepositoryInterface $orderRepository,
StateMachineInterface $stateMachine,
ObjectManager $orderManager,
OrderFullyRefundedTotalCheckerInterface $orderFullyRefundedTotalChecker,
FactoryInterface $stateMachineFactory,
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
): void {
$this->beConstructedWith($stateMachine, $orderManager, $orderFullyRefundedTotalChecker, $orderRepository);

$orderRepository->findOneByNumber('000222')->willReturn($order);
$orderFullyRefundedTotalChecker->isOrderFullyRefunded($order)->willReturn(true);
$order->getPaymentState()->willReturn(OrderPaymentStates::STATE_REFUNDED);

$stateMachineFactory->get(Argument::any())->shouldNotBeCalled();
$stateMachine
->apply($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_REFUND)
->shouldNotBeCalled()
;

$this->resolve('000222');
}

function it_does_nothing_if_order_is_not_fully_refunded(
OrderRepositoryInterface $orderRepository,
StateMachineInterface $stateMachine,
ObjectManager $orderManager,
OrderFullyRefundedTotalCheckerInterface $orderFullyRefundedTotalChecker,
FactoryInterface $stateMachineFactory,
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
): void {
$this->beConstructedWith($stateMachine, $orderManager, $orderFullyRefundedTotalChecker, $orderRepository);

$orderRepository->findOneByNumber('000222')->willReturn($order);
$orderFullyRefundedTotalChecker->isOrderFullyRefunded($order)->willReturn(false);

$stateMachineFactory->get(Argument::any())->shouldNotBeCalled();
$stateMachine
->apply($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_REFUND)
->shouldNotBeCalled()
;

$this->resolve('000222');
}

function it_throws_an_exception_if_there_is_no_order_with_given_number(OrderRepositoryInterface $orderRepository): void
{
function it_throws_an_exception_if_there_is_no_order_with_given_number(
StateMachineInterface $stateMachine,
ObjectManager $orderManager,
OrderFullyRefundedTotalCheckerInterface $orderFullyRefundedTotalChecker,
OrderRepositoryInterface $orderRepository,
): void {
$this->beConstructedWith($stateMachine, $orderManager, $orderFullyRefundedTotalChecker, $orderRepository);

$orderRepository->findOneByNumber('000222')->willReturn(null);

$this
->shouldThrow(\InvalidArgumentException::class)
->during('resolve', ['000222'])
;
}

function it_uses_winzou_state_machine_if_abstraction_not_passed_to_apply_refund_transition_on_order(
FactoryInterface $stateMachineFactory,
ObjectManager $orderManager,
OrderFullyRefundedTotalCheckerInterface $orderFullyRefundedTotalChecker,
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
WinzouStateMachineInterface $stateMachine,
): void {
$orderRepository->findOneByNumber('000222')->willReturn($order);
$orderFullyRefundedTotalChecker->isOrderFullyRefunded($order)->willReturn(true);
$order->getPaymentState()->willReturn(OrderPaymentStates::STATE_PAID);

$stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->apply(OrderPaymentTransitions::TRANSITION_REFUND)->shouldBeCalled();

$orderManager->flush()->shouldBeCalled();

$this->resolve('000222');
}
}
53 changes: 43 additions & 10 deletions spec/StateResolver/OrderPartiallyRefundedStateResolverSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

use Doctrine\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
use SM\StateMachine\StateMachineInterface as WinzouStateMachineInterface;
use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\OrderPaymentTransitions;
Expand All @@ -36,17 +36,20 @@ function let(

function it_marks_order_as_partially_refunded(
OrderRepositoryInterface $orderRepository,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine,
ObjectManager $orderManager,
OrderInterface $order,
StateMachineInterface $stateMachine,
): void {
$this->beConstructedWith($orderRepository, $stateMachine, $orderManager);

$orderRepository->findOneByNumber('000777')->willReturn($order);

$order->getPaymentState()->willReturn(OrderPaymentStates::STATE_PAID);

$stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->apply(OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND)->shouldBeCalled();
$stateMachine
->apply($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND)
->shouldBeCalled()
;

$orderManager->flush()->shouldBeCalled();

Expand All @@ -55,25 +58,55 @@ function it_marks_order_as_partially_refunded(

function it_does_nothing_if_order_is_already_marked_as_partially_refunded(
OrderRepositoryInterface $orderRepository,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine,
ObjectManager $orderManager,
OrderInterface $order,
): void {
$this->beConstructedWith($orderRepository, $stateMachine, $orderManager);

$orderRepository->findOneByNumber('000777')->willReturn($order);

$order->getPaymentState()->willReturn(OrderPaymentStates::STATE_PARTIALLY_REFUNDED);

$stateMachineFactory->get(Argument::any())->shouldNotBeCalled();
$stateMachine
->apply($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND)
->shouldNotBeCalled()
;

$this->resolve('000777');
}

function it_throws_exception_if_there_is_no_order_with_given_number(OrderRepositoryInterface $orderRepository): void
{
function it_throws_exception_if_there_is_no_order_with_given_number(
OrderRepositoryInterface $orderRepository,
StateMachineInterface $stateMachine,
ObjectManager $orderManager,
): void {
$this->beConstructedWith($orderRepository, $stateMachine, $orderManager);

$orderRepository->findOneByNumber('000777')->willReturn(null);

$this
->shouldThrow(OrderNotFound::withNumber('000777'))
->during('resolve', ['000777'])
;
}

function it_uses_winzou_state_machine_if_abstraction_not_passed_to_mark_order_as_partially_refunded(
OrderRepositoryInterface $orderRepository,
FactoryInterface $stateMachineFactory,
ObjectManager $orderManager,
OrderInterface $order,
WinzouStateMachineInterface $stateMachine,
): void {
$orderRepository->findOneByNumber('000777')->willReturn($order);

$order->getPaymentState()->willReturn(OrderPaymentStates::STATE_PAID);

$stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->apply(OrderPaymentTransitions::TRANSITION_PARTIALLY_REFUND)->shouldBeCalled();

$orderManager->flush()->shouldBeCalled();

$this->resolve('000777');
}
}
26 changes: 22 additions & 4 deletions spec/StateResolver/RefundPaymentCompletedStateApplierSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@

use Doctrine\Persistence\ObjectManager;
use PhpSpec\ObjectBehavior;
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
use SM\StateMachine\StateMachineInterface;
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface as WinzouStateMachineInterface;
use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Sylius\RefundPlugin\Entity\RefundPaymentInterface;
use Sylius\RefundPlugin\StateResolver\RefundPaymentCompletedStateApplier;
use Sylius\RefundPlugin\StateResolver\RefundPaymentCompletedStateApplierInterface;
use Sylius\RefundPlugin\StateResolver\RefundPaymentTransitions;

final class RefundPaymentCompletedStateApplierSpec extends ObjectBehavior
{
function let(StateMachineFactoryInterface $stateMachineFactory, ObjectManager $refundPaymentManager): void
function let(FactoryInterface $stateMachineFactory, ObjectManager $refundPaymentManager): void
{
$this->beConstructedWith($stateMachineFactory, $refundPaymentManager);
}
Expand All @@ -40,10 +41,27 @@ function it_implements_refund_payment_completed_state_applier_interface(): void
}

function it_applies_complete_transition_on_refund_payment(
StateMachineFactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine,
ObjectManager $refundPaymentManager,
RefundPaymentInterface $refundPayment,
): void {
$this->beConstructedWith($stateMachine, $refundPaymentManager);

$stateMachine
->apply($refundPayment, RefundPaymentTransitions::GRAPH, RefundPaymentTransitions::TRANSITION_COMPLETE)
->shouldBeCalled()
;

$refundPaymentManager->flush()->shouldBeCalled();

$this->apply($refundPayment);
}

function it_uses_winzou_state_machine_if_abstraction_not_passed_to_apply_complete_transition_on_refund_payment(
FactoryInterface $stateMachineFactory,
ObjectManager $refundPaymentManager,
WinzouStateMachineInterface $stateMachine,
RefundPaymentInterface $refundPayment,
): void {
$stateMachineFactory->get($refundPayment, RefundPaymentTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->apply(RefundPaymentTransitions::TRANSITION_COMPLETE)->shouldBeCalled();
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/config/services/state_resolvers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<defaults autowire="false" autoconfigure="false" public="true" />

<service id="Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolverInterface" class="Sylius\RefundPlugin\StateResolver\OrderFullyRefundedStateResolver">
<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="Sylius\RefundPlugin\Checker\OrderFullyRefundedTotalCheckerInterface" />
<argument type="service" id="sylius.repository.order" />
Expand All @@ -19,15 +19,15 @@

<service id="Sylius\RefundPlugin\StateResolver\OrderPartiallyRefundedStateResolverInterface" class="Sylius\RefundPlugin\StateResolver\OrderPartiallyRefundedStateResolver">
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sm.factory" />
<argument type="service" id="sylius_abstraction.state_machine" />
<argument type="service" id="sylius.manager.order" />
</service>
<service id="Sylius\RefundPlugin\StateResolver\OrderPartiallyRefundedStateResolver" alias="Sylius\RefundPlugin\StateResolver\OrderPartiallyRefundedStateResolverInterface">
<deprecated package="sylius/refund-plugin" version="1.0">The "%alias_id%" service alias is deprecated and will be removed in RefundPlugin 2.0, use Sylius\RefundPlugin\StateResolver\OrderPartiallyRefundedStateResolverInterface instead.</deprecated>
</service>

<service id="Sylius\RefundPlugin\StateResolver\RefundPaymentCompletedStateApplierInterface" class="Sylius\RefundPlugin\StateResolver\RefundPaymentCompletedStateApplier">
<argument type="service" id="sm.factory" />
<argument type="service" id="sylius_abstraction.state_machine" />
<argument type="service" id="sylius_refund.manager.refund_payment" />
</service>
<service id="Sylius\RefundPlugin\StateResolver\RefundPaymentCompletedStateApplier" alias="Sylius\RefundPlugin\StateResolver\RefundPaymentCompletedStateApplierInterface">
Expand Down
31 changes: 27 additions & 4 deletions src/StateResolver/OrderFullyRefundedStateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Doctrine\Persistence\ObjectManager;
use SM\Factory\FactoryInterface;
use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Sylius\Abstraction\StateMachine\WinzouStateMachineAdapter;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\OrderPaymentTransitions;
Expand All @@ -26,11 +28,22 @@ final class OrderFullyRefundedStateResolver implements OrderFullyRefundedStateRe
{
/** @param OrderRepositoryInterface<OrderInterface> $orderRepository */
public function __construct(
private readonly FactoryInterface $stateMachineFactory,
private readonly FactoryInterface|StateMachineInterface $stateMachineFactory,
private readonly ObjectManager $orderManager,
private readonly OrderFullyRefundedTotalCheckerInterface $orderFullyRefundedTotalChecker,
private readonly OrderRepositoryInterface $orderRepository,
) {
if ($this->stateMachineFactory instanceof FactoryInterface) {
trigger_deprecation(
'sylius/refund-plugin',
'1.6',
sprintf(
'Passing an instance of "%s" as the first argument is deprecated. It will accept only instances of "%s" in RefundPlugin 2.0.',
FactoryInterface::class,
StateMachineInterface::class,
),
);
}
}

public function resolve(string $orderNumber): void
Expand All @@ -46,10 +59,20 @@ public function resolve(string $orderNumber): void
return;
}

$stateMachine = $this->stateMachineFactory->get($order, OrderPaymentTransitions::GRAPH);

$stateMachine->apply(OrderPaymentTransitions::TRANSITION_REFUND);
$this
->getStateMachine()
->apply($order, OrderPaymentTransitions::GRAPH, OrderPaymentTransitions::TRANSITION_REFUND)
;

$this->orderManager->flush();
}

private function getStateMachine(): StateMachineInterface
{
if ($this->stateMachineFactory instanceof FactoryInterface) {
return new WinzouStateMachineAdapter($this->stateMachineFactory);
}

return $this->stateMachineFactory;
}
}
Loading
Loading