Skip to content

Commit e458aab

Browse files
Merge pull request #11 from robiningelbrecht/fix-division-by-zero-part-2
Fix divide by zero part 2
2 parents a1aec59 + 178ebaf commit e458aab

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-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-with-no-tracked-lines.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
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> *CommandHandler </>| 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: '*CommandHandler',
8+
minCoverage: 20,
9+
exitOnLowCoverage: true
10+
),
11+
];
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<coverage generated="1682083729">
3+
<project timestamp="1682083729">
4+
<file name="/var/www/symfony/src/Infrastructure/CQRS/Bus/CanNotRegisterCommandHandler.php">
5+
<class name="App\Infrastructure\CQRS\Bus\CanNotRegisterCommandHandler" namespace="global">
6+
<metrics complexity="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
7+
</class>
8+
<metrics loc="8" ncloc="8" classes="0" methods="0" coveredmethods="0" conditionals="0" coveredconditionals="0" statements="0" coveredstatements="0" elements="0" coveredelements="0"/>
9+
</file>
10+
</project>
11+
</coverage>

0 commit comments

Comments
 (0)