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

207 lines
7.9 KiB
PHP

<?php
namespace KupShop\AdminBundle\Tests;
use KupShop\AdminBundle\Util\SplitVariationsToProductUtil;
use KupShop\StoresBundle\Utils\MainStoreMigrationUtil;
use KupShop\StoresBundle\Utils\StoresInStore;
use Query\Operator;
class SplitProductVariationsActionTest extends \DatabaseTestCase
{
/**
* @var SplitVariationsToProductUtil
*/
protected $splitUtil;
/**
* @var StoresInStore
*/
protected $storesInStore;
protected function setUp(): void
{
parent::setUp();
$this->splitUtil = $this->get(SplitVariationsToProductUtil::class);
$this->storesInStore = $this->get(StoresInStore::class);
/** @var MainStoreMigrationUtil $migrate */
$migrate = $this->get(MainStoreMigrationUtil::class);
$migrate->migrate();
}
public function testSplitVariationsWithOneLabel()
{
$countProductsBefore = $this->countTable('products');
$newProducts = $this->splitUtil->splitByLabel(2, 11, true);
$this->assertEquals($countProductsBefore + 3, $this->countTable('products'));
$this->assertFalse(
sqlQueryBuilder()->select('id')
->from('products_variations')
->where(Operator::inIntArray([9, 10, 11, 12], 'id'))
->execute()->fetch()
);
$newProducts[] = 2;
$warehouseSum = 0;
$totalInStoreSum = 0;
$store2InStoreSum = 0;
$store1InStoreSum = 0;
$store2MinInStoreSum = 0;
foreach ($newProducts as $product) {
$store = $this->storesInStore->getProductInStoreAmounts($product, null);
$totalInStoreSum += $store['total_in_store'];
$store2InStoreSum += $store['store2_in_store'];
$store1InStoreSum += $store['store1_in_store'];
$store2MinInStoreSum += $store['store2_min_in_store'];
$warehouseSum += sqlQueryBuilder()->select('pieces')
->from('warehouse_products', 'wp')
->where(Operator::equals(['id_product' => $product]))
->andWhere(Operator::equalsNullable(['id_variation' => null]))
->execute()->fetchColumn();
}
// check if stores have same summed up capacity as before they were splitted
$this->assertEquals(9, $totalInStoreSum);
$this->assertEquals(4, $store2InStoreSum);
$this->assertEquals(5, $store1InStoreSum);
$this->assertEquals(2, $store2MinInStoreSum);
$this->assertEquals(4, $warehouseSum);
// check if order_items id_variation fields were set to null
$orderItem50 = $this->getOrderItem(50);
$orderItem51 = $this->getOrderItem(51);
$this->assertNull($orderItem50['id_variation']);
$this->assertNull($orderItem51['id_variation']);
$this->assertTrue($orderItem50['id_product'] != $orderItem51['id_product']);
$this->assertTrue(in_array($orderItem50['id_product'], $newProducts));
$this->assertTrue(in_array($orderItem51['id_product'], $newProducts));
// check if variation data moved to product correctly
$this->assertNotEmpty(
sqlQueryBuilder()->select('*')
->from('products')
->where(
Operator::equals(
[
'code' => 'CODE1',
'ean' => '111',
'in_store' => '2',
'price_buy' => '100',
'price' => 1337,
'delivery_time' => -1,
'width' => 1,
'height' => 2,
'depth' => 3,
'weight' => 20,
'bonus_points' => 1,
'note' => 'note1',
]
)
)
->execute()
->fetch()
);
}
public function testSplitVariationsWithTwoLabels()
{
$countProductsBefore = $this->countTable('products');
$newProducts = $this->splitUtil->splitByLabel(6, 12, false);
$newProducts[] = 6;
$this->assertEquals($countProductsBefore + 1, $this->countTable('products'));
$findCategorizationsWithLabel = function ($labelId) use ($newProducts) {
return sqlQueryBuilder()->select('*')
->from('products_variations_choices_categorization')
->where(Operator::inIntArray($newProducts, 'id_product'))
->andWhere(Operator::equals(['id_label' => $labelId]))->execute()->fetch();
};
// check if correct "products_variations_choices_categorizations" were deleted
$this->assertFalse($findCategorizationsWithLabel(12));
$this->assertNotFalse($findCategorizationsWithLabel(11));
$findCombinationWithLabel = function ($labelId) {
return sqlQueryBuilder()->select('*')
->from('products_variations_combination')
->where(Operator::inIntArray([17, 18, 19], 'id_variation'))
->andWhere(Operator::equals(['id_label' => $labelId]))->execute()->fetch();
};
// check if correct "products_variations_combinations" were deleted
$this->assertFalse($findCombinationWithLabel(12));
$this->assertNotFalse($findCombinationWithLabel(11));
$variations = sqlFetchAll(
sqlQueryBuilder()->select('*')->from('products_variations')
->where(Operator::inIntArray([17, 18, 19], 'id'))
->execute(),
'id'
);
// check if newly splitted variations have different id_product
$this->assertTrue(in_array($variations[17]['id_product'], $newProducts));
$this->assertTrue(in_array($variations[19]['id_product'], $newProducts));
$this->assertTrue($variations[17]['id_product'] == $variations[18]['id_product']);
$this->assertTrue($variations[18]['id_product'] != $variations[19]['id_product']);
// check if new prices are correct
$this->assertNull($variations[19]['price']);
$this->assertNull($variations[18]['price']);
$this->assertEquals(2000, $variations[17]['price']);
$warehouse = sqlFetchAll(
sqlQueryBuilder()->select('id_variation, id_product, SUM(pieces) pieces')->from('warehouse_products')
->where(Operator::inIntArray([17, 18, 19], 'id_variation'))
->groupBy('id_product, id_variation')
->execute(),
'id_variation'
);
// check if warehouse was correctly splitted for two new products
$this->assertEquals(2, $warehouse[17]['pieces']);
$this->assertEquals(2, $warehouse[19]['pieces']);
$this->assertFalse($warehouse[18] ?? false);
$this->assertTrue(in_array($warehouse[17]['id_product'], $newProducts));
$this->assertTrue(in_array($warehouse[19]['id_product'], $newProducts));
$this->assertTrue($warehouse[19]['id_product'] != $warehouse[17]['id_product']);
}
public function testProductPriceFixUtil()
{
sqlQueryBuilder()->delete('products_variations')->where('id = 19')->execute();
$this->splitUtil->fixProductPrice(6);
$this->assertNotFalse(
sqlQueryBuilder()->select('*')
->from('products')
->where('id = 6')
->andWhere('price = 1000')
->execute()->fetch()
);
}
private function countTable($table)
{
return sqlQueryBuilder()->select('COUNT(*)')
->from($table)
->execute()->fetchColumn();
}
protected function getOrderItem($idOrderItem)
{
return sqlQueryBuilder()->select('*')
->from('order_items')
->where(Operator::equals(['id' => $idOrderItem]))
->execute()->fetch();
}
protected function getDataSet()
{
return $this->getJsonDataSetFromFile();
}
}