Generate export using denormalization

This commit is contained in:
2025-02-23 23:16:30 +01:00
parent 1f1d38acef
commit 2c812fc5fe
7 changed files with 457 additions and 328 deletions

View File

@@ -0,0 +1,205 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Tests\Export;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Export\DirectExportInterface;
use Chill\MainBundle\Export\ExportConfigNormalizer;
use Chill\MainBundle\Export\ExportGenerationContext;
use Chill\MainBundle\Export\ExportGenerator;
use Chill\MainBundle\Export\ExportInterface;
use Chill\MainBundle\Export\ExportManager;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Export\FormattedExportGeneration;
use Chill\MainBundle\Export\FormatterInterface;
use Doctrine\ORM\NativeQuery;
use Doctrine\ORM\QueryBuilder;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\NullLogger;
/**
* @internal
*
* @coversNothing
*/
class ExportGeneratorTest extends TestCase
{
use ProphecyTrait;
public function testGenerateHappyScenario()
{
$initialData = ['initial' => 'test'];
$fullConfig = [
'export' => $formExportData = ['key' => 'form1'],
'filters' => [
'dummy_filter' => ['enabled' => true, 'form' => $formFilterData = ['key' => 'form2']],
'disabled_filter' => ['enabled' => false],
],
'aggregators' => [
'dummy_aggregator' => ['enabled' => true, 'form' => $formAggregatorData = ['key' => 'form3']],
'disabled_aggregator' => ['enabled' => false],
],
'pick_formatter' => 'xlsx',
'formatter' => ['form' => $formatterData = ['key' => 'form4']],
'centers' => [$centerA = new Center(), $centerB = new Center()],
];
$user = new User();
$export = $this->prophesize(ExportInterface::class);
$filter = $this->prophesize(FilterInterface::class);
$filter->applyOn()->willReturn('tagada');
$aggregator = $this->prophesize(AggregatorInterface::class);
$aggregator->applyOn()->willReturn('tsointsoin');
$formatter = $this->prophesize(FormatterInterface::class);
$query = $this->prophesize(QueryBuilder::class);
// required methods
$export->initiateQuery(
['tagada', 'tsointsoin'],
[['center' => $centerA, 'circles' => []], ['center' => $centerB, 'circles' => []]],
['key' => 'form1'],
)->shouldBeCalled()->willReturn($query->reveal());
$export->getResult($query->reveal(), $formExportData, Argument::that(static fn (ExportGenerationContext $context) => $context->byUser === $user))
->shouldBeCalled()->willReturn([['result0' => '0']]);
$filter->alterQuery($query->reveal(), $formFilterData, Argument::that(static fn (ExportGenerationContext $context) => $context->byUser === $user))
->shouldBeCalled();
$aggregator->alterQuery($query->reveal(), $formAggregatorData, Argument::that(static fn (ExportGenerationContext $context) => $context->byUser === $user))
->shouldBeCalled();
$formatter->generate(
[['result0' => '0']],
$formatterData,
'dummy',
$formExportData,
['dummy_filter' => $formFilterData],
['dummy_aggregator' => $formAggregatorData]
)
->shouldBeCalled()
->willReturn(new FormattedExportGeneration('export result', 'text/text'));
$exportConfigNormalizer = $this->prophesize(ExportConfigNormalizer::class);
$exportConfigNormalizer->denormalizeConfig('dummy', $initialData)->willReturn($fullConfig);
$exportManager = $this->prophesize(ExportManager::class);
$exportManager->getExport('dummy')->willReturn($export->reveal());
$exportManager->getFilter('dummy_filter')->willReturn($filter->reveal());
$exportManager->getAggregator('dummy_aggregator')->willReturn($aggregator->reveal());
$exportManager->getFormatter('xlsx')->willReturn($formatter->reveal());
$generator = new ExportGenerator($exportManager->reveal(), $exportConfigNormalizer->reveal(), new NullLogger());
$actual = $generator->generate('dummy', $initialData, $user);
self::assertInstanceOf(FormattedExportGeneration::class, $actual);
self::assertEquals('export result', $actual->content);
self::assertEquals('text/text', $actual->contentType);
}
public function testGenerateNativeSqlHappyScenario()
{
$initialData = ['initial' => 'test'];
$fullConfig = [
'export' => $formExportData = ['key' => 'form1'],
'filters' => [],
'aggregators' => [],
'pick_formatter' => 'xlsx',
'formatter' => ['form' => $formatterData = ['key' => 'form4']],
'centers' => [$centerA = new Center(), $centerB = new Center()],
];
$user = new User();
$export = $this->prophesize(ExportInterface::class);
$formatter = $this->prophesize(FormatterInterface::class);
$query = $this->prophesize(NativeQuery::class);
// required methods
$export->initiateQuery(
[],
[['center' => $centerA, 'circles' => []], ['center' => $centerB, 'circles' => []]],
['key' => 'form1'],
)->shouldBeCalled()->willReturn($query->reveal());
$export->getResult($query->reveal(), $formExportData, Argument::that(static fn (ExportGenerationContext $context) => $context->byUser === $user))
->shouldBeCalled()->willReturn([['result0' => '0']]);
$export->supportsModifiers()->willReturn([]);
$formatter->generate(
[['result0' => '0']],
$formatterData,
'dummy',
$formExportData,
[],
[]
)
->shouldBeCalled()
->willReturn(new FormattedExportGeneration('export result', 'text/text'));
$exportConfigNormalizer = $this->prophesize(ExportConfigNormalizer::class);
$exportConfigNormalizer->denormalizeConfig('dummy', $initialData)->willReturn($fullConfig);
$exportManager = $this->prophesize(ExportManager::class);
$exportManager->getExport('dummy')->willReturn($export->reveal());
$exportManager->getFormatter('xlsx')->willReturn($formatter->reveal());
$generator = new ExportGenerator($exportManager->reveal(), $exportConfigNormalizer->reveal(), new NullLogger());
$actual = $generator->generate('dummy', $initialData, $user);
self::assertInstanceOf(FormattedExportGeneration::class, $actual);
self::assertEquals('export result', $actual->content);
self::assertEquals('text/text', $actual->contentType);
}
public function testGenerateDirectExportHappyScenario()
{
$initialData = ['initial' => 'test'];
$fullConfig = [
'export' => $formExportData = ['key' => 'form1'],
'filters' => [],
'aggregators' => [],
'pick_formatter' => 'xlsx',
'formatter' => ['form' => $formatterData = ['key' => 'form4']],
'centers' => [$centerA = new Center(), $centerB = new Center()],
];
$user = new User();
$export = $this->prophesize(DirectExportInterface::class);
// required methods
$export->generate(
[['center' => $centerA, 'circles' => []], ['center' => $centerB, 'circles' => []]],
['key' => 'form1'],
Argument::that(static fn (ExportGenerationContext $context) => $user === $context->byUser),
)->shouldBeCalled()
->willReturn(new FormattedExportGeneration('export result', 'text/text'));
$exportConfigNormalizer = $this->prophesize(ExportConfigNormalizer::class);
$exportConfigNormalizer->denormalizeConfig('dummy', $initialData)->willReturn($fullConfig);
$exportManager = $this->prophesize(ExportManager::class);
$exportManager->getExport('dummy')->willReturn($export->reveal());
$generator = new ExportGenerator($exportManager->reveal(), $exportConfigNormalizer->reveal(), new NullLogger());
$actual = $generator->generate('dummy', $initialData, $user);
self::assertInstanceOf(FormattedExportGeneration::class, $actual);
self::assertEquals('export result', $actual->content);
self::assertEquals('text/text', $actual->contentType);
}
}