[export] create a parameter that will force to skip the filtering by center (ACL) when generating an export

This commit is contained in:
2023-10-19 17:19:28 +02:00
parent c7bd60a106
commit a6e930958b
47 changed files with 1145 additions and 407 deletions

View File

@@ -11,11 +11,15 @@ declare(strict_types=1);
namespace Chill\MainBundle\Test\Export;
use Chill\MainBundle\Export\DirectExportInterface;
use Chill\MainBundle\Export\ExportInterface;
use Chill\MainBundle\Test\PrepareClientTrait;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NativeQuery;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
/**
* This class provide a set of tests for exports.
@@ -94,7 +98,7 @@ abstract class AbstractExportTest extends WebTestCase
/**
* Create an instance of the report to test.
*
* @return \Chill\MainBundle\Export\ExportInterface an instance of the export to test
* @return ExportInterface|DirectExportInterface|iterable<ExportInterface>|iterable<DirectExportInterface> an instance of the export to test
*/
abstract public function getExport();
@@ -116,19 +120,40 @@ abstract class AbstractExportTest extends WebTestCase
*/
abstract public function getModifiersCombination();
protected function getParameters(bool $filterStatsByCenter): ParameterBagInterface
{
return new ParameterBag(['chill_main' => ['acl' => ['filter_stats_by_center' => $filterStatsByCenter]]]);
}
/**
* wrap the results of @see{self::getExports()}, which may be an iterable or an export into an iterble.
*/
private function getExports(): iterable
{
$exports = $this->getExport();
if (is_iterable($exports)) {
return $exports;
}
return [$exports];
}
/**
* Test the formatters type are string.
*/
public function testGetAllowedFormattersType()
{
$formattersTypes = $this->getExport()->getAllowedFormattersTypes();
foreach ($this->getExports() as $export) {
$formattersTypes = $export->getAllowedFormattersTypes();
$this->assertContainsOnly(
'string',
$formattersTypes,
true,
'Test that the method `getAllowedFormattersTypes` returns an array of string'
);
$this->assertContainsOnly(
'string',
$formattersTypes,
true,
'Test that the method `getAllowedFormattersTypes` returns an array of string'
);
}
}
/**
@@ -136,17 +161,17 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testGetDescription()
{
$export = $this->getExport();
$this->assertIsString(
$export->getDescription(),
'Assert that the `getDescription` method return a string'
);
$this->assertNotEmpty(
$export->getDescription(),
'Assert that the `getDescription` method does not return an empty '
.'string.'
);
foreach ($this->getExports() as $export) {
$this->assertIsString(
$export->getDescription(),
'Assert that the `getDescription` method return a string'
);
$this->assertNotEmpty(
$export->getDescription(),
'Assert that the `getDescription` method does not return an empty '
.'string.'
);
}
}
/**
@@ -156,19 +181,21 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testGetQueryKeys(array $data)
{
$queryKeys = $this->getExport()->getQueryKeys($data);
foreach ($this->getExports() as $export) {
$queryKeys = $export->getQueryKeys($data);
$this->assertContainsOnly(
'string',
$queryKeys,
true,
'test that the query keys returned by `getQueryKeys` are only strings'
);
$this->assertGreaterThanOrEqual(
1,
\count($queryKeys),
'test that there are at least one query key returned'
);
$this->assertContainsOnly(
'string',
$queryKeys,
true,
'test that the query keys returned by `getQueryKeys` are only strings'
);
$this->assertGreaterThanOrEqual(
1,
\count($queryKeys),
'test that there are at least one query key returned'
);
}
}
/**
@@ -187,61 +214,70 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testGetResultsAndLabels($modifiers, $acl, array $data)
{
// it is more convenient to group the `getResult` and `getLabels` test
// due to the fact that testing both methods use the same tools.
foreach ($this->getExports() as $export) {
// it is more convenient to group the `getResult` and `getLabels` test
// due to the fact that testing both methods use the same tools.
$queryKeys = $this->getExport()->getQueryKeys($data);
$query = $this->getExport()->initiateQuery($modifiers, $acl, $data);
$queryKeys = $export->getQueryKeys($data);
$query = $export->initiateQuery($modifiers, $acl, $data);
// limit the result for the query for performance reason (only for QueryBuilder,
// not possible in NativeQuery)
if ($query instanceof QueryBuilder) {
$query->setMaxResults(1);
}
// limit the result for the query for performance reason (only for QueryBuilder,
// not possible in NativeQuery)
if ($query instanceof QueryBuilder) {
$query->setMaxResults(1);
}
$results = $this->getExport()->getResult($query, $data);
$results = $export->getResult($query, $data);
$this->assertIsArray(
$results,
'assert that the returned result is an array'
);
if (0 === \count($results)) {
$this->markTestIncomplete('The result is empty. We cannot process tests '
.'on results');
}
// testing the result
$result = $results[0];
$this->assertTrue(
is_iterable($result),
'test that each row in the result is traversable or an array'
);
foreach ($result as $key => $value) {
$this->assertContains(
$key,
$queryKeys,
'test that each key is present in `getQueryKeys`'
$this->assertIsArray(
$results,
'assert that the returned result is an array'
);
$closure = $this->getExport()->getLabels($key, [$value], $data);
if (0 === \count($results)) {
$this->markTestIncomplete('The result is empty. We cannot process tests '
.'on results');
}
// testing the result
$result = $results[0];
$this->assertTrue(
\is_callable($closure, false),
'test that the `getLabels` for key is a callable'
is_iterable($result),
'test that each row in the result is traversable or an array'
);
$this->assertTrue(
// conditions
\is_string((string) \call_user_func($closure, '_header'))
&& !empty(\call_user_func($closure, '_header'))
&& '_header' !== \call_user_func($closure, '_header'),
// message
sprintf('Test that the callable return by `getLabels` for key %s '
.'can provide an header', $key)
);
$i = 0;
foreach ($result as $key => $value) {
$this->assertContains(
$key,
$queryKeys,
'test that each key is present in `getQueryKeys`'
);
$closure = $export->getLabels($key, [$value], $data);
$this->assertTrue(
\is_callable($closure, false),
'test that the `getLabels` for key is a callable'
);
$this->assertTrue(
// conditions
\is_string((string) \call_user_func($closure, '_header'))
&& !empty(\call_user_func($closure, '_header'))
&& '_header' !== \call_user_func($closure, '_header'),
// message
sprintf('Test that the callable return by `getLabels` for key %s '
.'can provide an header', $key)
);
++$i;
if ($i > 15) {
// do not iterate on each result
break;
}
}
}
}
@@ -250,14 +286,14 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testGetType()
{
$export = $this->getExport();
$this->assertIsString(
$export->getType(),
'Assert that the `getType` method return a string'
);
$this->assertNotEmpty($export->getType(), 'Assert that the `getType` method'
.' does not return an empty string.');
foreach ($this->getExports() as $export) {
$this->assertIsString(
$export->getType(),
'Assert that the `getType` method return a string'
);
$this->assertNotEmpty($export->getType(), 'Assert that the `getType` method'
.' does not return an empty string.');
}
}
/**
@@ -272,34 +308,36 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testInitiateQuery(mixed $modifiers, mixed $acl, mixed $data)
{
$query = $this->getExport()->initiateQuery($modifiers, $acl, $data);
foreach ($this->getExports() as $export) {
$query = $export->initiateQuery($modifiers, $acl, $data);
$this->assertTrue(
$query instanceof QueryBuilder || $query instanceof NativeQuery,
sprintf(
'Assert that the returned query is an instance of %s or %s',
QueryBuilder::class,
Query::class
)
);
if ($query instanceof QueryBuilder) {
$this->assertGreaterThanOrEqual(
1,
\count($query->getDQLPart('select')),
"assert there is at least one 'select' part"
$this->assertTrue(
$query instanceof QueryBuilder || $query instanceof NativeQuery,
sprintf(
'Assert that the returned query is an instance of %s or %s',
QueryBuilder::class,
NativeQuery::class
)
);
$this->assertGreaterThanOrEqual(
1,
\count($query->getDQLPart('from')),
"assert there is at least one 'from' part"
);
} elseif ($query instanceof NativeQuery) {
$this->assertNotEmpty(
$query->getSQL(),
'check that the SQL query is not empty'
);
if ($query instanceof QueryBuilder) {
$this->assertGreaterThanOrEqual(
1,
\count($query->getDQLPart('select')),
"assert there is at least one 'select' part"
);
$this->assertGreaterThanOrEqual(
1,
\count($query->getDQLPart('from')),
"assert there is at least one 'from' part"
);
} elseif ($query instanceof NativeQuery) {
$this->assertNotEmpty(
$query->getSQL(),
'check that the SQL query is not empty'
);
}
}
}
@@ -308,9 +346,11 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testRequiredRole()
{
$role = $this->getExport()->requiredRole();
foreach ($this->getExports() as $export) {
$role = $export->requiredRole();
self::assertIsString($role);
self::assertIsString($role);
}
}
/**
@@ -323,22 +363,23 @@ abstract class AbstractExportTest extends WebTestCase
*/
public function testSupportsModifier(mixed $modifiers, mixed $acl, mixed $data)
{
$export = $this->getExport();
$query = $export->initiateQuery($modifiers, $acl, $data);
foreach ($this->getExports() as $export) {
$query = $export->initiateQuery($modifiers, $acl, $data);
if ($query instanceof QueryBuilder) {
$this->assertContainsOnly(
'string',
$export->supportsModifiers(),
true,
'Test that the `supportsModifiers` method returns only strings'
);
} elseif ($query instanceof NativeQuery) {
$this->assertTrue(
null === $export->supportsModifiers()
|| 0 === \count($export->supportsModifiers()),
'Test that the `supportsModifier` methods returns null or an empty array'
);
if ($query instanceof QueryBuilder) {
$this->assertContainsOnly(
'string',
$export->supportsModifiers(),
true,
'Test that the `supportsModifiers` method returns only strings'
);
} elseif ($query instanceof NativeQuery) {
$this->assertTrue(
null === $export->supportsModifiers()
|| 0 === \count($export->supportsModifiers()),
'Test that the `supportsModifier` methods returns null or an empty array'
);
}
}
}
}