Skip to content
Advertisement

How to test for character references in Symfony with PHPUnit?

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?)

Advertisement

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement