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
packeta / tests / Module / Checkout / CheckoutTest.php

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

302 lines 11.4 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\Framework\WcAdapter;
20 use Packetery\Module\Framework\WpAdapter;
21 use Packetery\Module\Options\OptionsProvider;
22 use Packetery\Module\Order\Repository;
23 use Packetery\Module\Payment\PaymentHelper;
24 use PHPUnit\Framework\MockObject\MockObject;
25 use PHPUnit\Framework\TestCase;
26 use Tests\Core\DummyFactory;
27 use WC_Cart;
28
29 class CheckoutTest extends TestCase {
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;
47
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 );
65
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,
82 );
83 }
84
85 public function testDoesNothingIfNoChosenShippingMethod(): void {
86 $this->createCheckout();
87 $this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( null );
88
89 $this->rateCalculator->expects( $this->never() )->method( 'getCODSurcharge' );
90
91 $this->checkout->actionCalculateFees( $this->WCCart );
92 }
93
94 public function testDoesNothingIfShippingMethodNotPacketery(): void {
95 $this->createCheckout();
96 $this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( 'non_packetery_method' );
97 $this->checkoutService->method( 'isPacketeryShippingMethod' )->willReturn( false );
98
99 $this->rateCalculator->expects( $this->never() )->method( 'getCODSurcharge' );
100
101 $this->checkout->actionCalculateFees( $this->WCCart );
102 }
103
104 public function testSkipsWhenCouponAllowsFreeShipping(): void {
105 $this->createCheckout();
106 $this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( 'packetery_method' );
107 $this->checkoutService->method( 'isPacketeryShippingMethod' )->willReturn( true );
108
109 $carrierOptions = $this->createMock( Carrier\Options::class );
110 $carrierOptions->method( 'hasCouponFreeShippingForFeesAllowed' )->willReturn( true );
111
112 $this->carrierOptionsFactory->method( 'createByOptionId' )->willReturn( $carrierOptions );
113 $this->rateCalculator->method( 'isFreeShippingCouponApplied' )->willReturn( true );
114
115 $this->rateCalculator->expects( $this->never() )->method( 'getCODSurcharge' );
116 $this->checkout->actionCalculateFees( $this->WCCart );
117 }
118
119 public function testAddsFeesSuccessfully(): void {
120 $this->createCheckout();
121 $this->checkoutService->method( 'calculateShippingAndGetOptionId' )->willReturn( 'packetery_method' );
122 $this->checkoutService->method( 'isPacketeryShippingMethod' )->willReturn( true );
123
124 $carrierOptions = $this->createMock( Carrier\Options::class );
125 $carrierOptions->method( 'hasCouponFreeShippingForFeesAllowed' )->willReturn( false );
126 $carrierOptions->method( 'toArray' )->willReturn( [ 'key' => 'value' ] );
127
128 $this->carrierOptionsFactory->method( 'createByOptionId' )->willReturn( $carrierOptions );
129 $this->rateCalculator->method( 'isFreeShippingCouponApplied' )->willReturn( false );
130
131 $this->cartService->method( 'getTaxClassWithMaxRate' )->willReturn( 'standard' );
132 $this->rateCalculator
133 ->method( 'getCODSurcharge' )
134 ->with( [ 'key' => 'value' ], $this->anything() )
135 ->willReturn( 5.00 );
136
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 );
142
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' );
147
148 $this->WCCart->expects( self::exactly( 2 ) )->method( 'add_fee' );
149
150 $this->checkout->actionCalculateFees( $this->WCCart );
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 }
301 }
302