normalizer = self::$container->get(NormalizerInterface::class); $this->entityManager = self::$container->get(EntityManagerInterface::class); } public function testNormalizationRecursive(): void { $person = new Person(); $person->setFirstName('ok')->setLastName('ok'); $this->entityManager->persist($person); $member = new HouseholdMember(); $household = new Household(); $position = (new Position()) ->setShareHousehold(true) ->setAllowHolder(true); $member->setPerson($person) ->setStartDate(new DateTimeImmutable('1 year ago')) ->setEndDate(new DateTimeImmutable('1 month ago')) ->setPosition($position); $household->addMember($member); $normalized = $this->normalizer->normalize( $household, 'json', ['groups' => ['read']] ); $this->assertIsArray($normalized); $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']); } }