Skip to content

Commit

Permalink
Merge pull request #11 from robiningelbrecht/fix-division-by-zero-part-2
Browse files Browse the repository at this point in the history
Fix divide by zero part 2
  • Loading branch information
robiningelbrecht committed Oct 3, 2023
2 parents a1aec59 + 178ebaf commit e458aab
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/MinCoverage/MinCoverageResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public static function mapFromRulesAndMetrics(

$coveragePercentage = 0;
foreach ($metricsForPattern as $metric) {
if (0 === $totalTrackedLines) {
continue;
}
$weight = $metric->getNumberOfTrackedLines() / $totalTrackedLines;
$coveragePercentage += ($metric->getTotalPercentageCoverage() * $weight);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Subscriber/Application/ApplicationFinishedSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,40 @@ public function testDivideByZero(): void
$this->assertMatchesTextSnapshot($this->output);
}

public function testNotifyWhenNoTrackedLines(): void
{
$this->exitter
->expects($this->never())
->method('exit');

$subscriber = new ApplicationFinishedSubscriber(
relativePathToCloverXml: 'tests/clover-with-no-tracked-lines.xml',
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-no-tracked-lines.php'),
cleanUpCloverXml: false,
exitter: $this->exitter,
consoleOutput: new ConsoleOutput($this->output, $this->resourceUsageFormatter),
timer: $this->timer,
);

$subscriber->notify(event: new Finished(
new Info(
current: new Snapshot(
time: HRTime::fromSecondsAndNanoseconds(1, 0),
memoryUsage: MemoryUsage::fromBytes(100),
peakMemoryUsage: MemoryUsage::fromBytes(100),
garbageCollectorStatus: new GarbageCollectorStatus(0, 0, 0, 0, null, null, null, null, null, null, null, null)
),
durationSinceStart: Duration::fromSecondsAndNanoseconds(1, 0),
memorySinceStart: MemoryUsage::fromBytes(100),
durationSincePrevious: Duration::fromSecondsAndNanoseconds(1, 0),
memorySincePrevious: MemoryUsage::fromBytes(100),
),
0
));

$this->assertMatchesTextSnapshot($this->output);
}

public function testNotifyWithNonExistingCloverFile(): void
{
$this->exitter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

+-----------------------+-------<fg=black;bg=yellow;options=bold> Code coverage results </>-----------------+-------------+
|<bold> Pattern </bold>|<bold> Expected </bold>|<bold> Actual </bold>|<bold> </bold>|<bold> Exit on<fg=default;bg=default></> </bold>|
|<bold> </bold>|<bold> </bold>|<bold> </bold>|<bold> </bold>|<bold> fail? </bold>|
+-----------------------+------------+----------+-----------------------+-------------+
|<fg=default;bg=default> *CommandHandler </>| 20% | <warning>0%</warning> |<fg=default;bg=default> No lines to track...? </>| Yes |
+-----------------------+------------+----------+-----------------------+-------------+
|<warning> There was at least one pattern that did not match any covered classes. Please consider removing them. </warning>|
+-----------------------+------------+----------+-----------------------+-------------+
Time: 00:00.350, Memory: 12.00 MB
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageRule;

return [
new MinCoverageRule(
pattern: '*CommandHandler',
minCoverage: 20,
exitOnLowCoverage: true
),
];
11 changes: 11 additions & 0 deletions tests/clover-with-no-tracked-lines.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1682083729">
<project timestamp="1682083729">
<file name="/var/www/symfony/src/Infrastructure/CQRS/Bus/CanNotRegisterCommandHandler.php">
<class name="App\Infrastructure\CQRS\Bus\CanNotRegisterCommandHandler" namespace="global">
<metrics complexity="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
</class>
<metrics loc="8" ncloc="8" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
</file>
</project>
</coverage>

0 comments on commit e458aab

Please sign in to comment.