Fix barchart division by 0 error

This commit is contained in:
Michael Weimann 2022-07-18 19:22:45 +02:00 committed by Igor Scheller
parent c99531decb
commit 29aec8d72f
2 changed files with 66 additions and 42 deletions

View File

@ -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', [ return view('components/barchart', [
'groups' => self::calculateChartGroups( 'groups' => self::calculateChartGroups(
@ -74,7 +76,7 @@ class BarChart
$group['bars'][] = [ $group['bars'][] = [
'value' => $value, 'value' => $value,
'title' => $group['label'] . "\n" . $rowName . ': ' . $value, 'title' => $group['label'] . "\n" . $rowName . ': ' . $value,
'height' => ($value / $max * 100) . '%', 'height' => $max === 0 ? '0%' : ($value / $max * 100) . '%',
'bg' => $colors[$rowKey], 'bg' => $colors[$rowKey],
]; ];
} }
@ -97,7 +99,7 @@ class BarChart
for ($y = 0; $y <= $max; $y += $step) { for ($y = 0; $y <= $max; $y += $step) {
$yLabels[] = [ $yLabels[] = [
'label' => $y, '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 public static function generateChartDemoData(int $days): array
{ {
$step = floor(10000 / $days + 1); $step = $days === 0 ? 0 : floor(10000 / $days + 1);
$now = CarbonImmutable::now(); $now = CarbonImmutable::now();
$twoWeeksAgo = $now->subDays($days); $twoWeeksAgo = $now->subDays($days);
$current = $twoWeeksAgo; $current = $twoWeeksAgo;

View File

@ -8,6 +8,7 @@ use Carbon\CarbonImmutable;
use Engelsystem\Helpers\BarChart; use Engelsystem\Helpers\BarChart;
use Engelsystem\Renderer\Renderer; use Engelsystem\Renderer\Renderer;
use Engelsystem\Test\Unit\TestCase; use Engelsystem\Test\Unit\TestCase;
use Generator;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
class BarChartTest extends TestCase class BarChartTest extends TestCase
@ -40,17 +41,48 @@ class BarChartTest extends TestCase
$this->mockTranslator(); $this->mockTranslator();
} }
/** public function provideRenderTestData(): Generator
* @covers \Engelsystem\Helpers\BarChart::render
* @covers \Engelsystem\Helpers\BarChart::calculateChartGroups
* @covers \Engelsystem\Helpers\BarChart::calculateYLabels
* @covers \Engelsystem\Helpers\BarChart::generateChartDemoData
*/
public function testRender(): void
{ {
$this->rendererMock->expects(self::once()) $yLabels = [
->method('render') [
->with('components/barchart', [ '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' => [ 'groups' => [
[ [
'bars' => [ 'bars' => [
@ -73,37 +105,27 @@ class BarChartTest extends TestCase
'colors' => self::COLORS, 'colors' => self::COLORS,
'rowLabels' => self::ROW_LABELS, 'rowLabels' => self::ROW_LABELS,
'barChartClass' => '', 'barChartClass' => '',
'yLabels' => [ 'yLabels' => $yLabels,
[ ],
'label' => '0', ];
'bottom' => '0%', }
],
[ /**
'label' => '1', * @dataProvider provideRenderTestData
'bottom' => '20%', * @covers \Engelsystem\Helpers\BarChart::render
], * @covers \Engelsystem\Helpers\BarChart::calculateChartGroups
[ * @covers \Engelsystem\Helpers\BarChart::calculateYLabels
'label' => '2', * @covers \Engelsystem\Helpers\BarChart::generateChartDemoData
'bottom' => '40%', */
], public function testRender(array $testData, array $expected): void
[ {
'label' => '3', $this->rendererMock->expects(self::once())
'bottom' => '60%', ->method('render')
], ->with('components/barchart', $expected)
[
'label' => '4',
'bottom' => '80%',
],
[
'label' => '5',
'bottom' => '100%',
],
],
])
->willReturn('test bar chart'); ->willReturn('test bar chart');
self::assertSame( self::assertSame(
'test bar chart', 'test bar chart',
BarChart::render(self::ROW_LABELS, self::COLORS, self::DATA) BarChart::render(self::ROW_LABELS, self::COLORS, $testData)
); );
} }