How to test for character references in Symfony with PHPUnit?

410 views Asked by At

I want to test this very simple page generated by my PHP/Symfony project

            <div>Simple&nbsp;! Tranquille&nbsp;! Excellent&nbsp;!</div>

(It's in French, so it needs the &nbsp; hard spaces in front of the exclamation points.)

I thought an equally simple test such as

        $this->assertSelectorTextContains('div', 'Simple&nbsp;! Tranquille&nbsp;! Excellent&nbsp;!');

would do the trick, but I get a failure.

Further inquiry shows that

        $texte = $crawler->filter("div")->first();
        $this->assertEquals($texte->text(), "Simple&nbsp;! Tranquille&nbsp;! Excellent&nbsp;!");

returns

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Simple ! Tranquille ! Excellent !'
+'Simple&nbsp;! Tranquille&nbsp;! Excellent&nbsp;!'

So, how do I help assertSelectorTextContains() (and more generally, PHPUnit) understand that both strings are actually the same? (Or at least equivalent?)

1

There are 1 answers

1
Jean-David Lanz On BEST ANSWER

Changing my search criteria yielded the answer, html_entity_decode():

        $this->assertSelectorTextContains('div', html_entity_decode('Simple&nbsp;! Tranquille&nbsp;! Excellent&nbsp;!'));

does the job.