Skip to content

Commit

Permalink
Add fixes for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomanhez committed Jul 23, 2020
1 parent 6456377 commit e0857fc
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\VarDumper\VarDumper;
use Webmozart\Assert\Assert;

final class CheckoutContext implements Context
Expand Down Expand Up @@ -101,7 +100,7 @@ public function iCompleteAddressingStepWithEmail(string $email, AddressInterface
/**
* @When I proceed with :shippingMethod shipping method
*/
public function iHaveProceededSelectingShippingMethod(ShippingMethodInterface $shippingMethod): void
public function iProceededWithShippingMethod(ShippingMethodInterface $shippingMethod): void
{
$cartToken = $this->sharedStorage->get('cart_token');

Expand Down Expand Up @@ -158,6 +157,7 @@ public function iConfirmMyOrder(): void
json_encode([], \JSON_THROW_ON_ERROR)
);
}

/**
* @When I complete the addressing step
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,30 @@ public function __construct(

public function __invoke(ChoosePaymentMethod $choosePaymentMethod): OrderInterface
{
/** @var OrderInterface $cart */
/** @var OrderInterface|null $cart */
$cart = $this->orderRepository->findOneBy(['tokenValue' => $choosePaymentMethod->orderTokenValue]);

Assert::notNull($cart, 'Cart has not been found.');

$stateMachine = $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH);

Assert::true($stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT), 'Order cannot have payment method assigned.');
Assert::true($stateMachine->can(
OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT),
'Order cannot have payment method assigned.'
);

/** @var PaymentMethodInterface $paymentMethod */
/** @var PaymentMethodInterface|null $paymentMethod */
$paymentMethod = $this->paymentMethodRepository->findOneBy(['code' => $choosePaymentMethod->paymentMethod]);

$paymentIdentifier = $choosePaymentMethod->paymentIdentifier;

Assert::notNull($paymentMethod, 'Payment method has not been found');
Assert::true(isset($cart->getPayments()[$choosePaymentMethod->paymentIdentifier]), 'Can not find payment with given identifier.');
Assert::true(isset(
$cart->getPayments()[$paymentIdentifier]),
'Can not find payment with given identifier.'
);

$payment = $cart->getPayments()[$choosePaymentMethod->paymentIdentifier];
$payment = $cart->getPayments()[$paymentIdentifier];

$payment->setMethod($paymentMethod);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@ public function __invoke(ChooseShippingMethod $chooseShippingMethod): OrderInter
'Order cannot have shipment method assigned.'
);

/** @var ShippingMethodInterface $shippingMethod */
/** @var ShippingMethodInterface|null $shippingMethod */
$shippingMethod = $this->shippingMethodRepository->findOneBy(['code' => $chooseShippingMethod->shippingMethod]);

$shipmentIdentifier = $chooseShippingMethod->shipmentIdentifier;

Assert::notNull($shippingMethod, 'Shipping method has not been found');
Assert::true(
isset($cart->getShipments()[$chooseShippingMethod->shipmentIdentifier]),
isset($cart->getShipments()[$shipmentIdentifier]),
'Can not find shipment with given identifier.'
);

$shipment = $cart->getShipments()[$chooseShippingMethod->shipmentIdentifier];
$shipment = $cart->getShipments()[$shipmentIdentifier];

Assert::true(
$this->eligibilityChecker->isEligible($shipment, $shippingMethod),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ public function __construct(

public function __invoke(CompleteOrder $completeOrder): OrderInterface
{
/** @var OrderInterface $cart */
$cart = $this->orderRepository->findOneBy(['tokenValue' => $completeOrder->orderTokenValue]);
$orderTokenValue = $completeOrder->orderTokenValue;

Assert::notNull($cart, sprintf('Order with %s token has not been found.', $completeOrder->orderTokenValue));
/** @var OrderInterface|null $cart */
$cart = $this->orderRepository->findOneBy(['tokenValue' => $orderTokenValue]);

Assert::notNull($cart, sprintf('Order with %s token has not been found.', $orderTokenValue));

$stateMachine = $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH);

Assert::true(
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_COMPLETE),
sprintf('Order with %s token cannot be completed.', $completeOrder->orderTokenValue)
sprintf('Order with %s token cannot be completed.', $orderTokenValue)
);

$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,45 +88,45 @@
</attribute>
</itemOperation>

<itemOperation name="select-shipping-methods">
<itemOperation name="select_shipping_method">
<attribute name="security">is_granted('IS_AUTHENTICATED_ANONYMOUSLY')</attribute>
<attribute name="openapi_context">
<attribute name="summary">Selects shipping methods for particular shipment</attribute>
<attribute name="summary">Select shipping methods for particular shipment</attribute>
</attribute>
<attribute name="method">PATCH</attribute>
<attribute name="path">/orders/{id}/select-shipping-methods</attribute>
<attribute name="messenger">input</attribute>
<attribute name="input">Sylius\Bundle\ApiBundle\Command\Checkout\ChooseShippingMethod</attribute>
<attribute name="denormalization_context">
<attribute name="groups">cart:select-shipping-method</attribute>
<attribute name="groups">cart:select_shipping_method</attribute>
</attribute>
</itemOperation>

<itemOperation name="select-payment-methods">
<itemOperation name="select_payment_method">
<attribute name="security">is_granted('IS_AUTHENTICATED_ANONYMOUSLY')</attribute>
<attribute name="openapi_context">
<attribute name="summary">Selects payment methods for particular payment</attribute>
<attribute name="summary">Select payment methods for particular payment</attribute>
</attribute>
<attribute name="method">PATCH</attribute>
<attribute name="path">/orders/{id}/select-payment-methods</attribute>
<attribute name="messenger">input</attribute>
<attribute name="input">Sylius\Bundle\ApiBundle\Command\Checkout\ChoosePaymentMethod</attribute>
<attribute name="denormalization_context">
<attribute name="groups">cart:select-payment-method</attribute>
<attribute name="groups">cart:select_payment_method</attribute>
</attribute>
</itemOperation>

<itemOperation name="complete">
<attribute name="security">is_granted('IS_AUTHENTICATED_ANONYMOUSLY')</attribute>
<attribute name="openapi_context">
<attribute name="summary">Completes checkout</attribute>
<attribute name="summary">Complete checkout</attribute>
</attribute>
<attribute name="method">PATCH</attribute>
<attribute name="path">/orders/{id}/complete</attribute>
<attribute name="messenger">input</attribute>
<attribute name="input">Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder</attribute>
<attribute name="denormalization_context">
<attribute name="groups">cart:address</attribute>
<attribute name="groups">cart:complete</attribute>
</attribute>
</itemOperation>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
>
<class name="Sylius\Bundle\ApiBundle\Command\Checkout\ChoosePaymentMethod">
<attribute name="paymentMethod">
<group>cart:select-payment-method</group>
<group>cart:select_payment_method</group>
</attribute>
<attribute name="paymentIdentifier">
<group>cart:select-payment-method</group>
<group>cart:select_payment_method</group>
</attribute>
</class>
</serializer>
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
>
<class name="Sylius\Bundle\ApiBundle\Command\Checkout\ChooseShippingMethod">
<attribute name="shippingMethod">
<group>cart:select-shipping-method</group>
<group>cart:select_shipping_method</group>
</attribute>
<attribute name="shipmentIdentifier">
<group>cart:select-shipping-method</group>
<group>cart:select_shipping_method</group>
</attribute>
</class>
</serializer>
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ function let(
$this->beConstructedWith($orderRepository, $paymentMethodRepository, $stateMachineFactory);
}

function it_assignes_choosen_payment_method_to_specified_payment(
function it_assigns_chosen_payment_method_to_specified_payment(
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
PaymentMethodRepositoryInterface $paymentMethodRepository,
PaymentMethodInterface $paymentMethod,
PaymentInterface $payment,
FactoryInterface $stateMachineFactory,
OrderInterface $order,
PaymentInterface $payment,
PaymentMethodInterface $paymentMethod,
StateMachineInterface $stateMachine
): void {
$choosePaymentMethod = new ChoosePaymentMethod(0, 'CASH_ON_DELIVERY_METHOD');
Expand Down Expand Up @@ -80,11 +80,11 @@ function it_throws_an_exception_if_order_with_given_token_has_not_been_found(

function it_throws_an_exception_if_order_cannot_have_payment_selected(
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
PaymentMethodRepositoryInterface $paymentMethodRepository,
PaymentInterface $payment,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine
OrderInterface $order,
StateMachineInterface $stateMachine,
PaymentInterface $payment
): void {
$choosePaymentMethod = new ChoosePaymentMethod(0, 'CASH_ON_DELIVERY_METHOD');
$choosePaymentMethod->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -105,11 +105,11 @@ function it_throws_an_exception_if_order_cannot_have_payment_selected(

function it_throws_an_exception_if_payment_method_with_given_code_has_not_been_found(
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
PaymentMethodRepositoryInterface $paymentMethodRepository,
PaymentInterface $payment,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine
OrderInterface $order,
StateMachineInterface $stateMachine,
PaymentInterface $payment
): void {
$choosePaymentMethod = new ChoosePaymentMethod(0, 'CASH_ON_DELIVERY_METHOD');
$choosePaymentMethod->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -130,12 +130,12 @@ function it_throws_an_exception_if_payment_method_with_given_code_has_not_been_f

function it_throws_an_exception_if_ordered_payment_has_not_been_found(
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
PaymentMethodRepositoryInterface $paymentMethodRepository,
PaymentMethodInterface $paymentMethod,
PaymentInterface $payment,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine
OrderInterface $order,
PaymentMethodInterface $paymentMethod,
StateMachineInterface $stateMachine,
PaymentInterface $payment
): void {
$choosePaymentMethod = new ChoosePaymentMethod(0, 'CASH_ON_DELIVERY_METHOD');
$choosePaymentMethod->setOrderTokenValue('ORDERTOKEN');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ function let(
$this->beConstructedWith($orderRepository, $shippingMethodRepository, $eligibilityChecker, $stateMachineFactory);
}

function it_assignes_choosen_shipping_method_to_specified_shipment(
function it_assigns_choosen_shipping_method_to_specified_shipment(
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
ShippingMethodRepositoryInterface $shippingMethodRepository,
ShippingMethodInterface $shippingMethod,
ShipmentInterface $shipment,
ShippingMethodEligibilityCheckerInterface $eligibilityChecker,
FactoryInterface $stateMachineFactory,
OrderInterface $order,
ShippingMethodInterface $shippingMethod,
ShipmentInterface $shipment,
StateMachineInterface $stateMachine
): void {
$chooseShippingMethod = new ChooseShippingMethod(0, 'DHL_SHIPPING_METHOD');
Expand All @@ -68,12 +68,12 @@ function it_assignes_choosen_shipping_method_to_specified_shipment(

function it_throws_an_exception_if_shipping_method_is_not_eligible(
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
ShippingMethodRepositoryInterface $shippingMethodRepository,
ShippingMethodInterface $shippingMethod,
ShipmentInterface $shipment,
ShippingMethodEligibilityCheckerInterface $eligibilityChecker,
FactoryInterface $stateMachineFactory,
OrderInterface $order,
ShippingMethodInterface $shippingMethod,
ShipmentInterface $shipment,
StateMachineInterface $stateMachine
): void {
$chooseShippingMethod = new ChooseShippingMethod(0, 'DHL_SHIPPING_METHOD');
Expand Down Expand Up @@ -115,11 +115,11 @@ function it_throws_an_exception_if_order_with_given_token_has_not_been_found(

function it_throws_an_exception_if_order_cannot_have_shipping_selected(
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
ShippingMethodRepositoryInterface $shippingMethodRepository,
ShipmentInterface $shipment,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine
OrderInterface $order,
StateMachineInterface $stateMachine,
ShipmentInterface $shipment
): void {
$chooseShippingMethod = new ChooseShippingMethod(0, 'DHL_SHIPPING_METHOD');
$chooseShippingMethod->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -140,11 +140,11 @@ function it_throws_an_exception_if_order_cannot_have_shipping_selected(

function it_throws_an_exception_if_shipping_method_with_given_code_has_not_been_found(
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
ShippingMethodRepositoryInterface $shippingMethodRepository,
ShipmentInterface $shipment,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine
OrderInterface $order,
StateMachineInterface $stateMachine,
ShipmentInterface $shipment
): void {
$chooseShippingMethod = new ChooseShippingMethod(0, 'DHL_SHIPPING_METHOD');
$chooseShippingMethod->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -165,12 +165,12 @@ function it_throws_an_exception_if_shipping_method_with_given_code_has_not_been_

function it_throws_an_exception_if_ordered_shipment_has_not_been_found(
OrderRepositoryInterface $orderRepository,
OrderInterface $order,
ShippingMethodRepositoryInterface $shippingMethodRepository,
ShippingMethodInterface $shippingMethod,
ShipmentInterface $shipment,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine
OrderInterface $order,
ShippingMethodInterface $shippingMethod,
StateMachineInterface $stateMachine,
ShipmentInterface $shipment
): void {
$chooseShippingMethod = new ChooseShippingMethod(0, 'DHL_SHIPPING_METHOD');
$chooseShippingMethod->setOrderTokenValue('ORDERTOKEN');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ function let(OrderRepositoryInterface $orderRepository, FactoryInterface $stateM
}

function it_handles_order_completion(
OrderInterface $order,
OrderRepositoryInterface $orderRepository,
FactoryInterface $stateMachineFactory,
StateMachineInterface $stateMachine
StateMachineInterface $stateMachine,
OrderInterface $order,
FactoryInterface $stateMachineFactory
): void {
$completeOrder = new CompleteOrder();
$completeOrder->setOrderTokenValue('ORDERTOKEN');
Expand Down Expand Up @@ -63,10 +63,10 @@ function it_throws_an_exception_if_order_does_not_exist(
}

function it_throws_an_exception_if_order_cannot_be_completed(
FactoryInterface $stateMachineFactory,
OrderInterface $order,
OrderRepositoryInterface $orderRepository,
StateMachineInterface $stateMachine
StateMachineInterface $stateMachine,
OrderInterface $order,
FactoryInterface $stateMachineFactory
): void {
$completeOrder = new CompleteOrder();
$completeOrder->setOrderTokenValue('ORDERTOKEN');
Expand Down

0 comments on commit e0857fc

Please sign in to comment.