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

Check if transition can be applied before apply it during order creation #134

Closed
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
8 changes: 4 additions & 4 deletions spec/EventListener/OrderCreationListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ function it_completes_order_before_creation(
$event->getSubject()->willReturn($order);

$stateMachineFactory->get($order, 'sylius_order_checkout')->willReturn($stateMachine);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS)->shouldBeCalled();
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)->shouldBeCalled();
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)->shouldBeCalled();
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE)->shouldBeCalled();
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_ADDRESS)->shouldBeCalled();
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING)->shouldBeCalled();
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT)->shouldBeCalled();
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_COMPLETE)->shouldBeCalled();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe apply(...)->shouldBeCalled() should still be there. More, all of these should be $stateMachine->can(...)->willReturn(true); We should also create at least one spec when some of the transition cannot be called (and check that apply(...)->shouldNotBeCalled()


$this->completeOrderBeforeCreation($event);
}
Expand Down
16 changes: 12 additions & 4 deletions src/EventListener/OrderCreationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@ public function completeOrderBeforeCreation(GenericEvent $event): void
Assert::isInstanceOf($order, OrderInterface::class);

$stateMachine = $this->stateMachineFactory->get($order, 'sylius_order_checkout');
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);
$transitions = [
OrderCheckoutTransitions::TRANSITION_ADDRESS,
OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING,
OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT,
OrderCheckoutTransitions::TRANSITION_COMPLETE,
];

foreach ($transitions as $transition) {
if ($stateMachine->can($transition)) {
$stateMachine->apply($transition);
}
}
}
}