1717use Behat \Mink \Exception \ElementNotFoundException ;
1818use Sylius \Behat \Exception \NotificationExpectationMismatchException ;
1919use Sylius \Behat \NotificationType ;
20+ use Sylius \Behat \Page \Admin \Crud \IndexPageInterface ;
2021use Sylius \Behat \Page \Admin \PaymentMethod \CreatePageInterface ;
22+ use Sylius \Behat \Page \Admin \PaymentMethod \UpdatePageInterface ;
2123use Sylius \Behat \Service \NotificationCheckerInterface ;
24+ use Sylius \Behat \Service \SharedStorageInterface ;
25+ use Sylius \Component \Payment \Repository \PaymentMethodRepositoryInterface ;
2226use Sylius \PayPalPlugin \DependencyInjection \SyliusPayPalExtension ;
2327use Tests \Sylius \PayPalPlugin \Behat \Element \DownloadPayPalReportElementInterface ;
2428use 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}
0 commit comments