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 ;
29
30
use Swag \PayPal \Webhook \Registration \WebhookSystemConfigHelper ;
30
31
use Swag \PayPal \Webhook \WebhookRegistry ;
31
32
use Swag \PayPal \Webhook \WebhookService ;
33
+ use Symfony \Component \HttpFoundation \Request ;
32
34
use Symfony \Component \HttpFoundation \RequestStack ;
33
35
use Symfony \Component \Routing \RouterInterface ;
34
36
@@ -44,9 +46,15 @@ class WebhookSubscriberTest extends TestCase
44
46
45
47
private SystemConfigService $ systemConfigService ;
46
48
49
+ private WebhookSystemConfigHelper &MockObject $ webhookSystemConfigHelper ;
50
+
51
+ private RequestStack $ requestStack ;
52
+
47
53
protected function setUp (): void
48
54
{
49
55
$ this ->systemConfigService = SystemConfigServiceMock::createWithCredentials ();
56
+ $ this ->webhookSystemConfigHelper = $ this ->createMock (WebhookSystemConfigHelper::class);
57
+ $ this ->requestStack = new RequestStack ();
50
58
}
51
59
52
60
public function testRemoveWebhookWithInheritedConfiguration (): void
@@ -73,6 +81,156 @@ public function testRemoveWebhookWithNoConfiguration(): void
73
81
static ::assertEmpty ($ this ->systemConfigService ->getString (Settings::WEBHOOK_ID , TestDefaults::SALES_CHANNEL ));
74
82
}
75
83
84
+ public function testCheckWebhookBefore (): void
85
+ {
86
+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], null );
87
+
88
+ $ this ->webhookSystemConfigHelper
89
+ ->expects (static ::once ())
90
+ ->method ('checkWebhookBefore ' )
91
+ ->with (['' => ['some-key ' => 'some-value ' ]]);
92
+
93
+ $ this ->webhookSystemConfigHelper
94
+ ->expects (static ::once ())
95
+ ->method ('needsCheck ' )
96
+ ->with (['some-key ' => 'some-value ' ])
97
+ ->willReturn (true );
98
+
99
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
100
+ ->checkWebhookBefore ($ event );
101
+ }
102
+
103
+ public function testCheckWebhookBeforeWithSalesChannelId (): void
104
+ {
105
+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
106
+
107
+ $ this ->webhookSystemConfigHelper
108
+ ->expects (static ::once ())
109
+ ->method ('checkWebhookBefore ' )
110
+ ->with (['some-sales-channel-id ' => ['some-key ' => 'some-value ' ]]);
111
+
112
+ $ this ->webhookSystemConfigHelper
113
+ ->expects (static ::once ())
114
+ ->method ('needsCheck ' )
115
+ ->with (['some-key ' => 'some-value ' ])
116
+ ->willReturn (true );
117
+
118
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
119
+ ->checkWebhookBefore ($ event );
120
+ }
121
+
122
+ public function testCheckWebhookBeforeWithSaveRoute (): void
123
+ {
124
+ $ request = new Request (attributes: ['_route ' => 'api.action.paypal.settings.save ' ]);
125
+ $ this ->requestStack ->push ($ request );
126
+
127
+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
128
+
129
+ $ this ->webhookSystemConfigHelper
130
+ ->expects (static ::never ())
131
+ ->method ('checkWebhookBefore ' );
132
+
133
+ $ this ->webhookSystemConfigHelper
134
+ ->expects (static ::never ())
135
+ ->method ('needsCheck ' );
136
+
137
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
138
+ ->checkWebhookBefore ($ event );
139
+ }
140
+
141
+ public function testCheckWebhookBeforeWithNoCheckNeeded (): void
142
+ {
143
+ $ event = new BeforeSystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
144
+
145
+ $ this ->webhookSystemConfigHelper
146
+ ->expects (static ::never ())
147
+ ->method ('checkWebhookBefore ' );
148
+
149
+ $ this ->webhookSystemConfigHelper
150
+ ->expects (static ::once ())
151
+ ->method ('needsCheck ' )
152
+ ->with (['some-key ' => 'some-value ' ])
153
+ ->willReturn (false );
154
+
155
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
156
+ ->checkWebhookBefore ($ event );
157
+ }
158
+
159
+ public function testCheckWebhookAfter (): void
160
+ {
161
+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], null );
162
+
163
+ $ this ->webhookSystemConfigHelper
164
+ ->expects (static ::once ())
165
+ ->method ('checkWebhookAfter ' )
166
+ ->with (['' ]);
167
+
168
+ $ this ->webhookSystemConfigHelper
169
+ ->expects (static ::once ())
170
+ ->method ('needsCheck ' )
171
+ ->with (['some-key ' => 'some-value ' ])
172
+ ->willReturn (true );
173
+
174
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
175
+ ->checkWebhookAfter ($ event );
176
+ }
177
+
178
+ public function testCheckWebhookAfterWithSalesChannelId (): void
179
+ {
180
+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
181
+
182
+ $ this ->webhookSystemConfigHelper
183
+ ->expects (static ::once ())
184
+ ->method ('checkWebhookAfter ' )
185
+ ->with (['some-sales-channel-id ' ]);
186
+
187
+ $ this ->webhookSystemConfigHelper
188
+ ->expects (static ::once ())
189
+ ->method ('needsCheck ' )
190
+ ->with (['some-key ' => 'some-value ' ])
191
+ ->willReturn (true );
192
+
193
+ $ this ->createWebhookSubscriber (['' => null , TestDefaults::SALES_CHANNEL => null ])
194
+ ->checkWebhookAfter ($ event );
195
+ }
196
+
197
+ public function testCheckWebhookAfterWithSaveRoute (): void
198
+ {
199
+ $ request = new Request (attributes: ['_route ' => 'api.action.paypal.settings.save ' ]);
200
+ $ this ->requestStack ->push ($ request );
201
+
202
+ $ event = new SystemConfigMultipleChangedEvent (['some-key ' => 'some-value ' ], 'some-sales-channel-id ' );
203
+
204
+ $ this ->webhookSystemConfigHelper
205
+ ->expects (static ::never ())
206
+ ->method ('checkWebhookAfter ' );
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
+ ->checkWebhookAfter ($ event );
232
+ }
233
+
76
234
public function testSubscribedEvents (): void
77
235
{
78
236
static ::assertEqualsCanonicalizing ([
@@ -105,8 +263,8 @@ private function createWebhookSubscriber(array $configuration): WebhookSubscribe
105
263
new NullLogger (),
106
264
$ this ->systemConfigService ,
107
265
$ webhookService ,
108
- $ this ->createMock (WebhookSystemConfigHelper::class) ,
109
- new RequestStack () ,
266
+ $ this ->webhookSystemConfigHelper ,
267
+ $ this -> requestStack ,
110
268
);
111
269
}
112
270
0 commit comments