Skip to content

Commit 2946150

Browse files
authored
Merge pull request #14 from totov/fix/division-by-zero-bug
Fixes division by zero error.
2 parents 4554d0d + 5a88437 commit 2946150

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/SparkLineEntry.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public function __construct(
1313

1414
public function rebase(int $base, int $max): self
1515
{
16+
if($max === 0) {
17+
return $this;
18+
}
19+
1620
return new self(
1721
count: (int) floor($this->count * ($base / $max)),
1822
);

tests/SparkLineTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Brendt\SparkLine\SparkLine;
88
use Brendt\SparkLine\SparkLineEntry;
9+
use DivisionByZeroError;
910
use PHPUnit\Framework\TestCase;
1011

1112
final class SparkLineTest extends TestCase
@@ -70,4 +71,12 @@ public function test_get_total(): void
7071

7172
$this->assertEquals(3, $sparkLine->getTotal());
7273
}
74+
75+
/** @test */
76+
public function test_entries_all_with_zero_values_doesnt_cause_error(): void
77+
{
78+
$sparkLine = (new SparkLine(0, 0, 0, 0))->make();
79+
80+
$this->assertStringContainsString('<svg', $sparkLine);
81+
}
7382
}

0 commit comments

Comments
 (0)