Skip to content

Commit

Permalink
Fix divide by zero
Browse files Browse the repository at this point in the history
  • Loading branch information
robiningelbrecht committed Oct 3, 2023
1 parent f0c6672 commit 4ecab73
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/MinCoverage/MinCoverageResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public static function mapFromRulesAndMetrics(

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

public function testNotifyWhenNoTrackedLines(): void
{
$exitter = $this->createMock(Exitter::class);
$spyOutput = new SpyOutput();

$exitter
->expects($this->never())
->method('exit');

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

$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($spyOutput);
}

public function testNotifyWithNonExistingCloverFile(): void
{
$exitter = $this->createMock(Exitter::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

+-----------------------+-------<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> *NonExistingPattern </>| 100% | <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>|
+-----------------------+------------+----------+-----------------------+-------------+
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageRule;

return [
new MinCoverageRule(
pattern: '*NonExistingPattern',
minCoverage: 100,
exitOnLowCoverage: true
),
];

0 comments on commit 4ecab73

Please sign in to comment.