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

Fix state machine exception on order creation #176

Merged
merged 3 commits into from
Feb 25, 2022
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
/tests/Application/yarn.lock

/web/media

/.phpunit.result.cache
27 changes: 23 additions & 4 deletions spec/EventListener/OrderCreationListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function it_completes_order_before_creation(

$stateMachineFactory->get($order, 'sylius_order_checkout')->willReturn($stateMachine);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS)->shouldBeCalled();
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)->willReturn(true);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)->shouldBeCalled();
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)->willReturn(true);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)->shouldBeCalled();
Expand All @@ -62,6 +63,7 @@ function it_completes_order_without_payment_before_creation(

$stateMachineFactory->get($order, 'sylius_order_checkout')->willReturn($stateMachine);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS)->shouldBeCalled();
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)->willReturn(true);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)->shouldBeCalled();
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)->willReturn(false);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)->shouldNotBeCalled();
Expand All @@ -70,18 +72,35 @@ function it_completes_order_without_payment_before_creation(
$this->completeOrderBeforeCreation($event);
}

function it_completes_order_without_shipping_before_creation(
FactoryInterface $stateMachineFactory,
GenericEvent $event,
StateMachineInterface $stateMachine,
OrderInterface $order
) {
$event->getSubject()->willReturn($order);

$stateMachineFactory->get($order, 'sylius_order_checkout')->willReturn($stateMachine);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS)->shouldBeCalled();
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)->willReturn(false);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)->shouldNotBeCalled();
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)->willReturn(true);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)->shouldBeCalled();
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE)->shouldBeCalled();

$this->completeOrderBeforeCreation($event);
}

function it_throws_exception_if_event_subject_is_not_order(GenericEvent $event)
{
$event->getSubject()->willReturn('badObject', 'badObject');

$this
->shouldThrow(\InvalidArgumentException::class)
->during('processOrderBeforeCreation', [$event])
;
->during('processOrderBeforeCreation', [$event]);

$this
->shouldThrow(\InvalidArgumentException::class)
->during('completeOrderBeforeCreation', [$event])
;
->during('completeOrderBeforeCreation', [$event]);
}
}
4 changes: 3 additions & 1 deletion src/EventListener/OrderCreationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function completeOrderBeforeCreation(GenericEvent $event): void

$stateMachine = $this->stateMachineFactory->get($order, 'sylius_order_checkout');
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
if ($stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)) {
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
}
if ($stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)) {
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
}
Expand Down