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

173 lines
6.3 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\KupShopBundle\Tests;
use KupShop\KupShopBundle\EventListener\CleanupListener;
use PHPUnit\DbUnit\DataSet\IDataSet;
use Query\Operator as Op;
class CleanupTest extends \DatabaseTestCase
{
private const OLD_ORDER = 816121;
private const OLD_COUPON = 31;
private CleanupListener $cleanupListener;
protected function setUp(): void
{
parent::setUp();
$this->setUpData();
$this->cleanupListener = $this->get(CleanupListener::class);
}
public function testCleanupCoupons(): void
{
sqlQueryBuilder()->update('discounts_coupons', 'dc')
->andWhere(Op::equals(['dc.id' => self::OLD_COUPON]))
->set('dc.date_to', 'NOW() - INTERVAL 7 MONTH')
->execute();
$previousOld = sqlQueryBuilder()->select('dc.id')
->from('discounts_coupons', 'dc')
->andWhere(Op::equals(['dc.id' => self::OLD_COUPON]))
->execute()->fetchFirstColumn();
$this->assertNotEmpty($previousOld, 'Chyba v testovacích datech.');
$previousOther = sqlQueryBuilder()->select('dc.id')
->from('discounts_coupons', 'dc')
->andWhere(Op::not(
Op::equals(['dc.id' => self::OLD_COUPON]),
))
->execute()->fetchFirstColumn();
$this->cleanupListener->cleanupDiscountCoupons();
$afterOld = sqlQueryBuilder()->select('dc.id')
->from('discounts_coupons', 'dc')
->andWhere(Op::equals(['dc.id' => self::OLD_COUPON]))
->execute()->fetchFirstColumn();
$afterOther = sqlQueryBuilder()->select('dc.id')
->from('discounts_coupons', 'dc')
->andWhere(Op::not(
Op::equals(['dc.id' => self::OLD_COUPON]),
))
->execute()->fetchFirstColumn();
$this->assertEquals($previousOther, $afterOther, 'Smazaly se slevové kódy, které nejsou 6 měsíců po expiraci.');
$this->assertEmpty($afterOld, 'Nesmazaly se slevové kódy, které jsou 6 měsíců po expiraci.');
}
public function testCleanupOrdersHistorySetNullAfter2Years(): void
{
// set one order to 7 months old - should set comment and custom_data to NULL
sqlQueryBuilder()->update('orders', 'o')
->set('o.date_updated', 'NOW() - INTERVAL 25 MONTH')
->andWhere(Op::equals(['o.id' => self::OLD_ORDER]))
->execute();
$getOrderHistoryEntryIds = sqlQueryBuilder()->select('oh.id')
->from('orders_history', 'oh');
$getComments = sqlQueryBuilder()->select('oh.id_order', 'oh.comment', 'oh.custom_data')
->from('orders_history', 'oh');
foreach ($getComments->execute() as $comment) {
$this->assertNotEmpty($comment['comment'], 'Chyba v testovacích datech, tohle chci testovat.');
$this->assertNotEmpty($comment['custom_data'], 'Chyba v testovacích datech, tohle chci testovat.');
}
$idsBefore = $getOrderHistoryEntryIds->execute()->fetchFirstColumn();
$this->cleanupListener->cleanupOrderHistory();
foreach ($getComments->execute() as $comment) {
$customData = json_decode($comment['custom_data'] ?? '', true) ?: [];
if ($comment['id_order'] == self::OLD_ORDER && ($customData['email_type'] ?? '') != 'ORDER_CREATE') {
$this->assertNull($comment['comment'], 'Nesmazal se komentář u objednávky starší než 6 měsíců.');
} else {
$this->assertNotEmpty($comment['comment'], 'Smazal se komentář u objednávky, která není starší než 6 měsíců.');
}
}
$idsAfterCleanup = $getOrderHistoryEntryIds->execute()->fetchFirstColumn();
$this->assertEquals($idsBefore, $idsAfterCleanup, 'Smazala se komunikace u objednávek (neměla :))');
}
public function testCleanupOrdersHistoryDeleteAfter3Years(): void
{
sqlQueryBuilder()->update('orders', 'o')
->set('o.date_updated', 'NOW() - INTERVAL 4 YEAR')
->andWhere(Op::equals(['o.id' => self::OLD_ORDER]))
->execute();
$previousOld = sqlQueryBuilder()->select('oh.id')
->from('orders_history', 'oh')
->andWhere(Op::equals(['oh.id_order' => self::OLD_ORDER]))
->execute()->fetchFirstColumn();
$this->assertNotEmpty($previousOld, 'Chyba v testovacích datech.');
$previousOther = sqlQueryBuilder()->select('oh.id')
->from('orders_history', 'oh')
->andWhere(Op::not(
Op::equals(['oh.id_order' => self::OLD_ORDER]),
))
->execute()->fetchFirstColumn();
$this->cleanupListener->cleanupOrderHistory(); // poprvé se jen promaže custom_data a comment
$this->cleanupListener->cleanupOrderHistory(); // tohle smaže řádky, kde je custom_data a comment NULL
$afterOld = sqlQueryBuilder()->select('oh.id')
->from('orders_history', 'oh')
->andWhere(Op::equals(['oh.id_order' => self::OLD_ORDER]))
->andWhere('oh.comment IS NULL')
->andWhere('oh.custom_data IS NULL')
->execute()->fetchFirstColumn();
$afterOther = sqlQueryBuilder()->select('oh.id')
->from('orders_history', 'oh')
->andWhere(Op::not(
Op::equals(['oh.id_order' => self::OLD_ORDER]),
))
->execute()->fetchFirstColumn();
$this->assertEquals($previousOther, $afterOther, 'Smazala se komunikace u objednávek, které nejsou starší 3 let.');
$this->assertEmpty($afterOld, 'Nesmazala se komunikace u objednávky starší než 3 roky.');
}
public function testRemaining()
{
$exception = null;
try {
$this->cleanupListener->runCleanupOfOldObjectsFromDatabase();
} catch (\Throwable $e) {
$exception = $e;
}
$this->assertNull($exception);
}
public function setUpData(): void
{
sqlQueryBuilder()->update('orders', 'o')
->set('o.date_updated', 'NOW()')
->execute();
sqlQueryBuilder()->update('discounts_coupons', 'c')
->set('c.date_to', 'NOW()')
->execute();
}
public function getDataSet(): IDataSet
{
return $this->getJsonDataSetFromFile();
}
}