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 / Shipping / ShippingProviderTest.php

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

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