Files
firefly-iii/tests/Support/AmountTest.php
Hosh Sadiq 14dd185717 Use php-intl to format currencies
Currently the php function `number_format` is used to format currencies.
This is problematic as we have to figure out different things for
different currencies ourselves. These formats are determined based on
the libc's locale functions.

The issue arises where an OS doesn't have the proper locales installed,
or, in some cases, it's not supported (see below on multiple issues).

This addresses this issue by using the php-intl extensions to format the
numbers based on the locale. The extension is already a requirement in
`composer.json`. The solution does not rely on `LC_MONETARY` from the
underlying libc (which in Alpine Linux's case, which uses musl, is not
supported as of yet).

List of issues that are related and would potentially be fixed using
this PR:

- #2298
- #2946
- #3070
- #3306
- #3519
2020-07-19 18:34:39 +01:00

80 lines
3.8 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Support;
use FireflyIII\Support\Amount;
use Tests\TestCase;
use Steam;
class AmountTest extends TestCase
{
/**
* @dataProvider getTestLocales
* @param string $locale
* @param string $expectedAmount
* @param string $symbol
* @param int $decimalPlaces
* @param string $amount
*/
public function testFormatFlat(string $locale, string $expectedAmount, string $symbol, int $decimalPlaces, string $amount)
{
$this->mockDefaultConfiguration();
Steam::shouldReceive('getLocale')->andReturn($locale);
Steam::shouldReceive('getLocaleArray')->andReturn([$locale . ".UTF-8"]);
$amountObj = new Amount();
$result = $amountObj->formatFlat($symbol, $decimalPlaces, $amount, false);
$this->assertEquals($expectedAmount, $result);
}
public function getTestLocales()
{
return [
['en_US', '£6,000.00', '£', 2, '6000.00000000'],
['en_US', '-£6,000.00', '£', 2, '-6000.00000000'],
['en_US', '$6,000.00', '$', 2, '6000.00000000'],
['en_US', '-$6,000.00', '$', 2, '-6000.00000000'],
['en_GB', '£6,000.00', '£', 2, '6000.00000000'],
['en_GB', '-£6,000.00', '£', 2, '-6000.00000000'],
['en_GB', '$6,000.00', '$', 2, '6000.00000000'],
['en_GB', '-$6,000.00', '$', 2, '-6000.00000000'],
['cs_CZ', '6 000,00 Kč', 'Kč', 2, '6000.00000000'],
['cs_CZ', '-6 000,00 Kč', 'Kč', 2, '-6000.00000000'],
['el_GR', '6.000,00 €', '€', 2, '6000.00000000'],
['el_GR', '-6.000,00 €', '€', 2, '-6000.00000000'],
['es_ES', '6.000,00 €', '€', 2, '6000.00000000'],
['es_ES', '-6.000,00 €', '€', 2, '-6000.00000000'],
['de_DE', '6.000,00 €', '€', 2, '6000.00000000'],
['de_DE', '-6.000,00 €', '€', 2, '-6000.00000000'],
['fr_FR', '6000,00 €', '€', 2, '6000.00000000'],
['fr_FR', '-6000,00 €', '€', 2, '-6000.00000000'],
['it_IT', '6.000,00 €', '€', 2, '6000.00000000'],
['it_IT', '-6.000,00 €', '€', 2, '-6000.00000000'],
['nb_NO', 'kr 6 000,00', 'kr', 2, '6000.00000000'],
['nb_NO', 'kr 6 000,00', 'kr', 2, '-6000.00000000'],
['nl_NL', '€ 6.000,00', '€', 2, '6000.00000000'],
['nl_NL', '€ -6.000,00', '€', 2, '-6000.00000000'],
['pl_PL', '6 000,00 zł', 'zł', 2, '6000.00000000'],
['pl_PL', '-6 000,00 zł', 'zł', 2, '-6000.00000000'],
['pt_BR', 'R$ 6.000,00', 'R$', 2, '6000.00000000'],
['pt_BR', '-R$ 6.000,00', 'R$', 2, '-6000.00000000'],
['ro_RO', '6.000,00 lei', 'lei', 2, '6000.00000000'],
['ro_RO', '-6.000,00 lei', 'lei', 2, '-6000.00000000'],
['ru_RU', '6 000,00 ₽', '₽', 2, '6000.00000000'],
['ru_RU', '-6 000,00 ₽', '₽', 2, '-6000.00000000'],
['zh_TW', 'NT$6,000.00', 'NT$', 2, '6000.00000000'],
['zh_TW', '-NT$6,000.00', 'NT$', 2, '-6000.00000000'],
['zh_CN', '¥6,000.00', '¥', 2, '6000.00000000'],
['zh_CN', '-¥6,000.00', '¥', 2, '-6000.00000000'],
['hu_HU', '6 000,00 Ft', 'Ft', 2, '6000.00000000'],
['hu_HU', '-6 000,00 Ft', 'Ft', 2, '-6000.00000000'],
['sv_SE', '6 000,00 kr', 'kr', 2, '6000.00000000'],
['sv_SE', '6 000,00 kr', 'kr', 2, '-6000.00000000'],
['fi_FI', '6 000,00 €', '€', 2, '6000.00000000'],
['fi_FI', '6 000,00 €', '€', 2, '-6000.00000000'],
['vi_VN', '6.000,00 đ', 'đ', 2, '6000.00000000'],
['vi_VN', '-6.000,00 đ', 'đ', 2, '-6000.00000000'],
];
}
}