Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion features/checkout/emptying_the_cart_after_checkout.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Emptying the cart after checkout
And the store allows paying with "Cash on Delivery"
And I am a logged in customer

@ui
@ui @api
Scenario: Cart is emptied after the checkout
Given I have product "Sig Sauer P226" in the cart
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@checkout
Feature: Preventing cart from being modified after checkout
In order to have order immutable after checkout
As a Customer
I don't want to be able to modify placed order

Background:
Given the store operates on a single channel in "United States"
And the store has a product "Sig Sauer P226" priced at "$499.99"
And the store has a product "AK-47" priced at "$99.99"
And the store ships everywhere for free
And the store ships everywhere with ups
And the store allows paying with "Cash on Delivery"
And the store also allows paying with "Helicopter Money"
And I am a logged in customer

@api
Scenario: Preventing from changing billing address after checkout
Given I added product "Sig Sauer P226" to the cart
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceeded with "Free" shipping method and "Cash on Delivery" payment
And I confirmed my order
When I try to change the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
Then I should be informed that cart is no longer available

@api
Scenario: Preventing from changing shipping method after checkout
Given I added product "Sig Sauer P226" to the cart
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceeded with "Free" shipping method and "Cash on Delivery" payment
And I confirmed my order
When I try to change shipping method to "UPS"
Then I should be informed that cart is no longer available

@api
Scenario: Preventing from changing payment method after checkout
Given I added product "Sig Sauer P226" to the cart
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceeded with "Free" shipping method and "Cash on Delivery" payment
And I confirmed my order
When I try to change payment method to "Helicopter Money" payment
Then I should be informed that cart is no longer available

@api
Scenario: Preventing from adding product after checkout
Given I added product "Sig Sauer P226" to the cart
And I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceeded with "Free" shipping method and "Cash on Delivery" payment
And I confirmed my order
When I try to add product "AK-47" to the cart
Then I should be informed that cart is no longer available

@api
Scenario: Preventing from removing product after checkout
Given I added product "Sig Sauer P226" to the cart
And I added product "AK-47" to the cart
Then I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceeded with "Free" shipping method and "Cash on Delivery" payment
And I confirmed my order
When I try to remove product "AK-47" from the cart
Then I should be informed that cart is no longer available

@api
Scenario: Preventing from changing quantity of product after checkout
Given I added product "Sig Sauer P226" to the cart
And I added product "AK-47" to the cart
Then I specified the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
And I proceeded with "Free" shipping method and "Cash on Delivery" payment
And I confirmed my order
When I try to change quantity to 2 of product "AK-47" from the cart
Then I should be informed that cart is no longer available
85 changes: 85 additions & 0 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Service\Converter\AdminToShopIriConverterInterface;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\AddressInterface;
Expand All @@ -27,6 +28,7 @@
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -62,6 +64,9 @@ final class CheckoutContext implements Context
/** @var RepositoryInterface */
private $paymentMethodRepository;

/** @var ProductVariantResolverInterface */
private $productVariantResolver;

/** @var SharedStorageInterface */
private $sharedStorage;

Expand All @@ -76,6 +81,7 @@ public function __construct(
RepositoryInterface $shippingMethodRepository,
OrderRepositoryInterface $orderRepository,
RepositoryInterface $paymentMethodRepository,
ProductVariantResolverInterface $productVariantResolver,
SharedStorageInterface $sharedStorage
) {
$this->client = $client;
Expand All @@ -85,6 +91,7 @@ public function __construct(
$this->shippingMethodRepository = $shippingMethodRepository;
$this->orderRepository = $orderRepository;
$this->paymentMethodRepository = $paymentMethodRepository;
$this->productVariantResolver = $productVariantResolver;
$this->sharedStorage = $sharedStorage;
}

Expand Down Expand Up @@ -165,6 +172,7 @@ public function iDoNotSpecifyAnyBillingAddressInformation(AddressInterface $addr
/**
* @When /^I specified the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/
* @When /^I define the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/
* @When /^I try to change the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/
*/
public function iSpecifiedTheBillingAddressAs(AddressInterface $address): void
{
Expand Down Expand Up @@ -217,6 +225,7 @@ public function iProvideAdditionalNotesLike(string $notes): void
}

/**
* @Given I confirmed my order
* @When I confirm my order
* @When I try to confirm my order
* @When /^the (?:visitor|customer) confirm his order$/
Expand Down Expand Up @@ -254,6 +263,8 @@ public function iConfirmMyOrder(): void
* @When I select :shippingMethod shipping method
* @When /^the (?:visitor|customer) proceed with ("[^"]+" shipping method)$/
* @Given /^the (?:visitor|customer) has proceeded ("[^"]+" shipping method)$/
* @When /^the visitor try to proceed with ("[^"]+" shipping method) in the customer cart$/
* @When I try to change shipping method to :shippingMethod
*/
public function iProceededWithShippingMethod(ShippingMethodInterface $shippingMethod): void
{
Expand Down Expand Up @@ -301,6 +312,7 @@ public function iCompleteTheShippingStepWithFirstShippingMethod(): void
* @When I have proceeded selecting :paymentMethod payment method
* @When /^the (?:customer|visitor) proceed with ("[^"]+" payment)$/
* @Given /^the (?:customer|visitor) has proceeded ("[^"]+" payment)$/
* @When I try to change payment method to :paymentMethod payment
*/
public function iChoosePaymentMethod(PaymentMethodInterface $paymentMethod): void
{
Expand Down Expand Up @@ -688,6 +700,43 @@ public function iShouldBeInformedThatThisPaymentMethodHasBeenDisabled(PaymentMet
));
}

/**
* @When /^I try to add (product "[^"]+") to the (cart)$/
*/
public function iTryToAddProductToCart(ProductInterface $product, string $tokenValue): void
{
$this->putProductToCart($product, $tokenValue);
}

/**
* @When /^I try to remove (product "[^"]+") from the (cart)$/
*/
public function iTryToRemoveProductFromTheCart(ProductInterface $product, string $tokenValue): void
{
$this->removeOrderItemFromCart($product->getId(), $tokenValue);
}

/**
* @When /^I try to change quantity to (\d+) of (product "[^"]+") from the (cart)$/
*/
public function iTryToChangeQuantityToOfProductFromTheCart(int $quantity, ProductInterface $product, string $tokenValue): void
{
$this->putProductToCart($product, $tokenValue, $quantity);
}

/**
* @Then I should be informed that cart is no longer available
*/
public function iShouldBeInformedThatCartIsNoLongerAvailable(): void
{
/** @var Response $response */
$response = $this->client->getResponse();

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

Assert::same($this->responseChecker->getResponseContent($response)['message'], 'Not Found');
}

private function isViolationWithMessageInResponse(Response $response, string $message): bool
{
$violations = $this->responseChecker->getResponseContent($response)['violations'];
Expand Down Expand Up @@ -872,4 +921,40 @@ private function hasProvinceNameInAddress(string $provinceName, string $addressT
$provinceName
);
}

private function putProductToCart(ProductInterface $product, string $tokenValue, int $quantity = 1): void
{
$this->client->request(
Request::METHOD_PATCH,
\sprintf(
'/new-api/shop/orders/%s/items',
$tokenValue,
),
[],
[],
$this->getHeaders(),
json_encode([
'productCode' => $product->getCode(),
'productVariantCode' => $this->productVariantResolver->getVariant($product)->getCode(),
'quantity' => $quantity,
], \JSON_THROW_ON_ERROR)
);
}

private function removeOrderItemFromCart(int $orderItemId, string $tokenValue): void
{
$this->client->request(
Request::METHOD_PATCH,
\sprintf(
'/new-api/shop/orders/%s/remove',
$tokenValue,
),
[],
[],
$this->getHeaders(),
json_encode([
'orderItemId' => $orderItemId,
], \JSON_THROW_ON_ERROR)
);
}
}
14 changes: 5 additions & 9 deletions src/Sylius/Behat/Context/Setup/ShippingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ final class ShippingContext implements Context
/** @var ShippingMethodExampleFactory */
private $shippingMethodExampleFactory;

/** @var FactoryInterface */
private $shippingMethodTranslationFactory;

/** @var FactoryInterface */
private $shippingMethodRuleFactory;

Expand All @@ -63,15 +60,13 @@ public function __construct(
ShippingMethodRepositoryInterface $shippingMethodRepository,
RepositoryInterface $zoneRepository,
ShippingMethodExampleFactory $shippingMethodExampleFactory,
FactoryInterface $shippingMethodTranslationFactory,
FactoryInterface $shippingMethodRuleFactory,
ObjectManager $shippingMethodManager
) {
$this->sharedStorage = $sharedStorage;
$this->shippingMethodRepository = $shippingMethodRepository;
$this->zoneRepository = $zoneRepository;
$this->shippingMethodExampleFactory = $shippingMethodExampleFactory;
$this->shippingMethodTranslationFactory = $shippingMethodTranslationFactory;
$this->shippingMethodRuleFactory = $shippingMethodRuleFactory;
$this->shippingMethodManager = $shippingMethodManager;
}
Expand All @@ -93,15 +88,16 @@ public function storeShipsEverythingForFree(ZoneInterface $zone): void
}

/**
* @Given the store ships everywhere for free
* @Given the store ships everywhere for :shippingMethodName
* @Given the store ships everywhere with :shippingMethodName
*/
public function theStoreShipsEverywhereForFree(): void
public function theStoreShipsEverywhereWith(string $shippingMethodName): void
{
/** @var ZoneInterface $zone */
foreach ($this->zoneRepository->findBy(['scope' => [CoreScope::SHIPPING, Scope::ALL]]) as $zone) {
$this->saveShippingMethod($this->shippingMethodExampleFactory->create([
'name' => 'Free',
'code' => 'FREE-' . $zone->getCode(),
'name' => ucfirst($shippingMethodName),
'code' => strtoupper($shippingMethodName) . '-' . $zone->getCode(),
'enabled' => true,
'zone' => $zone,
'calculator' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<argument type="service" id="sylius.repository.shipping_method" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius.repository.payment_method" />
<argument type="service" id="sylius.product_variant_resolver.default" />
<argument type="service" id="sylius.behat.shared_storage" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@
<argument type="service" id="sylius.repository.shipping_method" />
<argument type="service" id="sylius.repository.zone" />
<argument type="service" id="sylius.fixture.example_factory.shipping_method" />
<argument type="service" id="sylius.factory.shipping_method_translation" />
<argument type="service" id="sylius.factory.shipping_method_rule" />
<argument type="service" id="sylius.manager.shipping_method" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart;
use Sylius\Component\Core\Factory\CartItemFactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
Expand Down Expand Up @@ -57,17 +59,20 @@ public function __construct(

public function __invoke(AddItemToCart $addItemToCart): OrderInterface
{
/** @var ProductVariantInterface|null $productVariant */
$productVariant = $this->productVariantRepository->findOneByCodeAndProductCode(
$addItemToCart->productVariantCode,
$addItemToCart->productCode
);

Assert::notNull($productVariant);

/** @var OrderInterface $cart */
$cart = $this->orderRepository->findCartByTokenValue($addItemToCart->orderTokenValue);

Assert::notNull($cart);

/** @var OrderItemInterface $cartItem */
$cartItem = $this->cartItemFactory->createNew();
$cartItem->setVariant($productVariant);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</property>
<property name="quantity">
<constraint name="NotNull" />
<constraint name="PositiveOrZero"></constraint>
<constraint name="PositiveOrZero" />
</property>
</class>
</constraint-mapping>