Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLEANUP] Whitespace-agnostic HTML test comparison #855

Merged
merged 3 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@
<code>assertInstanceOf</code> <!-- retained as per discussion in #537 -->
</RedundantConditionGivenDocblockType>
</file>
<!-- NonStaticSelfCall, to be reviewed with #792 -->
<file src="tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php">
<NonStaticSelfCall occurrences="1">
<code>self::assertSame($normalizedExpected, $normalizedActual, $message)</code>
</NonStaticSelfCall>
<RedundantConditionGivenDocblockType occurrences="4">
<code>assertInstanceOf</code> <!-- retained as per discussion in #537 -->
<code>assertInstanceOf</code> <!-- retained as per discussion in #537 -->
Expand Down
58 changes: 55 additions & 3 deletions tests/Unit/HtmlProcessor/AbstractHtmlProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,20 @@ public function renderRendersDocumentProvidedToFromDomDocument()
/**
* @test
*/
public function reformatsHtml()
public function renderPreservesBodyContentProvidedToFromHtml()
{
$innerHtml = '<p>Hello world!</p>';
$subject = TestingHtmlProcessor::fromHtml('<html>' . $innerHtml . '</html>');

$html = $subject->render();

self::assertContains($innerHtml, $html);
}

/**
* @test
*/
public function renderPreservesOuterHtmlProvidedToFromHtml()
{
$rawHtml = '<!DOCTYPE HTML>' .
'<html>' .
Expand All @@ -93,8 +106,9 @@ public function reformatsHtml()
"</html>\n";

$subject = TestingHtmlProcessor::fromHtml($rawHtml);
$html = $subject->render();

self::assertSame($formattedHtml, $subject->render());
self::assertEqualsHtml($formattedHtml, $html);
}

/**
Expand Down Expand Up @@ -706,7 +720,7 @@ public function getDomDocumentWithNormalizedHtmlRepresentsTheGivenHtml()

$domDocument = $subject->getDomDocument();

self::assertSame($html, $domDocument->saveHTML());
self::assertEqualsHtml($html, $domDocument->saveHTML());
}

/**
Expand All @@ -732,4 +746,42 @@ public function getDomDocumentVoidElementNotHasChildNodes(string $htmlWithNonXml
self::assertFalse($element->hasChildNodes());
}
}

/**
* Asserts that two HTML strings are equal, allowing for whitespace differences in the HTML element itself (but not
* its descendants) and after its closing tag.
*
* @param string $expected
* @param string $actual
* @param string $message
*/
private static function assertEqualsHtml(string $expected, string $actual, string $message = '')
{
$normalizedExpected = self::normalizeHtml($expected);
$normalizedActual = self::normalizeHtml($actual);

self::assertSame($normalizedExpected, $normalizedActual, $message);
}

/**
* Normalizes whitespace in the HTML element itself (but not its descendants) and after its closing tag, with a
* single newline inserted or replacing whitespace at positions where whitespace may occur but is superfluous.
*
* @param string $html
*
* @return string
*/
private static function normalizeHtml(string $html)
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
oliverklee marked this conversation as resolved.
Show resolved Hide resolved
{
return \preg_replace(
[
'%(<html(?=[\\s>])[^>]*+>)\\s*+(<head[\\s>])%',
'%(</head>)\\s*+(<body[\\s>])%',
'%(</body>)\\s*+(</html>)%',
'%(</html>)\\s*+($)%',
],
"$1\n$2",
$html
);
}
}