Skip to content

Commit ccb1e5c

Browse files
authored
JunitFormatterTest: pretty print xml (#116)
1 parent 755c94f commit ccb1e5c

File tree

3 files changed

+111
-10
lines changed

3 files changed

+111
-10
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"ext-tokenizer": "*"
2525
},
2626
"require-dev": {
27+
"ext-dom": "*",
28+
"ext-libxml": "*",
2729
"editorconfig-checker/editorconfig-checker": "^10.3.0",
2830
"ergebnis/composer-normalize": "^2.19",
2931
"phpstan/phpstan": "^1.10.63",

composer.lock

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/JunitFormatterTest.php

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ShipMonk\ComposerDependencyAnalyser;
44

55
use Closure;
6+
use DOMDocument;
67
use PHPUnit\Framework\TestCase;
78
use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
89
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;
@@ -13,12 +14,15 @@
1314
use function ob_get_clean;
1415
use function ob_start;
1516
use function str_replace;
17+
use function trim;
18+
use const LIBXML_NOEMPTYTAG;
1619

1720
class 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>
3338
OUT;
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>
3749
OUT;
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>
81128
OUT;
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>
84166
OUT;
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

Comments
 (0)