Skip to content

Commit

Permalink
[API] change error to violation
Browse files Browse the repository at this point in the history
  • Loading branch information
SirDomin committed Jun 24, 2021
1 parent ffcfd56 commit 85eafce
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 91 deletions.
11 changes: 4 additions & 7 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,10 @@ public function iTryToSelectPaymentMethod(string $paymentMethodCode): void
*/
public function iShouldBeNotifiedThatTheOrderShouldBeAddressedFirst(): void
{
$response = $this->ordersClient->getLastResponse();

Assert::same($response->getStatusCode(), Response::HTTP_UNPROCESSABLE_ENTITY);

$content = json_decode($response->getContent(), true);

Assert::same($content['message'], 'Order should be addressed first.');
Assert::true($this->isViolationWithMessageInResponse(
$this->ordersClient->getLastResponse(),
'Order should be addressed first.'
));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use SM\Factory\FactoryInterface;
use Sylius\Bundle\ApiBundle\Command\Checkout\ChooseShippingMethod;
use Sylius\Bundle\ApiBundle\Exception\OrderCannotBeShippedWithoutAddressing;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
Expand Down Expand Up @@ -67,10 +66,6 @@ public function __invoke(ChooseShippingMethod $chooseShippingMethod): OrderInter

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

if ($cart->getShippingAddress() === null) {
throw new OrderCannotBeShippedWithoutAddressing();
}

Assert::true(
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING),
'Order cannot have shipment method assigned.'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ api_platform:
SM\SMException: 422
Sylius\Bundle\ApiBundle\Exception\CannotRemoveCurrentlyLoggedInUser: 422
Sylius\Bundle\ApiBundle\Exception\ShippingMethodCannotBeRemoved: 422
Sylius\Bundle\ApiBundle\Exception\OrderCannotBeShippedWithoutAddressing: 422
collection:
pagination:
client_items_per_page: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ sylius:
shipping_method:
not_found: 'The shipping method with %code% code does not exist.'
not_available: 'The shipping method %name% is not available for this order. Please reselect your shipping method.'
shipping_address_not_found: 'Order should be addressed first.'
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ final class ChosenShippingMethodEligibility extends Constraint
/** @var string */
public $notFoundMessage = 'sylius.shipping_method.not_found';

/** @var string */
public $shippingAddressNotFoundMessage = 'sylius.shipping_method.shipping_address_not_found';

public function validatedBy(): string
{
return 'sylius_api_validator_chosen_shipping_method_eligibility';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public function validate($value, Constraint $constraint): void
$shipment = $this->shipmentRepository->find($value->shipmentId);
Assert::notNull($shipment);

$order = $shipment->getOrder();

if ($order->getShippingAddress() === null) {
$this->context->addViolation($constraint->shippingAddressNotFoundMessage);
}

if (!in_array($shippingMethod, $this->shippingMethodsResolver->getSupportedMethods($shipment), true)) {
$this->context->addViolation($constraint->message, ['%name%' => $shippingMethod->getName()]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
use Sylius\Bundle\ApiBundle\Command\Checkout\ChooseShippingMethod;
use Sylius\Bundle\ApiBundle\Exception\OrderCannotBeShippedWithoutAddressing;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
Expand Down Expand Up @@ -57,8 +55,7 @@ function it_assigns_choosen_shipping_method_to_specified_shipment(
OrderInterface $cart,
ShippingMethodInterface $shippingMethod,
ShipmentInterface $shipment,
StateMachineInterface $stateMachine,
AddressInterface $shippingAddress
StateMachineInterface $stateMachine
): void {
$chooseShippingMethod = new ChooseShippingMethod('DHL_SHIPPING_METHOD');
$chooseShippingMethod->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -71,8 +68,6 @@ function it_assigns_choosen_shipping_method_to_specified_shipment(

$shippingMethodRepository->findOneBy(['code' => 'DHL_SHIPPING_METHOD'])->willReturn($shippingMethod);

$cart->getShippingAddress()->willReturn($shippingAddress);

$cart->getShipments()->willReturn(new ArrayCollection([$shipment->getWrappedObject()]));

$cart->getId()->willReturn('111');
Expand All @@ -96,8 +91,7 @@ function it_throws_an_exception_if_shipping_method_is_not_eligible(
OrderInterface $cart,
ShippingMethodInterface $shippingMethod,
ShipmentInterface $shipment,
StateMachineInterface $stateMachine,
AddressInterface $shippingAddress
StateMachineInterface $stateMachine
): void {
$chooseShippingMethod = new ChooseShippingMethod('DHL_SHIPPING_METHOD');
$chooseShippingMethod->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -110,8 +104,6 @@ function it_throws_an_exception_if_shipping_method_is_not_eligible(

$shippingMethodRepository->findOneBy(['code' => 'DHL_SHIPPING_METHOD'])->willReturn($shippingMethod);

$cart->getShippingAddress()->willReturn($shippingAddress);

$cart->getShipments()->willReturn(new ArrayCollection([$shipment->getWrappedObject()]));

$cart->getId()->willReturn('111');
Expand Down Expand Up @@ -152,17 +144,14 @@ function it_throws_an_exception_if_order_cannot_have_shipping_selected(
FactoryInterface $stateMachineFactory,
OrderInterface $cart,
StateMachineInterface $stateMachine,
ShipmentInterface $shipment,
AddressInterface $shippingAddress
ShipmentInterface $shipment
): void {
$chooseShippingMethod = new ChooseShippingMethod('DHL_SHIPPING_METHOD');
$chooseShippingMethod->setOrderTokenValue('ORDERTOKEN');

$orderRepository->findOneBy(['tokenValue' => 'ORDERTOKEN'])->willReturn($cart);
$shippingMethodRepository->findOneBy(['code' => 'DHL_SHIPPING_METHOD'])->willReturn(null);

$cart->getShippingAddress()->willReturn($shippingAddress);

$stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->can('select_shipping')->willReturn(false);

Expand All @@ -181,17 +170,14 @@ function it_throws_an_exception_if_shipping_method_with_given_code_has_not_been_
FactoryInterface $stateMachineFactory,
OrderInterface $cart,
StateMachineInterface $stateMachine,
ShipmentInterface $shipment,
AddressInterface $shippingAddress
ShipmentInterface $shipment
): void {
$chooseShippingMethod = new ChooseShippingMethod('DHL_SHIPPING_METHOD');
$chooseShippingMethod->setOrderTokenValue('ORDERTOKEN');
$chooseShippingMethod->setSubresourceId('123');

$orderRepository->findOneBy(['tokenValue' => 'ORDERTOKEN'])->willReturn($cart);

$cart->getShippingAddress()->willReturn($shippingAddress);

$stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->can('select_shipping')->willReturn(true);

Expand All @@ -213,8 +199,7 @@ function it_throws_an_exception_if_ordered_shipment_has_not_been_found(
FactoryInterface $stateMachineFactory,
OrderInterface $cart,
ShippingMethodInterface $shippingMethod,
StateMachineInterface $stateMachine,
AddressInterface $shippingAddress
StateMachineInterface $stateMachine
): void {
$chooseShippingMethod = new ChooseShippingMethod('DHL_SHIPPING_METHOD');
$chooseShippingMethod->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -227,8 +212,6 @@ function it_throws_an_exception_if_ordered_shipment_has_not_been_found(

$shippingMethodRepository->findOneBy(['code' => 'DHL_SHIPPING_METHOD'])->willReturn($shippingMethod);

$cart->getShippingAddress()->willReturn($shippingAddress);

$cart->getId()->willReturn('111');

$shipmentRepository->findOneByOrderId('123', '111')->willReturn(null);
Expand All @@ -240,38 +223,4 @@ function it_throws_an_exception_if_ordered_shipment_has_not_been_found(
->during('__invoke', [$chooseShippingMethod])
;
}

function it_throws_an_exception_if_cart_doesnt_have_address(
OrderRepositoryInterface $orderRepository,
ShippingMethodRepositoryInterface $shippingMethodRepository,
ShipmentRepositoryInterface $shipmentRepository,
FactoryInterface $stateMachineFactory,
OrderInterface $cart,
ShippingMethodInterface $shippingMethod,
StateMachineInterface $stateMachine
): void {
$chooseShippingMethod = new ChooseShippingMethod('DHL_SHIPPING_METHOD');
$chooseShippingMethod->setOrderTokenValue('ORDERTOKEN');
$chooseShippingMethod->setSubresourceId('123');

$orderRepository->findOneBy(['tokenValue' => 'ORDERTOKEN'])->willReturn($cart);

$stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH)->willReturn($stateMachine);
$stateMachine->can('select_shipping')->willReturn(true);

$shippingMethodRepository->findOneBy(['code' => 'DHL_SHIPPING_METHOD'])->willReturn($shippingMethod);

$cart->getShippingAddress()->willReturn(null);

$cart->getId()->willReturn('111');

$shipmentRepository->findOneByOrderId('123', '111')->shouldNotBeCalled();

$stateMachine->apply('select_shipping')->shouldNotBeCalled();

$this
->shouldThrow(OrderCannotBeShippedWithoutAddressing::class)
->during('__invoke', [$chooseShippingMethod])
;
}
}

0 comments on commit 85eafce

Please sign in to comment.