Skip to content

Commit f6ac3a8

Browse files
Fix divide by zero part 2
1 parent a1aec59 commit f6ac3a8

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

src/MinCoverage/MinCoverageResult.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public static function mapFromRulesAndMetrics(
8585

8686
$coveragePercentage = 0;
8787
foreach ($metricsForPattern as $metric) {
88+
if (0 === $totalTrackedLines) {
89+
continue;
90+
}
8891
$weight = $metric->getNumberOfTrackedLines() / $totalTrackedLines;
8992
$coveragePercentage += ($metric->getTotalPercentageCoverage() * $weight);
9093
}

tests/Subscriber/Application/ApplicationFinishedSubscriberTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,40 @@ public function testDivideByZero(): void
282282
$this->assertMatchesTextSnapshot($this->output);
283283
}
284284

285+
public function testNotifyWhenNoTrackedLines(): void
286+
{
287+
$this->exitter
288+
->expects($this->never())
289+
->method('exit');
290+
291+
$subscriber = new ApplicationFinishedSubscriber(
292+
relativePathToCloverXml: 'tests/clover.xml',
293+
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-no-tracked-lines.php'),
294+
cleanUpCloverXml: false,
295+
exitter: $this->exitter,
296+
consoleOutput: new ConsoleOutput($this->output, $this->resourceUsageFormatter),
297+
timer: $this->timer,
298+
);
299+
300+
$subscriber->notify(event: new Finished(
301+
new Info(
302+
current: new Snapshot(
303+
time: HRTime::fromSecondsAndNanoseconds(1, 0),
304+
memoryUsage: MemoryUsage::fromBytes(100),
305+
peakMemoryUsage: MemoryUsage::fromBytes(100),
306+
garbageCollectorStatus: new GarbageCollectorStatus(0, 0, 0, 0, null, null, null, null, null, null, null, null)
307+
),
308+
durationSinceStart: Duration::fromSecondsAndNanoseconds(1, 0),
309+
memorySinceStart: MemoryUsage::fromBytes(100),
310+
durationSincePrevious: Duration::fromSecondsAndNanoseconds(1, 0),
311+
memorySincePrevious: MemoryUsage::fromBytes(100),
312+
),
313+
0
314+
));
315+
316+
$this->assertMatchesTextSnapshot($this->output);
317+
}
318+
285319
public function testNotifyWithNonExistingCloverFile(): void
286320
{
287321
$this->exitter
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
+-----------------------+-------<fg=black;bg=yellow;options=bold> Code coverage results </>-----------------+-------------+
3+
|<bold> Pattern </bold>|<bold> Expected </bold>|<bold> Actual </bold>|<bold> </bold>|<bold> Exit on<fg=default;bg=default></> </bold>|
4+
|<bold> </bold>|<bold> </bold>|<bold> </bold>|<bold> </bold>|<bold> fail? </bold>|
5+
+-----------------------+------------+----------+-----------------------+-------------+
6+
|<fg=default;bg=default> *NonExistingPattern </>| 20% | <warning>0%</warning> |<fg=default;bg=default> No lines to track...? </>| Yes |
7+
+-----------------------+------------+----------+-----------------------+-------------+
8+
|<warning> There was at least one pattern that did not match any covered classes. Please consider removing them. </warning>|
9+
+-----------------------+------------+----------+-----------------------+-------------+
10+
Time: 00:00.350, Memory: 12.00 MB
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageRule;
4+
5+
return [
6+
new MinCoverageRule(
7+
pattern: '*NonExistingPattern',
8+
minCoverage: 20,
9+
exitOnLowCoverage: true
10+
),
11+
];

0 commit comments

Comments
 (0)