1

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

Jean-David Lanz
  • 865
  • 9
  • 18
  • 1
    FYI: IIRC the _assertSelectorTextContains_ assertion is from Symfony (not Phpunit) and as you already found out, the text is without the HTML entities. The encoding of the text likely is UTF-8. – hakre Jun 27 '21 at 22:42

1 Answers1

2

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.

Jean-David Lanz
  • 865
  • 9
  • 18
  • 1
    You can also try with a double quoted string and the unicode codepoint escape syntax: `"Simple\u{A0}! ..."` https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double – hakre Jun 27 '21 at 22:39