2
2
3
3
namespace Tests \Module \Checkout ;
4
4
5
- use Packetery \Core \Entity \Carrier ;
5
+ use Packetery \Core \Entity ;
6
+ use Packetery \Core \Entity \Order ;
7
+ use Packetery \Module \Carrier ;
6
8
use Packetery \Module \Carrier \CarrierOptionsFactory ;
7
9
use Packetery \Module \Carrier \EntityRepository ;
8
- use Packetery \Module \Carrier \Options ;
9
10
use Packetery \Module \Checkout \CartService ;
10
11
use Packetery \Module \Checkout \Checkout ;
11
12
use Packetery \Module \Checkout \CheckoutRenderer ;
22
23
use Packetery \Module \Payment \PaymentHelper ;
23
24
use PHPUnit \Framework \MockObject \MockObject ;
24
25
use PHPUnit \Framework \TestCase ;
26
+ use Tests \Core \DummyFactory ;
25
27
use WC_Cart ;
26
28
27
29
class CheckoutTest extends TestCase {
@@ -43,7 +45,7 @@ class CheckoutTest extends TestCase {
43
45
private Checkout $ checkout ;
44
46
private WC_Cart |MockObject $ WCCart ;
45
47
46
- protected function creatCheckout (): void {
48
+ protected function createCheckout (): void {
47
49
$ this ->wpAdapter = $ this ->createMock ( WpAdapter::class );
48
50
$ this ->wcAdapter = $ this ->createMock ( WcAdapter::class );
49
51
$ this ->carrierOptionsFactory = $ this ->createMock ( CarrierOptionsFactory::class );
@@ -81,7 +83,7 @@ protected function creatCheckout(): void {
81
83
}
82
84
83
85
public function testDoesNothingIfNoChosenShippingMethod (): void {
84
- $ this ->creatCheckout ();
86
+ $ this ->createCheckout ();
85
87
$ this ->checkoutService ->method ( 'calculateShippingAndGetOptionId ' )->willReturn ( null );
86
88
87
89
$ this ->rateCalculator ->expects ( $ this ->never () )->method ( 'getCODSurcharge ' );
@@ -90,7 +92,7 @@ public function testDoesNothingIfNoChosenShippingMethod(): void {
90
92
}
91
93
92
94
public function testDoesNothingIfShippingMethodNotPacketery (): void {
93
- $ this ->creatCheckout ();
95
+ $ this ->createCheckout ();
94
96
$ this ->checkoutService ->method ( 'calculateShippingAndGetOptionId ' )->willReturn ( 'non_packetery_method ' );
95
97
$ this ->checkoutService ->method ( 'isPacketeryShippingMethod ' )->willReturn ( false );
96
98
@@ -100,11 +102,11 @@ public function testDoesNothingIfShippingMethodNotPacketery(): void {
100
102
}
101
103
102
104
public function testSkipsWhenCouponAllowsFreeShipping (): void {
103
- $ this ->creatCheckout ();
105
+ $ this ->createCheckout ();
104
106
$ this ->checkoutService ->method ( 'calculateShippingAndGetOptionId ' )->willReturn ( 'packetery_method ' );
105
107
$ this ->checkoutService ->method ( 'isPacketeryShippingMethod ' )->willReturn ( true );
106
108
107
- $ carrierOptions = $ this ->createMock ( Options::class );
109
+ $ carrierOptions = $ this ->createMock ( Carrier \ Options::class );
108
110
$ carrierOptions ->method ( 'hasCouponFreeShippingForFeesAllowed ' )->willReturn ( true );
109
111
110
112
$ this ->carrierOptionsFactory ->method ( 'createByOptionId ' )->willReturn ( $ carrierOptions );
@@ -115,11 +117,11 @@ public function testSkipsWhenCouponAllowsFreeShipping(): void {
115
117
}
116
118
117
119
public function testAddsFeesSuccessfully (): void {
118
- $ this ->creatCheckout ();
120
+ $ this ->createCheckout ();
119
121
$ this ->checkoutService ->method ( 'calculateShippingAndGetOptionId ' )->willReturn ( 'packetery_method ' );
120
122
$ this ->checkoutService ->method ( 'isPacketeryShippingMethod ' )->willReturn ( true );
121
123
122
- $ carrierOptions = $ this ->createMock ( Options::class );
124
+ $ carrierOptions = $ this ->createMock ( Carrier \ Options::class );
123
125
$ carrierOptions ->method ( 'hasCouponFreeShippingForFeesAllowed ' )->willReturn ( false );
124
126
$ carrierOptions ->method ( 'toArray ' )->willReturn ( [ 'key ' => 'value ' ] );
125
127
@@ -132,7 +134,7 @@ public function testAddsFeesSuccessfully(): void {
132
134
->with ( [ 'key ' => 'value ' ], $ this ->anything () )
133
135
->willReturn ( 5.00 );
134
136
135
- $ carrier = $ this ->createMock ( Carrier::class );
137
+ $ carrier = $ this ->createMock ( Entity \ Carrier::class );
136
138
$ carrier ->method ( 'supportsAgeVerification ' )->willReturn ( true );
137
139
$ this ->carrierEntityRepository ->method ( 'getAnyById ' )->willReturn ( $ carrier );
138
140
$ carrierOptions ->method ( 'getAgeVerificationFee ' )->willReturn ( 10.0 );
@@ -147,4 +149,153 @@ public function testAddsFeesSuccessfully(): void {
147
149
148
150
$ this ->checkout ->actionCalculateFees ( $ this ->WCCart );
149
151
}
152
+
153
+ public static function filterPaymentGatewaysProvider (): array {
154
+ $ gateway1 = (object ) [ 'id ' => 'gateway1 ' ];
155
+ $ gateway2 = (object ) [ 'id ' => 'gateway2 ' ];
156
+ $ codGateway = (object ) [ 'id ' => 'cod_gateway ' ];
157
+
158
+ $ carrierSupportingCod = DummyFactory::createCarrierCzechPp ();
159
+ $ carrierWithoutCod = DummyFactory::createCarDeliveryCarrier ();
160
+
161
+ $ order = DummyFactory::createOrderCzPp ();
162
+
163
+ return [
164
+ 'no packetery method selected ' => [
165
+ 'availableGateways ' => [ $ gateway1 , $ gateway2 , $ codGateway ],
166
+ 'orderPayParameter ' => null ,
167
+ 'order ' => null ,
168
+ 'chosenMethod ' => null ,
169
+ 'isPacketeryMethod ' => false ,
170
+ 'carrier ' => null ,
171
+ 'disallowedPaymentMethods ' => [],
172
+ 'codPaymentMethods ' => [],
173
+ 'expectedResult ' => [ $ gateway1 , $ gateway2 , $ codGateway ],
174
+ ],
175
+ 'packetery method with valid carrier ' => [
176
+ 'availableGateways ' => [ $ gateway1 , $ gateway2 , $ codGateway ],
177
+ 'orderPayParameter ' => 123 ,
178
+ 'order ' => $ order ,
179
+ 'chosenMethod ' => 'dummyRate ' ,
180
+ 'isPacketeryMethod ' => true ,
181
+ 'carrier ' => $ carrierSupportingCod ,
182
+ 'disallowedPaymentMethods ' => [],
183
+ 'codPaymentMethods ' => [
184
+ 'cod_gateway ' ,
185
+ ],
186
+ 'expectedResult ' => [ $ gateway1 , $ gateway2 , $ codGateway ],
187
+ ],
188
+ 'packetery method with disallowed gateway ' => [
189
+ 'availableGateways ' => [ $ gateway1 , $ gateway2 , $ codGateway ],
190
+ 'orderPayParameter ' => null ,
191
+ 'order ' => null ,
192
+ 'chosenMethod ' => 'dummyRate ' ,
193
+ 'isPacketeryMethod ' => true ,
194
+ 'carrier ' => $ carrierSupportingCod ,
195
+ 'disallowedPaymentMethods ' => [
196
+ 'gateway1 ' ,
197
+ ],
198
+ 'codPaymentMethods ' => [
199
+ 'cod_gateway ' ,
200
+ ],
201
+ 'expectedResult ' => [ $ gateway2 , $ codGateway ],
202
+ ],
203
+ 'packetery method, carrier without COD support ' => [
204
+ 'availableGateways ' => [ $ gateway1 , $ gateway2 , $ codGateway ],
205
+ 'orderPayParameter ' => null ,
206
+ 'order ' => null ,
207
+ 'chosenMethod ' => 'dummyRate ' ,
208
+ 'isPacketeryMethod ' => true ,
209
+ 'carrier ' => $ carrierWithoutCod ,
210
+ 'disallowedPaymentMethods ' => [],
211
+ 'codPaymentMethods ' => [
212
+ 'cod_gateway ' ,
213
+ ],
214
+ 'expectedResult ' => [ $ gateway1 , $ gateway2 ],
215
+ ],
216
+ 'packetery method with invalid carrier ' => [
217
+ 'availableGateways ' => [ $ gateway1 , $ gateway2 , $ codGateway ],
218
+ 'orderPayParameter ' => null ,
219
+ 'order ' => null ,
220
+ 'chosenMethod ' => 'dummyRate ' ,
221
+ 'isPacketeryMethod ' => true ,
222
+ 'carrier ' => null ,
223
+ 'disallowedPaymentMethods ' => [],
224
+ 'codPaymentMethods ' => [
225
+ 'cod_gateway ' ,
226
+ ],
227
+ 'expectedResult ' => [ $ gateway1 , $ gateway2 , $ codGateway ],
228
+ ],
229
+ ];
230
+ }
231
+
232
+ /**
233
+ * @dataProvider filterPaymentGatewaysProvider
234
+ */
235
+ public function testFilterPaymentGateways (
236
+ array $ availableGateways ,
237
+ ?int $ orderPayParameter ,
238
+ ?Order $ order ,
239
+ ?string $ chosenMethod ,
240
+ bool $ isPacketeryMethod ,
241
+ ?Entity \Carrier $ carrier ,
242
+ array $ disallowedPaymentMethods ,
243
+ array $ codPaymentMethods ,
244
+ array $ expectedResult
245
+ ): void {
246
+ $ this ->createCheckout ();
247
+
248
+ $ this ->checkoutService
249
+ ->method ( 'getOrderPayParameter ' )
250
+ ->willReturn ( $ orderPayParameter );
251
+
252
+ $ this ->orderRepository
253
+ ->method ( 'getByIdWithValidCarrier ' )
254
+ ->willReturn ( $ order );
255
+
256
+ $ this ->checkoutService
257
+ ->method ( 'isPacketeryShippingMethod ' )
258
+ ->willReturn ( $ isPacketeryMethod );
259
+
260
+ if ( $ carrier !== null ) {
261
+ $ this ->checkoutService
262
+ ->method ( 'getCarrierIdFromPacketeryShippingMethod ' )
263
+ ->willReturn ( $ carrier ->getId () );
264
+ }
265
+
266
+ $ this ->carrierEntityRepository
267
+ ->method ( 'getAnyById ' )
268
+ ->willReturn ( $ carrier );
269
+
270
+ if ( $ chosenMethod !== null ) {
271
+ $ this ->sessionService
272
+ ->method ( 'getChosenMethodFromSession ' )
273
+ ->willReturn ( $ chosenMethod );
274
+ }
275
+
276
+ $ carrierOptions = $ this ->createMock ( Carrier \Options::class );
277
+ $ this ->carrierOptionsFactory
278
+ ->method ( 'createByCarrierId ' )
279
+ ->willReturn ( $ carrierOptions );
280
+
281
+ $ carrierOptions
282
+ ->method ( 'hasCheckoutPaymentMethodDisallowed ' )
283
+ ->willReturnCallback (
284
+ function ( $ gatewayId ) use ( $ disallowedPaymentMethods ) {
285
+ return in_array ( $ gatewayId , $ disallowedPaymentMethods , true );
286
+ }
287
+ );
288
+
289
+ $ this ->paymentHelper
290
+ ->method ( 'isCodPaymentMethod ' )
291
+ ->willReturnCallback (
292
+ function ( $ gatewayId ) use ( $ codPaymentMethods ) {
293
+ return in_array ( $ gatewayId , $ codPaymentMethods , true );
294
+ }
295
+ );
296
+
297
+ $ result = $ this ->checkout ->filterPaymentGateways ( $ availableGateways );
298
+
299
+ $ this ->assertEqualsCanonicalizing ( $ expectedResult , $ result );
300
+ }
150
301
}
0 commit comments