6

I have a unit test where I am trying to test the output of NumberFormatter.

A simplified version of my code is:

public function testGetFormattedPrice()
{
    $formatter = NumberFormatter::create(
        "de_DE",
        NumbererFormatter::CURRENCY
    );

    $this->assertEquals(
        '16,66 €',
        $formatter->formatCurrency(16.66, "EUR")
    );
}

This results in a failure:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'16,66 €'
+'16,66 €'

I am assuming this is related to the Euro symbol (maybe character encoding) or some sort of hidden bytes in the string, but not really sure how to check this?

Can anyone give me some advice on how to debug this issue, or what might be the possible cause?

Cheers,

Mo

PsychoMo
  • 699
  • 5
  • 17

1 Answers1

9

So I found an answer, looks like NumberFormatter adds a non-breaking space to it's output (which makes sense for currency), more info here: https://www.php.net/manual/en/numberformatter.formatcurrency.php#118304

I was able to come up with a solution based on this: https://stackoverflow.com/a/40724830/4161644

public function testGetFormattedPrice()
{
    $formatter = NumberFormatter::create(
        "de_DE",
        NumbererFormatter::CURRENCY
    );

    $format = str_replace("\xc2\xa0", ' ', $formatter->formatCurrency(16.66, "EUR"));

    $this->assertEquals('16,66 €', $format);
}
PsychoMo
  • 699
  • 5
  • 17