Skip to content

Commit 6475e9d

Browse files
committed
Fix method bcov rate
1 parent acdc026 commit 6475e9d

File tree

5 files changed

+156
-4
lines changed

5 files changed

+156
-4
lines changed

src/PHPUnit/Cobertura/Formatter/Renderer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private function renderTable(ClassMetrics $classMetrics): void
4545
$this->addTableClassRow($table, $classMetrics);
4646

4747
foreach ($classMetrics->methodMetricsCollection->all() as $methodMetrics) {
48-
$this->addTableMethodRow($table, $methodMetrics);
48+
$this->addTableMethodRow($table, $methodMetrics, $classMetrics);
4949
}
5050

5151
$table->render();
@@ -90,12 +90,12 @@ private function addTableClassRow(Table $table, ClassMetrics $classMetrics): voi
9090
]);
9191
}
9292

93-
private function addTableMethodRow(Table $table, MethodMetrics $methodMetrics): void
93+
private function addTableMethodRow(Table $table, MethodMetrics $methodMetrics, ClassMetrics $classMetrics): void
9494
{
9595
$table->addRow([
9696
$this->colorizer->methodName($methodMetrics),
9797
$this->colorizer->methodLineCoverage($methodMetrics),
98-
$this->colorizer->methodBranchCoverage($methodMetrics),
98+
$this->colorizer->methodBranchCoverage($methodMetrics, $classMetrics),
9999
$this->colorizer->methodComplexity($methodMetrics),
100100
$this->colorizer->methodCrap($methodMetrics),
101101
]);

src/PHPUnit/Cobertura/Formatter/Renderer/Colorizer.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616
use AndreyTech\PHPUnit\Cobertura\Formatter\Parser\MethodMetrics;
1717
use AndreyTech\PHPUnit\Cobertura\Formatter\Renderer\Colorizer\Stats;
1818

19+
use function abs;
1920
use function array_shift;
2021
use function explode;
2122
use function implode;
2223
use function sprintf;
2324

25+
use const PHP_FLOAT_EPSILON;
2426
use const PHP_INT_MAX;
2527

2628
final readonly class Colorizer
2729
{
30+
private const string NO_VALUE = '-';
31+
2832
public function __construct(
2933
private Stats $stats,
3034
private Config $config
@@ -81,8 +85,16 @@ public function methodLineCoverage(MethodMetrics $methodMetrics): string
8185
);
8286
}
8387

84-
public function methodBranchCoverage(MethodMetrics $methodMetrics): string
88+
public function methodBranchCoverage(MethodMetrics $methodMetrics, ClassMetrics $classMetrics): string
8589
{
90+
if (
91+
$this->areFloatsEqual($methodMetrics->branchRate, 0)
92+
&&
93+
$this->areFloatsEqual($classMetrics->branchRate, 1)
94+
) {
95+
return self::NO_VALUE;
96+
}
97+
8698
return $this->colorize(
8799
100.0 * $methodMetrics->branchRate,
88100
$this->ranges('colorizer.metrics.method.bcov'),
@@ -158,4 +170,9 @@ private function ranges(string $key): ?array
158170

159171
return $ranges;
160172
}
173+
174+
private function areFloatsEqual(float $float1, float $float2): bool
175+
{
176+
return abs($float1 - $float2) < PHP_FLOAT_EPSILON;
177+
}
161178
}

tests/Functional/ScriptTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,27 @@ public function testNoColorSuccessIgnoreYellow(): void
194194
self::assertEmpty($process->getErrorOutput());
195195
}
196196

197+
public function testNoColorSuccessZeroBcovGreen(): void
198+
{
199+
$process = $this->runProcess([
200+
'--no-color',
201+
sprintf('--config-file=%s', (string) realpath(self::CONFIG_FILE)),
202+
$this->getDataFileAbsolutePath('cobertura-green-zero-bcov.xml')
203+
]);
204+
205+
self::assertStringContainsString(
206+
$this->getDataFileContents('no-color-success-zero-bcov-green.txt'),
207+
$process->getOutput()
208+
);
209+
210+
self::assertStringContainsString('Exit code:', $process->getOutput());
211+
self::assertStringContainsString('Time:', $process->getOutput());
212+
self::assertStringContainsString('Memory:', $process->getOutput());
213+
214+
self::assertSame(0, $process->getExitCode());
215+
self::assertEmpty($process->getErrorOutput());
216+
}
217+
197218
public function testNoCoberturaFileError(): void
198219
{
199220
$process = $this->runProcess();
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
3+
<coverage line-rate="1" branch-rate="1" lines-covered="54" lines-valid="54" branches-covered="35" branches-valid="35" complexity="20" version="0.4" timestamp="1756882475">
4+
<sources>
5+
<source>/app/src/Purchase</source>
6+
</sources>
7+
<packages>
8+
<package name="Fixer.php" line-rate="1" branch-rate="1" complexity="20">
9+
<classes>
10+
<class name="App\Purchase\Fixer" filename="Fixer.php" line-rate="1" branch-rate="1" complexity="8">
11+
<methods>
12+
<method name="fix" signature="ItemHotel $itemHotel, float $amount" line-rate="1" branch-rate="0" complexity="1">
13+
<lines>
14+
<line number="25" hits="1"/>
15+
<line number="26" hits="1"/>
16+
<line number="28" hits="1"/>
17+
</lines>
18+
</method>
19+
<method name="fixCharges" signature="ItemHotel $itemHotel, float $amount" line-rate="1" branch-rate="1" complexity="4">
20+
<lines>
21+
<line number="33" hits="1"/>
22+
<line number="35" hits="1"/>
23+
</lines>
24+
</method>
25+
<method name="fixPrice" signature="ItemHotel $itemHotel, float $amount" line-rate="1" branch-rate="1" complexity="2">
26+
<lines>
27+
<line number="40" hits="1"/>
28+
<line number="42" hits="1"/>
29+
<line number="44" hits="1"/>
30+
<line number="45" hits="1"/>
31+
<line number="46" hits="1"/>
32+
<line number="47" hits="1"/>
33+
<line number="48" hits="1"/>
34+
<line number="49" hits="1"/>
35+
<line number="50" hits="1"/>
36+
<line number="51" hits="1"/>
37+
<line number="52" hits="1"/>
38+
<line number="53" hits="1"/>
39+
<line number="54" hits="1"/>
40+
<line number="55" hits="1"/>
41+
<line number="56" hits="1"/>
42+
</lines>
43+
</method>
44+
</methods>
45+
<lines>
46+
<line number="25" hits="1"/>
47+
<line number="26" hits="1"/>
48+
<line number="28" hits="1"/>
49+
<line number="33" hits="1"/>
50+
<line number="35" hits="1"/>
51+
<line number="40" hits="1"/>
52+
<line number="42" hits="1"/>
53+
<line number="44" hits="1"/>
54+
<line number="45" hits="1"/>
55+
<line number="46" hits="1"/>
56+
<line number="47" hits="1"/>
57+
<line number="48" hits="1"/>
58+
<line number="49" hits="1"/>
59+
<line number="50" hits="1"/>
60+
<line number="51" hits="1"/>
61+
<line number="52" hits="1"/>
62+
<line number="53" hits="1"/>
63+
<line number="54" hits="1"/>
64+
<line number="55" hits="1"/>
65+
<line number="56" hits="1"/>
66+
<line number="77" hits="1"/>
67+
<line number="78" hits="1"/>
68+
<line number="79" hits="1"/>
69+
<line number="80" hits="1"/>
70+
<line number="81" hits="1"/>
71+
<line number="82" hits="1"/>
72+
<line number="84" hits="1"/>
73+
<line number="85" hits="1"/>
74+
<line number="86" hits="1"/>
75+
<line number="87" hits="1"/>
76+
<line number="89" hits="1"/>
77+
<line number="90" hits="1"/>
78+
<line number="93" hits="1"/>
79+
<line number="96" hits="1"/>
80+
<line number="98" hits="1"/>
81+
<line number="99" hits="1"/>
82+
<line number="112" hits="1"/>
83+
<line number="113" hits="1"/>
84+
<line number="115" hits="1"/>
85+
<line number="116" hits="1"/>
86+
<line number="117" hits="1"/>
87+
<line number="129" hits="1"/>
88+
<line number="130" hits="1"/>
89+
<line number="131" hits="1"/>
90+
<line number="132" hits="1"/>
91+
<line number="134" hits="1"/>
92+
<line number="135" hits="1"/>
93+
<line number="136" hits="1"/>
94+
<line number="137" hits="1"/>
95+
<line number="139" hits="1"/>
96+
<line number="140" hits="1"/>
97+
<line number="143" hits="1"/>
98+
<line number="155" hits="1"/>
99+
<line number="156" hits="1"/>
100+
</lines>
101+
</class>
102+
</classes>
103+
</package>
104+
</packages>
105+
</coverage>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CLASS: App\Purchase\Fixer
2+
+---------------------------+--------+--------+-----+------+
3+
| METHOD | lcov | bcov | ccn | crap |
4+
+---------------------------+--------+--------+-----+------+
5+
| CLASS | 100.00 | 100.00 | 8 | 8 |
6+
| fix | 100.00 | - | 1 | 1 |
7+
| fixCharges | 100.00 | 100.00 | 4 | 4 |
8+
| fixPrice | 100.00 | 100.00 | 2 | 2 |
9+
+---------------------------+--------+--------+-----+------+

0 commit comments

Comments
 (0)