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 / RateCalculatorTest.php

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

252 lines 7.5 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\Options;
8 use Packetery\Module\Checkout\RateCalculator;
9 use Packetery\Module\Framework\WcAdapter;
10 use Packetery\Module\Framework\WpAdapter;
11 use PHPUnit\Framework\MockObject\MockObject;
12 use PHPUnit\Framework\TestCase;
13 use Tests\Module\CarrierOptionsDummyFactory;
14 use Tests\Module\MockFactory;
15
16 class RateCalculatorTest extends TestCase {
17
18 private WpAdapter|MockObject $wpAdapter;
19 private RateCalculator $rateCalculator;
20
21 private function createRateCalculatorMock(): void {
22 $this->wpAdapter = MockFactory::createWpAdapter( $this );
23
24 $this->rateCalculator = new RateCalculator(
25 $this->wpAdapter,
26 $this->createMock( WcAdapter::class ),
27 MockFactory::createCurrencySwitcherFacade( $this ),
28 );
29 }
30
31 /**
32 * @return array
33 */
34 public static function calculationDataProvider(): array {
35 return [
36 [
37 'expectedCost' => 11.0,
38 CarrierOptionsDummyFactory::getDefaultCarrier(),
39 'totalProductValue' => 100.0,
40 'cartWeightKg' => 0.0,
41 'isCouponApplied' => false,
42 ],
43 [
44 'expectedCost' => 22.0,
45 CarrierOptionsDummyFactory::getDefaultCarrier(),
46 'totalProductValue' => 100.0,
47 'cartWeightKg' => 15.0,
48 'isCouponApplied' => false,
49 ],
50 [
51 'expectedCost' => 22.0,
52 CarrierOptionsDummyFactory::getDefaultCarrier(),
53 'totalProductValue' => 100.0,
54 'cartWeightKg' => 20.0,
55 'isCouponApplied' => false,
56 ],
57 [
58 'expectedCost' => 33.0,
59 CarrierOptionsDummyFactory::getDefaultCarrier(),
60 'totalProductValue' => 100.0,
61 'cartWeightKg' => 25.0,
62 'isCouponApplied' => false,
63 ],
64 [
65 'expectedCost' => null,
66 CarrierOptionsDummyFactory::getDefaultCarrier(),
67 'totalProductValue' => 100.0,
68 'cartWeightKg' => 31.0,
69 'isCouponApplied' => false,
70 ],
71 [
72 'expectedCost' => 111.0,
73 CarrierOptionsDummyFactory::getProductValuePricingCarrier(),
74 'totalProductValue' => 100.0,
75 'cartWeightKg' => 1.0,
76 'isCouponApplied' => false,
77 ],
78 [
79 'expectedCost' => 222.0,
80 CarrierOptionsDummyFactory::getProductValuePricingCarrier(),
81 'totalProductValue' => 150.0,
82 'cartWeightKg' => 15.0,
83 'isCouponApplied' => false,
84 ],
85 [
86 'expectedCost' => 222.0,
87 CarrierOptionsDummyFactory::getProductValuePricingCarrier(),
88 'totalProductValue' => 200.0,
89 'cartWeightKg' => 20.0,
90 'isCouponApplied' => false,
91 ],
92 [
93 'expectedCost' => 333.0,
94 CarrierOptionsDummyFactory::getProductValuePricingCarrier(),
95 'totalProductValue' => 300.0,
96 'cartWeightKg' => 25.0,
97 'isCouponApplied' => false,
98 ],
99 [
100 'expectedCost' => null,
101 CarrierOptionsDummyFactory::getProductValuePricingCarrier(),
102 'totalProductValue' => 400.0,
103 'cartWeightKg' => 31.0,
104 'isCouponApplied' => false,
105 ],
106 'free shipping threshold must be reached' => [
107 'expectedCost' => 0.0,
108 CarrierOptionsDummyFactory::getDefaultCarrier(),
109 'totalProductValue' => 15000.0,
110 'cartWeightKg' => 25.0,
111 'isCouponApplied' => false,
112 ],
113 'free shipping threshold must not be reached' => [
114 'expectedCost' => 33.0,
115 CarrierOptionsDummyFactory::getNoFreeShippingLimitCarrier(),
116 'totalProductValue' => 15000.0,
117 'cartWeightKg' => 25.0,
118 'isCouponApplied' => false,
119 ],
120 'free shipping coupon must affect cost' => [
121 'expectedCost' => 0.0,
122 CarrierOptionsDummyFactory::getDefaultCarrier(),
123 'totalProductValue' => 100.0,
124 'cartWeightKg' => 25.0,
125 'isCouponApplied' => true,
126 ],
127 'free shipping coupon must not affect cost' => [
128 'expectedCost' => 33.0,
129 CarrierOptionsDummyFactory::getNoCouponCarrier(),
130 'totalProductValue' => 100.0,
131 'cartWeightKg' => 25.0,
132 'isCouponApplied' => true,
133 ],
134 ];
135 }
136
137 /**
138 * @dataProvider calculationDataProvider
139 */
140 public function testGetShippingRateCost( ?float $expectedCost, Options $carrierOptions, float $totalProductValue, float $cartWeightKg, bool $isCouponApplied ): void {
141 $this->createRateCalculatorMock();
142
143 $cost = $this->rateCalculator->getShippingRateCost(
144 $carrierOptions,
145 $totalProductValue,
146 $totalProductValue,
147 $cartWeightKg,
148 $isCouponApplied
149 );
150
151 self::assertEquals( $expectedCost, $cost );
152 }
153
154 public function testGetCODSurchargeDefault(): void {
155 $this->createRateCalculatorMock();
156
157 $carrierOptions = [];
158 $cartPrice = 100.0;
159
160 $surcharge = $this->rateCalculator->getCODSurcharge( $carrierOptions, $cartPrice );
161
162 self::assertEquals( 0.0, $surcharge );
163 }
164
165 public function testGetCODSurchargeWithDefault(): void {
166 $this->createRateCalculatorMock();
167
168 $carrierOptions = [ 'default_COD_surcharge' => 15.0 ];
169 $cartPrice = 100.0;
170
171 $surcharge = $this->rateCalculator->getCODSurcharge( $carrierOptions, $cartPrice );
172
173 self::assertEquals( 15.0, $surcharge );
174 }
175
176 public function testGetCODSurchargeWithSurchargeLimits(): void {
177 $this->createRateCalculatorMock();
178
179 $carrierOptions = [
180 'surcharge_limits' => [
181 [
182 'order_price' => 50.0,
183 'surcharge' => 10.0,
184 ],
185 [
186 'order_price' => 150.0,
187 'surcharge' => 5.0,
188 ],
189 ],
190 'default_COD_surcharge' => 15.0,
191 ];
192 $cartPrice = 100.0;
193
194 $surcharge = $this->rateCalculator->getCODSurcharge( $carrierOptions, $cartPrice );
195
196 self::assertEquals( 5.0, $surcharge );
197 }
198
199 public function testGetCODSurchargeBelowSurchargeLimits(): void {
200 $this->createRateCalculatorMock();
201
202 $carrierOptions = [
203 'surcharge_limits' => [
204 [
205 'order_price' => 50.0,
206 'surcharge' => 10.45,
207 ],
208 [
209 'order_price' => 150.0,
210 'surcharge' => 5.0,
211 ],
212 ],
213 'default_COD_surcharge' => 15.0,
214 ];
215 $cartPrice = 40.0;
216
217 $surcharge = $this->rateCalculator->getCODSurcharge( $carrierOptions, $cartPrice );
218
219 self::assertEquals( 10.45, $surcharge );
220 }
221
222 public function testIsFreeShippingCouponAppliedReturnsFalseWhenCartOrOrderIsNull(): void {
223 $this->createRateCalculatorMock();
224
225 $isFreeShippingCouponApplied = $this->rateCalculator->isFreeShippingCouponApplied( null );
226
227 self::assertFalse( $isFreeShippingCouponApplied, 'Expected isFreeShippingCouponApplied to return false when $cartOrOrder is null.' );
228 }
229
230 public function testIsFreeShippingCouponAppliedReturnsFalseWhenNoFreeShippingCoupons(): void {
231 $this->createRateCalculatorMock();
232
233 $cartWithNoFreeShippingCoupon = $this->createMock( \WC_Cart::class );
234 $cartWithNoFreeShippingCoupon->method( 'get_coupons' )->willReturn( [] );
235 $isFreeShippingCouponApplied = $this->rateCalculator->isFreeShippingCouponApplied( $cartWithNoFreeShippingCoupon );
236
237 self::assertFalse( $isFreeShippingCouponApplied, 'Expected isFreeShippingCouponApplied to return false when $cartOrOrder does not have any free shipping coupons.' );
238 }
239
240 public function testIsFreeShippingCouponAppliedReturnsTrueWhenFreeShippingCouponExists(): void {
241 $this->createRateCalculatorMock();
242
243 $couponWithFreeShipping = $this->createMock( \WC_Coupon::class );
244 $couponWithFreeShipping->method( 'get_free_shipping' )->willReturn( true );
245 $orderWithFreeShippingCoupon = $this->createMock( \WC_Order::class );
246 $orderWithFreeShippingCoupon->method( 'get_coupons' )->willReturn( [ $couponWithFreeShipping ] );
247 $isFreeShippingCouponApplied = $this->rateCalculator->isFreeShippingCouponApplied( $orderWithFreeShippingCoupon );
248
249 self::assertTrue( $isFreeShippingCouponApplied, 'Expected isFreeShippingCouponApplied to return true when $cartOrOrder has a free shipping coupon.' );
250 }
251 }
252