7
7
8
8
namespace Swag \PayPal \Test \Webhook \Registration ;
9
9
10
+ use PHPUnit \Framework \MockObject \MockObject ;
10
11
use PHPUnit \Framework \TestCase ;
11
12
use Psr \Log \NullLogger ;
12
13
use Shopware \Core \Framework \Context ;
13
14
use Shopware \Core \Framework \DataAbstractionLayer \EntityWriteResult ;
14
15
use Shopware \Core \Framework \DataAbstractionLayer \Event \EntityDeletedEvent ;
15
16
use Shopware \Core \Framework \DataAbstractionLayer \Write \EntityExistence ;
17
+ use Shopware \Core \Framework \Feature ;
16
18
use Shopware \Core \Framework \Log \Package ;
17
19
use Shopware \Core \System \SalesChannel \SalesChannelDefinition ;
18
20
use Shopware \Core \System \SalesChannel \SalesChannelEvents ;
29
31
use Swag \PayPal \Webhook \Registration \WebhookSystemConfigHelper ;
30
32
use Swag \PayPal \Webhook \WebhookRegistry ;
31
33
use Swag \PayPal \Webhook \WebhookService ;
34
+ use Symfony \Component \HttpFoundation \Request ;
32
35
use Symfony \Component \HttpFoundation \RequestStack ;
33
36
use Symfony \Component \Routing \RouterInterface ;
34
37
@@ -44,9 +47,15 @@ class WebhookSubscriberTest extends TestCase
44
47
45
48
private SystemConfigService $ systemConfigService ;
46
49
50
+ private WebhookSystemConfigHelper &MockObject $ webhookSystemConfigHelper ;
51
+
52
+ private RequestStack $ requestStack ;
53
+
47
54
protected function setUp (): void
48
55
{
49
56
$ this ->systemConfigService = SystemConfigServiceMock::createWithCredentials ();
57
+ $ this ->webhookSystemConfigHelper = $ this ->createMock (WebhookSystemConfigHelper::class);
58
+ $ this ->requestStack = new RequestStack ();
50
59
}
51
60
52
61
public function testRemoveWebhookWithInheritedConfiguration (): void
@@ -73,6 +82,172 @@ public function testRemoveWebhookWithNoConfiguration(): void
73
82
static ::assertEmpty ($ this ->systemConfigService ->getString (Settings::WEBHOOK_ID , TestDefaults::SALES_CHANNEL ));
74
83
}
75
84
85
+ public function testCheckWebhookBefore (): void
86
+ {
87
+ Feature::skipTestIfInActive ('PAYPAL_SETTINGS_TWEAKS ' , $ this );
88
+
89
+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], null );
90
+
91
+ $ this ->webhookSystemConfigHelper
92
+ ->expects (static ::once ())
93
+ ->method ('checkWebhookBefore ' )
94
+ ->with (['' => ['some-key ' => 'some-value ' ]]);
95
+
96
+ $ this ->webhookSystemConfigHelper
97
+ ->expects (static ::once ())
98
+ ->method ('needsCheck ' )
99
+ ->with (['some-key ' => 'some-value ' ])
100
+ ->willReturn (true );
101
+
102
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
103
+ ->checkWebhookBefore ($ event );
104
+ }
105
+
106
+ public function testCheckWebhookBeforeWithSalesChannelId (): void
107
+ {
108
+ Feature::skipTestIfInActive ('PAYPAL_SETTINGS_TWEAKS ' , $ this );
109
+
110
+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
111
+
112
+ $ this ->webhookSystemConfigHelper
113
+ ->expects (static ::once ())
114
+ ->method ('checkWebhookBefore ' )
115
+ ->with (['some-sales-channel-id ' => ['some-key ' => 'some-value ' ]]);
116
+
117
+ $ this ->webhookSystemConfigHelper
118
+ ->expects (static ::once ())
119
+ ->method ('needsCheck ' )
120
+ ->with (['some-key ' => 'some-value ' ])
121
+ ->willReturn (true );
122
+
123
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
124
+ ->checkWebhookBefore ($ event );
125
+ }
126
+
127
+ public function testCheckWebhookBeforeWithSaveRoute (): void
128
+ {
129
+ Feature::skipTestIfInActive ('PAYPAL_SETTINGS_TWEAKS ' , $ this );
130
+
131
+ $ request = new Request (attributes: ['_route ' => 'api.action.paypal.settings.save ' ]);
132
+ $ this ->requestStack ->push ($ request );
133
+
134
+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
135
+
136
+ $ this ->webhookSystemConfigHelper
137
+ ->expects (static ::never ())
138
+ ->method ('checkWebhookBefore ' );
139
+
140
+ $ this ->webhookSystemConfigHelper
141
+ ->expects (static ::never ())
142
+ ->method ('needsCheck ' );
143
+
144
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
145
+ ->checkWebhookBefore ($ event );
146
+ }
147
+
148
+ public function testCheckWebhookBeforeWithNoCheckNeeded (): void
149
+ {
150
+ Feature::skipTestIfInActive ('PAYPAL_SETTINGS_TWEAKS ' , $ this );
151
+
152
+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
153
+
154
+ $ this ->webhookSystemConfigHelper
155
+ ->expects (static ::never ())
156
+ ->method ('checkWebhookBefore ' );
157
+
158
+ $ this ->webhookSystemConfigHelper
159
+ ->expects (static ::once ())
160
+ ->method ('needsCheck ' )
161
+ ->with (['some-key ' => 'some-value ' ])
162
+ ->willReturn (false );
163
+
164
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
165
+ ->checkWebhookBefore ($ event );
166
+ }
167
+
168
+ public function testCheckWebhookAfter (): void
169
+ {
170
+ Feature::skipTestIfInActive ('PAYPAL_SETTINGS_TWEAKS ' , $ this );
171
+
172
+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], null );
173
+
174
+ $ this ->webhookSystemConfigHelper
175
+ ->expects (static ::once ())
176
+ ->method ('checkWebhookAfter ' )
177
+ ->with (['' ]);
178
+
179
+ $ this ->webhookSystemConfigHelper
180
+ ->expects (static ::once ())
181
+ ->method ('needsCheck ' )
182
+ ->with (['some-key ' => 'some-value ' ])
183
+ ->willReturn (true );
184
+
185
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
186
+ ->checkWebhookAfter ($ event );
187
+ }
188
+
189
+ public function testCheckWebhookAfterWithSalesChannelId (): void
190
+ {
191
+ Feature::skipTestIfInActive ('PAYPAL_SETTINGS_TWEAKS ' , $ this );
192
+
193
+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
194
+
195
+ $ this ->webhookSystemConfigHelper
196
+ ->expects (static ::once ())
197
+ ->method ('checkWebhookAfter ' )
198
+ ->with (['some-sales-channel-id ' ]);
199
+
200
+ $ this ->webhookSystemConfigHelper
201
+ ->expects (static ::once ())
202
+ ->method ('needsCheck ' )
203
+ ->with (['some-key ' => 'some-value ' ])
204
+ ->willReturn (true );
205
+
206
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
207
+ ->checkWebhookAfter ($ event );
208
+ }
209
+
210
+ public function testCheckWebhookAfterWithSaveRoute (): void
211
+ {
212
+ Feature::skipTestIfInActive ('PAYPAL_SETTINGS_TWEAKS ' , $ this );
213
+
214
+ $ request = new Request (attributes: ['_route ' => 'api.action.paypal.settings.save ' ]);
215
+ $ this ->requestStack ->push ($ request );
216
+
217
+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
218
+
219
+ $ this ->webhookSystemConfigHelper
220
+ ->expects (static ::never ())
221
+ ->method ('checkWebhookAfter ' );
222
+
223
+ $ this ->webhookSystemConfigHelper
224
+ ->expects (static ::never ())
225
+ ->method ('needsCheck ' );
226
+
227
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
228
+ ->checkWebhookAfter ($ event );
229
+ }
230
+
231
+ public function testCheckWebhookAfterWithNoCheckNeeded (): void
232
+ {
233
+ Feature::skipTestIfInActive ('PAYPAL_SETTINGS_TWEAKS ' , $ this );
234
+
235
+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
236
+
237
+ $ this ->webhookSystemConfigHelper
238
+ ->expects (static ::never ())
239
+ ->method ('checkWebhookAfter ' );
240
+
241
+ $ this ->webhookSystemConfigHelper
242
+ ->expects (static ::once ())
243
+ ->method ('needsCheck ' )
244
+ ->with (['some-key ' => 'some-value ' ])
245
+ ->willReturn (false );
246
+
247
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
248
+ ->checkWebhookAfter ($ event );
249
+ }
250
+
76
251
public function testSubscribedEvents (): void
77
252
{
78
253
static ::assertEqualsCanonicalizing ([
@@ -105,8 +280,8 @@ private function createWebhookSubscriber(array $configuration): WebhookSubscribe
105
280
new NullLogger (),
106
281
$ this ->systemConfigService ,
107
282
$ webhookService ,
108
- $ this ->createMock (WebhookSystemConfigHelper::class) ,
109
- new RequestStack () ,
283
+ $ this ->webhookSystemConfigHelper ,
284
+ $ this -> requestStack ,
110
285
);
111
286
}
112
287
0 commit comments