From f8dd6628a074501dfa69bdb9501e74f5e38e75e7 Mon Sep 17 00:00:00 2001 From: Andrew Patterson Date: Sat, 14 Dec 2024 00:51:19 +1100 Subject: [PATCH 1/2] Added branch coverage statistics to XML report --- src/Report/Xml/Facade.php | 5 + src/Report/Xml/Totals.php | 17 ++ .../BankAccount.php.xml | 1 + .../XML/CoverageForBankAccount/index.xml | 2 + .../index.xml | 2 + ..._with_class_and_anonymous_function.php.xml | 1 + .../CoverageForFileWithIgnoredLines/index.xml | 2 + .../source_with_ignore.php.xml | 1 + .../BankAccount.php.xml | 273 ++++++++++++++++++ .../XML/PathCoverageForBankAccount/index.xml | 35 +++ tests/tests/Report/XmlTest.php | 10 + 11 files changed, 349 insertions(+) create mode 100644 tests/_files/Report/XML/PathCoverageForBankAccount/BankAccount.php.xml create mode 100644 tests/_files/Report/XML/PathCoverageForBankAccount/index.xml diff --git a/src/Report/Xml/Facade.php b/src/Report/Xml/Facade.php index 80b34ba28..9a05ec49b 100644 --- a/src/Report/Xml/Facade.php +++ b/src/Report/Xml/Facade.php @@ -252,6 +252,11 @@ private function setTotals(AbstractNode $node, Totals $totals): void $node->numberOfExecutedLines(), ); + $totals->setNumBranches( + $node->numberOfExecutableBranches(), + $node->numberOfExecutedBranches(), + ); + $totals->setNumClasses( $node->numberOfClasses(), $node->numberOfTestedClasses(), diff --git a/src/Report/Xml/Totals.php b/src/Report/Xml/Totals.php index 960b8e862..d17692d06 100644 --- a/src/Report/Xml/Totals.php +++ b/src/Report/Xml/Totals.php @@ -21,6 +21,7 @@ { private DOMNode $container; private DOMElement $linesNode; + private DOMElement $branchesNode; private DOMElement $methodsNode; private DOMElement $functionsNode; private DOMElement $classesNode; @@ -36,6 +37,11 @@ public function __construct(DOMElement $container) 'lines', ); + $this->branchesNode = $dom->createElementNS( + 'https://schema.phpunit.de/coverage/1.0', + 'branches', + ); + $this->methodsNode = $dom->createElementNS( 'https://schema.phpunit.de/coverage/1.0', 'methods', @@ -57,6 +63,7 @@ public function __construct(DOMElement $container) ); $container->appendChild($this->linesNode); + $container->appendChild($this->branchesNode); $container->appendChild($this->methodsNode); $container->appendChild($this->functionsNode); $container->appendChild($this->classesNode); @@ -81,6 +88,16 @@ 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 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..cea8aebe6 100644 --- a/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml +++ b/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml @@ -3,6 +3,7 @@ + diff --git a/tests/_files/Report/XML/CoverageForBankAccount/index.xml b/tests/_files/Report/XML/CoverageForBankAccount/index.xml index 6e551a135..250af209b 100644 --- a/tests/_files/Report/XML/CoverageForBankAccount/index.xml +++ b/tests/_files/Report/XML/CoverageForBankAccount/index.xml @@ -14,6 +14,7 @@ + @@ -22,6 +23,7 @@ + diff --git a/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml index 3bfba4dda..dc74a04e1 100644 --- a/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml +++ b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml @@ -11,6 +11,7 @@ + @@ -19,6 +20,7 @@ + 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..75199421d 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,7 @@ + diff --git a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml index a53b12c53..6af0b517c 100644 --- a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml +++ b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml @@ -11,6 +11,7 @@ + @@ -19,6 +20,7 @@ + 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..6eb9c527a 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,7 @@ + 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..dcb59bf06 --- /dev/null +++ b/tests/_files/Report/XML/PathCoverageForBankAccount/BankAccount.php.xml @@ -0,0 +1,273 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <?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..dec6939c8 --- /dev/null +++ b/tests/_files/Report/XML/PathCoverageForBankAccount/index.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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); From f3b093f3694ee20d8554d819375ece64a2acde0a Mon Sep 17 00:00:00 2001 From: Andrew Patterson Date: Sat, 14 Dec 2024 01:16:49 +1100 Subject: [PATCH 2/2] Added path coverage statistics to XML report --- src/Report/Xml/Facade.php | 5 +++++ src/Report/Xml/Totals.php | 17 +++++++++++++++++ .../CoverageForBankAccount/BankAccount.php.xml | 1 + .../Report/XML/CoverageForBankAccount/index.xml | 2 ++ .../index.xml | 2 ++ ...ce_with_class_and_anonymous_function.php.xml | 1 + .../CoverageForFileWithIgnoredLines/index.xml | 2 ++ .../source_with_ignore.php.xml | 1 + .../BankAccount.php.xml | 1 + .../XML/PathCoverageForBankAccount/index.xml | 2 ++ 10 files changed, 34 insertions(+) diff --git a/src/Report/Xml/Facade.php b/src/Report/Xml/Facade.php index 9a05ec49b..bae2bed6e 100644 --- a/src/Report/Xml/Facade.php +++ b/src/Report/Xml/Facade.php @@ -257,6 +257,11 @@ private function setTotals(AbstractNode $node, Totals $totals): void $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 d17692d06..ac15ae3cd 100644 --- a/src/Report/Xml/Totals.php +++ b/src/Report/Xml/Totals.php @@ -22,6 +22,7 @@ private DOMNode $container; private DOMElement $linesNode; private DOMElement $branchesNode; + private DOMElement $pathsNode; private DOMElement $methodsNode; private DOMElement $functionsNode; private DOMElement $classesNode; @@ -42,6 +43,11 @@ public function __construct(DOMElement $container) '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', @@ -64,6 +70,7 @@ 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); @@ -98,6 +105,16 @@ public function setNumBranches(int $count, int $tested): void ); } + 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 cea8aebe6..7edc6e9cf 100644 --- a/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml +++ b/tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml @@ -4,6 +4,7 @@ + diff --git a/tests/_files/Report/XML/CoverageForBankAccount/index.xml b/tests/_files/Report/XML/CoverageForBankAccount/index.xml index 250af209b..e479d6d7e 100644 --- a/tests/_files/Report/XML/CoverageForBankAccount/index.xml +++ b/tests/_files/Report/XML/CoverageForBankAccount/index.xml @@ -15,6 +15,7 @@ + @@ -24,6 +25,7 @@ + diff --git a/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml index dc74a04e1..5abb21220 100644 --- a/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml +++ b/tests/_files/Report/XML/CoverageForClassWithAnonymousFunction/index.xml @@ -12,6 +12,7 @@ + @@ -21,6 +22,7 @@ + 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 75199421d..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 @@ -4,6 +4,7 @@ + diff --git a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml index 6af0b517c..d136cc288 100644 --- a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml +++ b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml @@ -12,6 +12,7 @@ + @@ -21,6 +22,7 @@ + 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 6eb9c527a..04b034d7a 100644 --- a/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/source_with_ignore.php.xml +++ b/tests/_files/Report/XML/CoverageForFileWithIgnoredLines/source_with_ignore.php.xml @@ -4,6 +4,7 @@ + diff --git a/tests/_files/Report/XML/PathCoverageForBankAccount/BankAccount.php.xml b/tests/_files/Report/XML/PathCoverageForBankAccount/BankAccount.php.xml index dcb59bf06..b29b437e3 100644 --- a/tests/_files/Report/XML/PathCoverageForBankAccount/BankAccount.php.xml +++ b/tests/_files/Report/XML/PathCoverageForBankAccount/BankAccount.php.xml @@ -4,6 +4,7 @@ + diff --git a/tests/_files/Report/XML/PathCoverageForBankAccount/index.xml b/tests/_files/Report/XML/PathCoverageForBankAccount/index.xml index dec6939c8..98b225bcb 100644 --- a/tests/_files/Report/XML/PathCoverageForBankAccount/index.xml +++ b/tests/_files/Report/XML/PathCoverageForBankAccount/index.xml @@ -15,6 +15,7 @@ + @@ -24,6 +25,7 @@ +