PluginProbe
Packeta / 2.1
Packeta v2.1
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 / Shipping / ShippingProviderTest.php

ShippingProviderTest.php in Packeta 2.1, at tests/Module/Shipping/ShippingProviderTest.php

115 lines 3.8 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\Shipping;
6
7 use Packetery\Module\Carrier\CarDeliveryConfig;
8 use Packetery\Module\Carrier\CarrierOptionsFactory;
9 use Packetery\Module\Carrier\EntityRepository;
10 use Packetery\Module\Carrier\PacketaPickupPointsConfig;
11 use Packetery\Module\ContextResolver;
12 use Packetery\Module\Options\FlagManager\FeatureFlagProvider;
13 use Packetery\Module\Shipping\BaseShippingMethod;
14 use Packetery\Module\Shipping\ShippingProvider;
15 use Packetery\Module\ShippingMethod;
16 use Packetery\Module\ShippingZoneRepository;
17 use PHPUnit\Framework\MockObject\MockObject;
18 use PHPUnit\Framework\TestCase;
19 use ReflectionClass;
20
21 class ShippingProviderTest extends TestCase {
22
23 private ContextResolver&MockObject $contextResolver;
24 private ShippingZoneRepository&MockObject $shippingZoneRepository;
25
26 private function createShippingProvider(): ShippingProvider {
27 $this->contextResolver = $this->createMock( ContextResolver::class );
28 $this->shippingZoneRepository = $this->createMock( ShippingZoneRepository::class );
29
30 return new ShippingProvider(
31 $this->createMock( FeatureFlagProvider::class ),
32 $this->createMock( PacketaPickupPointsConfig::class ),
33 $this->createMock( CarDeliveryConfig::class ),
34 $this->contextResolver,
35 $this->shippingZoneRepository,
36 $this->createMock( EntityRepository::class ),
37 $this->createMock( CarrierOptionsFactory::class )
38 );
39 }
40
41 public function tearDown(): void {
42 $shippingProviderReflection = new ReflectionClass( ShippingProvider::class );
43 $property = $shippingProviderReflection->getProperty( 'sortedMethodsCache' );
44 $property->setAccessible( true );
45 $property->setValue( [] );
46 }
47
48 /**
49 * @return array
50 */
51 public static function shippingMethodsProvider(): array {
52 return [
53 [
54 'methodId' => 'free_shipping',
55 'isPacketaMethod' => false,
56 ],
57 [
58 'methodId' => 'flat_rate',
59 'isPacketaMethod' => false,
60 ],
61 [
62 'methodId' => ShippingMethod::PACKETERY_METHOD_ID,
63 'isPacketaMethod' => true,
64 ],
65 [
66 'methodId' => BaseShippingMethod::PACKETA_METHOD_PREFIX . 'zpointcz',
67 'isPacketaMethod' => true,
68 ],
69 [
70 'methodId' => BaseShippingMethod::PACKETA_METHOD_PREFIX . 'czzpoint',
71 'isPacketaMethod' => true,
72 ],
73 [
74 'methodId' => BaseShippingMethod::PACKETA_METHOD_PREFIX . '106',
75 'isPacketaMethod' => true,
76 ],
77 ];
78 }
79
80 /**
81 * @dataProvider shippingMethodsProvider
82 */
83 public function testIsPacketaMethod( $methodId, $isPacketaMethod ): void {
84 self::assertEquals( $isPacketaMethod, ShippingProvider::isPacketaMethod( $methodId ) );
85 }
86
87 public function testGetSortedCachedMethodsFillsCache(): void {
88 $shippingProvider = $this->createShippingProvider();
89
90 $dummyZoneId = 123;
91 $this->contextResolver->expects( $this->once() )->method( 'getShippingZoneId' )->willReturn( $dummyZoneId );
92 $this->shippingZoneRepository->expects( $this->once() )->method( 'getCountryCodesForShippingZone' )->willReturn( [] );
93
94 $shippingProvider->getSortedCachedMethods( [] );
95 }
96
97 public function testGetSortedCachedMethodsGetsFromCache(): void {
98 $shippingProvider = $this->createShippingProvider();
99
100 $originalMethods = [];
101 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
102 $cacheKey = crc32( serialize( $originalMethods ) );
103
104 $shippingProviderReflection = new ReflectionClass( ShippingProvider::class );
105 $property = $shippingProviderReflection->getProperty( 'sortedMethodsCache' );
106 $property->setAccessible( true );
107 $property->setValue( [ $cacheKey => [ 'dummyMethodId' => 'dummyMethodClass' ] ] );
108
109 $this->contextResolver->expects( $this->never() )->method( 'getShippingZoneId' );
110 $this->shippingZoneRepository->expects( $this->never() )->method( 'getCountryCodesForShippingZone' );
111
112 $shippingProvider->getSortedCachedMethods( $originalMethods );
113 }
114 }
115