1313
1414namespace phpDocumentor \Guides \Functional ;
1515
16+ use DOMDocument ;
1617use Exception ;
17- use Gajus \Dindent \Indenter ;
1818use League \Flysystem \Filesystem ;
1919use League \Flysystem \InMemory \InMemoryFilesystemAdapter ;
2020use Monolog \Handler \TestHandler ;
4949use function implode ;
5050use function in_array ;
5151use function is_string ;
52- use function rtrim ;
52+ use function libxml_clear_errors ;
53+ use function libxml_use_internal_errors ;
54+ use function preg_replace ;
5355use function setlocale ;
5456use function sprintf ;
5557use function str_replace ;
@@ -65,8 +67,6 @@ class_alias('League\Flysystem\Memory\MemoryAdapter', 'League\Flysystem\InMemory\
6567
6668final class FunctionalTest extends ApplicationTestCase
6769{
68- private const SKIP_INDENTER_FILES = ['code-block-diff ' ];
69-
7070 private const IGNORED_WARNINGS = ['Document has no title ' ];
7171
7272 protected function setUp (): void
@@ -81,7 +81,6 @@ public function testFunctional(
8181 string $ format ,
8282 string $ rst ,
8383 string $ expected ,
84- bool $ useIndenter = true ,
8584 array $ expectedLogs = [],
8685 ): void {
8786 $ expectedLines = explode ("\n" , $ expected );
@@ -146,19 +145,40 @@ public function testFunctional(
146145 );
147146 }
148147
149- if ($ format === 'html ' && $ useIndenter ) {
150- $ indenter = new Indenter ();
151- $ rendered = $ indenter ->indent ($ rendered );
152- }
153-
154148 if (isset ($ expectedExceptionMessage )) {
155149 return ;
156150 }
157151
158- self ::assertSame (
159- $ this ->trimTrailingWhitespace ($ expected ),
160- $ this ->trimTrailingWhitespace ($ rendered ),
161- );
152+ if ($ format === 'html ' ) {
153+ $ rendered = $ this ->removeRedundantWhitespaceFromHtml ($ rendered );
154+ $ expected = $ this ->removeRedundantWhitespaceFromHtml ($ expected );
155+
156+ $ previousUseInternalErrors = libxml_use_internal_errors (true );
157+ try {
158+ $ expectedDom = new DOMDocument ();
159+ $ expectedDom ->loadHTML ($ expected );
160+ $ expectedDom ->preserveWhiteSpace = false ;
161+
162+ $ actualDom = new DOMDocument ();
163+ $ actualDom ->loadHTML ($ rendered );
164+ $ actualDom ->preserveWhiteSpace = false ;
165+
166+ $ expectedHtml = $ expectedDom ->saveHTML ();
167+ $ actualHtml = $ actualDom ->saveHTML ();
168+
169+ self ::assertIsString ($ expectedHtml );
170+ self ::assertIsString ($ actualHtml );
171+
172+ self ::assertXmlStringEqualsXmlString ($ expectedHtml , $ actualHtml );
173+ } catch (Throwable ) {
174+ self ::assertSame (trim ($ expected ), trim ($ rendered ));
175+ } finally {
176+ libxml_clear_errors ();
177+ libxml_use_internal_errors ($ previousUseInternalErrors );
178+ }
179+ } else {
180+ self ::assertSame (trim ($ expected ), trim ($ rendered ));
181+ }
162182
163183 $ logHandler = $ this ->getContainer ()->get (TestHandler::class);
164184 assert ($ logHandler instanceof TestHandler);
@@ -227,8 +247,6 @@ public static function getFunctionalTests(): array
227247
228248 $ expected = $ file ->getContents ();
229249
230- $ useIndenter = !in_array ($ basename , self ::SKIP_INDENTER_FILES , true );
231-
232250 $ logFile = $ file ->getPath () . '/ ' . $ file ->getFilenameWithoutExtension () . '.log ' ;
233251 $ logs = [];
234252 if (file_exists ($ logFile )) {
@@ -237,21 +255,20 @@ public static function getFunctionalTests(): array
237255 $ logs = array_map (trim (...), $ logFileContent );
238256 }
239257
240- $ tests [$ basename . '_ ' . $ format ] = [$ basename , $ format , $ rst , trim ($ expected ), $ useIndenter , $ logs ];
258+ $ tests [$ basename . '_ ' . $ format ] = [$ basename , $ format , $ rst , trim ($ expected ), $ logs ];
241259 }
242260 }
243261
244262 return $ tests ;
245263 }
246264
247- private function trimTrailingWhitespace (string $ string ): string
265+ private function removeRedundantWhitespaceFromHtml (string $ html ): string
248266 {
249- $ lines = explode ("\n" , $ string );
250-
251- $ lines = array_map (static function (string $ line ): string {
252- return rtrim ($ line );
253- }, $ lines );
267+ $ html = implode ("\n" , array_map ('trim ' , explode ("\n" , $ html )));
268+ $ html = preg_replace ('#\s+# ' , ' ' , $ html ) ?? $ html ;
269+ $ html = preg_replace ('#\s<# ' , '< ' , $ html ) ?? $ html ;
270+ $ html = preg_replace ('#>\s# ' , '> ' , $ html ) ?? $ html ;
254271
255- return trim ( implode ( "\n" , $ lines )) ;
272+ return $ html ;
256273 }
257274}
0 commit comments