Skip to content

Commit

Permalink
Refactor change payment methods behats
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamKasp committed Oct 15, 2020
1 parent 6b1484e commit 5aa9537
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,24 @@ Feature: Changing the method after order confirmation
And the store has a product "PHP T-Shirt" priced at "$19.99"
And the store ships everywhere for free

@ui
@ui @api
Scenario: Retrying the payment with offline payment
Given I added product "PHP T-Shirt" to the cart
And I complete addressing step with email "john@example.com" and "United States" based billing address
And I have proceeded selecting "Free" shipping method
And I have proceeded selecting "Cash on delivery" payment method
And I have confirmed order
When I go to the change payment method page
And I try to pay with "Offline" payment method
Then I should see the thank you page
When I change payment method to "Offline" after checkout
Then I should have chosen "Offline" payment method

@ui
@ui @api
Scenario: Retrying the payment with offline payment works correctly together with inventory
Given there is 1 unit of product "PHP T-Shirt" available in the inventory
And this product is tracked by the inventory
And I added product "PHP T-Shirt" to the cart
And I complete addressing step with email "john@example.com" and "United States" based billing address
And I have proceeded selecting "Free" shipping method
And I have proceeded selecting "Cash on delivery" payment method
And I have confirmed order
When I go to the change payment method page
And I try to pay with "Offline" payment method
Then I should see the thank you page

@api
Scenario: Changing the payment method for placed order
Given I added product "PHP T-Shirt" to the cart
When I complete addressing step with email "john@example.com" and "United States" based billing address
And I select "Free" shipping method
And I select "Cash on delivery" payment method
And I confirm my order
And I change payment method to "Offline"
Then I should have chosen "Offline" payment method for my order
When I change payment method to "Offline" after checkout
Then I should have chosen "Offline" payment method
11 changes: 9 additions & 2 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentMethod;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
Expand All @@ -35,6 +34,7 @@
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
use Symfony\Component\HttpFoundation\Response;
use Webmozart\Assert\Assert;

Expand All @@ -58,6 +58,9 @@ final class CheckoutContext implements Context
/** @var ApiClientInterface */
private $addressesClient;

/** @var ApiClientInterface */
private $paymentClient;

/** @var IriConverterInterface */
private $iriConverter;

Expand Down Expand Up @@ -87,6 +90,7 @@ public function __construct(
ApiClientInterface $ordersClient,
ApiClientInterface $countriesClient,
ApiClientInterface $addressesClient,
ApiClientInterface $paymentClient,
IriConverterInterface $iriConverter,
ResponseCheckerInterface $responseChecker,
RepositoryInterface $shippingMethodRepository,
Expand All @@ -99,6 +103,7 @@ public function __construct(
$this->ordersClient = $ordersClient;
$this->countriesClient = $countriesClient;
$this->addressesClient = $addressesClient;
$this->paymentClient = $paymentClient;
$this->iriConverter = $iriConverter;
$this->responseChecker = $responseChecker;
$this->shippingMethodRepository = $shippingMethodRepository;
Expand Down Expand Up @@ -301,6 +306,7 @@ public function iProvideAdditionalNotesLike(string $notes): void

/**
* @Given I confirmed my order
* @Given I have confirmed order
* @When I confirm my order
* @When I try to confirm my order
* @When /^the (?:visitor|customer) confirm his order$/
Expand Down Expand Up @@ -340,6 +346,7 @@ public function iConfirmMyOrder(): void
* @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
* @Given I have proceeded selecting :shippingMethod shipping method
*/
public function iProceededWithShippingMethod(ShippingMethodInterface $shippingMethod): void
{
Expand Down Expand Up @@ -388,7 +395,7 @@ public function iCompleteTheShippingStepWithFirstShippingMethod(): void
* @When /^the (?:customer|visitor) proceed with ("[^"]+" payment)$/
* @Given /^the (?:customer|visitor) has proceeded ("[^"]+" payment)$/
* @When I try to change payment method to :paymentMethod payment
* @When I change payment method to :paymentMethod
* @When I change payment method to :paymentMethod after checkout
*/
public function iChoosePaymentMethod(PaymentMethodInterface $paymentMethod): void
{
Expand Down
8 changes: 3 additions & 5 deletions src/Sylius/Behat/Context/Api/Shop/OrderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,15 @@ public function theShouldHaveUnitPriceDiscountedFor(ProductInterface $product, i
}

/**
* @Then I should have chosen :paymentMethod payment method for my order
* @Then I should have chosen :paymentMethod payment method
*/
public function iShouldHaveChosenPaymentMethodForMyOrder(PaymentMethod $paymentMethod): void
{
$payment = $this->iriConverter->getItemFromIri(
$this->responseChecker->getValue($this->client->show($this->sharedStorage->get('cart_token')), 'payments')[0]
);
$paymentIri = $this->responseChecker->getValue($this->client->show($this->sharedStorage->get('cart_token')), 'payments')[0];

Assert::same(
$this->iriConverter->getIriFromItem($paymentMethod),
$this->iriConverter->getIriFromItem($payment->getMethod())
$this->responseChecker->getValue($this->client->showByIri($this->adminToShopIriConverter->convert($paymentIri)),'method')['@id']
);
}

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

use Behat\Behat\Context\Context;
use Sylius\Behat\Page\Shop\Order\ShowPageInterface;
use Sylius\Behat\Page\Shop\Order\ThankYouPageInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Webmozart\Assert\Assert;

Expand All @@ -23,40 +24,54 @@ final class CheckoutOrderDetailsContext implements Context
/** @var ShowPageInterface */
private $orderDetails;

public function __construct(ShowPageInterface $orderDetails)
/** @var ThankYouPageInterface */
private $thankYouPage;

public function __construct(ShowPageInterface $orderDetails, ThankYouPageInterface $thankYouPage)
{
$this->orderDetails = $orderDetails;
$this->thankYouPage = $thankYouPage;
}

/**
* @When /^I want to browse order details for (this order)$/
*/
public function iWantToBrowseOrderDetailsForThisOrder(OrderInterface $order)
public function iWantToBrowseOrderDetailsForThisOrder(OrderInterface $order): void
{
$this->orderDetails->open(['tokenValue' => $order->getTokenValue()]);
}

/**
* @When I try to pay with :paymentMethodName payment method
*/
public function iChangePaymentMethodTo($paymentMethodName)
public function iChangePaymentMethodTo(string $paymentMethodName): void
{
$this->orderDetails->choosePaymentMethod($paymentMethodName);
$this->orderDetails->pay();
}

/**
* @When I change payment method to :paymentMethodName after checkout
*/
public function iChoosePaymentMethod(string $paymentMethodName): void
{
$this->thankYouPage->goToTheChangePaymentMethodPage();
$this->orderDetails->choosePaymentMethod($paymentMethodName);
$this->orderDetails->pay();
}

/**
* @Then I should be able to pay (again)
*/
public function iShouldBeAbleToPay()
public function iShouldBeAbleToPay(): void
{
Assert::true($this->orderDetails->hasPayAction());
}

/**
* @Then I should not be able to pay (again)
*/
public function iShouldNotBeAbleToPay()
public function iShouldNotBeAbleToPay(): void
{
Assert::false($this->orderDetails->hasPayAction());
}
Expand All @@ -68,4 +83,13 @@ public function iShouldSeeAsNumberOfItems(int $quantity): void
{
Assert::same($this->orderDetails->getAmountOfItems(), $quantity);
}

/**
* @Then I should have chosen :paymentMethodName payment method
*/
public function iShouldHaveChosenPaymentMethod(string $paymentMethodName): void
{
$this->thankYouPage->goToTheChangePaymentMethodPage();
Assert::same($this->orderDetails->getChosenPaymentMethod(), $paymentMethodName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Behat\Behat\Context\Context;
use Sylius\Behat\Page\Shop\Account\Order\ShowPageInterface;
use Sylius\Behat\Page\Shop\Order\ShowPageInterface as OrderDetailsPage;
use Sylius\Behat\Page\Shop\Order\ThankYouPageInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
Expand All @@ -28,17 +29,22 @@ final class CheckoutThankYouContext implements Context
/** @var ShowPageInterface */
private $orderShowPage;

/** @var OrderDetailsPage */
private $orderDetails;

/** @var OrderRepositoryInterface */
private $orderRepository;

public function __construct(
ThankYouPageInterface $thankYouPage,
ShowPageInterface $orderShowPage,
OrderRepositoryInterface $orderRepository
OrderRepositoryInterface $orderRepository,
OrderDetailsPage $orderDetails
) {
$this->thankYouPage = $thankYouPage;
$this->orderShowPage = $orderShowPage;
$this->orderRepository = $orderRepository;
$this->orderDetails = $orderDetails;
}

/**
Expand All @@ -57,6 +63,16 @@ public function iProceedToTheRegistration(): void
$this->thankYouPage->createAccount();
}

/**
* @When I change payment method to :paymentMethodName
*/
public function iChoosePaymentMethod(string $paymentMethodName): void
{
$this->iGoToTheChangePaymentMethodPage();
$this->orderDetails->choosePaymentMethod($paymentMethodName);
$this->orderDetails->pay();
}

/**
* @Then I should be able to access this order's details
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Sylius/Behat/Page/Shop/Order/ShowPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ public function getAmountOfItems(): int
return count($paymentItems);
}

public function getChosenPaymentMethod(): string
{
$paymentMethodItems = $this->getDocument()->findAll('css', '[data-test-payment-item]');

foreach ($paymentMethodItems as $method) {
if ($method->find('css', '[data-test-payment-method-select]')->hasAttribute('checked')) {
return $method->find('css', 'a')->getText();
}
}

return '';
}

protected function getDefinedElements(): array
{
return array_merge(parent::getDefinedElements(), [
Expand Down
2 changes: 2 additions & 0 deletions src/Sylius/Behat/Page/Shop/Order/ShowPageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public function choosePaymentMethod(string $paymentMethodName): void;
public function getNotifications(): array;

public function getAmountOfItems(): int;

public function getChosenPaymentMethod(): string;
}
5 changes: 5 additions & 0 deletions src/Sylius/Behat/Resources/config/services/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
<argument>admin</argument>
</service>

<service id="sylius.behat.api_platform_client.shop.payment" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
<argument>payments</argument>
<argument>shop</argument>
</service>

<service id="sylius.behat.api_platform_client.admin.product_association_type" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
<argument>product-association-types</argument>
<argument>admin</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<argument type="service" id="sylius.behat.api_platform_client.shop.order" />
<argument type="service" id="sylius.behat.api_platform_client.shop.country" />
<argument type="service" id="sylius.behat.api_platform_client.shop.address" />
<argument type="service" id="sylius.behat.api_platform_client.shop.payment" />
<argument type="service" id="api_platform.iri_converter" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="sylius.repository.shipping_method" />
Expand Down
4 changes: 3 additions & 1 deletion src/Sylius/Behat/Resources/config/services/contexts/ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,14 @@

<service id="sylius.behat.context.ui.shop.checkout.thank_you" class="Sylius\Behat\Context\Ui\Shop\Checkout\CheckoutThankYouContext">
<argument type="service" id="sylius.behat.page.shop.order.thank_you" />
<argument type="service" id="sylius.behat.page.shop.account.order.show"></argument>
<argument type="service" id="sylius.behat.page.shop.account.order.show" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius.behat.page.shop.order.show" />
</service>

<service id="sylius.behat.context.ui.shop.checkout.order_details" class="Sylius\Behat\Context\Ui\Shop\Checkout\CheckoutOrderDetailsContext">
<argument type="service" id="sylius.behat.page.shop.order.show" />
<argument type="service" id="sylius.behat.page.shop.order.thank_you" />
</service>

<service id="sylius.behat.context.ui.shop.checkout.addressing" class="Sylius\Behat\Context\Ui\Shop\Checkout\CheckoutAddressingContext">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public function __invoke(ChoosePaymentMethod $choosePaymentMethod): OrderInterfa
if (in_array(
$cart->getCheckoutState(),
[OrderCheckoutStates::STATE_SHIPPING_SELECTED, OrderCheckoutStates::STATE_SHIPPING_SKIPPED, OrderCheckoutStates::STATE_PAYMENT_SELECTED],
true)) {
true)
) {
$stateMachine = $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH);

Assert::true($stateMachine->can(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ private function applyUserRulesToItem(
string $httpRequestMethodType,
string $operationName
): void {
if ($user === null && $operationName === 'shop_select_payment_method') {
$queryBuilder
->leftJoin(sprintf('%s.customer', $rootAlias), 'customer')
->leftJoin('customer.user', 'user')
->andWhere('user IS NULL')
->orWhere(sprintf('%s.customer IS NULL', $rootAlias))
;

if ($operationName === 'shop_select_payment_method') {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<attribute name="path">/admin/payments/{id}</attribute>
</itemOperation>

<itemOperation name="shop_get">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/payments/{id}</attribute>
</itemOperation>

<itemOperation name="admin_complete">
<attribute name="method">PATCH</attribute>
<attribute name="input">false</attribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</attribute>
<attribute name="name">
<group>payment_method:read</group>
<group>payment:read</group>
<group>checkout:read</group>
</attribute>
<attribute name="code">
Expand Down

0 comments on commit 5aa9537

Please sign in to comment.