Skip to content

Commit 6a0e2d1

Browse files
[Behat][Admin] Cover managing multiple PayPal methods
1 parent 98e81ba commit 6a0e2d1

File tree

5 files changed

+167
-35
lines changed

5 files changed

+167
-35
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@managing_payment_methods
2+
Feature: Managing multiple PayPal payment methods
3+
In order to switch between PayPal configurations
4+
As an Administrator
5+
I want to have multiple PayPal methods but only one enabled at a time
6+
7+
Background:
8+
Given the store operates on a single channel in "United States"
9+
And I am logged in as an administrator
10+
11+
@ui
12+
Scenario: Cannot create and enable a new PayPal method when another is already enabled
13+
Given the store allows paying with "PayPal Sandbox" with "PayPal" factory name
14+
When I create a new PayPal payment method "PayPal Production" and try to save it as enabled
15+
Then I should see a validation error that only one PayPal method can be enabled
16+
And the PayPal payment method "PayPal Production" should not exist
17+
18+
@ui
19+
Scenario: Cannot enable an existing PayPal method when another is already enabled
20+
Given the store allows paying with "PayPal Sandbox" with "PayPal" factory name
21+
And the store has a disabled "PayPal Production" payment method with "PayPal" gateway factory
22+
When I try to enable the PayPal payment method "PayPal Production"
23+
Then I should see a validation error that only one PayPal method can be enabled
24+
And the PayPal payment method "PayPal Production" should still be disabled
25+
26+
@ui
27+
Scenario: Can create and enable a new PayPal method when no other is enabled
28+
Given the store has a disabled "PayPal Sandbox" payment method with "PayPal" gateway factory
29+
When I create a new PayPal payment method "PayPal Production" and save it as enabled
30+
Then the new PayPal payment method should be in the list and enabled

features/trying_to_onboard_more_than_one_pay_pal_seller.feature

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/Behat/Context/Admin/ManagingPaymentMethodsContext.php

Lines changed: 121 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
use Behat\Mink\Exception\ElementNotFoundException;
1818
use Sylius\Behat\Exception\NotificationExpectationMismatchException;
1919
use Sylius\Behat\NotificationType;
20+
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
2021
use Sylius\Behat\Page\Admin\PaymentMethod\CreatePageInterface;
22+
use Sylius\Behat\Page\Admin\PaymentMethod\UpdatePageInterface;
2123
use Sylius\Behat\Service\NotificationCheckerInterface;
24+
use Sylius\Behat\Service\SharedStorageInterface;
25+
use Sylius\Component\Payment\Repository\PaymentMethodRepositoryInterface;
2226
use Sylius\PayPalPlugin\DependencyInjection\SyliusPayPalExtension;
2327
use Tests\Sylius\PayPalPlugin\Behat\Element\DownloadPayPalReportElementInterface;
2428
use Webmozart\Assert\Assert;
@@ -29,6 +33,10 @@ public function __construct(
2933
private DownloadPayPalReportElementInterface $downloadPayPalReportElement,
3034
private NotificationCheckerInterface $notificationChecker,
3135
private CreatePageInterface $createPage,
36+
private UpdatePageInterface $updatePage,
37+
private IndexPageInterface $indexPage,
38+
private PaymentMethodRepositoryInterface $paymentMethodRepository,
39+
private SharedStorageInterface $sharedStorage,
3240
) {
3341
}
3442

@@ -49,11 +57,52 @@ public function yesterdayReportCsvFileShouldBeSuccessfullyDownloaded(): void
4957
}
5058

5159
/**
52-
* @When I try to create a new payment method with "PayPal" gateway factory
60+
* @When I create a new PayPal payment method :name and try to save it as enabled
5361
*/
54-
public function iTryToCreateANewPaymentMethodWithGatewayFactory(): void
62+
public function iCreateANewPayPalPaymentMethodAndTryToSaveItAsEnabled(string $name): void
5563
{
56-
$this->createPage->tryToOpen(['factory' => SyliusPayPalExtension::PAYPAL_FACTORY_NAME]);
64+
$code = $this->normalizeCode($name);
65+
66+
$this->createPage->open(['factory' => SyliusPayPalExtension::PAYPAL_FACTORY_NAME]);
67+
$this->createPage->nameIt($name, 'en_US');
68+
$this->createPage->specifyCode($code);
69+
$this->createPage->create();
70+
71+
$this->sharedStorage->set('payment_method_name', $name);
72+
$this->sharedStorage->set('payment_method_code', $code);
73+
}
74+
75+
/**
76+
* @When I try to enable the PayPal payment method :name
77+
*/
78+
public function iTryToEnableThePayPalPaymentMethod(string $name): void
79+
{
80+
$code = $this->normalizeCode($name);
81+
$paymentMethod = $this->paymentMethodRepository->findOneBy(['code' => $code]);
82+
Assert::notNull($paymentMethod, sprintf('Payment method "%s" not found', $name));
83+
84+
$this->updatePage->open(['id' => $paymentMethod->getId()]);
85+
$this->updatePage->enable();
86+
$this->updatePage->saveChanges();
87+
88+
$this->sharedStorage->set('payment_method_name', $name);
89+
$this->sharedStorage->set('payment_method_code', $code);
90+
}
91+
92+
/**
93+
* @When I create a new PayPal payment method :name and save it as enabled
94+
*/
95+
public function iCreateANewPayPalPaymentMethodAndSaveItAsEnabled(string $name): void
96+
{
97+
$code = $this->normalizeCode($name);
98+
99+
$this->createPage->open(['factory' => SyliusPayPalExtension::PAYPAL_FACTORY_NAME]);
100+
$this->createPage->nameIt($name, 'en_US');
101+
$this->createPage->specifyCode($code);
102+
$this->createPage->create();
103+
104+
$this->sharedStorage->set('payment_method_name', $name);
105+
$this->sharedStorage->set('payment_method_code', $code);
57106
}
58107

59108
/**
@@ -68,19 +117,79 @@ public function iShouldBeNotifiedThatICannotOnboardMoreThanOnePayPalSeller(): vo
68117
}
69118

70119
/**
71-
* @Then I should not be notified that I cannot onboard more than one PayPal seller
120+
* @Then I should see a validation error that only one PayPal method can be enabled
72121
*/
73-
public function iShouldNotBeNotifiedThatICannotOnboardMoreThanOnePayPalSeller(): void
122+
public function iShouldSeeAValidationErrorThatOnlyOnePayPalMethodCanBeEnabled(): void
74123
{
75124
try {
76-
$this->notificationChecker->checkNotification(
77-
'You cannot onboard more than one PayPal seller!',
78-
NotificationType::failure(),
79-
);
80-
} catch (NotificationExpectationMismatchException|ElementNotFoundException $exception) {
81-
return;
125+
$message = $this->updatePage->getValidationMessage('enabled');
126+
} catch (ElementNotFoundException) {
127+
$message = $this->createPage->getValidationMessage('enabled');
82128
}
83129

84-
throw new \DomainException('Step should fail');
130+
Assert::contains(
131+
$message,
132+
'Only one PayPal payment method can be enabled at a time',
133+
);
134+
}
135+
136+
/**
137+
* @Then the PayPal payment method :name should not exist
138+
*/
139+
public function thePayPalPaymentMethodShouldNotExist(string $name): void
140+
{
141+
$this->indexPage->open();
142+
143+
Assert::false(
144+
$this->indexPage->isSingleResourceOnPage(['name' => $name]),
145+
sprintf('Payment method "%s" should not exist', $name),
146+
);
147+
}
148+
149+
/**
150+
* @Then the PayPal payment method :name should still be disabled
151+
*/
152+
public function thePayPalPaymentMethodShouldStillBeDisabled(string $name): void
153+
{
154+
$paymentMethod = $this->paymentMethodRepository->findOneBy(['code' => $this->normalizeCode($name)]);
155+
Assert::notNull($paymentMethod, sprintf('Payment method "%s" not found', $name));
156+
157+
$this->updatePage->open(['id' => $paymentMethod->getId()]);
158+
159+
Assert::false(
160+
$this->updatePage->isPaymentMethodEnabled(),
161+
sprintf('Payment method "%s" should be disabled', $name),
162+
);
163+
}
164+
165+
/**
166+
* @Then the new PayPal payment method should be in the list and enabled
167+
*/
168+
public function theNewPayPalPaymentMethodShouldBeInTheListAndEnabled(): void
169+
{
170+
$name = $this->sharedStorage->get('payment_method_name');
171+
$code = $this->sharedStorage->get('payment_method_code');
172+
173+
$this->indexPage->open();
174+
175+
Assert::true(
176+
$this->indexPage->isSingleResourceOnPage(['name' => $name]),
177+
sprintf('Payment method "%s" should exist in the list', $name),
178+
);
179+
180+
$paymentMethod = $this->paymentMethodRepository->findOneBy(['code' => $code]);
181+
Assert::notNull($paymentMethod, sprintf('Payment method "%s" not found', $name));
182+
183+
$this->updatePage->open(['id' => $paymentMethod->getId()]);
184+
185+
Assert::true(
186+
$this->updatePage->isPaymentMethodEnabled(),
187+
sprintf('Payment method "%s" should be enabled', $name),
188+
);
189+
}
190+
191+
private function normalizeCode(string $name): string
192+
{
193+
return 'PM_' . str_replace(' ', '_', strtoupper($name));
85194
}
86195
}

tests/Behat/Context/Setup/PaymentPayPalContext.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,17 @@ public function __construct(
3939
* @Given /^the store allows paying with "([^"]*)" with "([^"]*)" factory name at position (\d+)$/
4040
* @Given /^the store allows paying with "([^"]*)" with "([^"]*)" factory name$/
4141
*/
42-
public function theStoreAllowsPayingWithWithFactoryNameAtPosition(string $paymentMethodName, string $gatewayFactory, ?int $position = 0)
42+
public function theStoreAllowsPayingWithWithFactoryNameAtPosition(string $paymentMethodName, string $gatewayFactory, ?int $position = 0): void
4343
{
44-
$this->createPaymentMethod($paymentMethodName, 'PM_' . $paymentMethodName, $gatewayFactory, 'Payment method', $position);
44+
$this->createPaymentMethod($paymentMethodName, 'PM_' . str_replace(' ', '_', strtoupper($paymentMethodName)), $gatewayFactory, 'Payment method', $position, true);
45+
}
46+
47+
/**
48+
* @Given /^the store has a disabled "([^"]*)" payment method with "([^"]*)" gateway factory$/
49+
*/
50+
public function theStoreHasADisabledPaymentMethodWithGatewayFactory(string $paymentMethodName, string $gatewayFactory): void
51+
{
52+
$this->createPaymentMethod($paymentMethodName, 'PM_' . str_replace(' ', '_', strtoupper($paymentMethodName)), $gatewayFactory, 'Payment method', 0, false);
4553
}
4654

4755
/**
@@ -58,6 +66,7 @@ private function createPaymentMethod(
5866
string $gatewayFactory,
5967
string $description,
6068
int $position,
69+
bool $enabled = true,
6170
): void {
6271
$gatewayFactory = $this->findGatewayNameByTranslation($gatewayFactory, $this->gatewayFactories);
6372

@@ -68,7 +77,7 @@ private function createPaymentMethod(
6877
'description' => $description,
6978
'gatewayName' => $gatewayFactory,
7079
'gatewayFactory' => $gatewayFactory,
71-
'enabled' => true,
80+
'enabled' => $enabled,
7281
'channels' => ($this->sharedStorage->has('channel')) ? [$this->sharedStorage->get('channel')] : [],
7382
]);
7483

tests/Behat/Resources/services.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
<argument type="service" id="Tests\Sylius\PayPalPlugin\Behat\Element\DownloadPayPalReportElement" />
2525
<argument type="service" id="sylius.behat.notification_checker.admin" />
2626
<argument type="service" id="sylius.behat.page.admin.payment_method.create" />
27+
<argument type="service" id="sylius.behat.page.admin.payment_method.update" />
28+
<argument type="service" id="sylius.behat.page.admin.payment_method.index" />
29+
<argument type="service" id="sylius.repository.payment_method" />
30+
<argument type="service" id="sylius.behat.shared_storage" />
2731
</service>
2832

2933
<service id="sylius.behat.context.ui.admin.managing_payment_methods" class="Sylius\Behat\Context\Ui\Admin\ManagingPaymentMethodsContext">

0 commit comments

Comments
 (0)