Fixed: fix docgen normalization on household with "old" members

When a household had old members, the indexes of each "current" members
should be numerical and contiguous, to be transformed in a list. If this
is not the case, the members are mapped to an associative array.

This commit alter the generic DocGenObjectNormalizer to ensure that
the ReadableCollection are normalized using the
CollectionDocGenNormalizer as default, which do not preserve keys.
This commit is contained in:
2023-05-16 23:24:33 +02:00
parent 1abaf2acb0
commit 8a684734e7
4 changed files with 117 additions and 3 deletions

View File

@@ -18,6 +18,7 @@ use Chill\PersonBundle\Entity\Person;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
@@ -39,7 +40,7 @@ final class HouseholdNormalizerTest extends KernelTestCase
$this->entityManager = self::$container->get(EntityManagerInterface::class);
}
public function testNormalizationRecursive()
public function testNormalizationRecursive(): void
{
$person = new Person();
$person->setFirstName('ok')->setLastName('ok');
@@ -67,4 +68,53 @@ final class HouseholdNormalizerTest extends KernelTestCase
$this->assertArrayHasKey('type', $normalized);
$this->assertEquals('household', $normalized['type']);
}
/**
* When a household have old members (members which are not "current"),
* the indexes of the household must be reset to numerical and contiguous
* indexes. This ensure that it will be mapped as a list, not as an associative
* array.
*/
public function testHouseholdDocGenNormalizationWithOldMembers(): void
{
$previousPerson = new Person();
$previousPerson->setFirstName('ok')->setLastName('ok');
$this->entityManager->persist($previousPerson);
$member = new HouseholdMember();
$household = new Household();
$position = (new Position())
->setShareHousehold(true)
->setAllowHolder(true);
$member->setPerson($previousPerson)
->setStartDate(new DateTimeImmutable('1 year ago'))
->setEndDate(new DateTimeImmutable('1 month ago'))
->setPosition($position);
$household->addMember($member);
$currentPerson1 = new Person();
$currentPerson1->setFirstName('p1')->setLastName('p1');
$this->entityManager->persist($currentPerson1);
$member = new HouseholdMember();
$member->setPerson($currentPerson1)
->setStartDate(new DateTimeImmutable('1 year ago'))
->setPosition($position);
$household->addMember($member);
$normalized = $this->normalizer->normalize(
$household,
'docgen',
[
AbstractNormalizer::GROUPS => ['docgen:read'],
'docgen:expects' => Household::class,
'docgen:person:with-household' => false,
'docgen:person:with-relations' => false,
'docgen:person:with-budget' => false,
]
);
self::assertIsArray($normalized);
self::assertArrayHasKey(0, $normalized['currentMembers']);
}
}