33namespace ShipMonk \ComposerDependencyAnalyser ;
44
55use Closure ;
6+ use DOMDocument ;
67use PHPUnit \Framework \TestCase ;
78use ShipMonk \ComposerDependencyAnalyser \Config \Configuration ;
89use ShipMonk \ComposerDependencyAnalyser \Config \ErrorType ;
1314use function ob_get_clean ;
1415use function ob_start ;
1516use function str_replace ;
17+ use function trim ;
18+ use const LIBXML_NOEMPTYTAG ;
1619
1720class JunitFormatterTest extends TestCase
1821{
1922
2023 public function testPrintResult (): void
2124 {
25+ // editorconfig-checker-disable
2226 $ formatter = new JunitFormatter ('/app ' , new Printer ());
2327
2428 $ noIssuesOutput = $ this ->captureAndNormalizeOutput (static function () use ($ formatter ): void {
@@ -29,15 +33,23 @@ public function testPrintResult(): void
2933 });
3034
3135 $ expectedNoIssuesOutput = <<<'OUT'
32- <?xml version="1.0" encoding="UTF-8"?><testsuites></testsuites>
36+ <?xml version="1.0" encoding="UTF-8"?>
37+ <testsuites></testsuites>
3338OUT;
3439
3540 $ expectedNoIssuesButWarningsOutput = <<<'OUT'
36- <?xml version="1.0" encoding="UTF-8"?><testsuites><testsuite name="unused-ignore" failures="1"><testcase name="shadow-dependency"><failure>'shadow-dependency' was globally ignored, but it was never applied.</failure></testcase></testsuite></testsuites>
41+ <?xml version="1.0" encoding="UTF-8"?>
42+ <testsuites>
43+ <testsuite name="unused-ignore" failures="1">
44+ <testcase name="shadow-dependency">
45+ <failure>'shadow-dependency' was globally ignored, but it was never applied.</failure>
46+ </testcase>
47+ </testsuite>
48+ </testsuites>
3749OUT;
3850
39- self ::assertSame ($ this ->normalizeEol ($ expectedNoIssuesOutput ), $ noIssuesOutput );
40- self ::assertSame ($ this ->normalizeEol ($ expectedNoIssuesButWarningsOutput ), $ noIssuesButUnusedIgnores );
51+ self ::assertSame ($ this ->normalizeEol ($ expectedNoIssuesOutput ), $ this -> prettyPrintXml ( $ noIssuesOutput) );
52+ self ::assertSame ($ this ->normalizeEol ($ expectedNoIssuesButWarningsOutput ), $ this -> prettyPrintXml ( $ noIssuesButUnusedIgnores) );
4153
4254 $ analysisResult = new AnalysisResult (
4355 10 ,
@@ -77,14 +89,85 @@ public function testPrintResult(): void
7789 });
7890
7991 $ expectedRegularOutput = <<<'OUT'
80- <?xml version="1.0" encoding="UTF-8"?><testsuites><testsuite name="unknown classes" failures="1"><testcase name="Unknown\Thing"><failure>in app/init.php:1093</failure></testcase></testsuite><testsuite name="unknown functions" failures="1"><testcase name="Unknown\function"><failure>in app/foo.php:51</failure></testcase></testsuite><testsuite name="shadow dependencies" failures="2"><testcase name="shadow/another"><failure>e.g. Another\Controller in src/bootstrap.php:173</failure></testcase><testcase name="shadow/package"><failure>e.g. Forth\Provider in src/bootstrap.php:873 (+ 6 more)</failure></testcase></testsuite><testsuite name="dev dependencies in production code" failures="1"><testcase name="some/package"><failure>e.g. Another\Command in src/ProductGenerator.php:28</failure></testcase></testsuite><testsuite name="prod dependencies used only in dev paths" failures="1"><testcase name="misplaced/package"><failure></failure></testcase></testsuite><testsuite name="unused dependencies" failures="1"><testcase name="dead/package"><failure></failure></testcase></testsuite></testsuites>
92+ <?xml version="1.0" encoding="UTF-8"?>
93+ <testsuites>
94+ <testsuite name="unknown classes" failures="1">
95+ <testcase name="Unknown\Thing">
96+ <failure>in app/init.php:1093</failure>
97+ </testcase>
98+ </testsuite>
99+ <testsuite name="unknown functions" failures="1">
100+ <testcase name="Unknown\function">
101+ <failure>in app/foo.php:51</failure>
102+ </testcase>
103+ </testsuite>
104+ <testsuite name="shadow dependencies" failures="2">
105+ <testcase name="shadow/another">
106+ <failure>e.g. Another\Controller in src/bootstrap.php:173</failure>
107+ </testcase>
108+ <testcase name="shadow/package">
109+ <failure>e.g. Forth\Provider in src/bootstrap.php:873 (+ 6 more)</failure>
110+ </testcase>
111+ </testsuite>
112+ <testsuite name="dev dependencies in production code" failures="1">
113+ <testcase name="some/package">
114+ <failure>e.g. Another\Command in src/ProductGenerator.php:28</failure>
115+ </testcase>
116+ </testsuite>
117+ <testsuite name="prod dependencies used only in dev paths" failures="1">
118+ <testcase name="misplaced/package">
119+ <failure></failure>
120+ </testcase>
121+ </testsuite>
122+ <testsuite name="unused dependencies" failures="1">
123+ <testcase name="dead/package">
124+ <failure></failure>
125+ </testcase>
126+ </testsuite>
127+ </testsuites>
81128OUT;
82129 $ expectedVerboseOutput = <<<'OUT'
83- <?xml version="1.0" encoding="UTF-8"?><testsuites><testsuite name="unknown classes" failures="1"><testcase name="Unknown\Thing"><failure>app/init.php:1093</failure></testcase></testsuite><testsuite name="unknown functions" failures="1"><testcase name="Unknown\function"><failure>app/foo.php:51</failure></testcase></testsuite><testsuite name="shadow dependencies" failures="2"><testcase name="shadow/another"><failure>Another\Controller\n src/bootstrap.php:173</failure></testcase><testcase name="shadow/package"><failure>Forth\Provider\n src/bootstrap.php:873\nShadow\Comparator\n src/Printer.php:25\nShadow\Utils\n src/Utils.php:19\n src/Utils.php:22\n src/Application.php:128\n + 1 more\n + 1 more symbol</failure></testcase></testsuite><testsuite name="dev dependencies in production code" failures="1"><testcase name="some/package"><failure>Another\Command\n src/ProductGenerator.php:28</failure></testcase></testsuite><testsuite name="prod dependencies used only in dev paths" failures="1"><testcase name="misplaced/package"><failure></failure></testcase></testsuite><testsuite name="unused dependencies" failures="1"><testcase name="dead/package"><failure></failure></testcase></testsuite></testsuites>
130+ <?xml version="1.0" encoding="UTF-8"?>
131+ <testsuites>
132+ <testsuite name="unknown classes" failures="1">
133+ <testcase name="Unknown\Thing">
134+ <failure>app/init.php:1093</failure>
135+ </testcase>
136+ </testsuite>
137+ <testsuite name="unknown functions" failures="1">
138+ <testcase name="Unknown\function">
139+ <failure>app/foo.php:51</failure>
140+ </testcase>
141+ </testsuite>
142+ <testsuite name="shadow dependencies" failures="2">
143+ <testcase name="shadow/another">
144+ <failure>Another\Controller\n src/bootstrap.php:173</failure>
145+ </testcase>
146+ <testcase name="shadow/package">
147+ <failure>Forth\Provider\n src/bootstrap.php:873\nShadow\Comparator\n src/Printer.php:25\nShadow\Utils\n src/Utils.php:19\n src/Utils.php:22\n src/Application.php:128\n + 1 more\n + 1 more symbol</failure>
148+ </testcase>
149+ </testsuite>
150+ <testsuite name="dev dependencies in production code" failures="1">
151+ <testcase name="some/package">
152+ <failure>Another\Command\n src/ProductGenerator.php:28</failure>
153+ </testcase>
154+ </testsuite>
155+ <testsuite name="prod dependencies used only in dev paths" failures="1">
156+ <testcase name="misplaced/package">
157+ <failure></failure>
158+ </testcase>
159+ </testsuite>
160+ <testsuite name="unused dependencies" failures="1">
161+ <testcase name="dead/package">
162+ <failure></failure>
163+ </testcase>
164+ </testsuite>
165+ </testsuites>
84166OUT;
85167
86- self ::assertSame ($ this ->normalizeEol ($ expectedRegularOutput ), $ regularOutput );
87- self ::assertSame ($ this ->normalizeEol ($ expectedVerboseOutput ), $ verboseOutput );
168+ self ::assertSame ($ this ->normalizeEol ($ expectedRegularOutput ), $ this ->prettyPrintXml ($ regularOutput ));
169+ self ::assertSame ($ this ->normalizeEol ($ expectedVerboseOutput ), $ this ->prettyPrintXml ($ verboseOutput ));
170+ // editorconfig-checker-enable
88171 }
89172
90173 /**
@@ -102,4 +185,17 @@ private function normalizeEol(string $string): string
102185 return str_replace ("\r\n" , "\n" , $ string );
103186 }
104187
188+ private function prettyPrintXml (string $ inputXml ): string
189+ {
190+ $ dom = new DOMDocument ();
191+ $ dom ->preserveWhiteSpace = false ;
192+ $ dom ->formatOutput = true ;
193+ $ dom ->loadXML ($ inputXml );
194+
195+ $ outputXml = $ dom ->saveXML (null , LIBXML_NOEMPTYTAG );
196+ self ::assertNotFalse ($ outputXml );
197+
198+ return trim ($ outputXml );
199+ }
200+
105201}
0 commit comments