Skip to content

Commit

Permalink
[API] Prevent from using inexistent shipping method
Browse files Browse the repository at this point in the history
  • Loading branch information
pamil committed May 24, 2021
1 parent d1d4502 commit 8034c2e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,13 @@ Feature: Preventing not available shipping method selection
When I specify the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I complete the addressing step
Then I should not be able to select "Dragon Post" shipping method

@api
Scenario: Not being able to select inexistent shipping method
Given the store has "Raven Post" shipping method with "$10.00" fee
And I have product "Targaryen T-Shirt" in the cart
And I am at the checkout addressing step
When I specify the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I complete the addressing step
And I try to select "Inexistent" shipping method
Then I should be informed that shipping method with code "Inexistent" does not exist
30 changes: 30 additions & 0 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\ShippingMethod;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Core\OrderCheckoutStates;
Expand Down Expand Up @@ -345,6 +346,24 @@ public function iProceededWithShippingMethod(ShippingMethodInterface $shippingMe
$this->selectShippingMethod($shippingMethod);
}

/**
* @When I try to select :shippingMethodCode shipping method
*/
public function iTryToSelectShippingMethod(string $shippingMethodCode): void
{
$request = Request::customItemAction(
'shop',
'orders',
$this->sharedStorage->get('cart_token'),
HTTPRequest::METHOD_PATCH,
sprintf('shipments/%s', $this->getCart()['shipments'][0]['id'])
);

$request->setContent(['shippingMethod' => $this->iriConverter->getItemIriFromResourceClass(ShippingMethod::class, ['code' => $shippingMethodCode])]);

$this->ordersClient->executeCustomRequest($request);
}

/**
* @When I try to select :paymentMethodCode payment method
*/
Expand All @@ -363,6 +382,17 @@ public function iTryToSelectPaymentMethod(string $paymentMethodCode): void
$this->ordersClient->executeCustomRequest($request);
}

/**
* @Then I should be informed that shipping method with code :code does not exist
*/
public function iShouldBeInformedThatShippingMethodWithCodeDoesNotExist(string $code): void
{
Assert::true($this->isViolationWithMessageInResponse(
$this->ordersClient->getLastResponse(),
sprintf('The shipping method with %s code does not exist.', $code)
));
}

/**
* @Then the visitor has no access to proceed with :shippingMethod shipping method in the customer cart
* @Then the visitor has no access to proceed with :paymentMethod payment in the customer cart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ sylius:
shipment:
shipped: 'You cannot ship a shipment that was shipped before.'
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.'
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ final class ChosenShippingMethodEligibility extends Constraint
/** @var string */
public $message = 'sylius.shipping_method.not_available';

/** @var string */
public $notFoundMessage = 'sylius.shipping_method.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 @@ -55,7 +55,11 @@ public function validate($value, Constraint $constraint): void

/** @var ShippingMethodInterface|null $shippingMethod */
$shippingMethod = $this->shippingMethodRepository->findOneBy(['code' => $value->shippingMethodCode]);
Assert::notNull($shippingMethod);
if (null === $shippingMethod) {
$this->context->addViolation($constraint->notFoundMessage, ['%code%' => $value->shippingMethodCode]);

return;
}

/** @var ShipmentInterface|null $shipment */
$shipment = $this->shipmentRepository->find($value->shipmentId);
Expand Down

0 comments on commit 8034c2e

Please sign in to comment.