PluginProbe
Packeta / 2.1.2
Packeta v2.1.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 1.7.0 All 55 releases
packeta / tests / Module / Checkout / ShippingRateFactoryTest.php

ShippingRateFactoryTest.php in Packeta 2.1.2, at tests/Module/Checkout/ShippingRateFactoryTest.php

628 lines 20.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Tests\Module\Checkout;
6
7 use Packetery\Module\Carrier;
8 use Packetery\Module\Carrier\CarDeliveryConfig;
9 use Packetery\Module\Carrier\CarrierActivityBridge;
10 use Packetery\Module\Carrier\CarrierOptionsFactory;
11 use Packetery\Module\Checkout\CartService;
12 use Packetery\Module\Checkout\CheckoutService;
13 use Packetery\Module\Checkout\RateCalculator;
14 use Packetery\Module\Checkout\ShippingRateFactory;
15 use Packetery\Module\Framework\WcAdapter;
16 use Packetery\Module\Framework\WpAdapter;
17 use Packetery\Module\Options\OptionsProvider;
18 use Packetery\Module\Product;
19 use Packetery\Module\Product\ProductEntityFactory;
20 use Packetery\Module\ProductCategory;
21 use Packetery\Module\ProductCategory\ProductCategoryEntityFactory;
22 use Packetery\Module\Shipping\BaseShippingMethod;
23 use Packetery\Module\Shipping\Generated\ShippingMethod_zpointcz;
24 use Packetery\Module\ShippingMethod;
25 use PHPUnit\Framework\MockObject\MockObject;
26 use PHPUnit\Framework\TestCase;
27 use Tests\Core\DummyFactory;
28 use Tests\Module\MockFactory;
29 use WC_Cart;
30 use WC_Product;
31
32 class ShippingRateFactoryTest extends TestCase {
33
34 private WpAdapter|MockObject $wpAdapter;
35 private WcAdapter|MockObject $wcAdapter;
36 private ProductEntityFactory|MockObject $productEntityFactory;
37 private ProductCategoryEntityFactory|MockObject $productCategoryEntityFactory;
38 private CarrierOptionsFactory|MockObject $carrierOptionsFactory;
39 private WpAdapter|MockObject $currencySwitcherFacade;
40 private Carrier\EntityRepository|MockObject $carrierEntityRepository;
41 private CarDeliveryConfig|MockObject $carDeliveryConfig;
42 private OptionsProvider|MockObject $provider;
43 private CartService|MockObject $cartService;
44 private CheckoutService|MockObject $checkoutService;
45 private ShippingRateFactory $shippingRateFactory;
46 private CarrierActivityBridge|MockObject $carrierActivityBridge;
47
48 /**
49 * @return array
50 */
51 private static function createDummyZpointCzCarrierOptions(): array {
52 return [
53 'id' => 'zpoint-cz',
54 'name' => 'zpoint-cz',
55 'active' => true,
56 'free_shipping_limit' => null,
57 'weight_limits' =>
58 [
59 [
60 'weight' => 20.0,
61 'price' => 234.34,
62 ],
63 ],
64 ];
65 }
66
67 private function createShippingRateFactoryMock(): void {
68 $this->wpAdapter = MockFactory::createWpAdapter( $this );
69 $this->wcAdapter = $this->createMock( WcAdapter::class );
70 $this->productEntityFactory = $this->createMock( ProductEntityFactory::class );
71 $this->productCategoryEntityFactory = $this->createMock( ProductCategoryEntityFactory::class );
72 $this->carrierOptionsFactory = $this->createMock( CarrierOptionsFactory::class );
73 $this->currencySwitcherFacade = MockFactory::createCurrencySwitcherFacade( $this );
74 $this->carrierEntityRepository = $this->createMock( Carrier\EntityRepository::class );
75 $this->carDeliveryConfig = $this->createMock( CarDeliveryConfig::class );
76 $this->provider = $this->createMock( OptionsProvider::class );
77 $this->cartService = $this->createMock( CartService::class );
78 $this->checkoutService = $this->createMock( CheckoutService::class );
79 $this->carrierActivityBridge = $this->createMock( CarrierActivityBridge::class );
80
81 $this->shippingRateFactory = new ShippingRateFactory(
82 $this->wpAdapter,
83 $this->wcAdapter,
84 $this->checkoutService,
85 $this->carrierEntityRepository,
86 $this->cartService,
87 $this->carrierOptionsFactory,
88 $this->carDeliveryConfig,
89 new RateCalculator( $this->wpAdapter, $this->wcAdapter, $this->currencySwitcherFacade ),
90 $this->provider,
91 );
92 }
93
94 public static function rateCreationDataProvider(): array {
95 return [
96 'configured carriers must be present in rates' =>
97 [
98 'expectedRateCount' => 2,
99 'carriers' =>
100 [
101 DummyFactory::createCarrierCzechPp(),
102 DummyFactory::createCarrierCzechHdRequiresSize(),
103 ],
104 'carriersOptions' =>
105 [
106 'packetery_carrier_zpoint-cz' =>
107 self::createDummyZpointCzCarrierOptions(),
108 'packetery_carrier_106' =>
109 [
110 'id' => '106',
111 'name' => 'hd-cz',
112 'active' => true,
113 'free_shipping_limit' => null,
114 'weight_limits' =>
115 [
116 [
117 'weight' => 5.0,
118 'price' => 444.34,
119 ],
120 ],
121 ],
122 ],
123 'productDisallowedRateIds' => [],
124 'productCategoryDisallowedRateIds' => [],
125 'allowedCarrierNames' =>
126 [
127 'zpoint-cz' => 'zpoint-cz',
128 106 => 'hd-cz',
129 ],
130 'isCarDeliveryEnabled' => true,
131 'isAgeVerificationRequiredByProduct' => false,
132 'cartWeightKg' => 5.0,
133 'shippingMethod' => ShippingMethod::PACKETERY_METHOD_ID,
134 ],
135 'new style carrier' =>
136 [
137 'expectedRateCount' => 1,
138 'carriers' =>
139 [
140 DummyFactory::createCarrierCzechPp(),
141 ],
142 'carriersOptions' =>
143 [
144 'packetery_carrier_zpoint-cz' =>
145 self::createDummyZpointCzCarrierOptions(),
146 ],
147 'productDisallowedRateIds' => [],
148 'productCategoryDisallowedRateIds' => [],
149 'allowedCarrierNames' =>
150 [
151 'zpoint-cz' => 'zpoint-cz',
152 ],
153 'isCarDeliveryEnabled' => true,
154 'isAgeVerificationRequiredByProduct' => false,
155 'cartWeightKg' => 5.0,
156 'shippingMethod' => BaseShippingMethod::PACKETA_METHOD_PREFIX . ShippingMethod_zpointcz::CARRIER_ID,
157 ],
158 'new style carrier country mismatch' =>
159 [
160 'expectedRateCount' => 0,
161 'carriers' =>
162 [
163 DummyFactory::createCarrierSlovakPp(),
164 ],
165 'carriersOptions' =>
166 [
167 'packetery_carrier_zpoint-sk' =>
168 [
169 'id' => 'zpoint-sk',
170 'name' => 'zpoint-sk',
171 'active' => true,
172 'free_shipping_limit' => null,
173 'weight_limits' =>
174 [
175 [
176 'weight' => 20.0,
177 'price' => 234.34,
178 ],
179 ],
180 ],
181 ],
182 'productDisallowedRateIds' => [],
183 'productCategoryDisallowedRateIds' => [],
184 'allowedCarrierNames' =>
185 [
186 'zpoint-cz' => 'zpoint-cz',
187 ],
188 'isCarDeliveryEnabled' => true,
189 'isAgeVerificationRequiredByProduct' => false,
190 'cartWeightKg' => 5.0,
191 'shippingMethod' => BaseShippingMethod::PACKETA_METHOD_PREFIX . ShippingMethod_zpointcz::CARRIER_ID,
192 ],
193 'car delivery carrier must not be present in rates' =>
194 [
195 'expectedRateCount' => 1,
196 'carriers' =>
197 [
198 DummyFactory::createCarrierCzechPp(),
199 DummyFactory::createCarDeliveryCarrier(),
200 ],
201 'carriersOptions' =>
202 [
203 'packetery_carrier_zpoint-cz' =>
204 self::createDummyZpointCzCarrierOptions(),
205 'packetery_carrier_25061' =>
206 [
207 'id' => '25061',
208 'name' => 'CZ Zásilkovna do auta',
209 'active' => true,
210 'free_shipping_limit' => null,
211 'weight_limits' =>
212 [
213 [
214 'weight' => 5.0,
215 'price' => 444.34,
216 ],
217 ],
218 ],
219 ],
220 'productDisallowedRateIds' => [],
221 'productCategoryDisallowedRateIds' => [],
222 'allowedCarrierNames' => null,
223 'isCarDeliveryEnabled' => false,
224 'isAgeVerificationRequiredByProduct' => false,
225 'cartWeightKg' => 1.0,
226 'shippingMethod' => ShippingMethod::PACKETERY_METHOD_ID,
227 ],
228 'only one carrier is active' =>
229 [
230 'expectedRateCount' => 1,
231 'carriers' =>
232 [
233 DummyFactory::createCarrierCzechPp(),
234 DummyFactory::createCarrierCzechHdRequiresSize(),
235 ],
236 'carriersOptions' =>
237 [
238 'packetery_carrier_zpoint-cz' =>
239 self::createDummyZpointCzCarrierOptions(),
240 'packetery_carrier_106' =>
241 [
242 'id' => '106',
243 'name' => 'hd-cz',
244 'active' => false,
245 'free_shipping_limit' => null,
246 'weight_limits' =>
247 [
248 [
249 'weight' => 5.0,
250 'price' => 444.34,
251 ],
252 ],
253 ],
254 ],
255 'productDisallowedRateIds' => [],
256 'productCategoryDisallowedRateIds' => [],
257 'allowedCarrierNames' => null,
258 'isCarDeliveryEnabled' => true,
259 'isAgeVerificationRequiredByProduct' => false,
260 'cartWeightKg' => 1.0,
261 'shippingMethod' => ShippingMethod::PACKETERY_METHOD_ID,
262 ],
263 'carrier not supporting over-weight cart must be omitted' =>
264 [
265 'expectedRateCount' => 0,
266 'carriers' =>
267 [
268 DummyFactory::createCarrierCzechPp(),
269 ],
270 'carriersOptions' =>
271 [
272 'packetery_carrier_zpoint-cz' =>
273 [
274 'id' => 'zpoint-cz',
275 'name' => 'zpoint-cz',
276 'active' => true,
277 'free_shipping_limit' => null,
278 'weight_limits' =>
279 [
280 [
281 'weight' => 20.0,
282 'price' => 100.0,
283 ],
284 ],
285 ],
286 ],
287 'productDisallowedRateIds' => [],
288 'productCategoryDisallowedRateIds' => [],
289 'allowedCarrierNames' =>
290 [
291 'zpoint-cz' => 'zpoint-cz',
292 ],
293 'isCarDeliveryEnabled' => true,
294 'isAgeVerificationRequiredByProduct' => false,
295 'cartWeightKg' => 21.0,
296 'shippingMethod' => ShippingMethod::PACKETERY_METHOD_ID,
297 ],
298 'inactive carrier must be omitted' =>
299 [
300 'expectedRateCount' => 0,
301 'carriers' =>
302 [
303 DummyFactory::createCarrierCzechPp(),
304 ],
305 'carriersOptions' =>
306 [
307 'packetery_carrier_zpoint-cz' =>
308 [
309 'id' => 'zpoint-cz',
310 'name' => 'zpoint-cz',
311 'active' => false,
312 'free_shipping_limit' => null,
313 'weight_limits' =>
314 [
315 [
316 'weight' => 20.0,
317 'price' => 100.0,
318 ],
319 ],
320 ],
321 ],
322 'productDisallowedRateIds' => [],
323 'productCategoryDisallowedRateIds' => [],
324 'allowedCarrierNames' => [],
325 'isCarDeliveryEnabled' => true,
326 'isAgeVerificationRequiredByProduct' => false,
327 'cartWeightKg' => 1.0,
328 'shippingMethod' => ShippingMethod::PACKETERY_METHOD_ID,
329 ],
330 'carrier disallowed by product must be omitted' =>
331 [
332 'expectedRateCount' => 0,
333 'carriers' =>
334 [
335 DummyFactory::createCarrierCzechPp(),
336 ],
337 'carriersOptions' =>
338 [
339 'packetery_carrier_zpoint-cz' =>
340 [
341 'id' => 'zpoint-cz',
342 'name' => 'zpoint-cz',
343 'active' => true,
344 ],
345 ],
346 'productDisallowedRateIds' =>
347 [
348 0 => 'packetery_carrier_zpoint-cz',
349 ],
350 'productCategoryDisallowedRateIds' => [],
351 'allowedCarrierNames' =>
352 [
353 'zpoint-cz' => 'zpoint-cz',
354 ],
355 'isCarDeliveryEnabled' => true,
356 'isAgeVerificationRequiredByProduct' => false,
357 'cartWeightKg' => 1.0,
358 'shippingMethod' => ShippingMethod::PACKETERY_METHOD_ID,
359 ],
360 'carrier disallowed by product category must be omitted' =>
361 [
362 'expectedRateCount' => 0,
363 'carriers' =>
364 [
365 DummyFactory::createCarrierCzechPp(),
366 ],
367 'carriersOptions' =>
368 [
369 'packetery_carrier_zpoint-cz' =>
370 [
371 'id' => 'zpoint-cz',
372 'name' => 'zpoint-cz',
373 'active' => true,
374 ],
375 ],
376 'productDisallowedRateIds' => [],
377 'productCategoryDisallowedRateIds' =>
378 [
379 0 => 'packetery_carrier_zpoint-cz',
380 ],
381 'allowedCarrierNames' =>
382 [
383 'zpoint-cz' => 'zpoint-cz',
384 ],
385 'isCarDeliveryEnabled' => true,
386 'isAgeVerificationRequiredByProduct' => false,
387 'cartWeightKg' => 1.0,
388 'shippingMethod' => ShippingMethod::PACKETERY_METHOD_ID,
389 ],
390 'car delivery carriers must be supported' =>
391 [
392 'expectedRateCount' => 1,
393 'carriers' =>
394 [
395 DummyFactory::createCarDeliveryCarrier(),
396 ],
397 'carriersOptions' =>
398 [
399 'packetery_carrier_25061' =>
400 [
401 'id' => '25061',
402 'name' => 'CZ Zásilkovna do auta',
403 'active' => true,
404 'free_shipping_limit' => null,
405 'weight_limits' =>
406 [
407 [
408 'weight' => 20.0,
409 'price' => 234.34,
410 ],
411 ],
412 ],
413 ],
414 'productDisallowedRateIds' => [],
415 'productCategoryDisallowedRateIds' => [],
416 'allowedCarrierNames' =>
417 [
418 25061 => 'CZ Zásilkovna do auta',
419 ],
420 'isCarDeliveryEnabled' => true,
421 'isAgeVerificationRequiredByProduct' => false,
422 'cartWeightKg' => 1.0,
423 'shippingMethod' => ShippingMethod::PACKETERY_METHOD_ID,
424 ],
425 'carrier not supporting age verification must be omitted' =>
426 [
427 'expectedRateCount' => 0,
428 'carriers' =>
429 [
430 DummyFactory::createCarDeliveryCarrier(),
431 ],
432 'carriersOptions' =>
433 [
434 'packetery_carrier_25061' =>
435 [
436 'id' => '25061',
437 'name' => 'CZ Zásilkovna do auta',
438 'active' => true,
439 'free_shipping_limit' => null,
440 'weight_limits' =>
441 [
442 [
443 'weight' => 20.0,
444 'price' => 234.34,
445 ],
446 ],
447 ],
448 ],
449 'productDisallowedRateIds' => [],
450 'productCategoryDisallowedRateIds' => [],
451 'allowedCarrierNames' =>
452 [
453 25061 => 'CZ Zásilkovna do auta',
454 ],
455 'isCarDeliveryEnabled' => true,
456 'isAgeVerificationRequiredByProduct' => true,
457 'cartWeightKg' => 1.0,
458 'shippingMethod' => ShippingMethod::PACKETERY_METHOD_ID,
459 ],
460 'allowed carrier names argument must support null' =>
461 [
462 'expectedRateCount' => 1,
463 'carriers' =>
464 [
465 DummyFactory::createCarDeliveryCarrier(),
466 ],
467 'carriersOptions' =>
468 [
469 'packetery_carrier_25061' =>
470 [
471 'id' => '25061',
472 'name' => 'CZ Zásilkovna do auta',
473 'active' => true,
474 'free_shipping_limit' => null,
475 'weight_limits' =>
476 [
477 [
478 'weight' => 20.0,
479 'price' => 234.34,
480 ],
481 ],
482 ],
483 ],
484 'productDisallowedRateIds' => [],
485 'productCategoryDisallowedRateIds' => [],
486 'allowedCarrierNames' => null,
487 'isCarDeliveryEnabled' => true,
488 'isAgeVerificationRequiredByProduct' => false,
489 'cartWeightKg' => 1.0,
490 'shippingMethod' => ShippingMethod::PACKETERY_METHOD_ID,
491 ],
492 ];
493 }
494
495 /**
496 * @dataProvider rateCreationDataProvider
497 * @throws \PHPUnit\Framework\MockObject\Exception
498 */
499 public function testCreateShippingRates(
500 int $expectedRateCount,
501 array $carriers,
502 array $carriersOptions,
503 array $productDisallowedRateIds,
504 array $productCategoryDisallowedRateIds,
505 ?array $allowedCarrierNames,
506 bool $isCarDeliveryEnabled,
507 bool $isAgeVerificationRequiredByProduct,
508 float $cartWeightKg,
509 string $shippingMethod,
510 ): void {
511 $this->createShippingRateFactoryMock();
512
513 $this->wpAdapter
514 ->expects( self::atLeast( $expectedRateCount ) )
515 ->method( 'applyFilters' );
516 $this->wpAdapter
517 ->method( 'didAction' )
518 ->willReturn( 1 );
519
520 $cart = $this->createMock( WC_Cart::class );
521 $cart
522 ->method( 'get_coupons' )
523 ->willReturn( [] );
524 $this->wcAdapter
525 ->method( 'cart' )
526 ->willReturn( $cart );
527
528 $wcProduct = $this->createMock( WC_Product::class );
529 $wcProduct
530 ->method( 'get_price' )
531 ->willReturn( '100.00' );
532 $wcProduct
533 ->method( 'get_category_ids' )
534 ->willReturn( [ 1 ] );
535 $this->wcAdapter
536 ->method( 'productFactoryGetProduct' )
537 ->willReturn( $wcProduct );
538
539 $cartItem = [
540 'product_id' => 1,
541 'quantity' => 1.0,
542 'data' => $wcProduct,
543 ];
544 $this->wcAdapter
545 ->method( 'cartGetCartContents' )
546 ->willReturn( [ $cartItem ] );
547 $this->wcAdapter
548 ->method( 'cartGetCartContent' )
549 ->willReturn( [ $cartItem ] );
550
551 $this->checkoutService
552 ->method( 'getCustomerCountry' )
553 ->willReturn( 'cz' );
554 $this->wcAdapter
555 ->method( 'cartGetCartContentsTotal' )
556 ->willReturn( 100.0 );
557 $this->wcAdapter
558 ->method( 'cartGetCartContentsTax' )
559 ->willReturn( 21.0 );
560 $this->cartService
561 ->method( 'getCartWeightKg' )
562 ->willReturn( $cartWeightKg );
563
564 $productEntity = $this->createMock( Product\Entity::class );
565 $productEntity
566 ->method( 'isPhysical' )
567 ->willReturn( true );
568 $productEntity
569 ->method( 'isAgeVerificationRequired' )
570 ->willReturn( $isAgeVerificationRequiredByProduct );
571 $productEntity
572 ->method( 'getDisallowedShippingRateIds' )
573 ->willReturn( array_merge( [ Carrier\OptionPrefixer::getOptionId( 'anyDisallowedOnProduct' ) ], $productDisallowedRateIds ) );
574 $this->productEntityFactory
575 ->method( 'fromPostId' )
576 ->willReturn( $productEntity );
577 $this->cartService
578 ->method( 'isAgeVerificationRequired' )
579 ->willReturn( $isAgeVerificationRequiredByProduct );
580
581 $productCategory = $this->createMock( ProductCategory\Entity::class );
582 $productCategory
583 ->method( 'getDisallowedShippingRateIds' )
584 ->willReturn( array_merge( [ Carrier\OptionPrefixer::getOptionId( 'anyDisallowedByProductCategory' ) ], $productCategoryDisallowedRateIds ) );
585 $this->productCategoryEntityFactory = $this->createMock( ProductCategoryEntityFactory::class );
586 $this->productCategoryEntityFactory
587 ->method( 'fromTermId' )
588 ->willReturn( $productCategory );
589
590 $this->carrierOptionsFactory
591 ->method( 'createByOptionId' )
592 ->willReturnCallback(
593 function ( $optionId ) use ( $carriersOptions ) {
594 $carrierOptions = $carriersOptions[ $optionId ];
595
596 return new Carrier\Options(
597 $optionId,
598 $carrierOptions
599 );
600 }
601 );
602
603 $this->carrierEntityRepository
604 ->method( 'getByCountryIncludingNonFeed' )
605 ->willReturn( $carriers );
606 $this->carrierEntityRepository
607 ->method( 'getAnyById' )
608 ->willReturn( $carriers[0] );
609
610 $this->carDeliveryConfig
611 ->method( 'isDisabled' )
612 ->willReturn( ! $isCarDeliveryEnabled );
613
614 $this->carrierActivityBridge
615 ->method( 'isActive' )
616 ->willReturnOnConsecutiveCalls( ...array_column( $carriersOptions, 'active' ) );
617
618 $dummyInstanceId = 11;
619 $rates = $this->shippingRateFactory->createShippingRates(
620 $allowedCarrierNames,
621 $shippingMethod,
622 $dummyInstanceId,
623 );
624
625 self::assertCount( $expectedRateCount, $rates );
626 }
627 }
628