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

205 lines
7.1 KiB
PHP

<?php
namespace KupShop\OSSVatsBundle\Tests;
use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
use KupShop\DevelopmentBundle\Util\Tests\RequestSessionTrait;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\OrderDiscountBundle\Tests\OrderTestDiscountTrait;
use Query\Operator;
class OSSOrderVatsTest extends \DatabaseTestCase
{
use CartTestTrait;
use OrderTestDiscountTrait;
use RequestSessionTrait;
protected $contextManager;
protected function setUp(): void
{
parent::setUp();
$dbcfg = \Settings::getDefault();
$dbcfg->oss_vats['homeCountry'] = 'CZ';
$dbcfg->oss_vats['active'] = 'Y';
$this->contextManager = ServiceContainer::getService(\KupShop\KupShopBundle\Context\ContextManager::class);
$this->prepareRequestStackSession();
}
/**
* @dataProvider data_testOrderTotalPrice
*/
public function testOrderTotalPrice(
string $country,
float $expectedTotalPriceWithoutVat,
float $expectedTotalPriceWithVat,
?float $expectedTotalPriceCartWithVat,
string $dic,
array $products,
string $homeCountry = 'CZ',
): void {
$dbcfg = \Settings::getDefault();
$dbcfg->oss_vats['homeCountry'] = $homeCountry;
$expectedTotalPriceCartWithVat = $expectedTotalPriceCartWithVat ?: $expectedTotalPriceWithVat;
$this->initCart($products, $dic, $country);
$this->cart->createFromDB();
$this->assertEqualsWithDelta($expectedTotalPriceWithoutVat, $this->cart->totalPriceNoVat->asFloat(), 3);
$this->assertEqualsWithDelta($expectedTotalPriceCartWithVat, $this->cart->totalPriceWithVat->asFloat(), 3);
$order = $this->cart->submitOrder();
$this->assertEqualsWithDelta($expectedTotalPriceWithoutVat, $order->total_price_array['value_without_vat']->asFloat(), 3);
$this->assertEqualsWithDelta($expectedTotalPriceWithVat, $order->total_price_array['value_with_vat']->asFloat(), 3);
}
/**
* @dataProvider data_testOrderTotalPrice
*/
public function testCartChangeCountryTotalPrice(
string $country,
float $expectedTotalPriceWithoutVat,
float $expectedTotalPriceWithVat,
?float $expectedTotalPriceCartWithVat,
string $dic,
array $products,
string $homeCountry = 'CZ',
): void {
$dbcfg = \Settings::getDefault();
$dbcfg->oss_vats['homeCountry'] = $homeCountry;
$expectedTotalPriceCartWithVat = $expectedTotalPriceCartWithVat ?: $expectedTotalPriceWithVat;
$this->initCart($products, $dic, 'CZ');
$this->cart->createFromDB();
$this->setTestInvoice(['country' => $country, 'dic' => $dic]);
$this->assertEqualsWithDelta($expectedTotalPriceWithoutVat, $this->cart->totalPriceNoVat->asFloat(), 3);
$this->assertEqualsWithDelta($expectedTotalPriceCartWithVat, $this->cart->totalPriceWithVat->asFloat(), 3);
$order = $this->cart->submitOrder();
$this->assertEqualsWithDelta($expectedTotalPriceWithoutVat, $order->total_price_array['value_without_vat']->asFloat(), 3);
$this->assertEqualsWithDelta($expectedTotalPriceWithVat, $order->total_price_array['value_with_vat']->asFloat(), 3);
}
public function data_testOrderTotalPrice(): array
{
$products = [[10, null, 2], [8], [5, 13], [6, 19, 2]];
/* Country, expectedPriceWithoutVat, expected price with wat, ?expectedTotalPriceCartWithVat, dic, [products], homeCountry */
return [
['CZ', 11880, 14208, null, '', $products],
['CZ', 11880, 14129, null, '', $products, 'RS'],
['RS', 11880, 11880, null, '', $products],
['HU', 11880, 14718, null, '', $products],
['HR', 11880, 14356, null, '', $products],
['CZ', 11880, 14208, null, '123456789', $products],
['CZ', 11880, 11880, 11880, '123456789', $products, 'RS'],
['RS', 11880, 11880, null, '123456789', $products, 'RS'],
['RS', 11880, 11880, null, '', $products, 'RS'],
['RS', 11880, 11880, null, '', $products],
['RS', 11880, 11880, null, '123456789', $products],
['HU', 11880, 11880, 11880, '123456789', $products],
['HR', 11880, 11880, 11880, '123456789', $products],
['HU', 4959.5, 6297.5, null, '', [[6, 19, 2]]],
['HR', 4959.5, 6198.5, null, '', [[6, 19, 2]]],
// Tests with OSS Exceptions
['HU', 9794.3679, 12187, null, '', [[12, null, 2], [13, null, 5]]],
['CZ', 9793.1818, 10073, null, '', [[12, null, 2], [13, null, 5]], 'HU'],
];
}
/**
* @dataProvider data_testFlagOss
*/
public function testFlagOss(
string $country,
string $dic,
bool $hasFlag,
): void {
$this->initCart([[10]], $dic, $country);
$this->cart->createFromDB();
$order = $this->cart->submitOrder();
$foundFlag = sqlQueryBuilder()->select("FIND_IN_SET('OSS',flags) > 0")->from('orders')
->where(Operator::equals(['id' => $order->id]))
->execute()->fetchColumn();
self::assertTrue(!($foundFlag ^ $hasFlag));
}
public function data_testflagOss(): array
{
return [
['CZ', '', false],
['CZ', '12345678', false],
['RS', '12345678', false],
['HU', '', true],
['HU', '12345678', false],
['HR', '', true],
['HR', '12345678', false],
];
}
/**
* @dataProvider data_testOrderDiscount
*/
public function testOrderDiscount(string $country, int $expectedVat)
{
$this->initCart([[10]], '', $country);
$this->activateDiscount([46]);
$this->cart->addCoupon('AAA');
$this->cart->createFromDB();
$vat = $this->cart->getPurchaseState()->getDiscounts()[0]->getPrice()->getVat()->asInteger();
$this->assertEquals($expectedVat, $vat);
$order = $this->cart->submitOrder();
$vat = $order->getPurchaseState()->getDiscounts()[0]->getPrice()->getVat()->asInteger();
$this->assertEquals($expectedVat, $vat);
}
public function data_testOrderDiscount(): array
{
return [
['CZ', 21],
['HR', 25],
['HU', 27],
];
}
protected function initCart($products, $dic, $country)
{
$this->createCart();
foreach ($products as $product) {
$this->insertProduct($product[0], $product[1] ?? null, $product[2] ?? 1);
}
$this->setTestInvoice(['country' => $country, 'dic' => $dic]);
$this->setDeliveryType(1);
}
protected function setTestInvoice($data = [])
{
$this->cart->updateAddresses($data + [
'email' => 'test@wpj.cz',
'name' => 'Wpj',
'surname' => 'Wpj',
'street' => 'Wpj 123',
'city' => 'Vrchlabi',
'zip' => 12345,
'country' => 'CZ',
'phone' => '123456789',
], null);
}
public function getDataSet()
{
return $this->getJsonDataSetFromFile();
}
}