22
33namespace Phug \Test \Component ;
44
5+ use DOMAttr ;
6+ use DOMDocument ;
57use Exception ;
68use PHPUnit \Framework \TestCase ;
79use Phug \Compiler \Event \NodeEvent ;
2123use Phug \Util \Partial \ValueTrait ;
2224use Pug \Pug ;
2325use ReflectionException ;
26+ use SimpleXMLElement ;
2427use XhtmlFormatter \Formatter ;
2528
2629/**
@@ -63,7 +66,7 @@ protected function format(string $html): string
6366 protected function getReadmeContents (): string
6467 {
6568 if ($ this ->readme === null ) {
66- $ this ->readme = file_get_contents (__DIR__ . '/../../../README.md ' );
69+ $ this ->readme = str_replace ( "\r" , '' , file_get_contents (__DIR__ . '/../../../README.md ' ) );
6770 }
6871
6972 return $ this ->readme ;
@@ -403,15 +406,16 @@ public function testNamespace()
403406 public function getPugPhpTestsTemplates (): array
404407 {
405408 return array_map (function ($ file ) {
406- return [$ file , substr ($ file , 0 , -5 ).'.pug ' ];
407- }, glob (__DIR__ .'/../../templates/*.html ' ));
409+ $ file = basename ($ file );
410+
411+ return [substr ($ file , 0 , -4 ).'.html ' , $ file ];
412+ }, glob (__DIR__ .'/../../templates/*.pug ' ));
408413 }
409414
410415 /**
411416 * @dataProvider getPugPhpTestsTemplates
412417 *
413418 * @covers ::attachEvents
414- * @covers ::parseOutput
415419 *
416420 * @param string $htmlFile Expected output template file
417421 * @param string $pugFile Input template file
@@ -420,36 +424,117 @@ public function getPugPhpTestsTemplates(): array
420424 */
421425 public function testPugPhpTestsTemplates (string $ htmlFile , string $ pugFile )
422426 {
427+ $ templateFolder = __DIR__ .'/../../templates/ ' ;
423428 $ pug = new Pug ([
424429 'debug ' => false ,
425430 'pretty ' => true ,
426431 ]);
427432 ComponentExtension::enable ($ pug );
428433
434+ $ actualContent = $ this ->rawHtml ($ pug ->renderFile ($ templateFolder . $ pugFile , []));
435+ $ altHtmlFile = $ templateFolder . strtr ($ htmlFile , ['.html ' => '.alt.html ' ]);
436+
437+ if (file_exists ($ altHtmlFile )) {
438+ if ($ this ->rawHtml (file_get_contents ($ altHtmlFile )) === $ actualContent ) {
439+ $ this ->assertTrue (true );
440+
441+ return ;
442+ }
443+ }
444+
429445 $ this ->assertSame (
430- $ this ->rawHtml (file_get_contents ($ htmlFile )),
431- $ this -> rawHtml ( $ pug -> renderFile ( $ pugFile , [])) ,
446+ $ this ->rawHtml (file_get_contents ($ templateFolder . $ htmlFile )),
447+ $ actualContent ,
432448 basename ($ pugFile )
433449 );
434450 }
435451
436452 private function rawHtml ($ html )
437453 {
438- $ html = strtr ($ html , [
439- "' " => '" ' ,
440- "\r" => '' ,
441- ]);
454+ $ html = strtr ($ html , ["\r" => '' ]);
442455 $ html = preg_replace ('`\n{2,}` ' , "\n" , $ html );
443456 $ html = preg_replace ('`(?<!\n) {2,}` ' , ' ' , $ html );
444457 $ html = preg_replace ('` *$`m ' , '' , $ html );
445458 $ html = $ this ->format ($ html );
446- $ html = preg_replace_callback ('`(<(?:style|script)(?:[^>]*)>)([\s\S]+)(</(?:style|script)>)` ' , function ($ matches ) {
447- [, $ start , $ content , $ end ] = $ matches ;
448- $ content = trim (preg_replace ('`^ *`m ' , '' , $ content ));
459+ $ html = preg_replace_callback (
460+ '`(<(?:style|script)[^>]*>)([\s\S]+)(</(?:style|script)>)` ' ,
461+ function ($ matches ) {
462+ [, $ start , $ content , $ end ] = $ matches ;
463+ $ content = trim (preg_replace ('`^ *`m ' , '' , $ content ));
449464
450- return "$ start \n$ content \n$ end " ;
451- }, $ html );
465+ return "$ start \n$ content \n$ end " ;
466+ },
467+ $ html
468+ );
469+ $ html = preg_replace_callback (
470+ '`class="([^"]+)"` ' ,
471+ function ($ matches ) {
472+ $ classes = preg_split ('/\s+/ ' , $ matches [1 ]);
473+ sort ($ classes );
474+
475+ return 'class=" ' . implode (' ' , $ classes ) . '" ' ;
476+ },
477+ $ html
478+ );
479+ $ document = new DOMDocument ();
480+ $ html = preg_replace_callback (
481+ '/(?<start><(?<tag>\w+)\s(?<parameters>[^>]+))>/ ' ,
482+ function ($ matches ) use ($ document ) {
483+ try {
484+ $ tag = rtrim ($ matches ['start ' ], '/ ' ) . '/> ' ;
485+ $ document ->loadHTML ("<html><body> $ tag</body></html> " );
486+ $ node = $ document ->getElementsByTagName ('body ' )[0 ]->firstChild ;
487+ } catch (Exception $ error ) {
488+ return $ matches [0 ];
489+ }
490+
491+ $ attributes = iterator_to_array ($ node ->attributes );
492+ ksort ($ attributes );
493+
494+ return '< ' . $ matches ['tag ' ] .
495+ implode ('' , array_map (
496+ [$ this , 'formatAttribute ' ],
497+ $ attributes
498+ )) .
499+ '> ' ;
500+ },
501+ $ html
502+ );
503+ $ html = preg_replace_callback (
504+ '/(?<start><(\w+)(?:\s[^>]+)?>)(?<content>[^<]+)(?<end><\/\2>)/ ' ,
505+ function ($ matches ) use ($ document ) {
506+ return $ matches ['start ' ] .
507+ $ this ->escapeQuotes ($ matches ['content ' ]) .
508+ $ matches ['end ' ];
509+ },
510+ $ html
511+ );
452512
453513 return $ html ;
454514 }
515+
516+ private function formatAttribute (DOMAttr $ attribute ): string
517+ {
518+ $ name = $ attribute ->name ;
519+ $ value = $ attribute ->textContent ;
520+
521+ if ($ name === 'data-items ' ) {
522+ var_dump ($ value );
523+ exit ;
524+ }
525+
526+ if ($ value === '' ) {
527+ return " $ name " ;
528+ }
529+
530+ return " $ name= \"" . $ this ->escapeQuotes ($ value ) . '" ' ;
531+ }
532+
533+ private function escapeQuotes (string $ input ): string
534+ {
535+ return strtr ($ input , [
536+ '" ' => '" ' ,
537+ "' " => '' ' ,
538+ ]);
539+ }
455540}
0 commit comments