Files
kupshop/bundles/KupShop/KupShopBundle/Tests/DeliveryRestrictionsTest.php
2025-08-02 16:30:27 +02:00

302 lines
11 KiB
PHP

<?php
namespace KupShop\OrderingBundle\Tests\Purchase;
use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
use KupShop\KupShopBundle\Util\Delivery\RestrictionUtils;
use KupShop\OrderingBundle\Exception\DeliveryException;
use KupShop\OrderingBundle\Util\Purchase\PurchaseUtil;
class DeliveryRestrictionsTest extends \DatabaseTestCase
{
use CartTestTrait;
use PurchaseTestTrait;
/** @var PurchaseUtil */
private $purchaseUtil;
/** @var RestrictionUtils */
private $restrictionUtils;
protected function setUp(): void
{
parent::setUp();
$this->purchaseUtil = $this->get(PurchaseUtil::class);
$this->restrictionUtils = $this->get(RestrictionUtils::class);
}
protected function getRestrictionParams()
{
$purchaseState = $this->createPurchaseState();
return $this->restrictionUtils->createParamsFromPurchaseState($purchaseState);
}
public function testMaxLength()
{
$this->expectException(DeliveryException::class);
$this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([
'maxLength' => 70.0, // Maximální rozměr strany položky
], [])
);
}
public function testMinLength()
{
$this->expectException(DeliveryException::class);
$this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([
'minLength' => 81, // Minimální rozměr strany položky - u produktu 60
'percentageReserve' => 0,
], [])
);
}
public function testMaxSumOfDImensions()
{
$this->expectException(DeliveryException::class);
$this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([
'maxSumOfDimensions' => 150.0, // Maximální součet rozměrů všech tří stran položky
], [])
);
}
public function testMinSumOfDimensions()
{
$this->prepareCart();
$this->insertProduct(9); // product dimensions 10x60x60 => [60, 60, 10]
$this->cart->createFromDB();
$purchaseUtil = $this->get(PurchaseUtil::class);
$purchaseState = $purchaseUtil->createStateFromCart($this->cart);
$rParams = $this->restrictionUtils->createParamsFromPurchaseState($purchaseState);
$this->assertEquals(130, $rParams->getMinSumOfDimensions());
}
public function testProductDimensions()
{
$this->prepareCart();
$this->insertProduct(9); // product dimensions 10x60x60 => [60, 60, 10]
$this->cart->createFromDB();
$purchaseUtil = $this->get(PurchaseUtil::class);
$purchaseState = $purchaseUtil->createStateFromCart($this->cart);
$rParams = $this->restrictionUtils->createParamsFromPurchaseState($purchaseState);
$this->assertEquals([60.0, 60.0, 10.0], $rParams->getMaxDimensions());
}
public function testProductMinDimensions()
{
$this->prepareCart();
$this->insertProduct(9); // product dimensions 10x60x60 => [60, 60, 10]
$this->cart->createFromDB();
$purchaseUtil = $this->get(PurchaseUtil::class);
$purchaseState = $purchaseUtil->createStateFromCart($this->cart);
$rParams = $this->restrictionUtils->createParamsFromPurchaseState($purchaseState);
$this->assertEquals([10.0, 60.0, 60.0], $rParams->getMinDimensions());
}
public function testDimensionsInheritance()
{
$this->prepareCart();
$this->insertProduct(2, 9); // inherited dimensions 30x50x80 => [80, 50, 30]
$this->cart->createFromDB();
$purchaseUtil = $this->get(PurchaseUtil::class);
$purchaseState = $purchaseUtil->createStateFromCart($this->cart);
$rParams = $this->restrictionUtils->createParamsFromPurchaseState($purchaseState);
$this->assertEquals([80.0, 50.0, 30.0], $rParams->getMaxDimensions());
}
public function testDimensionsWithInheritance()
{
$this->prepareCart();
$this->insertProduct(9); // product dimensions 10x60x60 => [60, 60, 10]
$this->insertProduct(2, 9); // inherited dimensions 30x50x80 => [80, 50, 30]
$this->insertProduct(2, 30); // variation dimensions 10x40x90 => [90, 40, 10]
$this->cart->createFromDB();
$purchaseUtil = $this->get(PurchaseUtil::class);
$purchaseState = $purchaseUtil->createStateFromCart($this->cart);
$rParams = $this->restrictionUtils->createParamsFromPurchaseState($purchaseState);
$this->assertEquals([90.0, 60.0, 30.0], $rParams->getMaxDimensions());
$this->assertEquals([10.0, 40.0, 60.0], $rParams->getMinDimensions());
}
public function testMaxDimensions()
{
$this->expectException(DeliveryException::class);
$this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([
'maxDimensions' => [70.0, 50.0, 50.0],
], [])
);
}
public function testMinDimensions()
{
$this->prepareCart();
$this->insertProduct(9); // product dimensions 10x60x60 => [60, 60, 10]
$this->insertProduct(2, 9); // inherited dimensions 30x50x80 => [80, 50, 30]
$this->cart->createFromDB();
$purchaseUtil = $this->get(PurchaseUtil::class);
$purchaseState = $purchaseUtil->createStateFromCart($this->cart);
$rParams = $this->restrictionUtils->createParamsFromPurchaseState($purchaseState);
$this->assertEquals([10.0, 50.0, 60.0], $rParams->getMinDimensions());
$this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([
'minDimensions' => [10.0, 50.0, 60.0],
'percentageReserve' => 0,
], [])
);
}
public function testCustomMaxVolume()
{
$this->expectException(DeliveryException::class);
$this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([], [
'maxVolume' => 120.0, // Maximální objem zásilky
])
);
}
public function testMaxVolumeSum()
{
$this->prepareCart();
$this->insertProduct(9); // 10*60*60/1000 => 36 litrů
$this->insertProduct(2, 9, 2); // 2 * (30*50*80/1000) => 240 litrů
$this->cart->createFromDB();
$purchaseUtil = $this->get(PurchaseUtil::class);
$purchaseState = $purchaseUtil->createStateFromCart($this->cart);
$rParams = $this->restrictionUtils->createParamsFromPurchaseState($purchaseState);
$this->assertEquals(36.0 + 240.0, $rParams->getMaxVolume());
}
public function testMaxWeight()
{
$this->expectException(DeliveryException::class);
$this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([], [
'maxWeight' => 10.0, // Maximální hmotnost zásilky
])
);
}
public function testMinWeight()
{
$this->expectException(DeliveryException::class);
$this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([], [
'minWeight' => 50,
])
);
}
public function testMaxWeightSum()
{
$this->prepareCart();
$this->insertProduct(9); // 3.5 kg
$this->insertProduct(2, 9, 2); // 2 * 8 kg = 16 kg
$this->cart->createFromDB();
$purchaseUtil = $this->get(PurchaseUtil::class);
$purchaseState = $purchaseUtil->createStateFromCart($this->cart);
$rParams = $this->restrictionUtils->createParamsFromPurchaseState($purchaseState);
$this->assertEquals(3.5 + 16.0, $rParams->getMaxWeight());
}
public function testValidDelivery()
{
$this->assertNull($this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([
'maxWeight' => 25.0, // Maximální hmotnost položky
'maxLength' => 100.0, // Maximální rozměr strany položky
'maxSumOfDimensions' => 300.0, // Maximální součet rozměrů všech tří stran položky
'maxDimensions' => [100.0, 70.0, 50.0], // Maximální rozměry položky
], [
'maxVolume' => 300.0, // Maximální objem zásilky
])
));
}
public function testCustomRestrictions()
{
$this->expectException(DeliveryException::class);
$this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([
'maxLength' => 100.0, // Maximální rozměr strany položky
], [
'maxLength' => 70.0, // Předefinování výchozího omezení maxLength v nastavení
])
);
}
public function testCustomDisabled()
{
$this->assertNull($this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([
'maxLength' => 70.0, // Maximální rozměr strany položky
], [
'maxLength' => null, // Vypnutí výchozího omezení maxLength
])
));
}
public function testCartRestrictionsDisabled()
{
$this->prepareCart();
$this->insertProduct(9); // 3.5 kg
$this->insertProduct(2, 9, 2); // 2 * 8 kg = 16 kg
$this->cart->setTransport(10); // restrictions disabled
$this->cart->createFromDB();
$this->assertInstanceOf(\Order::class, $this->cart->submitOrder());
}
public function testCartRestrictionsEnabled()
{
$this->prepareCart();
$this->insertProduct(9); // 3.5 kg
$this->insertProduct(2, 9, 2); // 2 * 8 kg = 16 kg
$this->cart->setTransport(11); // restrictions enabled
$this->cart->createFromDB();
$this->expectException(DeliveryException::class);
$this->cart->submitOrder();
}
public function testMaxPieces()
{
$this->expectException(DeliveryException::class);
$this->restrictionUtils->checkSoftRequirements(
$this->getRestrictionParams(),
$this->restrictionUtils->createParamsFromConfig([], [
'maxPieces' => 2, // Maximální počet položek v zásilce
])
);
}
public function testCartMaxPieces()
{
$this->prepareCart();
$this->insertProduct(9, null, 4); // 4 kusy
$this->cart->setTransport(12); // max 2 kusy
$this->cart->createFromDB();
$this->expectException(DeliveryException::class);
$this->cart->submitOrder();
}
public function getDataSet()
{
return $this->getJsonDataSetFromFile();
}
}