Skip to content

Commit

Permalink
Add currency helper test for failing strip zeros #3716
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Oct 14, 2024
1 parent a156731 commit 2308a21
Showing 1 changed file with 153 additions and 0 deletions.
153 changes: 153 additions & 0 deletions tests/unit/helpers/CurrencyHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,157 @@ public function formatAsCurrencyDataProvider(): array
],
];
}

/**
* @param string $currency
* @param string $language
* @param string $expected
* @return void
* @dataProvider formatAsCurrencyStripZerosDataProvider
* @since 5.1.4
*/
public function testFormatAsCurrencyStripZeros(string $currency, string $language, float $amount, bool $zeros, string $expected): void
{
$originalLocale = \Craft::$app->getLocale();
Locale::switchAppLanguage($language);
$formattedValue = Currency::formatAsCurrency($amount, $currency, stripZeros: $zeros);

self::assertEquals($expected, $formattedValue);
Locale::switchAppLanguage($originalLocale->getLanguageID());
}

/**
* @return array[]
*/
public function formatAsCurrencyStripZerosDataProvider(): array
{
return [
'USD-US' => [
'USD',
'en-US',
1234.56,
true,
'$1,234.56',
],
'USD-US-strip' => [
'USD',
'en-US',
1234.00,
true,
'$1,234',
],
'USD-US-no-strip' => [
'USD',
'en-US',
1234.00,
false,
'$1,234.00',
],
'USD-GB' => [
'USD',
'en-GB',
1234.56,
true,
'US$1,234.56',
],
'USD-GB-strip' => [
'USD',
'en-GB',
1234.0,
true,
'US$1,234',
],
'USD-GB-no-strip' => [
'USD',
'en-GB',
1234.0,
false,
'US$1,234.00',
],
'USD-FR' => [
'USD',
'fr-FR',
1234.56,
true,
'1 234,56 $US',
],
'USD-FR-strip' => [
'USD',
'fr-FR',
1234.00,
true,
'1 234 $US',
],
'USD-FR-no-strip' => [
'USD',
'fr-FR',
1234.00,
false,
'1 234,00 $US',
],
'EUR-US' => [
'EUR',
'en-US',
1234.56,
true,
'€1,234.56',
],
'EUR-US-strip' => [
'EUR',
'en-US',
1234.00,
true,
'€1,234',
],
'EUR-US-no-strip' => [
'EUR',
'en-US',
1234.00,
false,
'€1,234.00',
],
'EUR-FR' => [
'EUR',
'fr-FR',
1234.56,
true,
'1 234,56 €',
],
'EUR-FR-strip' => [
'EUR',
'fr-FR',
1234.00,
true,
'1 234 €',
],
'EUR-FR-no-strip' => [
'EUR',
'fr-FR',
1234.00,
false,
'1 234,00 €',
],
'EUR-ARABIC' => [
'EUR',
'ar',
1234.56,
true,
'١٬٢٣٤٫٥٦ €',
],
'EUR-ARABIC-strip' => [
'EUR',
'ar',
1234.00,
true,
'١٬٢٣٤ €',
],
'EUR-ARABIC-no-strip' => [
'EUR',
'ar',
1234.00,
false,
'١٬٢٣٤٫٠٠ €',
],
];
}
}

0 comments on commit 2308a21

Please sign in to comment.