2022-06-07 22:59:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Helpers;
|
|
|
|
|
2022-07-12 00:36:38 +02:00
|
|
|
use Carbon\CarbonImmutable;
|
2022-06-07 22:59:49 +02:00
|
|
|
use Engelsystem\Helpers\BarChart;
|
|
|
|
use Engelsystem\Renderer\Renderer;
|
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
2022-07-18 19:22:45 +02:00
|
|
|
use Generator;
|
2022-06-07 22:59:49 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
|
|
|
class BarChartTest extends TestCase
|
|
|
|
{
|
|
|
|
private const ROW_LABELS = [
|
|
|
|
'a' => 'a label',
|
|
|
|
'b' => 'b label',
|
|
|
|
];
|
|
|
|
|
|
|
|
private const COLORS = [
|
|
|
|
'a' => '#000',
|
|
|
|
'b' => '#fff',
|
|
|
|
];
|
|
|
|
|
|
|
|
private const DATA = [
|
|
|
|
'2022-07-11' => [
|
|
|
|
'day' => '2022-07-11',
|
|
|
|
'a' => 1,
|
|
|
|
'b' => 2,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2022-12-15 19:50:56 +01:00
|
|
|
private Renderer&MockObject $rendererMock;
|
2022-06-07 22:59:49 +02:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->rendererMock = $this->mockRenderer(false);
|
|
|
|
$this->mockTranslator();
|
|
|
|
}
|
|
|
|
|
2022-07-18 19:22:45 +02:00
|
|
|
public function provideRenderTestData(): Generator
|
2022-06-07 22:59:49 +02:00
|
|
|
{
|
2022-07-18 19:22:45 +02:00
|
|
|
$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,
|
2023-02-05 18:03:00 +01:00
|
|
|
],
|
2022-07-18 19:22:45 +02:00
|
|
|
];
|
|
|
|
yield 'non-empty data' => [
|
|
|
|
self::DATA,
|
|
|
|
[
|
2022-06-07 22:59:49 +02:00
|
|
|
'groups' => [
|
|
|
|
[
|
|
|
|
'bars' => [
|
|
|
|
[
|
|
|
|
'value' => 1,
|
|
|
|
'title' => "2022-07-11\na label: 1",
|
|
|
|
'height' => '20%',
|
|
|
|
'bg' => '#000',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'value' => 2,
|
|
|
|
'title' => "2022-07-11\nb label: 2",
|
|
|
|
'height' => '40%',
|
|
|
|
'bg' => '#fff',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'label' => '2022-07-11',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'colors' => self::COLORS,
|
|
|
|
'rowLabels' => self::ROW_LABELS,
|
|
|
|
'barChartClass' => '',
|
2022-07-18 19:22:45 +02:00
|
|
|
'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)
|
2022-06-07 22:59:49 +02:00
|
|
|
->willReturn('test bar chart');
|
|
|
|
self::assertSame(
|
|
|
|
'test bar chart',
|
2022-07-18 19:22:45 +02:00
|
|
|
BarChart::render(self::ROW_LABELS, self::COLORS, $testData)
|
2022-06-07 22:59:49 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideBarChartClassTestData(): array
|
|
|
|
{
|
|
|
|
return [
|
2022-07-12 00:36:38 +02:00
|
|
|
[5, ''],
|
2022-06-07 22:59:49 +02:00
|
|
|
[10, 'barchart-20'], // number to be passed to BarChart::generateChartDemoData, expected class
|
|
|
|
[20, 'barchart-40'],
|
|
|
|
[25, 'barchart-50'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideBarChartClassTestData
|
2022-07-12 00:36:38 +02:00
|
|
|
* @covers \Engelsystem\Helpers\BarChart::calculateBarChartClass
|
2022-06-07 22:59:49 +02:00
|
|
|
*/
|
|
|
|
public function testRenderBarChartClass(int $testDataCount, string $expectedClass): void
|
|
|
|
{
|
|
|
|
$this->rendererMock->expects(self::once())
|
|
|
|
->method('render')
|
|
|
|
->willReturnCallback(
|
|
|
|
function (string $template, array $data = []) use ($expectedClass) {
|
|
|
|
self::assertSame($expectedClass, $data['barChartClass']);
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
);
|
|
|
|
BarChart::render(...BarChart::generateChartDemoData($testDataCount));
|
|
|
|
}
|
2022-07-12 00:36:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Helpers\BarChart::generateChartDemoData
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGenerateChartDemoData(): void
|
2022-07-12 00:36:38 +02:00
|
|
|
{
|
|
|
|
$first = CarbonImmutable::now()->subDays(2)->format('Y-m-d');
|
|
|
|
$second = CarbonImmutable::now()->subDays()->format('Y-m-d');
|
|
|
|
$expected = [
|
|
|
|
[
|
|
|
|
'count' => 'arrived',
|
|
|
|
'sum' => 'arrived sum',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'count' => '#090',
|
|
|
|
'sum' => '#888',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
$first => [
|
|
|
|
'day' => $first,
|
|
|
|
'count' => 5001.0,
|
|
|
|
'sum' => 5001.0,
|
|
|
|
],
|
|
|
|
$second => [
|
|
|
|
'day' => $second,
|
|
|
|
'count' => 5001.0,
|
|
|
|
'sum' => 10002.0,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$data = BarChart::generateChartDemoData(2);
|
|
|
|
$this->assertEquals($expected, $data);
|
|
|
|
}
|
2022-06-07 22:59:49 +02:00
|
|
|
}
|