155 lines
5.3 KiB
PHP
155 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\PreordersBundle\Tests;
|
|
|
|
use KupShop\PreordersBundle\Entity\UserPreorder;
|
|
use KupShop\PreordersBundle\Service\PreorderItemsUpdater;
|
|
use KupShop\PreordersBundle\Service\PreorderUtil;
|
|
use Query\Operator;
|
|
|
|
trait PreordersTestHelper
|
|
{
|
|
protected static array $itemsWithValues = [
|
|
'4/1' => ['id_product' => 4, 'id_variation' => 1, 'test_field_A' => 'A', 'test_field_B' => 'B', 'piece_price' => '1234.56', 'pieces' => 1],
|
|
'4/2' => ['id_product' => 4, 'id_variation' => 2, 'test_field_A' => 'A', 'test_field_B' => 'B', 'piece_price' => '2345.67', 'pieces' => 2],
|
|
'10' => ['id_product' => 10, 'id_variation' => null, 'test_field_A' => 'A', 'test_field_B' => 'B', 'piece_price' => '3456.78', 'pieces' => 3],
|
|
];
|
|
|
|
public const PRICE_LEVEL_BASE = 4;
|
|
|
|
/** 1_000 - 10_000 */
|
|
public const PRICE_LEVEL_8 = 1;
|
|
|
|
/** 10_001 - 20_000 */
|
|
public const PRICE_LEVEL_12 = 2;
|
|
|
|
/** 20_001 - INF */
|
|
public const PRICE_LEVEL_16 = 3;
|
|
|
|
public const PRICELIST_BASE = 1;
|
|
|
|
/** 20_001 - INF */
|
|
public const PRICELIST_INF = 2;
|
|
|
|
protected function createProductList(bool $variationsAsResult = false): \ProductList
|
|
{
|
|
$list = new \ProductList();
|
|
$list->setVariationsAsResult($variationsAsResult);
|
|
$list->fetchVariations(true);
|
|
|
|
$list->andSpec(PreorderUtil::specSelectProductAndVariationIds(self::$itemsWithValues));
|
|
$list->addResultModifiers(PreorderUtil::modifierInjectFields(self::$itemsWithValues, [
|
|
'test_field_A' => 'res_field_A',
|
|
'test_field_B' => 'res_field_B',
|
|
'pieces' => 'pieces',
|
|
]));
|
|
|
|
$list->addResultModifiers($this->get(PreorderUtil::class)->modifierInjectPiecePrices(
|
|
self::$itemsWithValues,
|
|
$variationsAsResult,
|
|
outKey: 'test_piece_price',
|
|
));
|
|
|
|
return $list;
|
|
}
|
|
|
|
protected function createDate(int $idPreorder, ?int $dateStart = null, ?int $dateEnd = null): int
|
|
{
|
|
sqlQueryBuilder()->insert('preorders_dates')
|
|
->directValues([
|
|
'id_preorder' => $idPreorder,
|
|
'date_start' => date('Y-m-d', $dateStart ?? strtotime('now -14 day')),
|
|
'date_end' => date('Y-m-d', $dateEnd ?? strtotime('now +14 day')),
|
|
])->execute();
|
|
|
|
return (int) sqlInsertId();
|
|
}
|
|
|
|
protected function createTestUserPreorder(): UserPreorder
|
|
{
|
|
$this->loginUser(1);
|
|
$dateId = $this->createDate(1);
|
|
|
|
$userPreorder = new UserPreorder(1, $dateId);
|
|
|
|
$this->get(PreorderItemsUpdater::class)->updateItems($userPreorder, [
|
|
['id_product' => 4, 'id_variation' => '1', 'pieces' => 1],
|
|
['id_product' => 4, 'id_variation' => '2', 'pieces' => 2],
|
|
['id_product' => 10, 'id_variation' => null, 'pieces' => 3],
|
|
]);
|
|
|
|
$userPreorder->getItems(true);
|
|
|
|
return $userPreorder;
|
|
}
|
|
|
|
protected function getDataSet()
|
|
{
|
|
return $this->getJsonDataSetFromFile(__DIR__.'/PreordersData.json');
|
|
}
|
|
|
|
protected function createTestMultipleDates(): array
|
|
{
|
|
$yesterday = $this->datetimeOffset(false, 1);
|
|
$tomorrow = $this->datetimeOffset(true, 1);
|
|
|
|
$dates = [
|
|
['id_preorder' => 1, 'date_start' => $this->sqlDateTime($yesterday), 'date_end' => $this->sqlDateTime($tomorrow)],
|
|
['id_preorder' => 1, 'date_start' => $this->sqlDateTime($yesterday), 'date_end' => $this->sqlDateTime($tomorrow)],
|
|
['id_preorder' => 1, 'date_start' => $this->sqlDateTime($yesterday), 'date_end' => $this->sqlDateTime($yesterday)],
|
|
['id_preorder' => 2, 'date_start' => $this->sqlDateTime($yesterday), 'date_end' => $this->sqlDateTime($tomorrow)],
|
|
['id_preorder' => 2, 'date_start' => $this->sqlDateTime($yesterday), 'date_end' => $this->sqlDateTime($yesterday)],
|
|
];
|
|
|
|
$dateIds = [];
|
|
foreach ($dates as $date) {
|
|
sqlQueryBuilder()->insert('preorders_dates')
|
|
->directValues($date)
|
|
->execute();
|
|
|
|
$dateIds[] = sqlInsertId();
|
|
}
|
|
|
|
return sqlQueryBuilder()->select('*')
|
|
->from('preorders_dates', 'pd')
|
|
->where(Operator::inIntArray($dateIds, 'pd.id'))
|
|
->execute()->fetchAllAssociative();
|
|
}
|
|
|
|
protected function sqlDateTime(\DateTimeInterface $dateTime = new \DateTimeImmutable()): string
|
|
{
|
|
return $dateTime->format('Y-m-d H:i:s');
|
|
}
|
|
|
|
protected function datetimeOffset(bool $add, int $days): \DateTimeInterface
|
|
{
|
|
$now = (new \DateTimeImmutable());
|
|
|
|
return match ($add) {
|
|
true => $now->add(new \DateInterval("P{$days}D")),
|
|
false => $now->sub(new \DateInterval("P{$days}D")),
|
|
};
|
|
}
|
|
|
|
protected function getUserWithAllPreorders(): \User
|
|
{
|
|
static $user = null;
|
|
|
|
return $user ??= \User::createFromId(3);
|
|
}
|
|
|
|
protected function setUseMultipleDates(bool $useMultipleDates = true): void
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
$dbcfg->saveValue('preorders', ['multiple_dates' => $useMultipleDates ? 'Y' : 'N']);
|
|
$dbcfg::clearCache(true);
|
|
}
|
|
|
|
protected function sumUserPreorderItems(UserPreorder $userPreorder): float
|
|
{
|
|
return array_sum(array_column($userPreorder->getItems(true), 'price_total'));
|
|
}
|
|
}
|