View source
<?php
namespace Drupal\Tests\commerce_price\Kernel;
use CommerceGuys\Intl\Currency\Currency;
use CommerceGuys\Intl\Exception\UnknownCurrencyException;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
class CurrencyRepositoryTest extends CommerceKernelTestBase {
protected $currencyRepository;
protected function setUp() : void {
parent::setUp();
$currency_importer = $this->container
->get('commerce_price.currency_importer');
$currency_importer
->import('EUR');
$this->currencyRepository = $this->container
->get('commerce_price.currency_repository');
}
public function testUnknownGet() {
$this
->expectException(UnknownCurrencyException::class);
$this->currencyRepository
->get('RSD');
}
public function testGet() {
$expected_eur = new Currency([
'currency_code' => 'EUR',
'name' => 'Euro',
'numeric_code' => '978',
'symbol' => '€',
'fraction_digits' => 2,
'locale' => 'en',
]);
$expected_usd = new Currency([
'currency_code' => 'USD',
'name' => 'US Dollar',
'numeric_code' => '840',
'symbol' => '$',
'fraction_digits' => 2,
'locale' => 'en',
]);
$this
->assertEquals($expected_eur, $this->currencyRepository
->get('EUR'));
$this
->assertEquals($expected_usd, $this->currencyRepository
->get('USD'));
}
public function testGetAll() {
$expected = [
'EUR' => $this->currencyRepository
->get('EUR'),
'USD' => $this->currencyRepository
->get('USD'),
];
$this
->assertEquals($expected, $this->currencyRepository
->getAll());
}
public function testGetList() {
$expected_list = [
'EUR' => 'Euro',
'USD' => 'US Dollar',
];
$this
->assertEquals($expected_list, $this->currencyRepository
->getList());
}
public function testGetDefaultFractionDigits(string $currency_code, int $expected_fraction_digits) {
$this
->assertEquals($this->currencyRepository
->getDefaultFractionDigits($currency_code), $expected_fraction_digits);
}
public function fractionDigitsData() {
return [
[
'BHD',
3,
],
[
'UGX',
0,
],
[
'USD',
2,
],
[
'UYU',
2,
],
[
'UYW',
4,
],
];
}
}