| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types=1 ); |
| 4 |
|
| 5 |
namespace Tests\Module\Checkout; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use Packetery\Module\Carrier; |
| 9 |
use Packetery\Module\Checkout\CartService; |
| 10 |
use Packetery\Module\Exception\ProductNotFoundException; |
| 11 |
use Packetery\Module\Framework\WcAdapter; |
| 12 |
use Packetery\Module\Framework\WpAdapter; |
| 13 |
use Packetery\Module\Options\OptionsProvider; |
| 14 |
use Packetery\Module\Product; |
| 15 |
use Packetery\Module\Product\ProductEntityFactory; |
| 16 |
use Packetery\Module\ProductCategory; |
| 17 |
use Packetery\Module\ProductCategory\ProductCategoryEntityFactory; |
| 18 |
use PHPUnit\Framework\MockObject\MockObject; |
| 19 |
use PHPUnit\Framework\TestCase; |
| 20 |
use WC_Product; |
| 21 |
|
| 22 |
class CartServiceTest extends TestCase { |
| 23 |
private CartService|MockObject $cartService; |
| 24 |
private MockObject|WpAdapter $wpAdapter; |
| 25 |
private ProductEntityFactory|MockObject $productEntityFactory; |
| 26 |
private MockObject|WcAdapter $wcAdapter; |
| 27 |
private MockObject|ProductCategoryEntityFactory $productCategoryEntityFactory; |
| 28 |
private MockObject|OptionsProvider $optionsProvider; |
| 29 |
|
| 30 |
private function createCartServiceMock(): void { |
| 31 |
$this->wcAdapter = $this->createMock( WcAdapter::class ); |
| 32 |
$this->wpAdapter = $this->createMock( WpAdapter::class ); |
| 33 |
$this->productEntityFactory = $this->createMock( ProductEntityFactory::class ); |
| 34 |
$this->productCategoryEntityFactory = $this->createMock( ProductCategoryEntityFactory::class ); |
| 35 |
$this->optionsProvider = $this->createMock( OptionsProvider::class ); |
| 36 |
|
| 37 |
$this->cartService = new CartService( |
| 38 |
$this->wpAdapter, |
| 39 |
$this->wcAdapter, |
| 40 |
$this->productEntityFactory, |
| 41 |
$this->productCategoryEntityFactory, |
| 42 |
$this->optionsProvider |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
public function testIsAgeVerificationRequiredWithoutWpLoaded(): void { |
| 47 |
$this->createCartServiceMock(); |
| 48 |
$this->wpAdapter->method( 'didAction' )->willReturn( 0 ); |
| 49 |
$this->assertFalse( $this->cartService->isAgeVerificationRequired() ); |
| 50 |
} |
| 51 |
|
| 52 |
public function testIsAgeVerificationRequiredWithNonPhysicalProduct(): void { |
| 53 |
$this->createCartServiceMock(); |
| 54 |
|
| 55 |
$this->wpAdapter->method( 'didAction' )->willReturn( 1 ); |
| 56 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 57 |
[ |
| 58 |
[ 'product_id' => 1 ], |
| 59 |
] |
| 60 |
); |
| 61 |
|
| 62 |
$productMock = $this->createMock( Product\Entity::class ); |
| 63 |
$productMock->method( 'isPhysical' )->willReturn( false ); |
| 64 |
|
| 65 |
$this->productEntityFactory->method( 'fromPostId' )->willReturn( $productMock ); |
| 66 |
$this->assertFalse( $this->cartService->isAgeVerificationRequired() ); |
| 67 |
} |
| 68 |
|
| 69 |
public function testIsAgeVerificationRequiredWithVerification(): void { |
| 70 |
$this->createCartServiceMock(); |
| 71 |
|
| 72 |
$this->wpAdapter->method( 'didAction' )->willReturn( 1 ); |
| 73 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 74 |
[ |
| 75 |
[ 'product_id' => 1 ], |
| 76 |
] |
| 77 |
); |
| 78 |
|
| 79 |
$productMock = $this->createMock( Product\Entity::class ); |
| 80 |
$productMock->method( 'isPhysical' )->willReturn( true ); |
| 81 |
$productMock->method( 'isAgeVerificationRequired' )->willReturn( true ); |
| 82 |
|
| 83 |
$this->productEntityFactory->method( 'fromPostId' )->willReturn( $productMock ); |
| 84 |
$this->assertTrue( $this->cartService->isAgeVerificationRequired() ); |
| 85 |
} |
| 86 |
|
| 87 |
public function testIsAgeVerificationRequiredWithoutVerification(): void { |
| 88 |
$this->createCartServiceMock(); |
| 89 |
|
| 90 |
$this->wpAdapter->method( 'didAction' )->willReturn( 1 ); |
| 91 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 92 |
[ |
| 93 |
[ 'product_id' => 1 ], |
| 94 |
] |
| 95 |
); |
| 96 |
|
| 97 |
$productMock = $this->createMock( Product\Entity::class ); |
| 98 |
$productMock->method( 'isPhysical' )->willReturn( true ); |
| 99 |
$productMock->method( 'isAgeVerificationRequired' )->willReturn( false ); |
| 100 |
|
| 101 |
$this->productEntityFactory->method( 'fromPostId' )->willReturn( $productMock ); |
| 102 |
$this->assertFalse( $this->cartService->isAgeVerificationRequired() ); |
| 103 |
} |
| 104 |
|
| 105 |
public function testGetCartWeightKgWithEmptyCart(): void { |
| 106 |
$this->createCartServiceMock(); |
| 107 |
$this->wpAdapter->method( 'didAction' )->willReturn( 1 ); |
| 108 |
$this->wcAdapter->method( 'cartGetCartContentsWeight' )->willReturn( 0.0 ); |
| 109 |
$this->wcAdapter->method( 'getWeight' )->willReturn( 0.0 ); |
| 110 |
$this->optionsProvider->method( 'getPackagingWeight' )->willReturn( 0.0 ); |
| 111 |
$this->assertSame( 0.0, $this->cartService->getCartWeightKg() ); |
| 112 |
} |
| 113 |
|
| 114 |
public function testGetCartWeightKgWithProductsInCart(): void { |
| 115 |
$this->createCartServiceMock(); |
| 116 |
$this->wpAdapter->method( 'didAction' )->willReturn( 1 ); |
| 117 |
$this->wcAdapter->method( 'cartGetCartContentsWeight' )->willReturn( 5.0 ); |
| 118 |
$this->wcAdapter->method( 'getWeight' )->willReturn( 5.0 ); |
| 119 |
$this->optionsProvider->method( 'getPackagingWeight' )->willReturn( 1.0 ); |
| 120 |
$this->assertSame( 6.0, $this->cartService->getCartWeightKg() ); |
| 121 |
} |
| 122 |
|
| 123 |
public function testGetCartWeightKgWithoutWpLoaded(): void { |
| 124 |
$this->createCartServiceMock(); |
| 125 |
$this->wpAdapter->method( 'didAction' )->willReturn( 0 ); |
| 126 |
$this->assertSame( 0.0, $this->cartService->getCartWeightKg() ); |
| 127 |
} |
| 128 |
|
| 129 |
public function testGetCartWeightKgWithExceptionOnGetWeight(): void { |
| 130 |
$this->createCartServiceMock(); |
| 131 |
$this->wpAdapter->method( 'didAction' )->willReturn( 1 ); |
| 132 |
$this->wcAdapter->method( 'cartGetCartContentsWeight' )->willReturn( 5.0 ); |
| 133 |
$this->wcAdapter->method( 'getWeight' )->will( $this->throwException( new Exception() ) ); |
| 134 |
$this->expectException( Exception::class ); |
| 135 |
$this->cartService->getCartWeightKg(); |
| 136 |
} |
| 137 |
|
| 138 |
public function testGetTotalCartProductValue(): void { |
| 139 |
$this->createCartServiceMock(); |
| 140 |
$cartItem1Price = 10.0; |
| 141 |
$cartItem1Quantity = 2.0; |
| 142 |
$cartItem2Price = 20.0; |
| 143 |
$cartItem2Quantity = 1.0; |
| 144 |
$item1DataMock = $this->createMock( WC_Product::class ); |
| 145 |
$item1DataMock->method( 'get_price' )->willReturn( $cartItem1Price ); |
| 146 |
$item2DataMock = $this->createMock( WC_Product::class ); |
| 147 |
$item2DataMock->method( 'get_price' )->willReturn( $cartItem2Price ); |
| 148 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 149 |
[ |
| 150 |
[ |
| 151 |
'data' => $item1DataMock, |
| 152 |
'quantity' => $cartItem1Quantity, |
| 153 |
], |
| 154 |
[ |
| 155 |
'data' => $item2DataMock, |
| 156 |
'quantity' => $cartItem2Quantity, |
| 157 |
], |
| 158 |
] |
| 159 |
); |
| 160 |
$totalProductPrice = ( $cartItem1Price * $cartItem1Quantity ) + ( $cartItem2Price * $cartItem2Quantity ); |
| 161 |
$this->assertSame( $totalProductPrice, $this->cartService->getTotalCartProductValue() ); |
| 162 |
} |
| 163 |
|
| 164 |
public function testGetDisallowedShippingRateIdsWithPhysicalProduct(): void { |
| 165 |
$this->createCartServiceMock(); |
| 166 |
|
| 167 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 168 |
[ |
| 169 |
[ 'product_id' => 1 ], |
| 170 |
] |
| 171 |
); |
| 172 |
|
| 173 |
$productMock = $this->createMock( Product\Entity::class ); |
| 174 |
$productMock->method( 'isPhysical' )->willReturn( true ); |
| 175 |
$productMock->method( 'getDisallowedShippingRateIds' )->willReturn( [ 1, 2, 3 ] ); |
| 176 |
|
| 177 |
$this->productEntityFactory->method( 'fromPostId' )->willReturn( $productMock ); |
| 178 |
$this->assertEquals( [ 1, 2, 3 ], $this->cartService->getDisallowedShippingRateIds() ); |
| 179 |
} |
| 180 |
|
| 181 |
public function testGetDisallowedShippingRateIdsWithNonPhysicalProduct(): void { |
| 182 |
$this->createCartServiceMock(); |
| 183 |
|
| 184 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 185 |
[ |
| 186 |
[ 'product_id' => 1 ], |
| 187 |
] |
| 188 |
); |
| 189 |
|
| 190 |
$productMock = $this->createMock( Product\Entity::class ); |
| 191 |
$productMock->method( 'isPhysical' )->willReturn( false ); |
| 192 |
$productMock->method( 'getDisallowedShippingRateIds' )->willReturn( [ 1, 2, 3 ] ); |
| 193 |
|
| 194 |
$this->productEntityFactory->method( 'fromPostId' )->willReturn( $productMock ); |
| 195 |
$this->assertEquals( [], $this->cartService->getDisallowedShippingRateIds() ); |
| 196 |
} |
| 197 |
|
| 198 |
public function testGetDisallowedShippingRateIdsCombined(): void { |
| 199 |
$this->createCartServiceMock(); |
| 200 |
|
| 201 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 202 |
[ |
| 203 |
[ 'product_id' => 1 ], |
| 204 |
[ 'product_id' => 2 ], |
| 205 |
] |
| 206 |
); |
| 207 |
|
| 208 |
$productMockPhysical = $this->createMock( Product\Entity::class ); |
| 209 |
$productMockPhysical->method( 'isPhysical' )->willReturn( true ); |
| 210 |
$productMockPhysical->method( 'getDisallowedShippingRateIds' )->willReturn( [ 1, 2 ] ); |
| 211 |
|
| 212 |
$productMockNonPhysical = $this->createMock( Product\Entity::class ); |
| 213 |
$productMockNonPhysical->method( 'isPhysical' )->willReturn( false ); |
| 214 |
$productMockNonPhysical->method( 'getDisallowedShippingRateIds' )->willReturn( [ 2, 3 ] ); |
| 215 |
|
| 216 |
$this->productEntityFactory->method( 'fromPostId' ) |
| 217 |
->willReturnCallback( |
| 218 |
function ( $id ) use ( $productMockPhysical, $productMockNonPhysical ) { |
| 219 |
if ( $id === 1 ) { |
| 220 |
return $productMockPhysical; |
| 221 |
} |
| 222 |
if ( $id === 2 ) { |
| 223 |
return $productMockNonPhysical; |
| 224 |
} |
| 225 |
|
| 226 |
return null; |
| 227 |
} |
| 228 |
); |
| 229 |
$this->assertEquals( [ 1, 2 ], $this->cartService->getDisallowedShippingRateIds() ); |
| 230 |
} |
| 231 |
|
| 232 |
public function testGetTaxClassWithMaxRateWithInvalidProduct(): void { |
| 233 |
$this->createCartServiceMock(); |
| 234 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 235 |
[ |
| 236 |
[ 'product_id' => 1 ], |
| 237 |
] |
| 238 |
); |
| 239 |
$product = null; |
| 240 |
$this->wcAdapter->expects( $this->once() ) |
| 241 |
->method( 'productFactoryGetProduct' ) |
| 242 |
->with( $this->equalTo( 1 ) ) |
| 243 |
->willReturn( $product ); |
| 244 |
|
| 245 |
$this->expectException( ProductNotFoundException::class ); |
| 246 |
$this->cartService->getTaxClassWithMaxRate(); |
| 247 |
} |
| 248 |
|
| 249 |
public function testGetTaxClassWithMaxRateWithNoTaxableProduct(): void { |
| 250 |
$this->createCartServiceMock(); |
| 251 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 252 |
[ |
| 253 |
[ 'product_id' => 1 ], |
| 254 |
] |
| 255 |
); |
| 256 |
$product = $this->createMock( WC_Product::class ); |
| 257 |
$product->method( 'is_taxable' )->willReturn( false ); |
| 258 |
$this->wcAdapter->expects( $this->once() ) |
| 259 |
->method( 'productFactoryGetProduct' ) |
| 260 |
->with( $this->equalTo( 1 ) ) |
| 261 |
->willReturn( $product ); |
| 262 |
$this->assertNull( $this->cartService->getTaxClassWithMaxRate() ); |
| 263 |
} |
| 264 |
|
| 265 |
public function testGetTaxClassWithMaxRateWithSingleTaxableProduct(): void { |
| 266 |
$this->createCartServiceMock(); |
| 267 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 268 |
[ |
| 269 |
[ 'product_id' => 1 ], |
| 270 |
] |
| 271 |
); |
| 272 |
$product = $this->createMock( WC_Product::class ); |
| 273 |
$product->method( 'is_taxable' )->willReturn( true ); |
| 274 |
$product->method( 'get_tax_class' )->willReturn( 'standard' ); |
| 275 |
$this->wcAdapter->expects( $this->once() ) |
| 276 |
->method( 'productFactoryGetProduct' ) |
| 277 |
->with( $this->equalTo( 1 ) ) |
| 278 |
->willReturn( $product ); |
| 279 |
$this->assertEquals( 'standard', $this->cartService->getTaxClassWithMaxRate() ); |
| 280 |
} |
| 281 |
|
| 282 |
public function testGetTaxClassWithMaxRateWithMultipleTaxClasses(): void { |
| 283 |
$this->createCartServiceMock(); |
| 284 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 285 |
[ |
| 286 |
[ 'product_id' => 1 ], |
| 287 |
[ 'product_id' => 2 ], |
| 288 |
] |
| 289 |
); |
| 290 |
$product1 = $this->createMock( WC_Product::class ); |
| 291 |
$product1->method( 'is_taxable' )->willReturn( true ); |
| 292 |
$product1->method( 'get_tax_class' )->willReturn( 'reduced' ); |
| 293 |
$product2 = $this->createMock( WC_Product::class ); |
| 294 |
$product2->method( 'is_taxable' )->willReturn( true ); |
| 295 |
$product2->method( 'get_tax_class' )->willReturn( 'standard' ); |
| 296 |
|
| 297 |
$index = 0; |
| 298 |
$this->wcAdapter->expects( $this->exactly( 2 ) ) |
| 299 |
->method( 'productFactoryGetProduct' ) |
| 300 |
->with( |
| 301 |
$this->callback( |
| 302 |
function ( $productId ) { |
| 303 |
return in_array( $productId, [ 1, 2 ], true ); |
| 304 |
} |
| 305 |
) |
| 306 |
) |
| 307 |
->willReturnCallback( |
| 308 |
function ( $productId ) use ( $product1, $product2, &$index ) { |
| 309 |
$index++; |
| 310 |
if ( $productId === 1 && $index === 1 ) { |
| 311 |
return $product1; |
| 312 |
} |
| 313 |
|
| 314 |
if ( $productId === 2 && $index === 2 ) { |
| 315 |
return $product2; |
| 316 |
} |
| 317 |
|
| 318 |
return null; |
| 319 |
} |
| 320 |
); |
| 321 |
|
| 322 |
$this->wcAdapter->method( 'taxGetRates' ) |
| 323 |
->willReturnOnConsecutiveCalls( |
| 324 |
[ |
| 325 |
'1' => [ |
| 326 |
'rate' => '10.00', |
| 327 |
'label' => 'Reduced Rate', |
| 328 |
'shipping' => 'no', |
| 329 |
'compound' => 'no', |
| 330 |
], |
| 331 |
], |
| 332 |
[ |
| 333 |
'1' => [ |
| 334 |
'rate' => '20.00', |
| 335 |
'label' => 'Standard Rate', |
| 336 |
'shipping' => 'no', |
| 337 |
'compound' => 'no', |
| 338 |
], |
| 339 |
] |
| 340 |
); |
| 341 |
$this->assertEquals( 'standard', $this->cartService->getTaxClassWithMaxRate() ); |
| 342 |
} |
| 343 |
|
| 344 |
public function testGetTaxClassWithMaxRateWithMultipleTaxClassesNoRates(): void { |
| 345 |
$this->createCartServiceMock(); |
| 346 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 347 |
[ |
| 348 |
[ 'product_id' => 1 ], |
| 349 |
[ 'product_id' => 2 ], |
| 350 |
] |
| 351 |
); |
| 352 |
$product1 = $this->createMock( WC_Product::class ); |
| 353 |
$product1->method( 'is_taxable' )->willReturn( true ); |
| 354 |
$product1->method( 'get_tax_class' )->willReturn( 'reduced' ); |
| 355 |
$product2 = $this->createMock( WC_Product::class ); |
| 356 |
$product2->method( 'is_taxable' )->willReturn( true ); |
| 357 |
$product2->method( 'get_tax_class' )->willReturn( 'standard' ); |
| 358 |
|
| 359 |
$index = 0; |
| 360 |
$this->wcAdapter->expects( $this->exactly( 2 ) ) |
| 361 |
->method( 'productFactoryGetProduct' ) |
| 362 |
->with( |
| 363 |
$this->callback( |
| 364 |
function ( $productId ) { |
| 365 |
return in_array( $productId, [ 1, 2 ], true ); |
| 366 |
} |
| 367 |
) |
| 368 |
) |
| 369 |
->willReturnCallback( |
| 370 |
function ( $productId ) use ( $product1, $product2, &$index ) { |
| 371 |
$index++; |
| 372 |
if ( $productId === 1 && $index === 1 ) { |
| 373 |
return $product1; |
| 374 |
} |
| 375 |
|
| 376 |
if ( $productId === 2 && $index === 2 ) { |
| 377 |
return $product2; |
| 378 |
} |
| 379 |
|
| 380 |
return null; |
| 381 |
} |
| 382 |
); |
| 383 |
|
| 384 |
$this->wcAdapter->method( 'taxGetRates' )->willReturn( [] ); |
| 385 |
$this->assertNull( $this->cartService->getTaxClassWithMaxRate() ); |
| 386 |
} |
| 387 |
|
| 388 |
public function testGetCartContentsTotalIncludingTaxWithNoTaxes(): void { |
| 389 |
$this->createCartServiceMock(); |
| 390 |
|
| 391 |
$this->wcAdapter->method( 'cartGetCartContentsTotal' )->willReturn( 15.00 ); |
| 392 |
$this->wcAdapter->method( 'cartGetCartContentsTax' )->willReturn( 0.00 ); |
| 393 |
|
| 394 |
$this->assertSame( 15.00, $this->cartService->getCartContentsTotalIncludingTax() ); |
| 395 |
} |
| 396 |
|
| 397 |
public function testGetCartContentsTotalIncludingTaxWithTaxes(): void { |
| 398 |
$this->createCartServiceMock(); |
| 399 |
|
| 400 |
$this->wcAdapter->method( 'cartGetCartContentsTotal' )->willReturn( 15.00 ); |
| 401 |
$this->wcAdapter->method( 'cartGetCartContentsTax' )->willReturn( 5.00 ); |
| 402 |
|
| 403 |
$this->assertSame( 20.00, $this->cartService->getCartContentsTotalIncludingTax() ); |
| 404 |
} |
| 405 |
|
| 406 |
public function testIsShippingRateRestrictedByProductsCategoryWithInvalidProduct(): void { |
| 407 |
$this->createCartServiceMock(); |
| 408 |
|
| 409 |
$this->wcAdapter->method( 'productFactoryGetProduct' )->willReturn( null ); |
| 410 |
|
| 411 |
$this->expectException( ProductNotFoundException::class ); |
| 412 |
$this->cartService->isShippingRateRestrictedByProductsCategory( 'testRate', [ [ 'product_id' => 1 ] ] ); |
| 413 |
} |
| 414 |
|
| 415 |
public function testIsShippingRateRestrictedByProductsCategoryWithProductWithoutId(): void { |
| 416 |
$this->createCartServiceMock(); |
| 417 |
|
| 418 |
$this->assertFalse( $this->cartService->isShippingRateRestrictedByProductsCategory( 'testRate', [ [ 'quantity' => 1.0 ] ] ) ); |
| 419 |
} |
| 420 |
|
| 421 |
public function testIsShippingRateRestrictedByProductsCategoryWithNoProductCategories(): void { |
| 422 |
$this->createCartServiceMock(); |
| 423 |
$this->wcAdapter->method( 'productFactoryGetProduct' )->willReturn( $this->createMock( WC_Product::class ) ); |
| 424 |
$this->assertFalse( $this->cartService->isShippingRateRestrictedByProductsCategory( 'testRate', [] ) ); |
| 425 |
} |
| 426 |
|
| 427 |
public function testIsShippingRateRestrictedByProductsCategoryWithProductNotInCategories(): void { |
| 428 |
$this->createCartServiceMock(); |
| 429 |
|
| 430 |
$productMock = $this->createMock( WC_Product::class ); |
| 431 |
$productMock->method( 'get_category_ids' )->willReturn( [ 1, 2 ] ); |
| 432 |
$this->wcAdapter->method( 'productFactoryGetProduct' )->willReturn( $productMock ); |
| 433 |
|
| 434 |
$productCategoryEntityMock = $this->createMock( ProductCategory\Entity::class ); |
| 435 |
$productCategoryEntityMock->method( 'getDisallowedShippingRateIds' )->willReturn( [ 'rate1', 'rate2' ] ); |
| 436 |
$this->productCategoryEntityFactory->method( 'fromTermId' )->willReturn( $productCategoryEntityMock ); |
| 437 |
|
| 438 |
$this->assertFalse( $this->cartService->isShippingRateRestrictedByProductsCategory( 'testRate', [ [ 'product_id' => 1 ] ] ) ); |
| 439 |
} |
| 440 |
|
| 441 |
public function testIsShippingRateRestrictedByProductsCategoryWithProductInRestrictedCategories(): void { |
| 442 |
$this->createCartServiceMock(); |
| 443 |
|
| 444 |
$productMock = $this->createMock( WC_Product::class ); |
| 445 |
$productMock->method( 'get_category_ids' )->willReturn( [ 1, 2 ] ); |
| 446 |
$this->wcAdapter->method( 'productFactoryGetProduct' )->willReturn( $productMock ); |
| 447 |
|
| 448 |
$productCategoryEntityMock = $this->createMock( ProductCategory\Entity::class ); |
| 449 |
$productCategoryEntityMock->method( 'getDisallowedShippingRateIds' )->willReturn( [ 'rate1', 'testRate' ] ); |
| 450 |
$this->productCategoryEntityFactory->method( 'fromTermId' )->willReturn( $productCategoryEntityMock ); |
| 451 |
|
| 452 |
$this->assertTrue( $this->cartService->isShippingRateRestrictedByProductsCategory( 'testRate', [ [ 'product_id' => 1 ] ] ) ); |
| 453 |
} |
| 454 |
|
| 455 |
public function testGetBiggestProductSizeBySum(): void { |
| 456 |
$this->createCartServiceMock(); |
| 457 |
$productMock1 = $this->createMock( Product\Entity::class ); |
| 458 |
$productMock1->method( 'isPhysical' )->willReturn( true ); |
| 459 |
$productMock1->method( 'getLengthInCm' )->willReturn( 15.5 ); |
| 460 |
$productMock1->method( 'getWidthInCm' )->willReturn( 25.2 ); |
| 461 |
$productMock1->method( 'getHeightInCm' )->willReturn( 10.7 ); |
| 462 |
|
| 463 |
$productMock2 = $this->createMock( Product\Entity::class ); |
| 464 |
$productMock2->method( 'isPhysical' )->willReturn( true ); |
| 465 |
$productMock2->method( 'getLengthInCm' )->willReturn( 35.8 ); |
| 466 |
$productMock2->method( 'getWidthInCm' )->willReturn( 20.1 ); |
| 467 |
$productMock2->method( 'getHeightInCm' )->willReturn( 5.4 ); |
| 468 |
|
| 469 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 470 |
[ |
| 471 |
[ 'product_id' => 1 ], |
| 472 |
[ 'product_id' => 2 ], |
| 473 |
] |
| 474 |
); |
| 475 |
|
| 476 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 477 |
[ |
| 478 |
[ 'product_id' => 1 ], |
| 479 |
[ 'product_id' => 2 ], |
| 480 |
] |
| 481 |
); |
| 482 |
|
| 483 |
$this->productEntityFactory |
| 484 |
->method( 'fromPostId' ) |
| 485 |
->willReturnOnConsecutiveCalls( $productMock1, $productMock2 ); |
| 486 |
|
| 487 |
$this->wpAdapter->method( 'didAction' )->willReturn( 1 ); |
| 488 |
|
| 489 |
$result = $this->cartService->getBiggestProductSize( CartService::CRITERIA_BY_SUM ); |
| 490 |
$this->assertEquals( |
| 491 |
[ |
| 492 |
'length' => 35.8, |
| 493 |
'width' => 20.1, |
| 494 |
'depth' => 5.4, |
| 495 |
], |
| 496 |
$result |
| 497 |
); |
| 498 |
} |
| 499 |
|
| 500 |
public static function cartContainsProductOversizedForCarrier(): array { |
| 501 |
return [ |
| 502 |
'all-ok' => [ |
| 503 |
'sizeRestrictions' => [ |
| 504 |
'maximum_length' => 100, |
| 505 |
'dimensions_sum' => 180, |
| 506 |
'length' => 100, |
| 507 |
'width' => 50, |
| 508 |
'height' => 30, |
| 509 |
], |
| 510 |
'productDimensions1' => [ 80.0, 40.0, 30.0 ], |
| 511 |
'productDimensions2' => [ 30.0, 40.0, 80.0 ], |
| 512 |
'expectedResult' => false, |
| 513 |
], |
| 514 |
'size-exceeds' => [ |
| 515 |
'sizeRestrictions' => [ |
| 516 |
'length' => 100, |
| 517 |
'width' => 50, |
| 518 |
'height' => 30, |
| 519 |
], |
| 520 |
'productDimensions1' => [ 110.0, 40.0, 30.0 ], |
| 521 |
'productDimensions2' => [ 80.0, 40.0, 30.0 ], |
| 522 |
'expectedResult' => true, |
| 523 |
], |
| 524 |
'size-exceeds-v2' => [ |
| 525 |
'sizeRestrictions' => [ |
| 526 |
'length' => 100, |
| 527 |
'width' => 50, |
| 528 |
'height' => 30, |
| 529 |
], |
| 530 |
'productDimensions1' => [ 80.0, 40.0, 30.0 ], |
| 531 |
'productDimensions2' => [ 10.0, 40.0, 110.0 ], |
| 532 |
'expectedResult' => true, |
| 533 |
], |
| 534 |
'sum-exceeds' => [ |
| 535 |
'sizeRestrictions' => [ |
| 536 |
'maximum_length' => 100, |
| 537 |
'dimensions_sum' => 180, |
| 538 |
], |
| 539 |
'productDimensions1' => [ 80.0, 80.0, 80.0 ], |
| 540 |
'productDimensions2' => [ 80.0, 40.0, 30.0 ], |
| 541 |
'expectedResult' => true, |
| 542 |
], |
| 543 |
'length-exceeds' => [ |
| 544 |
'sizeRestrictions' => [ |
| 545 |
'maximum_length' => 100, |
| 546 |
'dimensions_sum' => 180, |
| 547 |
], |
| 548 |
'productDimensions1' => [ 110.0, 40.0, 30.0 ], |
| 549 |
'productDimensions2' => [ 80.0, 40.0, 30.0 ], |
| 550 |
'expectedResult' => true, |
| 551 |
], |
| 552 |
'length-exceeds-by-unclean-numeric' => [ |
| 553 |
'sizeRestrictions' => [ |
| 554 |
'maximum_length' => ' 100', |
| 555 |
'dimensions_sum' => '180 ', |
| 556 |
], |
| 557 |
'productDimensions1' => [ 110.0, 40.0, 30.0 ], |
| 558 |
'productDimensions2' => [ 80.0, 40.0, 30.0 ], |
| 559 |
'expectedResult' => true, |
| 560 |
], |
| 561 |
'empty-maximum-length' => [ |
| 562 |
'sizeRestrictions' => [ |
| 563 |
'maximum_length' => '', |
| 564 |
], |
| 565 |
'productDimensions1' => [ 110.0, 40.0, 30.0 ], |
| 566 |
'productDimensions2' => [ 80.0, 40.0, 30.0 ], |
| 567 |
'expectedResult' => false, |
| 568 |
], |
| 569 |
'empty-maximum-length-with-other' => [ |
| 570 |
'sizeRestrictions' => [ |
| 571 |
'maximum_length' => '', |
| 572 |
'dimensions_sum' => 180, |
| 573 |
], |
| 574 |
'productDimensions1' => [ 10.0, 10.0, 10.0 ], |
| 575 |
'productDimensions2' => [ 8.0, 4.0, 5.0 ], |
| 576 |
'expectedResult' => false, |
| 577 |
], |
| 578 |
'empty-dimensions-sum' => [ |
| 579 |
'sizeRestrictions' => [ |
| 580 |
'dimensions_sum' => '', |
| 581 |
], |
| 582 |
'productDimensions1' => [ 110.0, 40.0, 30.0 ], |
| 583 |
'productDimensions2' => [ 80.0, 40.0, 30.0 ], |
| 584 |
'expectedResult' => false, |
| 585 |
], |
| 586 |
'empty-dimensions-sum-with-other' => [ |
| 587 |
'sizeRestrictions' => [ |
| 588 |
'maximum_length' => 100, |
| 589 |
'dimensions_sum' => '', |
| 590 |
], |
| 591 |
'productDimensions1' => [ 100.0, 40.0, 30.0 ], |
| 592 |
'productDimensions2' => [ 80.0, 40.0, 30.0 ], |
| 593 |
'expectedResult' => false, |
| 594 |
], |
| 595 |
'empty-restrictions-hd' => [ |
| 596 |
'sizeRestrictions' => [ |
| 597 |
'maximum_length' => '', |
| 598 |
'dimensions_sum' => '', |
| 599 |
], |
| 600 |
'productDimensions1' => [ 110.0, 40.0, 30.0 ], |
| 601 |
'productDimensions2' => [ 80.0, 40.0, 30.0 ], |
| 602 |
'expectedResult' => false, |
| 603 |
], |
| 604 |
'empty-restrictions-zbox' => [ |
| 605 |
'sizeRestrictions' => [ |
| 606 |
'length' => '', |
| 607 |
'width' => '', |
| 608 |
'height' => '', |
| 609 |
], |
| 610 |
'productDimensions1' => [ 110.0, 40.0, 30.0 ], |
| 611 |
'productDimensions2' => [ 80.0, 40.0, 30.0 ], |
| 612 |
'expectedResult' => false, |
| 613 |
], |
| 614 |
'semi-empty-restrictions-zbox' => [ |
| 615 |
'sizeRestrictions' => [ |
| 616 |
'length' => 100, |
| 617 |
'width' => '', |
| 618 |
'height' => '', |
| 619 |
], |
| 620 |
'productDimensions1' => [ 110.0, 40.0, 30.0 ], |
| 621 |
'productDimensions2' => [ 80.0, 40.0, 30.0 ], |
| 622 |
'expectedResult' => false, |
| 623 |
], |
| 624 |
'length-exceeds-v2' => [ |
| 625 |
'sizeRestrictions' => [ |
| 626 |
'maximum_length' => 100, |
| 627 |
'dimensions_sum' => 180, |
| 628 |
], |
| 629 |
'productDimensions1' => [ 80.0, 40.0, 30.0 ], |
| 630 |
'productDimensions2' => [ 10.0, 40.0, 110.0 ], |
| 631 |
'expectedResult' => true, |
| 632 |
], |
| 633 |
]; |
| 634 |
} |
| 635 |
|
| 636 |
/** |
| 637 |
* @dataProvider cartContainsProductOversizedForCarrier |
| 638 |
*/ |
| 639 |
public function testCartContainsProductOversizedForCarrier( |
| 640 |
array $sizeRestrictions, |
| 641 |
array $productDimensions1, |
| 642 |
array $productDimensions2, |
| 643 |
bool $expectedResult, |
| 644 |
): void { |
| 645 |
$this->createCartServiceMock(); |
| 646 |
$carrierOptionsMock = $this->createMock( Carrier\Options::class ); |
| 647 |
$carrierOptionsMock->method( 'getSizeRestrictions' )->willReturn( $sizeRestrictions ); |
| 648 |
|
| 649 |
$productMock1 = $this->createMock( Product\Entity::class ); |
| 650 |
$productMock1->method( 'isPhysical' )->willReturn( true ); |
| 651 |
$productMock1->method( 'getLengthInCm' )->willReturn( $productDimensions1[0] ); |
| 652 |
$productMock1->method( 'getWidthInCm' )->willReturn( $productDimensions1[1] ); |
| 653 |
$productMock1->method( 'getHeightInCm' )->willReturn( $productDimensions1[2] ); |
| 654 |
|
| 655 |
$productMock2 = $this->createMock( Product\Entity::class ); |
| 656 |
$productMock2->method( 'isPhysical' )->willReturn( true ); |
| 657 |
$productMock2->method( 'getLengthInCm' )->willReturn( $productDimensions2[0] ); |
| 658 |
$productMock2->method( 'getWidthInCm' )->willReturn( $productDimensions2[1] ); |
| 659 |
$productMock2->method( 'getHeightInCm' )->willReturn( $productDimensions2[2] ); |
| 660 |
|
| 661 |
$this->wcAdapter->method( 'cartGetCartContent' )->willReturn( |
| 662 |
[ |
| 663 |
[ 'product_id' => 1 ], |
| 664 |
[ 'product_id' => 2 ], |
| 665 |
] |
| 666 |
); |
| 667 |
$this->productEntityFactory |
| 668 |
->method( 'fromPostId' ) |
| 669 |
->willReturnOnConsecutiveCalls( $productMock1, $productMock2, $productMock1, $productMock2 ); |
| 670 |
|
| 671 |
$this->wpAdapter->method( 'didAction' )->willReturn( 1 ); |
| 672 |
|
| 673 |
$result = $this->cartService->cartContainsProductOversizedForCarrier( $carrierOptionsMock ); |
| 674 |
$this->assertSame( $result, $expectedResult ); |
| 675 |
} |
| 676 |
} |
| 677 |
|