Skip to content

Commit

Permalink
feature #12666 [API] Prevent from using inexistent shipping method (p…
Browse files Browse the repository at this point in the history
…amil)

This PR was merged into the 1.10-dev branch.

Discussion
----------



Commits
-------

648d021 [API] Prevent from using inexistent shipping method
  • Loading branch information
GSadee committed May 24, 2021
2 parents d1d4502 + 648d021 commit 33731fc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function it_does_not_add_violation_if_chosen_shipping_method_matches_supported_m
$this->validate($command, new ChosenShippingMethodEligibility());
}

function it_throws_an_exception_if_given_shipping_method_is_null(
function it_adds_a_violation_if_given_shipping_method_is_null(
ShipmentRepositoryInterface $shipmentRepository,
ShippingMethodRepositoryInterface $shippingMethodRepository,
ExecutionContextInterface $executionContext
Expand All @@ -137,11 +137,12 @@ function it_throws_an_exception_if_given_shipping_method_is_null(
->addViolation('sylius.shipping_method.not_available', Argument::any())
->shouldNotBeCalled()
;

$this
->shouldThrow(\InvalidArgumentException::class)
->during('validate', [$command, new ChosenShippingMethodEligibility()])
$executionContext
->addViolation('sylius.shipping_method.not_found', ['%code%' => 'SHIPPING_METHOD_CODE'])
->shouldBeCalled()
;

$this->validate($command, new ChosenShippingMethodEligibility());
}

function it_throws_an_exception_if_given_shipmnent_is_null(
Expand Down

0 comments on commit 33731fc

Please sign in to comment.