88use Prophecy \Argument ;
99use Sylius \Bundle \ResourceBundle \Event \ResourceControllerEvent ;
1010use Sylius \Component \Core \Model \PaymentMethodInterface ;
11+ use Sylius \PayPalPlugin \Exception \PayPalPaymentMethodNotFoundException ;
1112use Sylius \PayPalPlugin \Onboarding \Initiator \OnboardingInitiatorInterface ;
13+ use Sylius \PayPalPlugin \Provider \PayPalPaymentMethodProviderInterface ;
1214use Symfony \Component \HttpFoundation \RedirectResponse ;
15+ use Symfony \Component \HttpFoundation \Session \Flash \FlashBagInterface ;
16+ use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
1317
1418final class PayPalPaymentMethodListenerSpec extends ObjectBehavior
1519{
16- function let (OnboardingInitiatorInterface $ onboardingInitiator ): void
17- {
18- $ this ->beConstructedWith ($ onboardingInitiator );
20+ function let (
21+ OnboardingInitiatorInterface $ onboardingInitiator ,
22+ UrlGeneratorInterface $ urlGenerator ,
23+ FlashBagInterface $ flashBag ,
24+ PayPalPaymentMethodProviderInterface $ payPalPaymentMethodProvider
25+ ): void {
26+ $ this ->beConstructedWith (
27+ $ onboardingInitiator ,
28+ $ urlGenerator ,
29+ $ flashBag ,
30+ $ payPalPaymentMethodProvider
31+ );
1932 }
2033
2134 function it_initiates_onboarding_when_creating_a_supported_payment_method (
2235 OnboardingInitiatorInterface $ onboardingInitiator ,
36+ PayPalPaymentMethodProviderInterface $ payPalPaymentMethodProvider ,
2337 ResourceControllerEvent $ event ,
2438 PaymentMethodInterface $ paymentMethod
2539 ): void {
2640 $ event ->getSubject ()->willReturn ($ paymentMethod );
41+ $ payPalPaymentMethodProvider ->provide ()->willThrow (PayPalPaymentMethodNotFoundException::class);
2742
2843 $ onboardingInitiator ->supports ($ paymentMethod )->willReturn (true );
2944
@@ -34,6 +49,8 @@ function it_initiates_onboarding_when_creating_a_supported_payment_method(
3449 $ event ->setResponse (Argument::that (static function ($ argument ): bool {
3550 return $ argument instanceof RedirectResponse && $ argument ->getTargetUrl () === 'https://example.com/onboarding-url ' ;
3651 }))->shouldHaveBeenCalled ();
52+
53+ $ this ->initializeCreate ($ event );
3754 }
3855
3956 function it_throws_an_exception_if_subject_is_not_a_payment_method (ResourceControllerEvent $ event ): void
@@ -43,17 +60,42 @@ function it_throws_an_exception_if_subject_is_not_a_payment_method(ResourceContr
4360 $ this ->shouldThrow (\InvalidArgumentException::class)->during ('initializeCreate ' , [$ event ]);
4461 }
4562
46- function it_does_nothing_when_creating_an_unsupported_payment_method (
63+ function it_redirects_with_error_if_the_pay_pal_payment_method_already_exists (
64+ PayPalPaymentMethodProviderInterface $ payPalPaymentMethodProvider ,
4765 OnboardingInitiatorInterface $ onboardingInitiator ,
66+ UrlGeneratorInterface $ urlGenerator ,
67+ FlashBagInterface $ flashBag ,
4868 ResourceControllerEvent $ event ,
4969 PaymentMethodInterface $ paymentMethod
5070 ): void {
5171 $ event ->getSubject ()->willReturn ($ paymentMethod );
72+ $ payPalPaymentMethodProvider ->provide ()->willReturn ($ paymentMethod );
5273
53- $ onboardingInitiator ->supports ($ paymentMethod )->willReturn (false );
74+ $ flashBag ->add ('error ' , 'sylius.pay_pal.more_than_one_seller_not_allowed ' )->shouldBeCalled ();
75+
76+ $ urlGenerator ->generate ('sylius_admin_payment_method_index ' )->willReturn ('http://redirect-url.com ' );
77+ $ event ->setResponse (Argument::that (function (RedirectResponse $ response ): bool {
78+ return $ response ->getTargetUrl () === 'http://redirect-url.com ' ;
79+ }))->shouldBeCalled ();
80+
81+ $ onboardingInitiator ->initiate (Argument::any ())->shouldNotBeCalled ();
5482
5583 $ this ->initializeCreate ($ event );
84+ }
85+
86+ function it_does_nothing_when_creating_an_unsupported_payment_method (
87+ OnboardingInitiatorInterface $ onboardingInitiator ,
88+ PayPalPaymentMethodProviderInterface $ payPalPaymentMethodProvider ,
89+ ResourceControllerEvent $ event ,
90+ PaymentMethodInterface $ paymentMethod
91+ ): void {
92+ $ event ->getSubject ()->willReturn ($ paymentMethod );
93+ $ payPalPaymentMethodProvider ->provide ()->willThrow (PayPalPaymentMethodNotFoundException::class);
94+
95+ $ onboardingInitiator ->supports ($ paymentMethod )->willReturn (false );
5696
5797 $ event ->setResponse (Argument::any ())->shouldNotHaveBeenCalled ();
98+
99+ $ this ->initializeCreate ($ event );
58100 }
59101}
0 commit comments