I'm having problems unit testing a library that uses NumberFormatter::formatCurrency. After some trial and error, I've narrowed down the problem to this test case:
/**
* @dataProvider getLocales()
*/
public function test($locale, $expected)
{
$number_formatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$actual = $number_formatter->formatCurrency(3000.05, 'EUR');
$this->assertEquals($expected, $actual, $locale.' failed');
}
public function getLocales()
{
return array(
array('en_US', '€3,000.05'),
array('fr_FR', '3 000,05 €'),
array('de_DE', '3.000,05 €'),
);
}
The results are:
fr_FR failed
Failed asserting that two strings are equal.
Expected :3 000,05 €
Actual :3 000,05 €
de_DE failed
Failed asserting that two strings are equal.
Expected :3.000,05 €
Actual :3.000,05 €
As you can see the failed tests seem to have identical strings, so it should be a problem of locale.
I've tried comparing with strcoll, setting the locale before the comparison, and other combinations with no luck.
I guess it's something to do with different utf-8 codes in each language. But how can I compare those strings then?
I found the solution. PHP's NumberFormatter inserts a special space character, the so-called No-Break-Space (maybe you know
).
if you convert the read value
str_replace("\xc2\xa0", " ", $actual);
everything will be fine.