Skip to content

Commit 7feaa97

Browse files
authored
junit: eliminate newlines inside <failure> (#174)
1 parent 47ea1c8 commit 7feaa97

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

src/Result/JunitFormatter.php

+13-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use function count;
1414
use function extension_loaded;
1515
use function htmlspecialchars;
16-
use function implode;
1716
use function sprintf;
1817
use function strlen;
1918
use function strpos;
@@ -161,18 +160,14 @@ private function createSymbolBasedTestSuite(string $title, array $errors, int $m
161160
foreach ($errors as $symbol => $usages) {
162161
$xml .= sprintf('<testcase name="%s">', $this->escape($symbol));
163162

164-
$failureUsage = [];
165-
166163
foreach ($usages as $index => $usage) {
167-
$failureUsage[] = $this->relativizeUsage($usage);
164+
$xml .= sprintf('<failure>%s</failure>', $this->escape($this->relativizeUsage($usage)));
168165

169-
if ($index === $maxShownUsages) {
166+
if ($index === $maxShownUsages - 1) {
170167
break;
171168
}
172169
}
173170

174-
$xml .= sprintf('<failure>%s</failure>', $this->escape(implode("\n", $failureUsage)));
175-
176171
$xml .= '</testcase>';
177172
}
178173

@@ -194,17 +189,17 @@ private function createPackageBasedTestSuite(string $title, array $errors, int $
194189
$printedSymbols = 0;
195190

196191
foreach ($usagesPerClassname as $symbol => $usages) {
197-
$printedSymbols++;
198-
$xml .= sprintf(
199-
'<failure message="%s">%s</failure>',
200-
$symbol,
201-
$this->escape(
202-
implode("\n", $this->createUsages($usages, $maxShownUsages))
203-
)
204-
);
205-
206-
if ($printedSymbols === $maxShownUsages) {
207-
break;
192+
foreach ($this->createUsages($usages, $maxShownUsages) as $usage) {
193+
$printedSymbols++;
194+
$xml .= sprintf(
195+
'<failure message="%s">%s</failure>',
196+
$symbol,
197+
$this->escape($usage)
198+
);
199+
200+
if ($printedSymbols === $maxShownUsages) {
201+
break 2;
202+
}
208203
}
209204
}
210205

tests/JunitFormatterTest.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public function testPrintResult(): void
4747
10,
4848
0.123,
4949
[],
50-
['Unknown\\Thing' => [new SymbolUsage('/app/app/init.php', 1093, SymbolKind::CLASSLIKE)]],
50+
['Unknown\\Thing' => [
51+
new SymbolUsage('/app/app/init.php', 1091, SymbolKind::CLASSLIKE),
52+
new SymbolUsage('/app/app/init.php', 1093, SymbolKind::CLASSLIKE),
53+
]],
5154
['Unknown\\function' => [new SymbolUsage('/app/app/foo.php', 51, SymbolKind::FUNCTION)]],
5255
[
5356
'shadow/package' => [
@@ -76,7 +79,7 @@ public function testPrintResult(): void
7679
});
7780
$verboseOutput = $this->getFormatterNormalizedOutput(static function ($formatter) use ($analysisResult): void {
7881
$options = new CliOptions();
79-
$options->verbose = true;
82+
$options->showAllUsages = true;
8083
$formatter->format($analysisResult, $options, new Configuration());
8184
});
8285

@@ -85,7 +88,7 @@ public function testPrintResult(): void
8588
<testsuites>
8689
<testsuite name="unknown classes" failures="1">
8790
<testcase name="Unknown\Thing">
88-
<failure>app/init.php:1093</failure>
91+
<failure>app/init.php:1091</failure>
8992
</testcase>
9093
</testsuite>
9194
<testsuite name="unknown functions" failures="1">
@@ -120,6 +123,7 @@ public function testPrintResult(): void
120123
<testsuites>
121124
<testsuite name="unknown classes" failures="1">
122125
<testcase name="Unknown\Thing">
126+
<failure>app/init.php:1091</failure>
123127
<failure>app/init.php:1093</failure>
124128
</testcase>
125129
</testsuite>
@@ -135,9 +139,11 @@ public function testPrintResult(): void
135139
<testcase name="shadow/package">
136140
<failure message="Forth\Provider">src/bootstrap.php:873</failure>
137141
<failure message="Shadow\Comparator">src/Printer.php:25</failure>
138-
<failure message="Shadow\Utils">src/Utils.php:19
139-
src/Utils.php:22
140-
src/Application.php:128</failure>
142+
<failure message="Shadow\Utils">src/Utils.php:19</failure>
143+
<failure message="Shadow\Utils">src/Utils.php:22</failure>
144+
<failure message="Shadow\Utils">src/Application.php:128</failure>
145+
<failure message="Shadow\Utils">src/Controller.php:229</failure>
146+
<failure message="Third\Parser">src/bootstrap.php:317</failure>
141147
</testcase>
142148
</testsuite>
143149
<testsuite name="dev dependencies in production code" failures="1">
@@ -151,7 +157,7 @@ public function testPrintResult(): void
151157
<testsuite name="unused dependencies" failures="1">
152158
<testcase name="dead/package"></testcase>
153159
</testsuite>
154-
<!-- showing only first 3 example failure usages -->
160+
<!-- showing all failure usages -->
155161
</testsuites>
156162
OUT;
157163

0 commit comments

Comments
 (0)