mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Add data normalization test and context handling improvements
Introduced data normalization testing methods to validate form data processing. Enhanced `initiateQuery` to include `ExportGenerationContext` with user retrieval logic for improved query handling. These changes strengthen data integrity and contextual query execution within export functionality.
This commit is contained in:
parent
80b9ce3c3e
commit
694b1f3c1f
@ -11,7 +11,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Test\Export;
|
||||
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Export\DirectExportInterface;
|
||||
use Chill\MainBundle\Export\ExportGenerationContext;
|
||||
use Chill\MainBundle\Export\ExportInterface;
|
||||
use Chill\MainBundle\Test\PrepareClientTrait;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
@ -31,6 +33,18 @@ abstract class AbstractExportTest extends WebTestCase
|
||||
{
|
||||
use PrepareClientTrait;
|
||||
|
||||
/**
|
||||
* A list of data to normalize.
|
||||
*
|
||||
* @return iterable{array}
|
||||
*/
|
||||
public static function dataProviderFormDataToNormalize(): iterable
|
||||
{
|
||||
foreach (static::getFormData() as $data) {
|
||||
yield [$data, 1, []];
|
||||
}
|
||||
}
|
||||
|
||||
public static function dataProviderGetQueryKeys()
|
||||
{
|
||||
foreach (static::getFormData() as $data) {
|
||||
@ -120,6 +134,19 @@ abstract class AbstractExportTest extends WebTestCase
|
||||
*/
|
||||
abstract public static function getModifiersCombination(): array;
|
||||
|
||||
protected function getUser(): User
|
||||
{
|
||||
$em = static::getContainer()->get(EntityManagerInterface::class);
|
||||
if (null === $user = $em->createQueryBuilder()->select('u')
|
||||
->from(User::class, 'u')
|
||||
->setMaxResults(1)
|
||||
->getQuery()->getOneOrNullResult()) {
|
||||
throw new \RuntimeException('User not found');
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
protected function getParameters(bool $filterStatsByCenter): ParameterBagInterface
|
||||
{
|
||||
return new ParameterBag(['chill_main' => ['acl' => ['filter_stats_by_center' => $filterStatsByCenter]]]);
|
||||
@ -139,6 +166,41 @@ abstract class AbstractExportTest extends WebTestCase
|
||||
return [$exports];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderFormDataToNormalize
|
||||
*/
|
||||
public function testDataNormalization(array $data, int $version, array $customAssert): void
|
||||
{
|
||||
$export = $this->getExport();
|
||||
|
||||
if (is_iterable($export)) {
|
||||
foreach ($export as $e) {
|
||||
$this->testOneDataNormalization($e, $data, $version, $customAssert);
|
||||
}
|
||||
} else {
|
||||
$this->testOneDataNormalization($export, $data, $version, $customAssert);
|
||||
}
|
||||
}
|
||||
|
||||
private function testOneDataNormalization(ExportInterface|DirectExportInterface $export, array $data, int $version, array $customAssert): void
|
||||
{
|
||||
$normalized = $export->normalizeFormData($data);
|
||||
$actual = $export->denormalizeFormData($normalized, $version);
|
||||
|
||||
self::assertEqualsCanonicalizing(array_keys($data), array_keys($actual));
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
self::assertArrayHasKey($key, $actual);
|
||||
if (array_key_exists($key, $customAssert)) {
|
||||
call_user_func($customAssert[$key], $actual[$key], $value);
|
||||
} elseif (is_object($value) && method_exists($value, 'getId')) {
|
||||
self::assertEquals($value->getId(), $actual[$key]->getId());
|
||||
} else {
|
||||
self::assertEquals($value, $actual[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the formatters type are string.
|
||||
*/
|
||||
@ -219,7 +281,7 @@ abstract class AbstractExportTest extends WebTestCase
|
||||
// due to the fact that testing both methods use the same tools.
|
||||
|
||||
$queryKeys = $export->getQueryKeys($data);
|
||||
$query = $export->initiateQuery($modifiers, $acl, $data);
|
||||
$query = $export->initiateQuery($modifiers, $acl, $data, $exportGenerationContext = new ExportGenerationContext($this->getUser()));
|
||||
|
||||
// limit the result for the query for performance reason (only for QueryBuilder,
|
||||
// not possible in NativeQuery)
|
||||
@ -227,7 +289,7 @@ abstract class AbstractExportTest extends WebTestCase
|
||||
$query->setMaxResults(1);
|
||||
}
|
||||
|
||||
$results = $export->getResult($query, $data);
|
||||
$results = $export->getResult($query, $data, $exportGenerationContext);
|
||||
|
||||
$this->assertIsArray(
|
||||
$results,
|
||||
@ -309,7 +371,7 @@ abstract class AbstractExportTest extends WebTestCase
|
||||
public function testInitiateQuery(mixed $modifiers, mixed $acl, mixed $data)
|
||||
{
|
||||
foreach ($this->getExports() as $export) {
|
||||
$query = $export->initiateQuery($modifiers, $acl, $data);
|
||||
$query = $export->initiateQuery($modifiers, $acl, $data, new ExportGenerationContext($this->getUser()));
|
||||
|
||||
$this->assertTrue(
|
||||
$query instanceof QueryBuilder || $query instanceof NativeQuery,
|
||||
@ -364,7 +426,7 @@ abstract class AbstractExportTest extends WebTestCase
|
||||
public function testSupportsModifier(mixed $modifiers, mixed $acl, mixed $data)
|
||||
{
|
||||
foreach ($this->getExports() as $export) {
|
||||
$query = $export->initiateQuery($modifiers, $acl, $data);
|
||||
$query = $export->initiateQuery($modifiers, $acl, $data, new ExportGenerationContext($this->getUser()));
|
||||
|
||||
if ($query instanceof QueryBuilder) {
|
||||
$this->assertContainsOnly(
|
||||
|
Loading…
x
Reference in New Issue
Block a user