|
10 | 10 | namespace SebastianBergmann\CodeCoverage\Report\Xml; |
11 | 11 |
|
12 | 12 | use DOMElement; |
13 | | -use SebastianBergmann\CodeCoverage\ReportAlreadyFinalizedException; |
14 | 13 | use XMLWriter; |
15 | 14 |
|
16 | 15 | /** |
17 | 16 | * @internal This class is not covered by the backward compatibility promise for phpunit/php-code-coverage |
18 | 17 | */ |
19 | 18 | final class Coverage |
20 | 19 | { |
21 | | - private readonly XMLWriter $writer; |
22 | 20 | private readonly DOMElement $contextNode; |
23 | | - private bool $finalized = false; |
| 21 | + private readonly string $line; |
24 | 22 |
|
25 | 23 | public function __construct(DOMElement $context, string $line) |
26 | 24 | { |
27 | 25 | $this->contextNode = $context; |
28 | | - |
29 | | - $this->writer = new XMLWriter; |
30 | | - $this->writer->openMemory(); |
31 | | - $this->writer->startElementNs(null, $context->nodeName, 'https://schema.phpunit.de/coverage/1.0'); |
32 | | - $this->writer->writeAttribute('nr', $line); |
| 26 | + $this->line = $line; |
33 | 27 | } |
34 | 28 |
|
35 | | - /** |
36 | | - * @throws ReportAlreadyFinalizedException |
37 | | - */ |
38 | | - public function addTest(string $test): void |
| 29 | + public function finalize(array $tests): void |
39 | 30 | { |
40 | | - if ($this->finalized) { |
41 | | - // @codeCoverageIgnoreStart |
42 | | - throw new ReportAlreadyFinalizedException; |
43 | | - // @codeCoverageIgnoreEnd |
| 31 | + $writer = new XMLWriter; |
| 32 | + $writer->openMemory(); |
| 33 | + $writer->startElementNs(null, $this->contextNode->nodeName, 'https://schema.phpunit.de/coverage/1.0'); |
| 34 | + $writer->writeAttribute('nr', $this->line); |
| 35 | + |
| 36 | + foreach ($tests as $test) { |
| 37 | + $writer->startElement('covered'); |
| 38 | + $writer->writeAttribute('by', $test); |
| 39 | + $writer->endElement(); |
44 | 40 | } |
45 | | - |
46 | | - $this->writer->startElement('covered'); |
47 | | - $this->writer->writeAttribute('by', $test); |
48 | | - $this->writer->endElement(); |
49 | | - } |
50 | | - |
51 | | - public function finalize(): void |
52 | | - { |
53 | | - $this->writer->endElement(); |
| 41 | + $writer->endElement(); |
54 | 42 |
|
55 | 43 | $fragment = $this->contextNode->ownerDocument->createDocumentFragment(); |
56 | | - $fragment->appendXML($this->writer->outputMemory()); |
| 44 | + $fragment->appendXML($writer->outputMemory()); |
57 | 45 |
|
58 | 46 | $this->contextNode->parentNode->replaceChild( |
59 | 47 | $fragment, |
60 | 48 | $this->contextNode, |
61 | 49 | ); |
62 | | - |
63 | | - $this->finalized = true; |
64 | 50 | } |
65 | 51 | } |
0 commit comments