| 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\PacketaPickupPointsConfig; |
| 10 |
use Packetery\Module\Checkout\CheckoutService; |
| 11 |
use Packetery\Module\Framework\WcAdapter; |
| 12 |
use Packetery\Module\Options\OptionsProvider; |
| 13 |
use Packetery\Module\ShippingMethod; |
| 14 |
use Packetery\Nette\Http\Request; |
| 15 |
use PHPUnit\Framework\MockObject\MockObject; |
| 16 |
use PHPUnit\Framework\TestCase; |
| 17 |
use WC_Shipping_Rate; |
| 18 |
|
| 19 |
class CheckoutServiceTest extends TestCase { |
| 20 |
|
| 21 |
const SHIPPING_RATE_INTERNAL_PICKUP_POINTS = 'packetery_carrier_zpointcz'; |
| 22 |
|
| 23 |
private WcAdapter|MockObject $wcAdapter; |
| 24 |
private Carrier\Repository|MockObject $carrierRepository; |
| 25 |
private Carrier\EntityRepository|MockObject $carrierEntityRepository; |
| 26 |
private CarDeliveryConfig|MockObject $carDeliveryConfig; |
| 27 |
private OptionsProvider|MockObject $provider; |
| 28 |
private Request|MockObject $httpRequest; |
| 29 |
private PacketaPickupPointsConfig|MockObject $pickupPointsConfig; |
| 30 |
private CheckoutService $checkoutService; |
| 31 |
|
| 32 |
private function createCheckoutServiceMock(): void { |
| 33 |
$this->wcAdapter = $this->createMock( WcAdapter::class ); |
| 34 |
$this->carrierRepository = $this->createMock( Carrier\Repository::class ); |
| 35 |
$this->carrierEntityRepository = $this->createMock( Carrier\EntityRepository::class ); |
| 36 |
$this->carDeliveryConfig = $this->createMock( CarDeliveryConfig::class ); |
| 37 |
$this->provider = $this->createMock( OptionsProvider::class ); |
| 38 |
$this->httpRequest = $this->createMock( Request::class ); |
| 39 |
$this->pickupPointsConfig = $this->createMock( PacketaPickupPointsConfig::class ); |
| 40 |
|
| 41 |
$this->checkoutService = new CheckoutService( |
| 42 |
$this->wcAdapter, |
| 43 |
$this->httpRequest, |
| 44 |
$this->carDeliveryConfig, |
| 45 |
$this->carrierRepository, |
| 46 |
$this->carrierEntityRepository, |
| 47 |
$this->pickupPointsConfig, |
| 48 |
$this->provider, |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
public function testCalculateShippingAndGetOptionIdReturnsEmptyStringWhenNoShippingRatesExists(): void { |
| 53 |
$this->createCheckoutServiceMock(); |
| 54 |
|
| 55 |
$this->wcAdapter->method( 'cartCalculateShipping' )->willReturn( [] ); |
| 56 |
$this->assertEquals( '', $this->checkoutService->calculateShippingAndGetOptionId() ); |
| 57 |
} |
| 58 |
|
| 59 |
public function testCalculateShippingAndGetOptionIdReturnsShippingRateIdStrippedOfPrefix(): void { |
| 60 |
$this->createCheckoutServiceMock(); |
| 61 |
|
| 62 |
$shippingRateId = self::SHIPPING_RATE_INTERNAL_PICKUP_POINTS; |
| 63 |
$shippingRateFullId = ShippingMethod::PACKETERY_METHOD_ID . ':' . $shippingRateId; |
| 64 |
$mockedShippingRate = $this->createMock( WC_Shipping_Rate::class ); |
| 65 |
$mockedShippingRate->method( 'get_id' )->willReturn( $shippingRateFullId ); |
| 66 |
$this->wcAdapter->method( 'cartCalculateShipping' )->willReturn( [ $mockedShippingRate ] ); |
| 67 |
|
| 68 |
$this->assertEquals( $shippingRateId, $this->checkoutService->calculateShippingAndGetOptionId() ); |
| 69 |
} |
| 70 |
|
| 71 |
public function testGetChosenMethodWhenPostShippingMethodIsNull(): void { |
| 72 |
$this->createCheckoutServiceMock(); |
| 73 |
|
| 74 |
$this->httpRequest->method( 'getPost' )->with( 'shipping_method' )->willReturn( null ); |
| 75 |
|
| 76 |
$this->assertEquals( '', $this->checkoutService->resolveChosenMethod() ); |
| 77 |
} |
| 78 |
|
| 79 |
public function testGetChosenMethodWhenPostShippingMethodIsNotNull(): void { |
| 80 |
$this->createCheckoutServiceMock(); |
| 81 |
|
| 82 |
$shippingRateId = self::SHIPPING_RATE_INTERNAL_PICKUP_POINTS; |
| 83 |
$shippingRateFullId = ShippingMethod::PACKETERY_METHOD_ID . ':' . $shippingRateId; |
| 84 |
$this->httpRequest->method( 'getPost' )->with( 'shipping_method' )->willReturn( [ $shippingRateFullId ] ); |
| 85 |
|
| 86 |
$this->assertEquals( $shippingRateId, $this->checkoutService->resolveChosenMethod() ); |
| 87 |
} |
| 88 |
|
| 89 |
public function testGetShippingMethodOptionIdWithValueNotContainingPrefix(): void { |
| 90 |
$this->createCheckoutServiceMock(); |
| 91 |
$chosenMethod = 'dummyRate'; |
| 92 |
$this->assertEquals( 'dummyRate', $this->checkoutService->getShippingMethodOptionId( $chosenMethod ) ); |
| 93 |
} |
| 94 |
|
| 95 |
public function testGetShippingMethodOptionIdWithValueContainingPrefix(): void { |
| 96 |
$this->createCheckoutServiceMock(); |
| 97 |
$shippingRateId = self::SHIPPING_RATE_INTERNAL_PICKUP_POINTS; |
| 98 |
$shippingRateFullId = ShippingMethod::PACKETERY_METHOD_ID . ':' . $shippingRateId; |
| 99 |
$this->assertEquals( $shippingRateId, $this->checkoutService->getShippingMethodOptionId( $shippingRateFullId ) ); |
| 100 |
} |
| 101 |
|
| 102 |
public function testGetShippingMethodOptionIdWithEmptyValue(): void { |
| 103 |
$this->createCheckoutServiceMock(); |
| 104 |
$this->assertEquals( '', $this->checkoutService->getShippingMethodOptionId( '' ) ); |
| 105 |
} |
| 106 |
|
| 107 |
public function testIsPacketeryShippingMethodReturnsFalseWhenGivenInvalidOptionId(): void { |
| 108 |
$this->createCheckoutServiceMock(); |
| 109 |
$invalidOptionId = 'third_party_carrier_dummy'; |
| 110 |
|
| 111 |
$this->assertFalse( $this->checkoutService->isPacketeryShippingMethod( $invalidOptionId ) ); |
| 112 |
} |
| 113 |
|
| 114 |
public function testIsPacketeryShippingMethodReturnsTrueWhenGivenValidOptionId(): void { |
| 115 |
$this->createCheckoutServiceMock(); |
| 116 |
$validOptionId = 'packetery_carrier_dummy'; |
| 117 |
|
| 118 |
$this->assertTrue( $this->checkoutService->isPacketeryShippingMethod( $validOptionId ) ); |
| 119 |
} |
| 120 |
|
| 121 |
public function testGetCarrierIdFromShippingMethodNonPacketery(): void { |
| 122 |
$this->createCheckoutServiceMock(); |
| 123 |
|
| 124 |
$this->assertNull( $this->checkoutService->getCarrierIdFromShippingMethod( 'dummy_carrier' ) ); |
| 125 |
} |
| 126 |
|
| 127 |
public function testGetCarrierIdFromShippingMethodPacketery(): void { |
| 128 |
$this->createCheckoutServiceMock(); |
| 129 |
|
| 130 |
$shippingMethod = self::SHIPPING_RATE_INTERNAL_PICKUP_POINTS; |
| 131 |
$this->assertEquals( 'zpointcz', $this->checkoutService->getCarrierIdFromShippingMethod( $shippingMethod ) ); |
| 132 |
} |
| 133 |
|
| 134 |
public function testGetCarrierIdFromShippingMethodEmpty(): void { |
| 135 |
$this->createCheckoutServiceMock(); |
| 136 |
|
| 137 |
$this->assertNull( $this->checkoutService->getCarrierIdFromShippingMethod( '' ) ); |
| 138 |
} |
| 139 |
|
| 140 |
public function testGetCarrierIdFromPacketeryShippingMethod(): void { |
| 141 |
$this->createCheckoutServiceMock(); |
| 142 |
$expected = 'zpointcz'; |
| 143 |
$this->assertEquals( |
| 144 |
$expected, |
| 145 |
$this->checkoutService->getCarrierIdFromPacketeryShippingMethod( |
| 146 |
self::SHIPPING_RATE_INTERNAL_PICKUP_POINTS |
| 147 |
) |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
public function testIsPickupPointOrderWhenNoMethod(): void { |
| 152 |
$this->createCheckoutServiceMock(); |
| 153 |
|
| 154 |
$this->httpRequest->method( 'getPost' )->willReturn( null ); |
| 155 |
$this->wcAdapter->method( 'cartCalculateShipping' )->willReturn( [] ); |
| 156 |
|
| 157 |
$this->assertFalse( $this->checkoutService->isPickupPointOrder() ); |
| 158 |
} |
| 159 |
|
| 160 |
public function testIsPickupPointOrderWhenItIsNot(): void { |
| 161 |
$this->createCheckoutServiceMock(); |
| 162 |
$this->httpRequest->method( 'getPost' )->willReturn( [ 'dummy_method' ] ); |
| 163 |
$this->pickupPointsConfig->method( 'isInternalPickupPointCarrier' )->willReturn( false ); |
| 164 |
$this->carrierRepository->method( 'hasPickupPoints' )->willReturn( false ); |
| 165 |
|
| 166 |
$this->assertFalse( $this->checkoutService->isPickupPointOrder() ); |
| 167 |
} |
| 168 |
|
| 169 |
public function testIsPickupPointOrderWhenItIs(): void { |
| 170 |
$this->createCheckoutServiceMock(); |
| 171 |
$this->httpRequest->method( 'getPost' )->willReturn( [ 'packetery_carrier_3060' ] ); |
| 172 |
$this->pickupPointsConfig->method( 'isInternalPickupPointCarrier' )->willReturn( false ); |
| 173 |
$this->carrierRepository->method( 'hasPickupPoints' )->willReturn( true ); |
| 174 |
|
| 175 |
$this->assertTrue( $this->checkoutService->isPickupPointOrder() ); |
| 176 |
} |
| 177 |
|
| 178 |
public function testIsPickupPointOrderWhenItIsInternal(): void { |
| 179 |
$this->createCheckoutServiceMock(); |
| 180 |
$this->httpRequest->method( 'getPost' )->willReturn( [ self::SHIPPING_RATE_INTERNAL_PICKUP_POINTS ] ); |
| 181 |
$this->pickupPointsConfig->method( 'isInternalPickupPointCarrier' )->willReturn( true ); |
| 182 |
$this->carrierRepository->method( 'hasPickupPoints' )->willReturn( false ); |
| 183 |
|
| 184 |
$this->assertTrue( $this->checkoutService->isPickupPointOrder() ); |
| 185 |
} |
| 186 |
|
| 187 |
public function testIsHomeDeliveryOrderWithHomeDelivery(): void { |
| 188 |
$this->createCheckoutServiceMock(); |
| 189 |
|
| 190 |
$this->httpRequest->method( 'getPost' )->willReturn( [ 'packetery_carrier_106' ] ); |
| 191 |
$this->carrierEntityRepository->method( 'isHomeDeliveryCarrier' )->willReturn( true ); |
| 192 |
|
| 193 |
$this->assertTrue( $this->checkoutService->isHomeDeliveryOrder() ); |
| 194 |
} |
| 195 |
|
| 196 |
public function testIsHomeDeliveryOrderWithNonHomeDelivery(): void { |
| 197 |
$this->createCheckoutServiceMock(); |
| 198 |
|
| 199 |
$this->httpRequest->method( 'getPost' )->willReturn( [ self::SHIPPING_RATE_INTERNAL_PICKUP_POINTS ] ); |
| 200 |
$this->carrierEntityRepository->method( 'isHomeDeliveryCarrier' )->willReturn( false ); |
| 201 |
|
| 202 |
$this->assertFalse( $this->checkoutService->isHomeDeliveryOrder() ); |
| 203 |
} |
| 204 |
|
| 205 |
public function testIsHomeDeliveryOrderWithNoChosenMethod(): void { |
| 206 |
$this->createCheckoutServiceMock(); |
| 207 |
|
| 208 |
$this->httpRequest->method( 'getPost' )->willReturn( null ); |
| 209 |
|
| 210 |
$this->assertFalse( $this->checkoutService->isHomeDeliveryOrder() ); |
| 211 |
} |
| 212 |
|
| 213 |
public function testIsHomeDeliveryOrderWithEmptyChosenMethod(): void { |
| 214 |
$this->createCheckoutServiceMock(); |
| 215 |
|
| 216 |
$this->httpRequest->method( 'getPost' )->willReturn( [ '' ] ); |
| 217 |
|
| 218 |
$this->assertFalse( $this->checkoutService->isHomeDeliveryOrder() ); |
| 219 |
} |
| 220 |
|
| 221 |
public function testIsCarDeliveryOrderWithCarDelivery(): void { |
| 222 |
$this->createCheckoutServiceMock(); |
| 223 |
|
| 224 |
$this->httpRequest->method( 'getPost' )->willReturn( [ 'packetery_carrier_25061' ] ); |
| 225 |
$this->carDeliveryConfig->method( 'isCarDeliveryCarrier' )->willReturn( true ); |
| 226 |
|
| 227 |
$this->assertTrue( $this->checkoutService->isCarDeliveryOrder() ); |
| 228 |
} |
| 229 |
|
| 230 |
public function testIsCarDeliveryOrderWithNonCarDelivery(): void { |
| 231 |
$this->createCheckoutServiceMock(); |
| 232 |
|
| 233 |
$this->httpRequest->method( 'getPost' )->willReturn( [ self::SHIPPING_RATE_INTERNAL_PICKUP_POINTS ] ); |
| 234 |
$this->carDeliveryConfig->method( 'isCarDeliveryCarrier' )->willReturn( false ); |
| 235 |
|
| 236 |
$this->assertFalse( $this->checkoutService->isCarDeliveryOrder() ); |
| 237 |
} |
| 238 |
|
| 239 |
public function testIsCarDeliveryOrderWithNoChosenMethod(): void { |
| 240 |
$this->createCheckoutServiceMock(); |
| 241 |
|
| 242 |
$this->httpRequest->method( 'getPost' )->willReturn( null ); |
| 243 |
|
| 244 |
$this->assertFalse( $this->checkoutService->isCarDeliveryOrder() ); |
| 245 |
} |
| 246 |
|
| 247 |
public function testIsCarDeliveryOrderWithEmptyChosenMethod(): void { |
| 248 |
$this->createCheckoutServiceMock(); |
| 249 |
|
| 250 |
$this->httpRequest->method( 'getPost' )->willReturn( [ '' ] ); |
| 251 |
|
| 252 |
$this->assertFalse( $this->checkoutService->isCarDeliveryOrder() ); |
| 253 |
} |
| 254 |
|
| 255 |
public function testGetCustomerCountryWhenCustomerShippingCountryIsNotNull(): void { |
| 256 |
$this->createCheckoutServiceMock(); |
| 257 |
|
| 258 |
$expectedCountry = 'cz'; |
| 259 |
$this->wcAdapter->method( 'customerGetShippingCountry' )->willReturn( strtoupper( $expectedCountry ) ); |
| 260 |
|
| 261 |
$this->assertEquals( $expectedCountry, $this->checkoutService->getCustomerCountry() ); |
| 262 |
} |
| 263 |
|
| 264 |
public function testGetCustomerCountryWhenCustomerShippingCountryIsNull(): void { |
| 265 |
$this->createCheckoutServiceMock(); |
| 266 |
|
| 267 |
$expectedCountry = 'cz'; |
| 268 |
$this->wcAdapter->method( 'customerGetShippingCountry' )->willReturn( null ); |
| 269 |
$this->wcAdapter->method( 'customerGetBillingCountry' )->willReturn( strtoupper( $expectedCountry ) ); |
| 270 |
|
| 271 |
$this->assertEquals( $expectedCountry, $this->checkoutService->getCustomerCountry() ); |
| 272 |
} |
| 273 |
|
| 274 |
public function testGetCustomerCountryWhenCustomerShippingCountryAndCustomerBillingCountryAreNull(): void { |
| 275 |
$this->createCheckoutServiceMock(); |
| 276 |
|
| 277 |
$this->wcAdapter->method( 'customerGetShippingCountry' )->willReturn( null ); |
| 278 |
$this->wcAdapter->method( 'customerGetBillingCountry' )->willReturn( null ); |
| 279 |
|
| 280 |
$this->assertEquals( '', $this->checkoutService->getCustomerCountry() ); |
| 281 |
} |
| 282 |
|
| 283 |
public function testAreBlocksUsedInCheckoutBlockDetection(): void { |
| 284 |
$this->createCheckoutServiceMock(); |
| 285 |
|
| 286 |
$this->provider->method( 'getCheckoutDetection' )->willReturn( OptionsProvider::BLOCK_CHECKOUT_DETECTION ); |
| 287 |
$this->assertTrue( $this->checkoutService->areBlocksUsedInCheckout() ); |
| 288 |
} |
| 289 |
|
| 290 |
public function testAreBlocksUsedInCheckoutClassicDetection(): void { |
| 291 |
$this->createCheckoutServiceMock(); |
| 292 |
|
| 293 |
$this->provider->method( 'getCheckoutDetection' )->willReturn( OptionsProvider::CLASSIC_CHECKOUT_DETECTION ); |
| 294 |
$this->assertFalse( $this->checkoutService->areBlocksUsedInCheckout() ); |
| 295 |
} |
| 296 |
|
| 297 |
public function testAreBlocksUsedInCheckoutAutomaticDetectionWithBlock(): void { |
| 298 |
$this->createCheckoutServiceMock(); |
| 299 |
|
| 300 |
$this->provider->method( 'getCheckoutDetection' )->willReturn( OptionsProvider::AUTOMATIC_CHECKOUT_DETECTION ); |
| 301 |
|
| 302 |
$this->wcAdapter->method( 'hasBlockInPage' )->willReturn( true ); |
| 303 |
$this->assertTrue( $this->checkoutService->areBlocksUsedInCheckout() ); |
| 304 |
} |
| 305 |
|
| 306 |
public function testAreBlocksUsedInCheckoutAutomaticDetectionWithoutBlock(): void { |
| 307 |
$this->createCheckoutServiceMock(); |
| 308 |
|
| 309 |
$this->provider->method( 'getCheckoutDetection' )->willReturn( OptionsProvider::AUTOMATIC_CHECKOUT_DETECTION ); |
| 310 |
|
| 311 |
$this->wcAdapter->method( 'hasBlockInPage' )->willReturn( false ); |
| 312 |
$this->assertFalse( $this->checkoutService->areBlocksUsedInCheckout() ); |
| 313 |
} |
| 314 |
} |
| 315 |
|