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', [
'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;

View File

@ -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)
);
}