mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Fix tests
This commit is contained in:
parent
f12d689382
commit
ec3d901d2f
@ -194,176 +194,6 @@ final class ExportManagerTest extends KernelTestCase
|
|||||||
$this->assertNotContains($formatterBar->reveal(), $obtained);
|
$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()
|
public function testIsGrantedForElementWithExportAndUserIsGranted()
|
||||||
{
|
{
|
||||||
$center = $this->prepareCenter(100, 'center A');
|
$center = $this->prepareCenter(100, 'center A');
|
||||||
@ -506,6 +336,7 @@ final class ExportManagerTest extends KernelTestCase
|
|||||||
array $exports = [],
|
array $exports = [],
|
||||||
array $aggregators = [],
|
array $aggregators = [],
|
||||||
array $filters = [],
|
array $filters = [],
|
||||||
|
array $formatters = [],
|
||||||
): ExportManager {
|
): ExportManager {
|
||||||
$localUser = $user ?? self::getContainer()->get(
|
$localUser = $user ?? self::getContainer()->get(
|
||||||
UserRepositoryInterface::class
|
UserRepositoryInterface::class
|
||||||
@ -516,13 +347,14 @@ final class ExportManagerTest extends KernelTestCase
|
|||||||
$tokenStorage->setToken($token);
|
$tokenStorage->setToken($token);
|
||||||
|
|
||||||
return new ExportManager(
|
return new ExportManager(
|
||||||
$logger ?? self::getContainer()->get('logger'),
|
$logger ?? self::getContainer()->get(LoggerInterface::class),
|
||||||
$authorizationChecker ?? self::getContainer()->get('security.authorization_checker'),
|
$authorizationChecker ?? self::getContainer()->get('security.authorization_checker'),
|
||||||
$authorizationHelper ?? self::getContainer()->get('chill.main.security.authorization.helper'),
|
$authorizationHelper ?? self::getContainer()->get('chill.main.security.authorization.helper'),
|
||||||
$tokenStorage,
|
$tokenStorage,
|
||||||
$exports,
|
$exports,
|
||||||
$aggregators,
|
$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
|
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
|
public function requiredRole(): string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user