Skip to content

Commit

Permalink
bug #176 Fix state machine exception on order creation (maciekpaprock…
Browse files Browse the repository at this point in the history
…i, GSadee)

This PR was merged into the 1.1-dev branch.

Discussion
----------

This is similar to already existing PR, but i think original poster stopped responding so I can try to get that working. 


Issue:
#134

However, the issue also appears when product is not virtual, but skipping_shipping_step_allowed in channel

PR:
#132


Commits
-------

c1f47db Fix state machine exception on order creation
bc25387 added phpspec tests
10bf4b5 Remove PHPUnit cache file
  • Loading branch information
GSadee authored Feb 25, 2022
2 parents 2386921 + 10bf4b5 commit 98bbfcb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
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

0 comments on commit 98bbfcb

Please sign in to comment.