stevegrunwell / phpunit-markup-assertions

Assertions for PHPUnit to verify the presence or state of elements within markup

Home Page:https://stevegrunwell.com/blog/phpunit-markup-assertions/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FR: Support foreign character sets

tyler36 opened this issue · comments

Thank you for the package. It works for my simple needs but recently I came across an internationalization issue.

I have a Laravel 7 app that contains Japanese translations.

    /**
     * Test setup.
     */
    protected function setUp(): void
    {
        parent::setUp();

        $this->response = $this->get('/);
        $this->body = $this->response->content();
    }


    /** test */
    public function the_title_is_set_to_app_name()
    {
        $title = 'リンゴ;

        $this->assertNotNull($title);
        $this->assertElementContains($title, 'title', $this->body);
    }

However I encountered this error:

Failed asserting that 'リンゴ' contains "リンゴ".

Somewhere along the line, the encoding is getting muddled. Everything appears to be encoded correctly.

After working through the issue, I created a patch that resolved this issue for me. I attempted to create a PR for Hacktoberfest, but this solution messes with the expected HTML in provideInnerHtml cause a chain of failures.

--- /dev/null
+++ ../src/MarkupAssertionsTrait.php
@@ -14,7 +14,13 @@
 
 trait MarkupAssertionsTrait
 {
+
     /**
+     * Wrapper used to set character set
+     */
+    protected static $html_wrapper = '<html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"></head>';
+
+    /**
      * Assert that the given string contains an element matching the given selector.
      *
      * @since 1.0.0
@@ -257,6 +263,7 @@
         // Loop through results and collect their innerHTML values.
         foreach ($results as $result) {
             $document = new DOMDocument;
+            $document->loadHtml(SELF::$html_wrapper);
             $document->appendChild($document->importNode($result->firstChild, true));
 
             $contents[] = trim($document->saveHTML());

I thought about creating another parameters to pass through to enable this. Or adding addition assertElementContainsNonEnglishText functions but it didn't feel elequent enough.

What are your thoughts?