Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Api][Checkout] cover skipping payment steps in behat #11705

Merged
merged 3 commits into from
Aug 5, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ Feature: Skipping payment step when only one payment method is available
And the store has a product "Guards! Guards!" priced at "$20.00"
And the store allows paying with "Paypal Express Checkout"

@ui
@ui @api
Scenario: Seeing checkout completion page after shipping if only one payment method is available
Given I have product "Guards! Guards!" in the cart
And I have completed addressing step with email "[email protected]" and "United States" based billing address
And I try to complete the shipping step
When I add product "Guards! Guards!" to the cart
And I complete addressing step with email "[email protected]" and "United States" based billing address
And I complete the shipping step with first shipping method
Then I should be on the checkout complete step
And my order's payment method should be "Paypal Express Checkout"

@ui
@ui @api
Scenario: Seeing checkout completion page after shipping if only one payment method is available
Given the store has "Offline" payment method not assigned to any channel
And I have product "Guards! Guards!" in the cart
And I have completed addressing step with email "[email protected]" and "United States" based billing address
And I try to complete the shipping step
When I add product "Guards! Guards!" to the cart
And I complete addressing step with email "[email protected]" and "United States" based billing address
And I complete the shipping step with first shipping method
Then I should be on the checkout complete step
And my order's payment method should be "Paypal Express Checkout"

@ui
@ui @api
Scenario: Seeing checkout completion page after shipping if only one payment method is available
Given the store allows paying with "Offline"
And the payment method "Offline" is disabled
And I have product "Guards! Guards!" in the cart
And I have completed addressing step with email "[email protected]" and "United States" based billing address
And I try to complete the shipping step
When I add product "Guards! Guards!" to the cart
And I complete addressing step with email "[email protected]" and "United States" based billing address
And I complete the shipping step with first shipping method
Then I should be on the checkout complete step
And my order's payment method should be "Paypal Express Checkout"
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature: Skipping payment selection when order total is zero
And the promotion gives "$10.00" discount to every order with quantity at least 1
And I am a logged in customer

@ui
@ui @api
Scenario: Seeing order summary after shipping selection when order total is zero
Given I have product "PHP T-Shirt" in the cart
And I am at the checkout addressing step
Expand All @@ -24,7 +24,7 @@ Feature: Skipping payment selection when order total is zero
Then I should be on the checkout summary step
And I should not see any information about payment method

@ui
@ui @api
Scenario: Seeing payment selection after shipping selection when order total is not zero
Given I have product "PHP T-Shirt" in the cart
And I am at the checkout addressing step
Expand Down
1 change: 1 addition & 0 deletions src/Sylius/Behat/Context/Api/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function iSeeTheSummaryOfMyCart(string $tokenValue): void
/**
* @When /^I (?:add|added) (this product) to the (cart)$/
* @When /^I (?:add|added) ("[^"]+" product) to the (cart)$/
* @When /^I add (product "[^"]+") to the (cart)$/
*/
public function iAddThisProductToTheCart(ProductInterface $product, string $tokenValue): void
{
Expand Down
84 changes: 78 additions & 6 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Webmozart\Assert\Assert;

final class CheckoutContext implements Context
Expand All @@ -35,21 +37,28 @@ final class CheckoutContext implements Context
/** @var SharedStorageInterface */
private $sharedStorage;

/** @var RepositoryInterface */
private $shippingMethodRepository;

/** @var string[] */
private $content = [];

public function __construct(
AbstractBrowser $client,
ResponseCheckerInterface $responseChecker,
SharedStorageInterface $sharedStorage
SharedStorageInterface $sharedStorage,
RepositoryInterface $shippingMethodRepository
) {
$this->client = $client;
$this->responseChecker = $responseChecker;
$this->sharedStorage = $sharedStorage;
$this->shippingMethodRepository = $shippingMethodRepository;
}

/**
* @Given I am at the checkout addressing step
* @When I complete the shipping step
* @When And I complete the shipping step
*/
public function iAmAtTheCheckoutAddressingStep(): void
{
Expand Down Expand Up @@ -120,6 +129,7 @@ public function iCompleteAddressingStepWithEmail(string $email, AddressInterface

/**
* @When I proceed with :shippingMethod shipping method
* @When I select :shippingMethod shipping method
*/
public function iProceededWithShippingMethod(ShippingMethodInterface $shippingMethod): void
{
Expand Down Expand Up @@ -204,24 +214,78 @@ public function iCompleteTheAddressingStep(): void
$this->content = [];
}

/**
* @When I complete the shipping step with first shipping method
*/
public function iCompleteTheShippingStepWithFirstShippingMethod(): void
{
/** @var ShippingMethodInterface $shippingMethod */
$shippingMethod = $this->shippingMethodRepository->findOneBy([]);

$this->iProceededWithShippingMethod($shippingMethod);
}

/**
* @Then I should see the thank you page
*/
public function iShouldSeeTheThankYouPage(): void
{
$value = $this->responseChecker->getValue($this->client->getResponse(), 'checkoutState');

Assert::same($value, OrderCheckoutStates::STATE_COMPLETED);
Assert::same($this->getCheckoutState(), OrderCheckoutStates::STATE_COMPLETED);
}

/**
* @Then I should be on the checkout shipping step
*/
public function iShouldBeOnTheCheckoutShippingStep(): void
{
$value = $this->responseChecker->getValue($this->client->getResponse(), 'checkoutState');
Assert::same($this->getCheckoutState(), OrderCheckoutStates::STATE_ADDRESSED);
}

/**
* @Then I should be on the checkout payment step
*/
public function iShouldBeOnTheCheckoutPaymentStep(): void
{
Assert::inArray(
$this->getCheckoutState(),
[OrderCheckoutStates::STATE_SHIPPING_SELECTED, OrderCheckoutStates::STATE_SHIPPING_SKIPPED]
);
}

/**
* @Then I should be on the checkout complete step
* @Then I should be on the checkout summary step
*/
public function iShouldBeOnTheCheckoutCompleteStep(): void
{
Assert::inArray(
$this->getCheckoutState(),
[OrderCheckoutStates::STATE_PAYMENT_SKIPPED, OrderCheckoutStates::STATE_PAYMENT_SELECTED]
);
}

/**
* @Then my order's payment method should be :paymentMethod
*/
public function myOrdersPaymentMethodShouldBe(PaymentMethodInterface $paymentMethod): void
{
/** @var Response $response */
$response = $this->client->getResponse();
Assert::same(
$this->responseChecker->getResponseContent($response)['payments'][0]['method']['name'],
$paymentMethod->getName()
);
}

/**
* @Then I should not see any information about payment method
*/
public function iShouldNotSeeAnyInformationAboutPaymentMethod()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function iShouldNotSeeAnyInformationAboutPaymentMethod()
public function iShouldNotSeeAnyInformationAboutPaymentMethod(): void

{
/** @var Response $response */
$response = $this->client->getResponse();

Assert::same($value, OrderCheckoutStates::STATE_ADDRESSED);
Assert::true(empty($this->responseChecker->getResponseContent($response)['payments']));
}

private function addressOrder(array $content): void
Expand Down Expand Up @@ -252,4 +316,12 @@ private function getHeaders(array $headers = []): array

return $headers;
}

private function getCheckoutState(): string
{
/** @var Response $response */
$response = $this->client->getResponse();

return $this->responseChecker->getValue($response, 'checkoutState');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sylius\Behat\Page\Shop\Checkout\CompletePageInterface;
use Sylius\Behat\Page\Shop\Checkout\SelectPaymentPageInterface;
use Sylius\Behat\Page\Shop\Checkout\SelectShippingPageInterface;
use Sylius\Component\Shipping\Model\ShippingMethodInterface;
use Webmozart\Assert\Assert;

final class CheckoutShippingContext implements Context
Expand Down Expand Up @@ -44,7 +45,7 @@ public function __construct(
* @Given I have proceeded selecting :shippingMethodName shipping method
* @When I proceed with :shippingMethodName shipping method
*/
public function iHaveProceededSelectingShippingMethod($shippingMethodName)
public function iHaveProceededSelectingShippingMethod(string $shippingMethodName): void
{
$this->iSelectShippingMethod($shippingMethodName);
$this->selectShippingPage->nextStep();
Expand All @@ -69,8 +70,9 @@ public function iTryToOpenCheckoutShippingPage()

/**
* @When /^I(?:| try to) complete the shipping step$/
* @When I complete the shipping step with first shipping method
*/
public function iCompleteTheShippingStep()
public function iCompleteTheShippingStep(): void
{
$this->selectShippingPage->nextStep();
}
Expand Down
15 changes: 9 additions & 6 deletions src/Sylius/Behat/Context/Ui/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Sylius\Behat\Page\Shop\Checkout\SelectShippingPageInterface;
use Sylius\Behat\Service\Resolver\CurrentPageResolverInterface;
use Sylius\Component\Addressing\Model\CountryInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Webmozart\Assert\Assert;

final class CheckoutContext implements Context
Expand Down Expand Up @@ -94,10 +95,10 @@ public function iWasAtTheCheckoutSummaryStep()
}

/**
* @Given I chose :shippingMethodName shipping method
* @Given I chose :shippingMethodname shipping method
* @When I proceed selecting :shippingMethodName shipping method
*/
public function iProceedSelectingShippingMethod($shippingMethodName)
public function iProceedSelectingShippingMethod(string $shippingMethodName): void
{
$this->iProceedSelectingBillingCountryAndShippingMethod(null, $shippingMethodName);
}
Expand All @@ -116,7 +117,7 @@ public function iProceedSelectingPaymentMethod($paymentMethodName)
* @Given I have proceeded order with :shippingMethodName shipping method and :paymentMethodName payment
* @When I proceed with :shippingMethodName shipping method and :paymentMethodName payment
*/
public function iProceedOrderWithShippingMethodAndPayment($shippingMethodName, $paymentMethodName)
public function iProceedOrderWithShippingMethodAndPayment(string $shippingMethodName, $paymentMethodName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function iProceedOrderWithShippingMethodAndPayment(string $shippingMethodName, $paymentMethodName)
public function iProceedOrderWithShippingMethodAndPayment(string $shippingMethodName, string $paymentMethodName): void

{
$this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName);
$this->paymentContext->iChoosePaymentMethod($paymentMethodName);
Expand All @@ -138,16 +139,18 @@ public function iProceedThroughCheckoutProcess(string $localeCode = 'en_US', ?st
/**
* @When /^I proceed selecting ("[^"]+" as billing country) with "([^"]+)" method$/
*/
public function iProceedSelectingBillingCountryAndShippingMethod(CountryInterface $shippingCountry = null, $shippingMethodName = null)
{
public function iProceedSelectingBillingCountryAndShippingMethod(
CountryInterface $shippingCountry = null,
?string $shippingMethodName = null
): void {
$this->addressingContext->iProceedSelectingBillingCountry($shippingCountry);
$this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName ?: 'Free');
}

/**
* @When /^I change shipping method to "([^"]*)"$/
*/
public function iChangeShippingMethod($shippingMethodName)
public function iChangeShippingMethod(string $shippingMethodName): void
{
$this->paymentContext->iDecideToChangeMyShippingMethod();
$this->shippingContext->iHaveProceededSelectingShippingMethod($shippingMethodName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<argument type="service" id="test.client" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="sylius.behat.shared_storage" />
<argument type="service" id="sylius.repository.shipping_method" />
</service>

<service id="sylius.behat.context.api.shop.homepage" class="Sylius\Behat\Context\Api\Shop\HomepageContext">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ default:
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.payment
- sylius.behat.context.setup.product
- sylius.behat.context.setup.promotion
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.shop_api_security

- sylius.behat.context.api.admin.managing_orders

- sylius.behat.context.api.shop.cart
- sylius.behat.context.api.shop.checkout
- sylius.behat.context.api.shop.login

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function __invoke(AddressOrder $addressOrder): OrderInterface
Assert::notNull($order, sprintf('Order with %s token has not been found.', $tokenValue));

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

Assert::true(
$stateMachine->can(OrderCheckoutTransitions::TRANSITION_ADDRESS),
sprintf('Order with %s token cannot be addressed.', $tokenValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<attribute name="denormalization_context">
<attribute name="groups">cart:address</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">checkout:read</attribute>
</attribute>
</itemOperation>

<itemOperation name="select_shipping_method">
Expand All @@ -100,6 +103,9 @@
<attribute name="denormalization_context">
<attribute name="groups">cart:select_shipping_method</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">checkout:read</attribute>
</attribute>
</itemOperation>

<itemOperation name="select_payment_method">
Expand All @@ -114,6 +120,9 @@
<attribute name="denormalization_context">
<attribute name="groups">cart:select_payment_method</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">checkout:read</attribute>
</attribute>
</itemOperation>

<itemOperation name="complete">
Expand All @@ -128,6 +137,9 @@
<attribute name="denormalization_context">
<attribute name="groups">cart:complete</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">checkout:read</attribute>
</attribute>
</itemOperation>

<itemOperation name="remove_item">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
</itemOperations>

<property name="id" identifier="true" writable="false" />
<property name="name" writable="true" />
</resource>
</resources>
Loading