PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
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
← All changes | tests/Module/Checkout/CheckoutTest.php +227 -735 2.3.22.0.9 View file →
@@ -15,9 +15,8 @@
15 15 use Packetery\Module\Checkout\CurrencySwitcherService;
16 16 use Packetery\Module\Checkout\OrderUpdater;
17 17 use Packetery\Module\Checkout\RateCalculator;
18 18 use Packetery\Module\Checkout\SessionService;
19 -use Packetery\Module\DiagnosticsLogger\DiagnosticsLogger;
20 19 use Packetery\Module\Framework\WcAdapter;
21 20 use Packetery\Module\Framework\WpAdapter;
22 21 use Packetery\Module\Options\OptionsProvider;
23 22 use Packetery\Module\Order\Repository;
@@ -23,787 +22,280 @@
23 22 use Packetery\Module\Order\Repository;
24 23 use Packetery\Module\Payment\PaymentHelper;
25 24 use PHPUnit\Framework\MockObject\MockObject;
26 25 use PHPUnit\Framework\TestCase;
26 +use Tests\Core\DummyFactory;
27 27 use WC_Cart;
28 -use WC_Payment_Gateway;
29 28
30 29 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;
30 + private WpAdapter|MockObject $wpAdapter;
31 + private CheckoutService|MockObject $checkoutService;
32 + private CarrierOptionsFactory|MockObject $carrierOptionsFactory;
33 + private EntityRepository|MockObject $carrierEntityRepository;
34 + private CartService|MockObject $cartService;
35 + private RateCalculator|MockObject $rateCalculator;
36 + private PaymentHelper|MockObject $paymentHelper;
37 + private WcAdapter|MockObject $wcAdapter;
38 + private OptionsProvider|MockObject $optionsProvider;
39 + private Repository|MockObject $orderRepository;
40 + private CurrencySwitcherService|MockObject $currencySwitcherService;
41 + private CheckoutRenderer|MockObject $checkoutRenderer;
42 + private SessionService|MockObject $sessionService;
43 + private CheckoutValidator|MockObject $checkoutValidator;
44 + private OrderUpdater|MockObject $orderUpdater;
45 + private Checkout $checkout;
46 + private WC_Cart|MockObject $WCCart;
43 47
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 );
48 + protected function createCheckout(): void {
49 + $this->wpAdapter = $this->createMock( WpAdapter::class );
50 + $this->wcAdapter = $this->createMock( WcAdapter::class );
51 + $this->carrierOptionsFactory = $this->createMock( CarrierOptionsFactory::class );
52 + $this->optionsProvider = $this->createMock( OptionsProvider::class );
53 + $this->orderRepository = $this->createMock( Repository::class );
54 + $this->currencySwitcherService = $this->createMock( CurrencySwitcherService::class );
55 + $this->rateCalculator = $this->createMock( RateCalculator::class );
56 + $this->carrierEntityRepository = $this->createMock( EntityRepository::class );
57 + $this->paymentHelper = $this->createMock( PaymentHelper::class );
58 + $this->checkoutService = $this->createMock( CheckoutService::class );
59 + $this->checkoutRenderer = $this->createMock( CheckoutRenderer::class );
60 + $this->cartService = $this->createMock( CartService::class );
61 + $this->sessionService = $this->createMock( SessionService::class );
62 + $this->checkoutValidator = $this->createMock( CheckoutValidator::class );
63 + $this->orderUpdater = $this->createMock( OrderUpdater::class );
64 + $this->WCCart = $this->createMock( WC_Cart::class );
60 65
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 )
66 + $this->checkout = new Checkout(
67 + $this->wpAdapter,
68 + $this->wcAdapter,
69 + $this->carrierOptionsFactory,
70 + $this->optionsProvider,
71 + $this->orderRepository,
72 + $this->currencySwitcherService,
73 + $this->rateCalculator,
74 + $this->carrierEntityRepository,
75 + $this->paymentHelper,
76 + $this->checkoutService,
77 + $this->checkoutRenderer,
78 + $this->cartService,
79 + $this->sessionService,
80 + $this->checkoutValidator,
81 + $this->orderUpdater,
86 82 );
87 83 }
88 84
89 - public function testActionCalculateFeesHappyPathWithAgeVerificationAndCodFees(): void {
90 - $checkout = $this->createCheckout();
91 - $cartMock = $this->createMock( WC_Cart::class );
85 + public function testDoesNothingIfNoChosenShippingMethod(): void {
86 + $this->createCheckout();
87 + $this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( null );
92 88
93 - $carrierMock = $this->createMock( Entity\Carrier::class );
94 - $carrierMock->method( 'supportsAgeVerification' )->willReturn( true );
89 + $this->rateCalculator->expects( $this->never() )->method( 'getCODSurcharge' );
95 90
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] );
91 + $this->checkout->actionCalculateFees( $this->WCCart );
148 92 }
149 93
150 - public function testActionCalculateFeesWithShippingFreeCoupon(): void {
151 - $checkout = $this->createCheckout();
152 - $cartMock = $this->createMock( WC_Cart::class );
94 + public function testDoesNothingIfShippingMethodNotPacketery(): void {
95 + $this->createCheckout();
96 + $this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( 'non_packetery_method' );
97 + $this->checkoutService->method( 'isPacketeryShippingMethod' )->willReturn( false );
153 98
154 - $carrierMock = $this->createMock( Entity\Carrier::class );
155 - $carrierMock->method( 'supportsAgeVerification' )->willReturn( true );
99 + $this->rateCalculator->expects( $this->never() )->method( 'getCODSurcharge' );
156 100
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 );
101 + $this->checkout->actionCalculateFees( $this->WCCart );
180 102 }
181 103
182 - public function testActionCalculateFeesWhenChosenShippingMethodOptionIdIsNull(): void {
183 - $checkout = $this->createCheckout();
184 - $cartMock = $this->createMock( WC_Cart::class );
104 + public function testSkipsWhenCouponAllowsFreeShipping(): void {
105 + $this->createCheckout();
106 + $this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( 'packetery_method' );
107 + $this->checkoutService->method( 'isPacketeryShippingMethod' )->willReturn( true );
185 108
186 - $this->checkoutServiceMock->method( 'calculateShippingAndGetOptionId' )->willReturn( null );
109 + $carrierOptions = $this->createMock( Carrier\Options::class );
110 + $carrierOptions->method( 'hasCouponFreeShippingForFeesAllowed' )->willReturn( true );
187 111
188 - $cartMock->expects( self::never() )->method( 'add_fee' );
112 + $this->carrierOptionsFactory->method( 'createByOptionId' )->willReturn( $carrierOptions );
113 + $this->rateCalculator->method( 'isFreeShippingCouponApplied' )->willReturn( true );
189 114
190 - $checkout->actionCalculateFees( $cartMock );
115 + $this->rateCalculator->expects( $this->never() )->method( 'getCODSurcharge' );
116 + $this->checkout->actionCalculateFees( $this->WCCart );
191 117 }
192 118
193 - public function testActionCalculateFeesWhenIsPacketeryShippingMethodReturnsFalse(): void {
194 - $checkout = $this->createCheckout();
195 - $cartMock = $this->createMock( WC_Cart::class );
119 + public function testAddsFeesSuccessfully(): void {
120 + $this->createCheckout();
121 + $this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( 'packetery_method' );
122 + $this->checkoutService->method( 'isPacketeryShippingMethod' )->willReturn( true );
196 123
197 - $this->checkoutServiceMock->method( 'calculateShippingAndGetOptionId' )->willReturn( 'some_other_carrier_123' );
198 - $this->checkoutServiceMock->method( 'isPacketeryShippingMethod' )->willReturn( false );
124 + $carrierOptions = $this->createMock( Carrier\Options::class );
125 + $carrierOptions->method( 'hasCouponFreeShippingForFeesAllowed' )->willReturn( false );
126 + $carrierOptions->method( 'toArray' )->willReturn( [ 'key' => 'value' ] );
199 127
200 - $cartMock->expects( self::never() )->method( 'add_fee' );
128 + $this->carrierOptionsFactory->method( 'createByOptionId' )->willReturn( $carrierOptions );
129 + $this->rateCalculator->method( 'isFreeShippingCouponApplied' )->willReturn( false );
201 130
202 - $checkout->actionCalculateFees( $cartMock );
203 - }
131 + $this->cartService->method( 'getTaxClassWithMaxRate' )->willReturn( 'standard' );
132 + $this->rateCalculator
133 + ->method( 'getCODSurcharge' )
134 + ->with( [ 'key' => 'value' ], $this->anything() )
135 + ->willReturn( 5.00 );
204 136
205 - public function testAddAgeVerificationFeeHappyPath(): void {
206 - $checkout = $this->createCheckout();
207 - $cartMock = $this->createMock( WC_Cart::class );
137 + $carrier = $this->createMock( Entity\Carrier::class );
138 + $carrier->method( 'supportsAgeVerification' )->willReturn( true );
139 + $this->carrierEntityRepository->method( 'getAnyById' )->willReturn( $carrier );
140 + $carrierOptions->method( 'getAgeVerificationFee' )->willReturn( 10.0 );
141 + $this->cartService->method( 'isAgeVerificationRequired' )->willReturn( true );
208 142
209 - $carrierMock = $this->createMock( Entity\Carrier::class );
210 - $carrierMock->method( 'supportsAgeVerification' )->willReturn( true );
143 + $this->sessionService->method( 'getChosenPaymentMethod' )->willReturn( 'cod' );
144 + $this->paymentHelper->method( 'isCodPaymentMethod' )->willReturn( true );
145 + $this->currencySwitcherService->method( 'getConvertedPrice' )->willReturn( 10.0 );
146 + $this->wpAdapter->method( '__' )->willReturn( 'COD surcharge' );
211 147
212 - $carrierOptionsMock = $this->createMock( Carrier\Options::class );
213 - $carrierOptionsMock->method( 'getAgeVerificationFee' )->willReturn( 50.0 );
148 + $this->WCCart->expects( self::exactly( 2 ) )->method( 'add_fee' );
214 149
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' );
150 + $this->checkout->actionCalculateFees( $this->WCCart );
227 151 }
228 152
229 - public function testAddAgeVerificationFeeWhenCarrierIsNull(): void {
230 - $checkout = $this->createCheckout();
231 - $cartMock = $this->createMock( WC_Cart::class );
153 + public static function filterPaymentGatewaysProvider(): array {
154 + $gateway1 = (object) [ 'id' => 'gateway1' ];
155 + $gateway2 = (object) [ 'id' => 'gateway2' ];
156 + $codGateway = (object) [ 'id' => 'cod_gateway' ];
232 157
233 - $carrierOptionsMock = $this->createMock( Carrier\Options::class );
158 + $carrierSupportingCod = DummyFactory::createCarrierCzechPp();
159 + $carrierWithoutCod = DummyFactory::createCarDeliveryCarrier();
234 160
235 - $cartMock->expects( self::never() )->method( 'add_fee' );
161 + $order = DummyFactory::createOrderCzPp();
236 162
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,
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 + ],
543 229 ];
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 230 }
553 231
554 - public function testFilterPaymentGatewaysWhenCarrierIsNull(): void {
555 - $checkout = $this->createCheckout();
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();
556 247
557 - $gatewayGopayMock = $this->createMock( WC_Payment_Gateway::class );
558 - $gatewayGopayMock->id = 'gopay';
559 - $gatewayCodMock = $this->createMock( WC_Payment_Gateway::class );
560 - $gatewayCodMock->id = 'cod';
248 + $this->checkoutService
249 + ->method( 'getOrderPayParameter' )
250 + ->willReturn( $orderPayParameter );
561 251
562 - $availableGateways = [
563 - 'gopay' => $gatewayGopayMock,
564 - 'cod' => $gatewayCodMock,
565 - ];
252 + $this->orderRepository
253 + ->method( 'getByIdWithValidCarrier' )
254 + ->willReturn( $order );
566 255
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 );
256 + $this->checkoutService
257 + ->method( 'isPacketeryShippingMethod' )
258 + ->willReturn( $isPacketeryMethod );
572 259
573 - $result = $checkout->filterPaymentGateways( $availableGateways );
260 + if ( $carrier !== null ) {
261 + $this->checkoutService
262 + ->method( 'getCarrierIdFromPacketeryShippingMethod' )
263 + ->willReturn( $carrier->getId() );
264 + }
574 265
575 - $this->assertEquals( $availableGateways, $result );
576 - }
266 + $this->carrierEntityRepository
267 + ->method( 'getAnyById' )
268 + ->willReturn( $carrier );
577 269
578 - public function testFilterPaymentGatewaysFiltersCodWhenCarrierDoesNotSupportCod(): void {
579 - $checkout = $this->createCheckout();
270 + if ( $chosenMethod !== null ) {
271 + $this->sessionService
272 + ->method( 'getChosenMethodFromSession' )
273 + ->willReturn( $chosenMethod );
274 + }
580 275
581 - $gatewayGopayMock = $this->createMock( WC_Payment_Gateway::class );
582 - $gatewayGopayMock->id = 'gopay';
583 - $gatewayCodMock = $this->createMock( WC_Payment_Gateway::class );
584 - $gatewayCodMock->id = 'cod';
276 + $carrierOptions = $this->createMock( Carrier\Options::class );
277 + $this->carrierOptionsFactory
278 + ->method( 'createByCarrierId' )
279 + ->willReturn( $carrierOptions );
585 280
586 - $availableGateways = [
587 - 'gopay' => $gatewayGopayMock,
588 - 'cod' => $gatewayCodMock,
589 - ];
281 + $carrierOptions
282 + ->method( 'hasCheckoutPaymentMethodDisallowed' )
283 + ->willReturnCallback(
284 + function ( $gatewayId ) use ( $disallowedPaymentMethods ) {
285 + return in_array( $gatewayId, $disallowedPaymentMethods, true );
286 + }
287 + );
590 288
591 - $carrierMock = $this->createMock( Entity\Carrier::class );
592 - $carrierMock->method( 'supportsCod' )->willReturn( false );
289 + $this->paymentHelper
290 + ->method( 'isCodPaymentMethod' )
291 + ->willReturnCallback(
292 + function ( $gatewayId ) use ( $codPaymentMethods ) {
293 + return in_array( $gatewayId, $codPaymentMethods, true );
294 + }
295 + );
593 296
594 - $carrierOptionsMock = $this->createMock( Carrier\Options::class );
595 - $carrierOptionsMock->method( 'hasCheckoutPaymentMethodDisallowed' )->willReturn( false );
297 + $result = $this->checkout->filterPaymentGateways( $availableGateways );
596 298
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 );
299 + $this->assertEqualsCanonicalizing( $expectedResult, $result );
808 300 }
809 301 }