spatie / phpunit-snapshot-assertions

A way to test without writing actual test cases
https://spatie.be/courses/testing-laravel-with-pest/snapshot-testing
MIT License
645 stars 71 forks source link

UTF8 support for assertMatchesHtmlSnapshot #104

Closed jacekkarczmarczyk closed 4 years ago

jacekkarczmarczyk commented 4 years ago

Following code replaces utf8 characters with html entities in snapshot:

<?php

use PHPUnit\Framework\TestCase;
use Spatie\Snapshots\MatchesSnapshots;

final class SomeTest extends TestCase
{
    use MatchesSnapshots;

    public function testUtf(): void
    {
        $this->assertMatchesHtmlSnapshot('<p>żółć देवनागरी Кириллица</p>');
    }
}

Result:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>&Aring;&frac14;&Atilde;&sup3;&Aring;&#130;&Auml;&#135; &agrave;&curren;&brvbar;&agrave;&yen;&#135;&agrave;&curren;&micro;&agrave;&curren;&uml;&agrave;&curren;&frac34;&agrave;&curren;&#151;&agrave;&curren;&deg;&agrave;&yen;&#128; &ETH;&#154;&ETH;&cedil;&Ntilde;&#128;&ETH;&cedil;&ETH;&raquo;&ETH;&raquo;&ETH;&cedil;&Ntilde;&#134;&ETH;&deg;</p></body></html>

Maybe assertMatchesHtmlSnapshot could accept encoding as a second parameter (it could be passed to new HtmlDriver($encoding) and then to $domDocument = new DOMDocument('1.0', $encoding);?

jacekkarczmarczyk commented 4 years ago

Looks it works fine with proper encoding in <meta>

<?php

use PHPUnit\Framework\TestCase;
use Spatie\Snapshots\MatchesSnapshots;

final class SomeTest extends TestCase
{
    use MatchesSnapshots;

    public function testUtf(): void
    {
        $this->assertMatchesHtmlSnapshot('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><p>żółć देवनागरी Кириллица</p>');
    }
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body><p>żółć देवनागरी Кириллица</p></body>
</html>