diff --git a/src/Report/Xml/Facade.php b/src/Report/Xml/Facade.php index 80b34ba28..bae2bed6e 100644 --- a/src/Report/Xml/Facade.php +++ b/src/Report/Xml/Facade.php @@ -252,6 +252,16 @@ private function setTotals(AbstractNode $node, Totals $totals): void $node->numberOfExecutedLines(), ); + $totals->setNumBranches( + $node->numberOfExecutableBranches(), + $node->numberOfExecutedBranches(), + ); + + $totals->setNumPaths( + $node->numberOfExecutablePaths(), + $node->numberOfExecutedPaths(), + ); + $totals->setNumClasses( $node->numberOfClasses(), $node->numberOfTestedClasses(), diff --git a/src/Report/Xml/Totals.php b/src/Report/Xml/Totals.php index 960b8e862..ac15ae3cd 100644 --- a/src/Report/Xml/Totals.php +++ b/src/Report/Xml/Totals.php @@ -21,6 +21,8 @@ { private DOMNode $container; private DOMElement $linesNode; + private DOMElement $branchesNode; + private DOMElement $pathsNode; private DOMElement $methodsNode; private DOMElement $functionsNode; private DOMElement $classesNode; @@ -36,6 +38,16 @@ public function __construct(DOMElement $container) 'lines', ); + $this->branchesNode = $dom->createElementNS( + 'https://schema.phpunit.de/coverage/1.0', + 'branches', + ); + + $this->pathsNode = $dom->createElementNS( + 'https://schema.phpunit.de/coverage/1.0', + 'paths', + ); + $this->methodsNode = $dom->createElementNS( 'https://schema.phpunit.de/coverage/1.0', 'methods', @@ -57,6 +69,8 @@ public function __construct(DOMElement $container) ); $container->appendChild($this->linesNode); + $container->appendChild($this->branchesNode); + $container->appendChild($this->pathsNode); $container->appendChild($this->methodsNode); $container->appendChild($this->functionsNode); $container->appendChild($this->classesNode); @@ -81,6 +95,26 @@ public function setNumLines(int $loc, int $cloc, int $ncloc, int $executable, in ); } + public function setNumBranches(int $count, int $tested): void + { + $this->branchesNode->setAttribute('count', (string) $count); + $this->branchesNode->setAttribute('tested', (string) $tested); + $this->branchesNode->setAttribute( + 'percent', + $count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()), + ); + } + + public function setNumPaths(int $count, int $tested): void + { + $this->pathsNode->setAttribute('count', (string) $count); + $this->pathsNode->setAttribute('tested', (string) $tested); + $this->pathsNode->setAttribute( + 'percent', + $count === 0 ? '0' : sprintf('%01.2F', Percentage::fromFractionAndTotal($tested, $count)->asFloat()), + ); + } + public function setNumClasses(int $count, int $tested): void { $this->classesNode->setAttribute('count', (string) $count); diff --git a/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml b/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml index b49cdf8ed..7edc6e9cf 100644 --- a/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml +++ b/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml @@ -3,6 +3,8 @@ + + diff --git a/tests/_files/Report/XML/CoverageForBankAccount/index.xml b/tests/_files/Report/XML/CoverageForBankAccount/index.xml index 6e551a135..e479d6d7e 100644 --- a/tests/_files/Report/XML/CoverageForBankAccount/index.xml +++ b/tests/_files/Report/XML/CoverageForBankAccount/index.xml @@ -14,6 +14,8 @@ + + @@ -22,6 +24,8 @@ + + diff --git a/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml index 3bfba4dda..5abb21220 100644 --- a/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml +++ b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml @@ -11,6 +11,8 @@ + + @@ -19,6 +21,8 @@ + + diff --git a/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.xml b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.xml index 95d121dc8..81a2b99ce 100644 --- a/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.xml +++ b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/source_with_class_and_anonymous_function.php.xml @@ -3,6 +3,8 @@ + + diff --git a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml index a53b12c53..d136cc288 100644 --- a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml +++ b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml @@ -11,6 +11,8 @@ + + @@ -19,6 +21,8 @@ + + diff --git a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/source_with_ignore.php.xml b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/source_with_ignore.php.xml index b862f3d1e..04b034d7a 100644 --- a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/source_with_ignore.php.xml +++ b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/source_with_ignore.php.xml @@ -3,6 +3,8 @@ + + diff --git a/tests/_files/Report/XML/PathCoverageForBankAccount/BankAccount.php.xml b/tests/_files/Report/XML/PathCoverageForBankAccount/BankAccount.php.xml new file mode 100644 index 000000000..b29b437e3 --- /dev/null +++ b/tests/_files/Report/XML/PathCoverageForBankAccount/BankAccount.php.xml @@ -0,0 +1,274 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <?php + + + class + + BankAccount + + + { + + + + protected + + $balance + + = + + 0 + ; + + + + + public + + function + + getBalance + ( + ) + + + + { + + + + return + + $this + -> + balance + ; + + + + } + + + + + protected + + function + + setBalance + ( + $balance + ) + + + + { + + + + if + + ( + $balance + + >= + + 0 + ) + + { + + + + $this + -> + balance + + = + + $balance + ; + + + + } + + else + + { + + + + throw + + new + + RuntimeException + ; + + + + } + + + + } + + + + + public + + function + + depositMoney + ( + $balance + ) + + + + { + + + + $this + -> + setBalance + ( + $this + -> + getBalance + ( + ) + + + + + $balance + ) + ; + + + + + return + + $this + -> + getBalance + ( + ) + ; + + + + } + + + + + public + + function + + withdrawMoney + ( + $balance + ) + + + + { + + + + $this + -> + setBalance + ( + $this + -> + getBalance + ( + ) + + - + + $balance + ) + ; + + + + + return + + $this + -> + getBalance + ( + ) + ; + + + + return + + $this + -> + getBalance + ( + ) + ; + + + + } + + + } + + + + + diff --git a/tests/_files/Report/XML/PathCoverageForBankAccount/index.xml b/tests/_files/Report/XML/PathCoverageForBankAccount/index.xml new file mode 100644 index 000000000..98b225bcb --- /dev/null +++ b/tests/_files/Report/XML/PathCoverageForBankAccount/index.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/tests/Report/XmlTest.php b/tests/tests/Report/XmlTest.php index 53deb784e..c078804f5 100644 --- a/tests/tests/Report/XmlTest.php +++ b/tests/tests/Report/XmlTest.php @@ -67,6 +67,16 @@ public function testForClassWithAnonymousFunction(): void $this->assertFilesEquals($expectedFilesPath, TEST_FILES_PATH . 'tmp'); } + public function testForBankAccountWithPathCoverage(): void + { + $expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'PathCoverageForBankAccount'; + + $xml = new Facade('1.0.0'); + $xml->process($this->getPathCoverageForBankAccount(), TEST_FILES_PATH . 'tmp'); + + $this->assertFilesEquals($expectedFilesPath, TEST_FILES_PATH . 'tmp'); + } + private function assertFilesEquals(string $expectedFilesPath, string $actualFilesPath): void { $expectedFilesIterator = new FilesystemIterator($expectedFilesPath);