From ec3d901d2f3c323df2a2f01fcfe1557a7d2af04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 8 Apr 2025 15:30:25 +0200 Subject: [PATCH] Fix tests --- .../Tests/Export/ExportManagerTest.php | 178 +----------------- 1 file changed, 5 insertions(+), 173 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Tests/Export/ExportManagerTest.php b/src/Bundle/ChillMainBundle/Tests/Export/ExportManagerTest.php index f68a8d7d6..77006cd81 100644 --- a/src/Bundle/ChillMainBundle/Tests/Export/ExportManagerTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Export/ExportManagerTest.php @@ -194,176 +194,6 @@ final class ExportManagerTest extends KernelTestCase $this->assertNotContains($formatterBar->reveal(), $obtained); } - /** - * Test the generation of an export. - */ - public function testGenerate() - { - $center = $this->prepareCenter(100, 'center'); - $user = $this->prepareUser([]); - - $authorizationChecker = $this->prophet->prophesize(); - $authorizationChecker->willImplement(AuthorizationCheckerInterface::class); - $authorizationChecker->isGranted('CHILL_STAT_DUMMY', $center) - ->willReturn(true); - $exports = []; - $filters = []; - $aggregators = []; - - $em = self::getContainer()->get(EntityManagerInterface::class); - - $export = $this->prophet->prophesize(); - $export->willImplement(ExportInterface::class); - $export->initiateQuery( - Argument::is(['foo']), - Argument::Type('array'), - Argument::is(['a' => 'b']) - ) - ->will(static function () use ($em) { - $qb = $em->createQueryBuilder(); - - return $qb->addSelect('COUNT(user.id) as export') - ->from(User::class, 'user'); - }); - $export->initiateQuery( - Argument::is(['foo']), - Argument::Type('array'), - Argument::is(['a' => 'b']) - )->shouldBeCalled(); - $export->supportsModifiers()->willReturn(['foo']); - $export->requiredRole()->willReturn('CHILL_STAT_DUMMY'); - $export->getResult(Argument::Type(QueryBuilder::class), Argument::Type('array'))->willReturn([ - [ - 'aggregator' => 'cat a', - 'export' => 0, - ], - [ - 'aggregator' => 'cat b', - 'export' => 1, - ], - ]); - $export->getLabels( - Argument::is('export'), - Argument::is([0, 1]), - Argument::Type('array') - ) - ->willReturn(static function ($value) { - switch ($value) { - case 0: - case 1: - return $value; - - case '_header': - return 'export'; - - default: throw new \RuntimeException(sprintf('The value %s is not valid', $value)); - } - }); - - $export->getQueryKeys(Argument::Type('array'))->willReturn(['export']); - $export->getTitle()->willReturn('dummy title'); - $exports['dummy'] = $export->reveal(); - - $filter = $this->prophet->prophesize(); - $filter->willImplement(FilterInterface::class); - $filter->alterQuery(Argument::Type(QueryBuilder::class), Argument::Type('array')) - ->willReturn(null); - $filter->alterQuery(Argument::Type(QueryBuilder::class), Argument::Type('array')) - ->shouldBeCalled(); - $filter->addRole()->shouldBeCalled(); - $filter->addRole()->willReturn(null); - $filter->applyOn()->willReturn('foo'); - $filter->describeAction(Argument::cetera())->willReturn('filtered string'); - $filters['filter_foo'] = $filter->reveal(); - - $aggregator = $this->prophet->prophesize(); - $aggregator->willImplement(AggregatorInterface::class); - $aggregator->addRole()->willReturn(null); - $aggregator->applyOn()->willReturn('foo'); - $aggregator->alterQuery(Argument::Type(QueryBuilder::class), Argument::Type('array')) - ->willReturn(null); - $aggregator->alterQuery(Argument::Type(QueryBuilder::class), Argument::Type('array')) - ->shouldBeCalled(); - $aggregator->getQueryKeys(Argument::Type('array'))->willReturn(['aggregator']); - $aggregator->getLabels( - Argument::is('aggregator'), - Argument::is(['cat a', 'cat b']), - Argument::is([]) - ) - ->willReturn(static fn ($value) => match ($value) { - '_header' => 'foo_header', - 'cat a' => 'label cat a', - 'cat b' => 'label cat b', - default => throw new \RuntimeException(sprintf('This value (%s) is not valid', $value)), - }); - $aggregator->addRole()->willReturn(null); - $aggregator->addRole()->shouldBeCalled(); - $aggregators['aggregator_foo'] = $aggregator->reveal(); - - $exportManager = $this->createExportManager( - null, - null, - $authorizationChecker->reveal(), - null, - $user, - $exports, - $aggregators, - $filters - ); - - // add formatter interface - $formatter = new \Chill\MainBundle\Export\Formatter\SpreadSheetFormatter( - self::getContainer()->get(TranslatorInterface::class), - $exportManager - ); - - $exportManager->addFormatter($formatter, 'spreadsheet'); - - $response = $exportManager->generate( - 'dummy', - [$center], - [ - ExportType::FILTER_KEY => [ - 'filter_foo' => [ - 'enabled' => true, - 'form' => [], - ], - ], - ExportType::AGGREGATOR_KEY => [ - 'aggregator_foo' => [ - 'enabled' => true, - 'form' => [], - ], - ], - ExportType::PICK_FORMATTER_KEY => [ - 'alias' => 'spreadsheet', - ], - ExportType::EXPORT_KEY => [ - 'a' => 'b', - ], - ], - [ - 'format' => 'csv', - 'aggregator_foo' => [ - 'order' => 1, - ], - ] - ); - - $this->assertInstanceOf(Response::class, $response); - $expected = <<<'EOT' - "dummy title","" - "","" - "filtered string","" - "foo_header","export" - "label cat a","0" - "label cat b","1" - - EOT; - - $this->assertEquals($expected, $response->getContent()); - } - public function testIsGrantedForElementWithExportAndUserIsGranted() { $center = $this->prepareCenter(100, 'center A'); @@ -506,6 +336,7 @@ final class ExportManagerTest extends KernelTestCase array $exports = [], array $aggregators = [], array $filters = [], + array $formatters = [], ): ExportManager { $localUser = $user ?? self::getContainer()->get( UserRepositoryInterface::class @@ -516,13 +347,14 @@ final class ExportManagerTest extends KernelTestCase $tokenStorage->setToken($token); return new ExportManager( - $logger ?? self::getContainer()->get('logger'), + $logger ?? self::getContainer()->get(LoggerInterface::class), $authorizationChecker ?? self::getContainer()->get('security.authorization_checker'), $authorizationHelper ?? self::getContainer()->get('chill.main.security.authorization.helper'), $tokenStorage, $exports, $aggregators, - $filters + $filters, + $formatters, ); } } @@ -648,7 +480,7 @@ class DummyExport implements ExportInterface public function initiateQuery(array $requiredModifiers, array $acl, array $data, \Chill\MainBundle\Export\ExportGenerationContext $context): QueryBuilder { - return null; + throw new \RuntimeException("not implemented"); } public function requiredRole(): string