Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
run: composer test

- name: Upload coverage
if: github.ref == 'refs/heads/main'
env:
COVERAGE_KEY: ${{ secrets.COVERAGE_KEY }}
COVERAGE_HOST: ${{ secrets.COVERAGE_HOST }}
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.0 || ^11.0",
"robiningelbrecht/phpunit-coverage-tools": "^1.8",
"squizlabs/php_codesniffer": "^3.6"
},
"autoload": {
Expand All @@ -27,7 +28,7 @@
"parallel-lint --exclude vendor ."
],
"phpunit": [
"XDEBUG_MODE=coverage phpunit --coverage-clover coverage.clover tests"
"XDEBUG_MODE=coverage phpunit -d --min-coverage=100 --coverage-clover coverage.clover tests"
],
"sniff": [
"phpcs --standard=PSR12 src/ tests/"
Expand Down
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
<directory suffix=".php">./src</directory>
</include>
</source>
<extensions>
<bootstrap class="RobinIngelbrecht\PHPUnitCoverageTools\PhpUnitExtension">
<parameter name="exitOnLowCoverage" value="1" />
</bootstrap>
</extensions>
</phpunit>
21 changes: 10 additions & 11 deletions src/CloverParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

use SimpleXMLElement;

/**
* Calculation reference https://confluence.atlassian.com/pages/viewpage.action?pageId=79986990
* So in order to calculate the Total Percentage Coverage metric using data from an XML report
* you have to use the following equation:
*
* TPC = (coveredconditionals + coveredstatements + coveredmethods) / (conditionals + statements + methods)
*
* Using elements is incorrect as according to that page notes, "elements" is conditionals + statements,
* which means including them in the calculation would double-count statements.
*/
class CloverParser
{
/** @var array<string> */
Expand Down Expand Up @@ -52,14 +62,12 @@ protected function reset(): void
'methods' => 0,
'conditionals' => 0,
'statements' => 0,
'elements' => 0,
];

$this->coveredTotals = [
'coveredmethods' => 0,
'coveredconditionals' => 0,
'coveredstatements' => 0,
'coveredelements' => 0,
];

$this->hasCalculatedTotals = false;
Expand All @@ -77,13 +85,6 @@ public function calculateTotals(): self

continue;
}

foreach ($project->xpath('//file') as $file) {
// no other way of seeing if the element exists?
if ($file->metrics->count() > 0) {
$this->incrementTotalsWithMetrics($file->metrics);
}
}
}
}

Expand All @@ -105,8 +106,6 @@ protected function incrementTotalsWithMetrics(SimpleXMLElement $metrics): void
$this->coveredTotals['coveredconditionals'] += (int) $metrics['coveredconditionals'];
$this->totals['statements'] += (int) $metrics['statements'];
$this->coveredTotals['coveredstatements'] += (int) $metrics['coveredstatements'];
$this->totals['elements'] += (int) $metrics['elements'];
$this->coveredTotals['coveredelements'] += (int) $metrics['coveredelements'];
}

public function getPercentage(): float
Expand Down
5 changes: 3 additions & 2 deletions tests/CloverParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ public function testCalculationFromProject(): void
$this->assertSame(77.54237288135593, $parser->getPercentage());
}

public function testCalculationFromFiles(): void
public function testCalculationMissingProjectMetrics(): void
{
$parser = new CloverParser();

// This is not a realistic file; clover metrics should always have top-level project metrics
$clover = <<<ENDCLOVER
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1618905787">
Expand Down Expand Up @@ -114,7 +115,7 @@ public function testCalculationFromFiles(): void

$parser->addFile($path);

$this->assertSame(20.51282051282051, $parser->getPercentage());
$this->assertSame(0.0, $parser->getPercentage());
}

public function testCalculationAvoidsDivisionByZero(): void
Expand Down