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

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, I had this problem and you just solved it 😀

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.