2929use Swag \PayPal \Webhook \Registration \WebhookSystemConfigHelper ;
3030use Swag \PayPal \Webhook \WebhookRegistry ;
3131use Swag \PayPal \Webhook \WebhookService ;
32+ use Symfony \Component \HttpFoundation \Request ;
3233use Symfony \Component \HttpFoundation \RequestStack ;
3334use Symfony \Component \Routing \RouterInterface ;
3435
@@ -44,9 +45,15 @@ class WebhookSubscriberTest extends TestCase
4445
4546 private SystemConfigService $ systemConfigService ;
4647
48+ private WebhookSystemConfigHelper $ webhookSystemConfigHelper ;
49+
50+ private RequestStack $ requestStack ;
51+
4752 protected function setUp (): void
4853 {
4954 $ this ->systemConfigService = SystemConfigServiceMock::createWithCredentials ();
55+ $ this ->webhookSystemConfigHelper = $ this ->createMock (WebhookSystemConfigHelper::class);
56+ $ this ->requestStack = new RequestStack ();
5057 }
5158
5259 public function testRemoveWebhookWithInheritedConfiguration (): void
@@ -73,6 +80,157 @@ public function testRemoveWebhookWithNoConfiguration(): void
7380 static ::assertEmpty ($ this ->systemConfigService ->getString (Settings::WEBHOOK_ID , TestDefaults::SALES_CHANNEL ));
7481 }
7582
83+ public function testCheckWebhookBefore (): void
84+ {
85+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], null );
86+
87+ $ this ->webhookSystemConfigHelper
88+ ->expects (static ::once ())
89+ ->method ('checkWebhookBefore ' )
90+ ->with (['' => ['some-key ' => 'some-value ' ]]);
91+
92+ $ this ->webhookSystemConfigHelper
93+ ->expects (static ::once ())
94+ ->method ('needsCheck ' )
95+ ->with (['some-key ' => 'some-value ' ])
96+ ->willReturn (true );
97+
98+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
99+ ->checkWebhookBefore ($ event );
100+ }
101+
102+ public function testCheckWebhookBeforeWithSalesChannelId (): void
103+ {
104+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
105+
106+ $ this ->webhookSystemConfigHelper
107+ ->expects (static ::once ())
108+ ->method ('checkWebhookBefore ' )
109+ ->with (['some-sales-channel-id ' => ['some-key ' => 'some-value ' ]]);
110+
111+ $ this ->webhookSystemConfigHelper
112+ ->expects (static ::once ())
113+ ->method ('needsCheck ' )
114+ ->with (['some-key ' => 'some-value ' ])
115+ ->willReturn (true );
116+
117+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
118+ ->checkWebhookBefore ($ event );
119+ }
120+
121+ public function testCheckWebhookBeforeWithSaveRoute (): void
122+ {
123+ $ request = new Request (attributes: ['_route ' => 'api.action.paypal.settings.save ' ]);
124+ $ this ->requestStack ->push ($ request );
125+
126+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
127+
128+ $ this ->webhookSystemConfigHelper
129+ ->expects (static ::never ())
130+ ->method ('checkWebhookBefore ' );
131+
132+ $ this ->webhookSystemConfigHelper
133+ ->expects (static ::never ())
134+ ->method ('needsCheck ' );
135+
136+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
137+ ->checkWebhookBefore ($ event );
138+ }
139+
140+ public function testCheckWebhookBeforeWithNoCheckNeeded (): void
141+ {
142+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
143+
144+ $ this ->webhookSystemConfigHelper
145+ ->expects (static ::never ())
146+ ->method ('checkWebhookBefore ' );
147+
148+ $ this ->webhookSystemConfigHelper
149+ ->expects (static ::once ())
150+ ->method ('needsCheck ' )
151+ ->with (['some-key ' => 'some-value ' ])
152+ ->willReturn (false );
153+
154+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
155+ ->checkWebhookBefore ($ event );
156+ }
157+
158+ public function testCheckWebhookAfter (): void
159+ {
160+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], null );
161+
162+ $ this ->webhookSystemConfigHelper
163+ ->expects (static ::once ())
164+ ->method ('checkWebhookAfter ' )
165+ ->with (['' ]);
166+
167+ $ this ->webhookSystemConfigHelper
168+ ->expects (static ::once ())
169+ ->method ('needsCheck ' )
170+ ->with (['some-key ' => 'some-value ' ])
171+ ->willReturn (true );
172+
173+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
174+ ->checkWebhookAfter ($ event );
175+ }
176+
177+ public function testCheckWebhookAfterWithSalesChannelId (): void
178+ {
179+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
180+
181+ $ this ->webhookSystemConfigHelper
182+ ->expects (static ::once ())
183+ ->method ('checkWebhookAfter ' )
184+ ->with (['some-sales-channel-id ' ]);
185+
186+ $ this ->webhookSystemConfigHelper
187+ ->expects (static ::once ())
188+ ->method ('needsCheck ' )
189+ ->with (['some-key ' => 'some-value ' ])
190+ ->willReturn (true );
191+
192+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
193+ ->checkWebhookAfter ($ event );
194+ }
195+
196+ public function testCheckWebhookAfterWithSaveRoute (): void
197+ {
198+ $ request = new Request (attributes: ['_route ' => 'api.action.paypal.settings.save ' ]);
199+ $ this ->requestStack ->push ($ request );
200+
201+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
202+
203+ $ this ->webhookSystemConfigHelper
204+ ->expects (static ::once ())
205+ ->method ('checkWebhookAfter ' )
206+ ->with (['some-sales-channel-id ' ]);
207+
208+ $ this ->webhookSystemConfigHelper
209+ ->expects (static ::never ())
210+ ->method ('needsCheck ' );
211+
212+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
213+ ->checkWebhookAfter ($ event );
214+ }
215+
216+ public function testCheckWebhookAfterWithNoCheckNeeded (): void
217+ {
218+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
219+
220+ $ this ->webhookSystemConfigHelper
221+ ->expects (static ::never ())
222+ ->method ('checkWebhookAfter ' );
223+
224+ $ this ->webhookSystemConfigHelper
225+ ->expects (static ::once ())
226+ ->method ('needsCheck ' )
227+ ->with (['some-key ' => 'some-value ' ])
228+ ->willReturn (false );
229+
230+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
231+ ->checkWebhookBefore ($ event );
232+ }
233+
76234 public function testSubscribedEvents (): void
77235 {
78236 static ::assertEqualsCanonicalizing ([
@@ -105,8 +263,8 @@ private function createWebhookSubscriber(array $configuration): WebhookSubscribe
105263 new NullLogger (),
106264 $ this ->systemConfigService ,
107265 $ webhookService ,
108- $ this ->createMock (WebhookSystemConfigHelper::class) ,
109- new RequestStack () ,
266+ $ this ->webhookSystemConfigHelper ,
267+ $ this -> requestStack ,
110268 );
111269 }
112270
0 commit comments