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