112 lines
3.8 KiB
PHP
112 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace KupShop\I18nBundle\Tests;
|
|
|
|
use KupShop\I18nBundle\Util\PriceConverter;
|
|
use KupShop\KupShopBundle\Context\CurrencyContext;
|
|
|
|
class PriceConverterTest extends \DatabaseTestCase
|
|
{
|
|
/** @var PriceConverter */
|
|
private $priceConverter;
|
|
/** @var CurrencyContext */
|
|
private $currencyContext;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// potrebuju pregenerovat container kvuli currencies, ktere se uz naloadovali v migracich, tudiz se mi potom
|
|
// nenaloudovali ty, ktere jsou v DataSet
|
|
$this->tearDownContainer();
|
|
|
|
$this->priceConverter = $this->get(PriceConverter::class);
|
|
$this->currencyContext = $this->get(CurrencyContext::class);
|
|
}
|
|
|
|
public function testConvertFromCZK()
|
|
{
|
|
$currencies = $this->currencyContext->getSupported();
|
|
|
|
$convertedToEUR = $this->priceConverter->convert($currencies['CZK'], $currencies['EUR'], toDecimal(520));
|
|
$convertedToUSD = $this->priceConverter->convert($currencies['CZK'], $currencies['USD'], toDecimal(110));
|
|
|
|
$this->assertEquals('20', $convertedToEUR->asFloat());
|
|
$this->assertEquals('5', $convertedToUSD->asFloat());
|
|
}
|
|
|
|
public function testConvertToCZK()
|
|
{
|
|
$currencies = $this->currencyContext->getSupported();
|
|
|
|
$convertedFromEUR = $this->priceConverter->convert($currencies['EUR'], $currencies['CZK'], toDecimal(20));
|
|
$convertedFromUSD = $this->priceConverter->convert($currencies['USD'], $currencies['CZK'], toDecimal(5));
|
|
|
|
$this->assertEquals(520, $convertedFromEUR->asFloat());
|
|
$this->assertEquals(110, $convertedFromUSD->asFloat());
|
|
}
|
|
|
|
public function testConvertForeignCurrency()
|
|
{
|
|
$currencies = $this->currencyContext->getSupported();
|
|
|
|
$converted1 = $this->priceConverter->convert($currencies['EUR'], $currencies['USD'], toDecimal(20));
|
|
$converted2 = $this->priceConverter->convert($currencies['USD'], $currencies['EUR'], toDecimal(25));
|
|
|
|
$this->assertEqualsWithDelta(23.636363636364, $converted1->asFloat(), 0.00000000001);
|
|
$this->assertEqualsWithDelta(21.153846153846, $converted2->asFloat(), 0.00000000001);
|
|
}
|
|
|
|
protected function getDataSet()
|
|
{
|
|
return $this->getJsonDataSet('
|
|
{
|
|
"currencies": [
|
|
{
|
|
"id":"CZK",
|
|
"name":"Česká koruna",
|
|
"symbol":"Kč",
|
|
"price_round":100,
|
|
"price_round_order":0,
|
|
"price_round_direction":"math",
|
|
"price_precision":0,
|
|
"price_decimal_mark":",",
|
|
"rate":"1.0000",
|
|
"sync":"N",
|
|
"corrections":"0.00",
|
|
"sync_date_update":null
|
|
},
|
|
{
|
|
"id":"EUR",
|
|
"name":"Euro",
|
|
"symbol":"€",
|
|
"price_round":5,
|
|
"price_round_order":5,
|
|
"price_round_direction":"math",
|
|
"price_precision":2,
|
|
"price_decimal_mark":",",
|
|
"rate":"26.0000",
|
|
"sync":"Y",
|
|
"corrections":"0.00",
|
|
"sync_date_update":null
|
|
},
|
|
{
|
|
"id":"USD",
|
|
"name":"Dollar",
|
|
"symbol":"$",
|
|
"price_round":5,
|
|
"price_round_order":5,
|
|
"price_round_direction":"math",
|
|
"price_precision":2,
|
|
"price_decimal_mark":",",
|
|
"rate":"22.0000",
|
|
"sync":"Y",
|
|
"corrections":"0.00",
|
|
"sync_date_update":null
|
|
}
|
|
]
|
|
}
|
|
');
|
|
}
|
|
}
|