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

72 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\BonusProgramBundle\Tests;
use KupShop\BonusProgramBundle\Utils\Exchange\BonusExchangeUtil;
class BonusProgramExchangeTest extends \DatabaseTestCase
{
private ?BonusExchangeUtil $bonusExchangeUtil = null;
protected function setUp(): void
{
parent::setUp();
$this->bonusExchangeUtil = $this->get(BonusExchangeUtil::class);
}
/** @dataProvider data_testExchangePoints */
public function testExchangePoints(int $exchangeItemId, int $userId, bool $expected, int $expectedUserPoints): void
{
$result = $this->bonusExchangeUtil->exchange($exchangeItemId, $userId);
$this->assertEquals($expected, $result);
if ($userId) {
$userPoints = sqlQueryBuilder()
->select('SUM(points)')
->from('bonus_points')
->where(\Query\Operator::equals(['id_user' => $userId, 'status' => 'active']))
->execute()->fetchOne() ?? 0;
$this->assertEquals($expectedUserPoints, $userPoints);
}
}
public function data_testExchangePoints(): array
{
return [
// uzivatel ma body, takze result musi byt true a musi se mu odecist 50 bodu
[1, 1, true, 50],
// uzivatel nema dostatek bodu, takze resul musi byt false a nesmi se mu odecist zadne body
[2, 1, false, 100],
];
}
/** @dataProvider data_testExchangePointsException */
public function testExchangePointsException(int $exchangeItemId, ?int $userId, string $expectedException): void
{
$this->expectException($expectedException);
$this->bonusExchangeUtil->exchange($exchangeItemId, $userId);
}
public function data_testExchangePointsException(): array
{
return [
// neni zalogovany user a zadneho tomu neposlu
[1, null, \InvalidArgumentException::class],
// zkousim si aktivovat neco, co je skryte / neexistuje
[3, 1, \RuntimeException::class],
[99, 1, \RuntimeException::class],
];
}
protected function getDataSet()
{
return $this->getJsonDataSetFromFile();
}
}