Last active
June 13, 2022 14:30
-
-
Save tanakahisateru/7f9a8b7580adb74862ffb1232d73bef5 to your computer and use it in GitHub Desktop.
assertHtmlStringEqualsHtmlString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @method assertEquals(mixed $expected, mixed $actual, string $message) | |
*/ | |
trait AssetHtmlTrait | |
{ | |
public function assertHtmlStringEqualsHtmlString(string $expectedHtml, string $actualHtml, string $message = ''): void | |
{ | |
$expected = (new \PHPUnit\Util\Xml\Loader)->load( | |
$this->cleanHtml($expectedHtml), | |
true | |
); | |
$actual = (new \PHPUnit\Util\Xml\Loader)->load( | |
$this->cleanHtml($actualHtml), | |
true | |
); | |
$this->assertEquals($expected, $actual, $message); | |
} | |
protected function cleanHtml(string $html): string | |
{ | |
$html = preg_replace(['/>\s+/', '/\s+</'], ['>', '<'], $html); | |
return preg_replace('/\s+/', ' ', $html); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ExampleTest | |
{ | |
use AssetHtmlTrait; | |
public function testSomeOperation() | |
{ | |
$actual = sprintf(<<<HTML | |
<div><a href="%s" title="%s">link</a></div> | |
HTML, "https://example.com", "example.com"); | |
// You can check HTML equivalence: | |
$this->assertHtmlStringEqualsHtmlString(<<<HTML | |
<div> | |
<a | |
title="example.com" | |
href="https://example.com" | |
>link</a> | |
</div> | |
HTML), $actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment