From 29aec8d72f1c05c7ea0417e82aa7639bbc0153f0 Mon Sep 17 00:00:00 2001 From: Michael Weimann Date: Mon, 18 Jul 2022 19:22:45 +0200 Subject: [PATCH] Fix barchart division by 0 error --- src/Helpers/BarChart.php | 10 +-- tests/Unit/Helpers/BarChartTest.php | 98 ++++++++++++++++++----------- 2 files changed, 66 insertions(+), 42 deletions(-) diff --git a/src/Helpers/BarChart.php b/src/Helpers/BarChart.php index 2790596c..e6378145 100644 --- a/src/Helpers/BarChart.php +++ b/src/Helpers/BarChart.php @@ -37,7 +37,9 @@ class BarChart } } - $roundedMax = (int) ceil($max / 5) * 5; + $roundedMax = $max === 0 + ? 5 + : (int) ceil($max / 5) * 5; return view('components/barchart', [ 'groups' => self::calculateChartGroups( @@ -74,7 +76,7 @@ class BarChart $group['bars'][] = [ 'value' => $value, 'title' => $group['label'] . "\n" . $rowName . ': ' . $value, - 'height' => ($value / $max * 100) . '%', + 'height' => $max === 0 ? '0%' : ($value / $max * 100) . '%', 'bg' => $colors[$rowKey], ]; } @@ -97,7 +99,7 @@ class BarChart for ($y = 0; $y <= $max; $y += $step) { $yLabels[] = [ 'label' => $y, - 'bottom' => ($y / $max * 100) . '%', + 'bottom' => $max === 0 ? '0%' : ($y / $max * 100) . '%', ]; } @@ -131,7 +133,7 @@ class BarChart */ public static function generateChartDemoData(int $days): array { - $step = floor(10000 / $days + 1); + $step = $days === 0 ? 0 : floor(10000 / $days + 1); $now = CarbonImmutable::now(); $twoWeeksAgo = $now->subDays($days); $current = $twoWeeksAgo; diff --git a/tests/Unit/Helpers/BarChartTest.php b/tests/Unit/Helpers/BarChartTest.php index 2f31cc78..57fa1a56 100644 --- a/tests/Unit/Helpers/BarChartTest.php +++ b/tests/Unit/Helpers/BarChartTest.php @@ -8,6 +8,7 @@ use Carbon\CarbonImmutable; use Engelsystem\Helpers\BarChart; use Engelsystem\Renderer\Renderer; use Engelsystem\Test\Unit\TestCase; +use Generator; use PHPUnit\Framework\MockObject\MockObject; class BarChartTest extends TestCase @@ -40,17 +41,48 @@ class BarChartTest extends TestCase $this->mockTranslator(); } - /** - * @covers \Engelsystem\Helpers\BarChart::render - * @covers \Engelsystem\Helpers\BarChart::calculateChartGroups - * @covers \Engelsystem\Helpers\BarChart::calculateYLabels - * @covers \Engelsystem\Helpers\BarChart::generateChartDemoData - */ - public function testRender(): void + public function provideRenderTestData(): Generator { - $this->rendererMock->expects(self::once()) - ->method('render') - ->with('components/barchart', [ + $yLabels = [ + [ + 'label' => '0', + 'bottom' => '0%', + ], + [ + 'label' => '1', + 'bottom' => '20%', + ], + [ + 'label' => '2', + 'bottom' => '40%', + ], + [ + 'label' => '3', + 'bottom' => '60%', + ], + [ + 'label' => '4', + 'bottom' => '80%', + ], + [ + 'label' => '5', + 'bottom' => '100%', + ], + ]; + + yield 'empty data' => [ + [], + [ + 'groups' => [], + 'colors' => self::COLORS, + 'rowLabels' => self::ROW_LABELS, + 'barChartClass' => '', + 'yLabels' => $yLabels, + ] + ]; + yield 'non-empty data' => [ + self::DATA, + [ 'groups' => [ [ 'bars' => [ @@ -73,37 +105,27 @@ class BarChartTest extends TestCase 'colors' => self::COLORS, 'rowLabels' => self::ROW_LABELS, 'barChartClass' => '', - 'yLabels' => [ - [ - 'label' => '0', - 'bottom' => '0%', - ], - [ - 'label' => '1', - 'bottom' => '20%', - ], - [ - 'label' => '2', - 'bottom' => '40%', - ], - [ - 'label' => '3', - 'bottom' => '60%', - ], - [ - 'label' => '4', - 'bottom' => '80%', - ], - [ - 'label' => '5', - 'bottom' => '100%', - ], - ], - ]) + 'yLabels' => $yLabels, + ], + ]; + } + + /** + * @dataProvider provideRenderTestData + * @covers \Engelsystem\Helpers\BarChart::render + * @covers \Engelsystem\Helpers\BarChart::calculateChartGroups + * @covers \Engelsystem\Helpers\BarChart::calculateYLabels + * @covers \Engelsystem\Helpers\BarChart::generateChartDemoData + */ + public function testRender(array $testData, array $expected): void + { + $this->rendererMock->expects(self::once()) + ->method('render') + ->with('components/barchart', $expected) ->willReturn('test bar chart'); self::assertSame( 'test bar chart', - BarChart::render(self::ROW_LABELS, self::COLORS, self::DATA) + BarChart::render(self::ROW_LABELS, self::COLORS, $testData) ); }