9
9
*/
10
10
namespace SebastianBergmann \CodeCoverage \Report ;
11
11
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 ;
12
19
use PHPUnit \Framework \Attributes \CoversClass ;
13
20
use SebastianBergmann \CodeCoverage \TestCase ;
14
21
@@ -19,7 +26,7 @@ public function testLineCoverageForBankAccountTest(): void
19
26
{
20
27
$ clover = new Clover ;
21
28
22
- $ this ->assertStringMatchesFormatFile (
29
+ $ this ->validateAndAssert (
23
30
TEST_FILES_PATH . 'BankAccount-clover-line.xml ' ,
24
31
$ clover ->process ($ this ->getLineCoverageForBankAccount (), null , 'BankAccount ' ),
25
32
);
@@ -29,7 +36,7 @@ public function testPathCoverageForBankAccountTest(): void
29
36
{
30
37
$ clover = new Clover ;
31
38
32
- $ this ->assertStringMatchesFormatFile (
39
+ $ this ->validateAndAssert (
33
40
TEST_FILES_PATH . 'BankAccount-clover-path.xml ' ,
34
41
$ clover ->process ($ this ->getPathCoverageForBankAccount (), null , 'BankAccount ' ),
35
42
);
@@ -39,7 +46,7 @@ public function testCloverForFileWithIgnoredLines(): void
39
46
{
40
47
$ clover = new Clover ;
41
48
42
- $ this ->assertStringMatchesFormatFile (
49
+ $ this ->validateAndAssert (
43
50
TEST_FILES_PATH . 'ignored-lines-clover.xml ' ,
44
51
$ clover ->process ($ this ->getCoverageForFileWithIgnoredLines ()),
45
52
);
@@ -49,9 +56,43 @@ public function testCloverForClassWithAnonymousFunction(): void
49
56
{
50
57
$ clover = new Clover ;
51
58
52
- $ this ->assertStringMatchesFormatFile (
59
+ $ this ->validateAndAssert (
53
60
TEST_FILES_PATH . 'class-with-anonymous-function-clover.xml ' ,
54
61
$ clover ->process ($ this ->getCoverageForClassWithAnonymousFunction ()),
55
62
);
56
63
}
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
+ }
57
98
}
0 commit comments