Skip to content

Commit 95c32f3

Browse files
Validate generated Clover XML against Clover XSD
1 parent 90b3049 commit 95c32f3

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

tests/tests/Report/CloverTest.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
*/
1010
namespace SebastianBergmann\CodeCoverage\Report;
1111

12+
use const PHP_EOL;
13+
use function libxml_clear_errors;
14+
use function libxml_get_errors;
15+
use function libxml_use_internal_errors;
16+
use function sprintf;
17+
use function trim;
18+
use DOMDocument;
1219
use PHPUnit\Framework\Attributes\CoversClass;
1320
use SebastianBergmann\CodeCoverage\TestCase;
1421

@@ -19,7 +26,7 @@ public function testLineCoverageForBankAccountTest(): void
1926
{
2027
$clover = new Clover;
2128

22-
$this->assertStringMatchesFormatFile(
29+
$this->validateAndAssert(
2330
TEST_FILES_PATH . 'BankAccount-clover-line.xml',
2431
$clover->process($this->getLineCoverageForBankAccount(), null, 'BankAccount'),
2532
);
@@ -29,7 +36,7 @@ public function testPathCoverageForBankAccountTest(): void
2936
{
3037
$clover = new Clover;
3138

32-
$this->assertStringMatchesFormatFile(
39+
$this->validateAndAssert(
3340
TEST_FILES_PATH . 'BankAccount-clover-path.xml',
3441
$clover->process($this->getPathCoverageForBankAccount(), null, 'BankAccount'),
3542
);
@@ -39,7 +46,7 @@ public function testCloverForFileWithIgnoredLines(): void
3946
{
4047
$clover = new Clover;
4148

42-
$this->assertStringMatchesFormatFile(
49+
$this->validateAndAssert(
4350
TEST_FILES_PATH . 'ignored-lines-clover.xml',
4451
$clover->process($this->getCoverageForFileWithIgnoredLines()),
4552
);
@@ -49,9 +56,43 @@ public function testCloverForClassWithAnonymousFunction(): void
4956
{
5057
$clover = new Clover;
5158

52-
$this->assertStringMatchesFormatFile(
59+
$this->validateAndAssert(
5360
TEST_FILES_PATH . 'class-with-anonymous-function-clover.xml',
5461
$clover->process($this->getCoverageForClassWithAnonymousFunction()),
5562
);
5663
}
64+
65+
/**
66+
* @param non-empty-string $expectationFile
67+
* @param non-empty-string $cloverXml
68+
*/
69+
private function validateAndAssert(string $expectationFile, string $cloverXml): void
70+
{
71+
libxml_use_internal_errors(true);
72+
73+
$document = new DOMDocument;
74+
$document->loadXML($cloverXml);
75+
76+
if (!$document->schemaValidate(__DIR__ . '/../../_files/clover.xsd')) {
77+
$buffer = 'Generated XML document does not validate against Clover schema:' . PHP_EOL . PHP_EOL;
78+
79+
foreach (libxml_get_errors() as $error) {
80+
$buffer .= sprintf(
81+
'- Line %d: %s' . PHP_EOL,
82+
$error->line,
83+
trim($error->message),
84+
);
85+
}
86+
87+
$buffer .= PHP_EOL;
88+
}
89+
90+
libxml_clear_errors();
91+
92+
if (isset($buffer)) {
93+
$this->fail($buffer);
94+
}
95+
96+
$this->assertStringMatchesFormatFile($expectationFile, $cloverXml);
97+
}
5798
}

0 commit comments

Comments
 (0)