PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / tests / Module / Checkout / CheckoutTest.php

CheckoutTest.php in Packeta trunk, at tests/Module/Checkout/CheckoutTest.php

810 lines 35.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Tests\Module\Checkout;
4
5 use Packetery\Core\Entity;
6 use Packetery\Core\Entity\Order;
7 use Packetery\Module\Carrier;
8 use Packetery\Module\Carrier\CarrierOptionsFactory;
9 use Packetery\Module\Carrier\EntityRepository;
10 use Packetery\Module\Checkout\CartService;
11 use Packetery\Module\Checkout\Checkout;
12 use Packetery\Module\Checkout\CheckoutRenderer;
13 use Packetery\Module\Checkout\CheckoutService;
14 use Packetery\Module\Checkout\CheckoutValidator;
15 use Packetery\Module\Checkout\CurrencySwitcherService;
16 use Packetery\Module\Checkout\OrderUpdater;
17 use Packetery\Module\Checkout\RateCalculator;
18 use Packetery\Module\Checkout\SessionService;
19 use Packetery\Module\DiagnosticsLogger\DiagnosticsLogger;
20 use Packetery\Module\Framework\WcAdapter;
21 use Packetery\Module\Framework\WpAdapter;
22 use Packetery\Module\Options\OptionsProvider;
23 use Packetery\Module\Order\Repository;
24 use Packetery\Module\Payment\PaymentHelper;
25 use PHPUnit\Framework\MockObject\MockObject;
26 use PHPUnit\Framework\TestCase;
27 use WC_Cart;
28 use WC_Payment_Gateway;
29
30 class CheckoutTest extends TestCase {
31 private WpAdapter&MockObject $wpAdapterMock;
32 private SessionService&MockObject $sessionServiceMock;
33 private CartService&MockObject $cartServiceMock;
34 private PaymentHelper&MockObject $paymentHelperMock;
35 private EntityRepository&MockObject $carrierEntityRepositoryMock;
36 private RateCalculator&MockObject $rateCalculatorMock;
37 private CurrencySwitcherService&MockObject $currencySwitcherServiceMock;
38 private OptionsProvider&MockObject $optionsProviderMock;
39 private CarrierOptionsFactory&MockObject $carrierOptionsFactoryMock;
40 private Repository&MockObject $orderRepositoryMock;
41 private WcAdapter&MockObject $wcAdapterMock;
42 private CheckoutService&MockObject $checkoutServiceMock;
43
44 private function createCheckout(): Checkout {
45 $this->wpAdapterMock = $this->createMock( WpAdapter::class );
46 $this->wcAdapterMock = $this->createMock( WcAdapter::class );
47 $this->carrierOptionsFactoryMock = $this->createMock( CarrierOptionsFactory::class );
48 $this->optionsProviderMock = $this->createMock( OptionsProvider::class );
49 $this->orderRepositoryMock = $this->createMock( Repository::class );
50 $this->currencySwitcherServiceMock = $this->createMock( CurrencySwitcherService::class );
51 $this->rateCalculatorMock = $this->createMock( RateCalculator::class );
52 $this->carrierEntityRepositoryMock = $this->createMock( EntityRepository::class );
53 $this->paymentHelperMock = $this->createMock( PaymentHelper::class );
54 $this->checkoutServiceMock = $this->createMock( CheckoutService::class );
55 $checkoutRendererMock = $this->createMock( CheckoutRenderer::class );
56 $this->cartServiceMock = $this->createMock( CartService::class );
57 $this->sessionServiceMock = $this->createMock( SessionService::class );
58 $checkoutValidatorMock = $this->createMock( CheckoutValidator::class );
59 $orderUpdaterMock = $this->createMock( OrderUpdater::class );
60
61 $this->wpAdapterMock
62 ->method( '__' )
63 ->willReturnCallback(
64 static function ( $text ) {
65 return $text;
66 }
67 );
68
69 return new Checkout(
70 $this->wpAdapterMock,
71 $this->wcAdapterMock,
72 $this->carrierOptionsFactoryMock,
73 $this->optionsProviderMock,
74 $this->orderRepositoryMock,
75 $this->currencySwitcherServiceMock,
76 $this->rateCalculatorMock,
77 $this->carrierEntityRepositoryMock,
78 $this->paymentHelperMock,
79 $this->checkoutServiceMock,
80 $checkoutRendererMock,
81 $this->cartServiceMock,
82 $this->sessionServiceMock,
83 $checkoutValidatorMock,
84 $orderUpdaterMock,
85 $this->createMock( DiagnosticsLogger::class )
86 );
87 }
88
89 public function testActionCalculateFeesHappyPathWithAgeVerificationAndCodFees(): void {
90 $checkout = $this->createCheckout();
91 $cartMock = $this->createMock( WC_Cart::class );
92
93 $carrierMock = $this->createMock( Entity\Carrier::class );
94 $carrierMock->method( 'supportsAgeVerification' )->willReturn( true );
95
96 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
97 $carrierOptionsMock->method( 'getAgeVerificationFee' )->willReturn( 50.0 );
98 $carrierOptionsMock->method( 'hasCouponFreeShippingForFeesAllowed' )->willReturn( false );
99 $carrierOptionsMock->method( 'toArray' )->willReturn( [] );
100
101 $this->checkoutServiceMock->method( 'calculateShippingAndGetOptionId' )->willReturn( 'packetery_carrier_123' );
102 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( true );
103 $this->checkoutServiceMock->method( 'getCarrierIdFromPacketeryShippingMethod' )->willReturn( '123' );
104 $this->checkoutServiceMock->method( 'areBlocksUsedInCheckout' )->willReturn( false );
105
106 $this->carrierOptionsFactoryMock->method( 'createByOptionId' )->willReturn( $carrierOptionsMock );
107
108 $this->carrierEntityRepositoryMock->method( 'getAnyById' )->willReturn( $carrierMock );
109
110 $this->cartServiceMock->method( 'isAgeVerificationRequired' )->willReturn( true );
111 $this->cartServiceMock->method( 'getTaxClassWithMaxRate' )->willReturn( 'standard' );
112
113 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( true );
114
115 $this->sessionServiceMock->method( 'getChosenPaymentMethod' )->willReturn( 'cod' );
116
117 $this->rateCalculatorMock->method( 'isFreeShippingCouponApplied' )->willReturn( false );
118 $this->rateCalculatorMock->method( 'getCODSurcharge' )->willReturn( 30.0 );
119
120 $this->currencySwitcherServiceMock->method( 'getConvertedPrice' )->willReturn( 30.0 );
121
122 $this->optionsProviderMock->method( 'arePricesTaxInclusive' )->willReturn( true );
123
124 $this->wcAdapterMock->method( 'cart' )->willReturn( $cartMock );
125 $this->wcAdapterMock->method( 'cartGetSubtotal' )->willReturn( 1000.0 );
126 $this->wcAdapterMock->method( 'calcTax' )->willReturn( [ 5.206612 ] );
127
128 $callsAddFee = [];
129 $cartMock->expects( $this->exactly( 2 ) )
130 ->method( 'add_fee' )
131 ->willReturnCallback(
132 function ( ...$args ) use ( &$callsAddFee ) {
133 $callsAddFee[] = $args;
134 }
135 );
136
137 $checkout->actionCalculateFees( $cartMock );
138
139 $this->assertCount( 2, $callsAddFee );
140
141 $this->assertEquals( 24.793388, $callsAddFee[0][1] );
142 $this->assertTrue( $callsAddFee[0][2] );
143 $this->assertEquals( 'standard', $callsAddFee[0][3] );
144
145 $this->assertEquals( 24.793388, $callsAddFee[1][1] );
146 $this->assertTrue( $callsAddFee[1][2] );
147 $this->assertEquals( 'standard', $callsAddFee[1][3] );
148 }
149
150 public function testActionCalculateFeesWithShippingFreeCoupon(): void {
151 $checkout = $this->createCheckout();
152 $cartMock = $this->createMock( WC_Cart::class );
153
154 $carrierMock = $this->createMock( Entity\Carrier::class );
155 $carrierMock->method( 'supportsAgeVerification' )->willReturn( true );
156
157 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
158 $carrierOptionsMock->method( 'getAgeVerificationFee' )->willReturn( 50.0 );
159 $carrierOptionsMock->method( 'hasCouponFreeShippingForFeesAllowed' )->willReturn( true );
160 $carrierOptionsMock->method( 'toArray' )->willReturn( [] );
161
162 $this->checkoutServiceMock->method( 'calculateShippingAndGetOptionId' )->willReturn( 'packetery_carrier_123' );
163 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( true );
164 $this->checkoutServiceMock->method( 'getCarrierIdFromPacketeryShippingMethod' )->willReturn( '123' );
165 $this->checkoutServiceMock->method( 'areBlocksUsedInCheckout' )->willReturn( false );
166
167 $this->carrierOptionsFactoryMock->method( 'createByOptionId' )->willReturn( $carrierOptionsMock );
168
169 $this->carrierEntityRepositoryMock->method( 'getAnyById' )->willReturn( $carrierMock );
170
171 $this->cartServiceMock->method( 'getTaxClassWithMaxRate' )->willReturn( 'standard' );
172
173 $this->rateCalculatorMock->method( 'isFreeShippingCouponApplied' )->willReturn( true );
174
175 $this->wcAdapterMock->method( 'cart' )->willReturn( $cartMock );
176
177 $cartMock->expects( self::never() )->method( 'add_fee' );
178
179 $checkout->actionCalculateFees( $cartMock );
180 }
181
182 public function testActionCalculateFeesWhenChosenShippingMethodOptionIdIsNull(): void {
183 $checkout = $this->createCheckout();
184 $cartMock = $this->createMock( WC_Cart::class );
185
186 $this->checkoutServiceMock->method( 'calculateShippingAndGetOptionId' )->willReturn( null );
187
188 $cartMock->expects( self::never() )->method( 'add_fee' );
189
190 $checkout->actionCalculateFees( $cartMock );
191 }
192
193 public function testActionCalculateFeesWhenIsPacketeryShippingMethodReturnsFalse(): void {
194 $checkout = $this->createCheckout();
195 $cartMock = $this->createMock( WC_Cart::class );
196
197 $this->checkoutServiceMock->method( 'calculateShippingAndGetOptionId' )->willReturn( 'some_other_carrier_123' );
198 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( false );
199
200 $cartMock->expects( self::never() )->method( 'add_fee' );
201
202 $checkout->actionCalculateFees( $cartMock );
203 }
204
205 public function testAddAgeVerificationFeeHappyPath(): void {
206 $checkout = $this->createCheckout();
207 $cartMock = $this->createMock( WC_Cart::class );
208
209 $carrierMock = $this->createMock( Entity\Carrier::class );
210 $carrierMock->method( 'supportsAgeVerification' )->willReturn( true );
211
212 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
213 $carrierOptionsMock->method( 'getAgeVerificationFee' )->willReturn( 50.0 );
214
215 $this->cartServiceMock->method( 'isAgeVerificationRequired' )->willReturn( true );
216 $this->currencySwitcherServiceMock->method( 'getConvertedPrice' )->willReturn( 50.0 );
217 $this->optionsProviderMock->method( 'arePricesTaxInclusive' )->willReturn( false );
218
219 $cartMock->expects( $this->once() )
220 ->method( 'add_fee' )
221 ->with( 'Age verification fee', 50.0, true, 'standard' );
222
223 $reflection = new \ReflectionClass( $checkout );
224 $method = $reflection->getMethod( 'addAgeVerificationFee' );
225 $method->setAccessible( true );
226 $method->invoke( $checkout, $cartMock, $carrierMock, $carrierOptionsMock, true, 'standard' );
227 }
228
229 public function testAddAgeVerificationFeeWhenCarrierIsNull(): void {
230 $checkout = $this->createCheckout();
231 $cartMock = $this->createMock( WC_Cart::class );
232
233 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
234
235 $cartMock->expects( self::never() )->method( 'add_fee' );
236
237 $reflection = new \ReflectionClass( $checkout );
238 $method = $reflection->getMethod( 'addAgeVerificationFee' );
239 $method->setAccessible( true );
240 $method->invoke( $checkout, $cartMock, null, $carrierOptionsMock, true, 'standard' );
241 }
242
243 public function testAddAgeVerificationFeeWhenCarrierDoesNotSupportAgeVerification(): void {
244 $checkout = $this->createCheckout();
245 $cartMock = $this->createMock( WC_Cart::class );
246
247 $carrierMock = $this->createMock( Entity\Carrier::class );
248 $carrierMock->method( 'supportsAgeVerification' )->willReturn( false );
249
250 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
251
252 $cartMock->expects( self::never() )->method( 'add_fee' );
253
254 $reflection = new \ReflectionClass( $checkout );
255 $method = $reflection->getMethod( 'addAgeVerificationFee' );
256 $method->setAccessible( true );
257 $method->invoke( $checkout, $cartMock, $carrierMock, $carrierOptionsMock, true, 'standard' );
258 }
259
260 public function testAddAgeVerificationFeeWhenFeeIsNull(): void {
261 $checkout = $this->createCheckout();
262 $cartMock = $this->createMock( WC_Cart::class );
263
264 $carrierMock = $this->createMock( Entity\Carrier::class );
265 $carrierMock->method( 'supportsAgeVerification' )->willReturn( true );
266
267 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
268 $carrierOptionsMock->method( 'getAgeVerificationFee' )->willReturn( null );
269
270 $cartMock->expects( self::never() )->method( 'add_fee' );
271
272 $reflection = new \ReflectionClass( $checkout );
273 $method = $reflection->getMethod( 'addAgeVerificationFee' );
274 $method->setAccessible( true );
275 $method->invoke( $checkout, $cartMock, $carrierMock, $carrierOptionsMock, true, 'standard' );
276 }
277
278 public function testAddAgeVerificationFeeWhenAgeVerificationNotRequired(): void {
279 $checkout = $this->createCheckout();
280 $cartMock = $this->createMock( WC_Cart::class );
281
282 $carrierMock = $this->createMock( Entity\Carrier::class );
283 $carrierMock->method( 'supportsAgeVerification' )->willReturn( true );
284
285 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
286 $carrierOptionsMock->method( 'getAgeVerificationFee' )->willReturn( 50.0 );
287
288 $this->cartServiceMock->method( 'isAgeVerificationRequired' )->willReturn( false );
289
290 $cartMock->expects( self::never() )->method( 'add_fee' );
291
292 $reflection = new \ReflectionClass( $checkout );
293 $method = $reflection->getMethod( 'addAgeVerificationFee' );
294 $method->setAccessible( true );
295 $method->invoke( $checkout, $cartMock, $carrierMock, $carrierOptionsMock, true, 'standard' );
296 }
297
298 public function testAddAgeVerificationFeeWithTaxInclusivePricing(): void {
299 $checkout = $this->createCheckout();
300 $cartMock = $this->createMock( WC_Cart::class );
301
302 $carrierMock = $this->createMock( Entity\Carrier::class );
303 $carrierMock->method( 'supportsAgeVerification' )->willReturn( true );
304
305 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
306 $carrierOptionsMock->method( 'getAgeVerificationFee' )->willReturn( 50.0 );
307
308 $this->cartServiceMock->method( 'isAgeVerificationRequired' )->willReturn( true );
309 $this->currencySwitcherServiceMock->method( 'getConvertedPrice' )->willReturn( 50.0 );
310 $this->optionsProviderMock->method( 'arePricesTaxInclusive' )->willReturn( true );
311 $this->wcAdapterMock->method( 'calcTax' )->willReturn( [ 8.33 ] );
312
313 $cartMock->expects( $this->once() )
314 ->method( 'add_fee' )
315 ->with( 'Age verification fee', 41.67, true, 'standard' );
316
317 $reflection = new \ReflectionClass( $checkout );
318 $method = $reflection->getMethod( 'addAgeVerificationFee' );
319 $method->setAccessible( true );
320 $method->invoke( $checkout, $cartMock, $carrierMock, $carrierOptionsMock, true, 'standard' );
321 }
322
323 public function testAddAgeVerificationFeeWithNonTaxableScenario(): void {
324 $checkout = $this->createCheckout();
325 $cartMock = $this->createMock( WC_Cart::class );
326
327 $carrierMock = $this->createMock( Entity\Carrier::class );
328 $carrierMock->method( 'supportsAgeVerification' )->willReturn( true );
329
330 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
331 $carrierOptionsMock->method( 'getAgeVerificationFee' )->willReturn( 50.0 );
332
333 $this->cartServiceMock->method( 'isAgeVerificationRequired' )->willReturn( true );
334 $this->currencySwitcherServiceMock->method( 'getConvertedPrice' )->willReturn( 50.0 );
335 $cartMock->expects( $this->once() )
336 ->method( 'add_fee' )
337 ->with( 'Age verification fee', 50.0, false, null );
338
339 $reflection = new \ReflectionClass( $checkout );
340 $method = $reflection->getMethod( 'addAgeVerificationFee' );
341 $method->setAccessible( true );
342 $method->invoke( $checkout, $cartMock, $carrierMock, $carrierOptionsMock, false, null );
343 }
344
345 public function testAddCodSurchargeFeeHappyPathWithBlocksCheckout(): void {
346 $checkout = $this->createCheckout();
347 $cartMock = $this->createMock( WC_Cart::class );
348
349 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
350 $carrierOptionsMock->method( 'toArray' )->willReturn( [] );
351
352 $this->checkoutServiceMock->method( 'areBlocksUsedInCheckout' )->willReturn( true );
353 $this->wcAdapterMock->method( 'sessionGetString' )->with( 'packetery_checkout_payment_method' )->willReturn( 'cod' );
354 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( true );
355 $this->rateCalculatorMock->method( 'getCODSurcharge' )->willReturn( 30.0 );
356 $this->wcAdapterMock->method( 'cartGetSubtotal' )->willReturn( 1000.0 );
357 $this->currencySwitcherServiceMock->method( 'getConvertedPrice' )->willReturn( 30.0 );
358 $this->optionsProviderMock->method( 'arePricesTaxInclusive' )->willReturn( false );
359
360 $cartMock->expects( $this->once() )
361 ->method( 'add_fee' )
362 ->with( 'COD surcharge', 30.0, true, 'standard' );
363
364 $reflection = new \ReflectionClass( $checkout );
365 $method = $reflection->getMethod( 'addCodSurchargeFee' );
366 $method->setAccessible( true );
367 $method->invoke( $checkout, $cartMock, $carrierOptionsMock, true, 'standard' );
368 }
369
370 public function testAddCodSurchargeFeeHappyPathWithClassicCheckout(): void {
371 $checkout = $this->createCheckout();
372 $cartMock = $this->createMock( WC_Cart::class );
373
374 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
375 $carrierOptionsMock->method( 'toArray' )->willReturn( [] );
376
377 $this->checkoutServiceMock->method( 'areBlocksUsedInCheckout' )->willReturn( false );
378 $this->sessionServiceMock->method( 'getChosenPaymentMethod' )->willReturn( 'cod' );
379 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( true );
380 $this->rateCalculatorMock->method( 'getCODSurcharge' )->willReturn( 25.0 );
381 $this->wcAdapterMock->method( 'cartGetSubtotal' )->willReturn( 800.0 );
382 $this->currencySwitcherServiceMock->method( 'getConvertedPrice' )->willReturn( 25.0 );
383 $this->optionsProviderMock->method( 'arePricesTaxInclusive' )->willReturn( false );
384
385 $cartMock->expects( $this->once() )
386 ->method( 'add_fee' )
387 ->with( 'COD surcharge', 25.0, true, 'standard' );
388
389 $reflection = new \ReflectionClass( $checkout );
390 $method = $reflection->getMethod( 'addCodSurchargeFee' );
391 $method->setAccessible( true );
392 $method->invoke( $checkout, $cartMock, $carrierOptionsMock, true, 'standard' );
393 }
394
395 public function testAddCodSurchargeFeeWhenPaymentMethodIsNull(): void {
396 $checkout = $this->createCheckout();
397 $cartMock = $this->createMock( WC_Cart::class );
398
399 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
400
401 $this->checkoutServiceMock->method( 'areBlocksUsedInCheckout' )->willReturn( false );
402 $this->sessionServiceMock->method( 'getChosenPaymentMethod' )->willReturn( null );
403
404 $cartMock->expects( self::never() )->method( 'add_fee' );
405
406 $reflection = new \ReflectionClass( $checkout );
407 $method = $reflection->getMethod( 'addCodSurchargeFee' );
408 $method->setAccessible( true );
409 $method->invoke( $checkout, $cartMock, $carrierOptionsMock, true, 'standard' );
410 }
411
412 public function testAddCodSurchargeFeeWhenPaymentMethodIsNotCod(): void {
413 $checkout = $this->createCheckout();
414 $cartMock = $this->createMock( WC_Cart::class );
415
416 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
417
418 $this->checkoutServiceMock->method( 'areBlocksUsedInCheckout' )->willReturn( false );
419 $this->sessionServiceMock->method( 'getChosenPaymentMethod' )->willReturn( 'gopay' );
420 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( false );
421
422 $cartMock->expects( self::never() )->method( 'add_fee' );
423
424 $reflection = new \ReflectionClass( $checkout );
425 $method = $reflection->getMethod( 'addCodSurchargeFee' );
426 $method->setAccessible( true );
427 $method->invoke( $checkout, $cartMock, $carrierOptionsMock, true, 'standard' );
428 }
429
430 public function testAddCodSurchargeFeeWhenSurchargeIsZero(): void {
431 $checkout = $this->createCheckout();
432 $cartMock = $this->createMock( WC_Cart::class );
433
434 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
435 $carrierOptionsMock->method( 'toArray' )->willReturn( [] );
436
437 $this->checkoutServiceMock->method( 'areBlocksUsedInCheckout' )->willReturn( false );
438 $this->sessionServiceMock->method( 'getChosenPaymentMethod' )->willReturn( 'cod' );
439 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( true );
440 $this->rateCalculatorMock->method( 'getCODSurcharge' )->willReturn( 0.0 );
441 $this->wcAdapterMock->method( 'cartGetSubtotal' )->willReturn( 1000.0 );
442 $this->currencySwitcherServiceMock->method( 'getConvertedPrice' )->willReturn( 0.0 );
443
444 $cartMock->expects( self::never() )->method( 'add_fee' );
445
446 $reflection = new \ReflectionClass( $checkout );
447 $method = $reflection->getMethod( 'addCodSurchargeFee' );
448 $method->setAccessible( true );
449 $method->invoke( $checkout, $cartMock, $carrierOptionsMock, true, 'standard' );
450 }
451
452 public function testAddCodSurchargeFeeWhenSurchargeIsNegative(): void {
453 $checkout = $this->createCheckout();
454 $cartMock = $this->createMock( WC_Cart::class );
455
456 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
457 $carrierOptionsMock->method( 'toArray' )->willReturn( [] );
458
459 $this->checkoutServiceMock->method( 'areBlocksUsedInCheckout' )->willReturn( false );
460 $this->sessionServiceMock->method( 'getChosenPaymentMethod' )->willReturn( 'cod' );
461 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( true );
462 $this->rateCalculatorMock->method( 'getCODSurcharge' )->willReturn( -5.0 );
463 $this->wcAdapterMock->method( 'cartGetSubtotal' )->willReturn( 1000.0 );
464 $this->currencySwitcherServiceMock->method( 'getConvertedPrice' )->willReturn( -5.0 );
465
466 $cartMock->expects( self::never() )->method( 'add_fee' );
467
468 $reflection = new \ReflectionClass( $checkout );
469 $method = $reflection->getMethod( 'addCodSurchargeFee' );
470 $method->setAccessible( true );
471 $method->invoke( $checkout, $cartMock, $carrierOptionsMock, true, 'standard' );
472 }
473
474 public function testAddCodSurchargeFeeWithTaxInclusivePricing(): void {
475 $checkout = $this->createCheckout();
476 $cartMock = $this->createMock( WC_Cart::class );
477
478 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
479 $carrierOptionsMock->method( 'toArray' )->willReturn( [] );
480
481 $this->checkoutServiceMock->method( 'areBlocksUsedInCheckout' )->willReturn( false );
482 $this->sessionServiceMock->method( 'getChosenPaymentMethod' )->willReturn( 'cod' );
483 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( true );
484 $this->rateCalculatorMock->method( 'getCODSurcharge' )->willReturn( 30.0 );
485 $this->wcAdapterMock->method( 'cartGetSubtotal' )->willReturn( 1000.0 );
486 $this->currencySwitcherServiceMock->method( 'getConvertedPrice' )->willReturn( 30.0 );
487 $this->optionsProviderMock->method( 'arePricesTaxInclusive' )->willReturn( true );
488 $this->wcAdapterMock->method( 'calcTax' )->willReturn( [ 5.206612 ] );
489
490 $cartMock->expects( $this->once() )
491 ->method( 'add_fee' )
492 ->with( 'COD surcharge', 24.793388, true, 'standard' );
493
494 $reflection = new \ReflectionClass( $checkout );
495 $method = $reflection->getMethod( 'addCodSurchargeFee' );
496 $method->setAccessible( true );
497 $method->invoke( $checkout, $cartMock, $carrierOptionsMock, true, 'standard' );
498 }
499
500 public function testAddCodSurchargeFeeWithNonTaxableScenario(): void {
501 $checkout = $this->createCheckout();
502 $cartMock = $this->createMock( WC_Cart::class );
503
504 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
505 $carrierOptionsMock->method( 'toArray' )->willReturn( [] );
506
507 $this->checkoutServiceMock->method( 'areBlocksUsedInCheckout' )->willReturn( false );
508 $this->sessionServiceMock->method( 'getChosenPaymentMethod' )->willReturn( 'cod' );
509 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( true );
510 $this->rateCalculatorMock->method( 'getCODSurcharge' )->willReturn( 20.0 );
511 $this->wcAdapterMock->method( 'cartGetSubtotal' )->willReturn( 500.0 );
512 $this->currencySwitcherServiceMock->method( 'getConvertedPrice' )->willReturn( 20.0 );
513
514 $cartMock->expects( $this->once() )
515 ->method( 'add_fee' )
516 ->with( 'COD surcharge', 20.0, false, null );
517
518 $reflection = new \ReflectionClass( $checkout );
519 $method = $reflection->getMethod( 'addCodSurchargeFee' );
520 $method->setAccessible( true );
521 $method->invoke( $checkout, $cartMock, $carrierOptionsMock, false, null );
522 }
523
524 public function testFilterPaymentGatewaysWhenAvailableGatewaysIsNotArray(): void {
525 $checkout = $this->createCheckout();
526
527 $result = $checkout->filterPaymentGateways( 'not_an_array' );
528
529 $this->assertEquals( 'not_an_array', $result );
530 }
531
532 public function testFilterPaymentGatewaysWhenChosenMethodIsNotPacketeryShippingMethod(): void {
533 $checkout = $this->createCheckout();
534
535 $gatewayGopayMock = $this->createMock( WC_Payment_Gateway::class );
536 $gatewayGopayMock->id = 'gopay';
537 $gatewayCodMock = $this->createMock( WC_Payment_Gateway::class );
538 $gatewayCodMock->id = 'cod';
539
540 $availableGateways = [
541 'gopay' => $gatewayGopayMock,
542 'cod' => $gatewayCodMock,
543 ];
544
545 $this->checkoutServiceMock->method( 'getOrderPayParameter' )->willReturn( null );
546 $this->sessionServiceMock->method( 'getChosenMethodFromSession' )->willReturn( 'other_shipping_method' );
547 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( false );
548
549 $result = $checkout->filterPaymentGateways( $availableGateways );
550
551 $this->assertEquals( $availableGateways, $result );
552 }
553
554 public function testFilterPaymentGatewaysWhenCarrierIsNull(): void {
555 $checkout = $this->createCheckout();
556
557 $gatewayGopayMock = $this->createMock( WC_Payment_Gateway::class );
558 $gatewayGopayMock->id = 'gopay';
559 $gatewayCodMock = $this->createMock( WC_Payment_Gateway::class );
560 $gatewayCodMock->id = 'cod';
561
562 $availableGateways = [
563 'gopay' => $gatewayGopayMock,
564 'cod' => $gatewayCodMock,
565 ];
566
567 $this->checkoutServiceMock->method( 'getOrderPayParameter' )->willReturn( null );
568 $this->sessionServiceMock->method( 'getChosenMethodFromSession' )->willReturn( 'packetery_carrier_123' );
569 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( true );
570 $this->checkoutServiceMock->method( 'getCarrierIdFromPacketeryShippingMethod' )->willReturn( '123' );
571 $this->carrierEntityRepositoryMock->method( 'getAnyById' )->willReturn( null );
572
573 $result = $checkout->filterPaymentGateways( $availableGateways );
574
575 $this->assertEquals( $availableGateways, $result );
576 }
577
578 public function testFilterPaymentGatewaysFiltersCodWhenCarrierDoesNotSupportCod(): void {
579 $checkout = $this->createCheckout();
580
581 $gatewayGopayMock = $this->createMock( WC_Payment_Gateway::class );
582 $gatewayGopayMock->id = 'gopay';
583 $gatewayCodMock = $this->createMock( WC_Payment_Gateway::class );
584 $gatewayCodMock->id = 'cod';
585
586 $availableGateways = [
587 'gopay' => $gatewayGopayMock,
588 'cod' => $gatewayCodMock,
589 ];
590
591 $carrierMock = $this->createMock( Entity\Carrier::class );
592 $carrierMock->method( 'supportsCod' )->willReturn( false );
593
594 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
595 $carrierOptionsMock->method( 'hasCheckoutPaymentMethodDisallowed' )->willReturn( false );
596
597 $this->checkoutServiceMock->method( 'getOrderPayParameter' )->willReturn( null );
598 $this->sessionServiceMock->method( 'getChosenMethodFromSession' )->willReturn( 'packetery_carrier_123' );
599 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( true );
600 $this->checkoutServiceMock->method( 'getCarrierIdFromPacketeryShippingMethod' )->willReturn( '123' );
601 $this->carrierEntityRepositoryMock->method( 'getAnyById' )->willReturn( $carrierMock );
602 $this->carrierOptionsFactoryMock->method( 'createByCarrierId' )->willReturn( $carrierOptionsMock );
603 $this->paymentHelperMock->method( 'isCodPaymentMethod' )
604 ->willReturnCallback(
605 function ( $id ) {
606 return $id === 'cod';
607 }
608 );
609
610 $result = $checkout->filterPaymentGateways( $availableGateways );
611
612 $this->assertCount( 1, $result );
613 $this->assertArrayHasKey( 'gopay', $result );
614 $this->assertArrayNotHasKey( 'cod', $result );
615 }
616
617 public function testFilterPaymentGatewaysFiltersDisallowedPaymentMethods(): void {
618 $checkout = $this->createCheckout();
619
620 $gatewayGopayMock = $this->createMock( WC_Payment_Gateway::class );
621 $gatewayGopayMock->id = 'gopay';
622 $gatewayCodMock = $this->createMock( WC_Payment_Gateway::class );
623 $gatewayCodMock->id = 'paypal';
624
625 $availableGateways = [
626 'gopay' => $gatewayGopayMock,
627 'paypal' => $gatewayCodMock,
628 ];
629
630 $carrierMock = $this->createMock( Entity\Carrier::class );
631 $carrierMock->method( 'supportsCod' )->willReturn( true );
632
633 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
634 $carrierOptionsMock->method( 'hasCheckoutPaymentMethodDisallowed' )
635 ->willReturnCallback(
636 function ( $id ) {
637 return $id === 'paypal';
638 }
639 );
640
641 $this->checkoutServiceMock->method( 'getOrderPayParameter' )->willReturn( null );
642 $this->sessionServiceMock->method( 'getChosenMethodFromSession' )->willReturn( 'packetery_carrier_123' );
643 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( true );
644 $this->checkoutServiceMock->method( 'getCarrierIdFromPacketeryShippingMethod' )->willReturn( '123' );
645 $this->carrierEntityRepositoryMock->method( 'getAnyById' )->willReturn( $carrierMock );
646 $this->carrierOptionsFactoryMock->method( 'createByCarrierId' )->willReturn( $carrierOptionsMock );
647 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( false );
648
649 $result = $checkout->filterPaymentGateways( $availableGateways );
650
651 $this->assertCount( 1, $result );
652 $this->assertArrayHasKey( 'gopay', $result );
653 $this->assertArrayNotHasKey( 'paypal', $result );
654 }
655
656 public function testFilterPaymentGatewaysWithOrderPayScenario(): void {
657 $checkout = $this->createCheckout();
658
659 $gatewayGopayMock = $this->createMock( WC_Payment_Gateway::class );
660 $gatewayGopayMock->id = 'gopay';
661 $gatewayCodMock = $this->createMock( WC_Payment_Gateway::class );
662 $gatewayCodMock->id = 'cod';
663
664 $availableGateways = [
665 'gopay' => $gatewayGopayMock,
666 'cod' => $gatewayCodMock,
667 ];
668
669 $orderCarrierMock = $this->createMock( Entity\Carrier::class );
670 $orderCarrierMock->method( 'getId' )->willReturn( '456' );
671
672 $orderMock = $this->createMock( Order::class );
673 $orderMock->method( 'getCarrier' )->willReturn( $orderCarrierMock );
674
675 $carrierMock = $this->createMock( Entity\Carrier::class );
676 $carrierMock->method( 'supportsCod' )->willReturn( true );
677
678 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
679 $carrierOptionsMock->method( 'hasCheckoutPaymentMethodDisallowed' )->willReturn( false );
680
681 $this->checkoutServiceMock->method( 'getOrderPayParameter' )->willReturn( '123' );
682 $this->orderRepositoryMock->method( 'getByIdWithValidCarrier' )->willReturn( $orderMock );
683 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( true );
684 $this->checkoutServiceMock->method( 'getCarrierIdFromPacketeryShippingMethod' )->willReturn( '456' );
685 $this->carrierEntityRepositoryMock->method( 'getAnyById' )->willReturn( $carrierMock );
686 $this->carrierOptionsFactoryMock->method( 'createByCarrierId' )->willReturn( $carrierOptionsMock );
687 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( false );
688
689 $result = $checkout->filterPaymentGateways( $availableGateways );
690
691 $this->assertEquals( $availableGateways, $result );
692 }
693
694 public function testFilterPaymentGatewaysWithInvalidGatewayInstance(): void {
695 $checkout = $this->createCheckout();
696
697 $gatewayGopayMock = $this->createMock( WC_Payment_Gateway::class );
698 $gatewayGopayMock->id = 'gopay';
699 $invalidGateway = 'not_a_gateway_object';
700
701 $availableGateways = [
702 'gopay' => $gatewayGopayMock,
703 'invalid' => $invalidGateway,
704 ];
705
706 $carrierMock = $this->createMock( Entity\Carrier::class );
707 $carrierMock->method( 'supportsCod' )->willReturn( true );
708
709 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
710 $carrierOptionsMock->method( 'hasCheckoutPaymentMethodDisallowed' )->willReturn( false );
711
712 $this->checkoutServiceMock->method( 'getOrderPayParameter' )->willReturn( null );
713 $this->sessionServiceMock->method( 'getChosenMethodFromSession' )->willReturn( 'packetery_carrier_123' );
714 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( true );
715 $this->checkoutServiceMock->method( 'getCarrierIdFromPacketeryShippingMethod' )->willReturn( '123' );
716 $this->carrierEntityRepositoryMock->method( 'getAnyById' )->willReturn( $carrierMock );
717 $this->carrierOptionsFactoryMock->method( 'createByCarrierId' )->willReturn( $carrierOptionsMock );
718 $this->paymentHelperMock->method( 'isCodPaymentMethod' )->willReturn( false );
719
720 $result = $checkout->filterPaymentGateways( $availableGateways );
721
722 $this->assertEquals( $availableGateways, $result );
723 }
724
725 public function testFilterPaymentGatewaysHappyPathNoFiltering(): void {
726 $checkout = $this->createCheckout();
727
728 $gatewayGopayMock = $this->createMock( WC_Payment_Gateway::class );
729 $gatewayGopayMock->id = 'gopay';
730 $gatewayCodMock = $this->createMock( WC_Payment_Gateway::class );
731 $gatewayCodMock->id = 'cod';
732
733 $availableGateways = [
734 'gopay' => $gatewayGopayMock,
735 'cod' => $gatewayCodMock,
736 ];
737
738 $carrierMock = $this->createMock( Entity\Carrier::class );
739 $carrierMock->method( 'supportsCod' )->willReturn( true );
740
741 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
742 $carrierOptionsMock->method( 'hasCheckoutPaymentMethodDisallowed' )->willReturn( false );
743
744 $this->checkoutServiceMock->method( 'getOrderPayParameter' )->willReturn( null );
745 $this->sessionServiceMock->method( 'getChosenMethodFromSession' )->willReturn( 'packetery_carrier_123' );
746 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( true );
747 $this->checkoutServiceMock->method( 'getCarrierIdFromPacketeryShippingMethod' )->willReturn( '123' );
748 $this->carrierEntityRepositoryMock->method( 'getAnyById' )->willReturn( $carrierMock );
749 $this->carrierOptionsFactoryMock->method( 'createByCarrierId' )->willReturn( $carrierOptionsMock );
750 $this->paymentHelperMock->method( 'isCodPaymentMethod' )
751 ->willReturnCallback(
752 function ( $id ) {
753 return $id === 'cod';
754 }
755 );
756
757 $result = $checkout->filterPaymentGateways( $availableGateways );
758
759 $this->assertEquals( $availableGateways, $result );
760 }
761
762 public function testFilterPaymentGatewaysMultipleFilteringConditions(): void {
763 $checkout = $this->createCheckout();
764
765 $gatewayGopayMock = $this->createMock( WC_Payment_Gateway::class );
766 $gatewayGopayMock->id = 'gopay';
767 $gatewayCodMock = $this->createMock( WC_Payment_Gateway::class );
768 $gatewayCodMock->id = 'cod';
769 $gatewayPaypalMock = $this->createMock( WC_Payment_Gateway::class );
770 $gatewayPaypalMock->id = 'paypal';
771
772 $availableGateways = [
773 'gopay' => $gatewayGopayMock,
774 'cod' => $gatewayCodMock,
775 'paypal' => $gatewayPaypalMock,
776 ];
777
778 $carrierMock = $this->createMock( Entity\Carrier::class );
779 $carrierMock->method( 'supportsCod' )->willReturn( false );
780
781 $carrierOptionsMock = $this->createMock( Carrier\Options::class );
782 $carrierOptionsMock->method( 'hasCheckoutPaymentMethodDisallowed' )
783 ->willReturnCallback(
784 function ( $id ) {
785 return $id === 'paypal';
786 }
787 );
788
789 $this->checkoutServiceMock->method( 'getOrderPayParameter' )->willReturn( null );
790 $this->sessionServiceMock->method( 'getChosenMethodFromSession' )->willReturn( 'packetery_carrier_123' );
791 $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( true );
792 $this->checkoutServiceMock->method( 'getCarrierIdFromPacketeryShippingMethod' )->willReturn( '123' );
793 $this->carrierEntityRepositoryMock->method( 'getAnyById' )->willReturn( $carrierMock );
794 $this->carrierOptionsFactoryMock->method( 'createByCarrierId' )->willReturn( $carrierOptionsMock );
795 $this->paymentHelperMock->method( 'isCodPaymentMethod' )
796 ->willReturnCallback(
797 function ( $id ) {
798 return $id === 'cod';
799 }
800 );
801
802 $result = $checkout->filterPaymentGateways( $availableGateways );
803
804 $this->assertCount( 1, $result );
805 $this->assertArrayHasKey( 'gopay', $result );
806 $this->assertArrayNotHasKey( 'cod', $result );
807 $this->assertArrayNotHasKey( 'paypal', $result );
808 }
809 }
810