'', 'lastname' => '', 'altNames' => '', 'text' => '', 'isNull' => true, 'type' => 'person', 'birthdate' => ['short' => '', 'long' => ''], 'deathdate' => ['short' => '', 'long' => ''], 'gender' => '', 'civility' => '@ignored', 'address' => '@ignored', 'maritalStatus' => '', 'maritalStatusDate' => ['short' => '', 'long' => ''], 'email' => '', 'firstPhoneNumber' => '', 'fixPhoneNumber' => '', 'mobilePhoneNumber' => '', 'nationality' => '', 'placeOfBirth' => '', 'memo' => '', 'numberOfChildren' => '', ]; private NormalizerInterface $normalizer; protected function setUp() { self::bootKernel(); $this->normalizer = self::$container->get(NormalizerInterface::class); } public function generateData() { $person = new Person(); $person ->setFirstName('Renaud') ->setLastName('Mégane'); $expected = array_merge( self::BLANK, ['firstname' => 'Renaud', 'lastname' => 'Mégane', 'text' => 'Renaud Mégane', ] ); yield [$person, $expected, 'partial normalization for a person']; yield [null, self::BLANK, 'normalization for a null person']; } public function testNormalizationNullOrNotNullHaveSameKeys() { $period = new Person(); $notNullData = $this->buildPersonNormalizer()->normalize($period, 'docgen', ['docgen:expects' => Person::class]); $nullData = $this->buildPersonNormalizer()->normalize(null, 'docgen', ['docgen:expects' => Person::class]); $this->assertEqualsCanonicalizing( array_keys($notNullData), array_keys($nullData), 'test that the data returned by null value and a Person have the same keys' ); } /** * @dataProvider generateData * * @param mixed $expected * @param mixed $msg */ public function testNormalize(?Person $person, $expected, $msg) { $normalized = $this->normalizer->normalize($person, 'docgen', [ 'docgen:expects' => Person::class, 'groups' => 'docgen:read', ]); $this->assertIsArray($normalized); foreach ($normalized as $key => $value) { if ('@ignored' === $value) { continue; } $this->assertEquals($value, $normalized[$key]); } $this->assertEqualsCanonicalizing(array_keys($expected), array_keys($normalized), $msg); } public function testNormalizePersonWithHousehold() { $household = new Household(); $person = new Person(); $person ->setFirstName('Renaud') ->setLastName('Mégane'); $householdMember = new HouseholdMember(); $householdMember ->setPosition((new Position())->setAllowHolder(true)->setLabel(['fr' => 'position']) ->setShareHousehold(true)) ->setHolder(true); $person->addHouseholdParticipation($householdMember); $household->addMember($householdMember); $person = new Person(); $person ->setFirstName('Citroen') ->setLastName('Xsara'); $householdMember = new HouseholdMember(); $householdMember ->setPosition((new Position())->setAllowHolder(true)->setLabel(['fr' => 'position2']) ->setShareHousehold(true)) ->setHolder(false); $person->addHouseholdParticipation($householdMember); $household->addMember($householdMember); $actual = $this->normalizer->normalize($person, 'docgen', [ 'groups' => 'docgen:read', 'docgen:expects' => Person::class, 'docgen:person:with-household' => true, ]); $this->assertCount(2, $household->getMembers()); $this->assertIsArray($actual); $this->assertArrayHasKey('household', $actual); $this->assertCount(2, $actual['household']['currentMembers']); $this->assertCount(2, $actual['household']['members']); } public function testNormalizePersonWithRelationships() { $person = (new Person())->setFirstName('Renaud')->setLastName('megane'); $father = (new Person())->setFirstName('Clément')->setLastName('megane'); $mother = (new Person())->setFirstName('Mireille')->setLastName('Mathieu'); $sister = (new Person())->setFirstName('Callie')->setLastName('megane'); $relations = [ (new Relationship())->setFromPerson($person)->setToPerson($father) ->setReverse(false)->setRelation((new Relation())->setTitle(['fr' => 'Père']) ->setReverseTitle(['fr' => 'Fils'])), (new Relationship())->setFromPerson($person)->setToPerson($mother) ->setReverse(false)->setRelation((new Relation())->setTitle(['fr' => 'Mère']) ->setReverseTitle(['fr' => 'Fils'])), (new Relationship())->setFromPerson($person)->setToPerson($sister) ->setReverse(true)->setRelation((new Relation())->setTitle(['fr' => 'Frère']) ->setReverseTitle(['fr' => 'Soeur'])), ]; $repository = $this->prophesize(RelationshipRepository::class); $repository->findByPerson($person)->willReturn($relations); $normalizer = $this->buildPersonNormalizer(null, $repository->reveal(), null, null); $actual = $normalizer->normalize($person, 'docgen', [ 'groups' => 'docgen:read', 'docgen:expects' => Person::class, 'docgen:person:with-relations' => true, ]); $this->assertIsArray($actual); $this->assertArrayHasKey('relations', $actual); $this->assertCount(3, $actual['relations']); } private function buildPersonNormalizer( ?PersonRender $personRender = null, ?RelationshipRepository $relationshipRepository = null, ?TranslatorInterface $translator = null, ?TranslatableStringHelper $translatableStringHelper = null ): PersonDocGenNormalizer { $normalizer = new PersonDocGenNormalizer( $personRender ?? self::$container->get(PersonRender::class), $relationshipRepository ?? self::$container->get(RelationshipRepository::class), $translator ?? self::$container->get(TranslatorInterface::class), $translatableStringHelper ?? self::$container->get(TranslatableStringHelperInterface::class) ); $normalizerManager = $this->prophesize(NormalizerInterface::class); $normalizerManager->supportsNormalization(Argument::any(), 'docgen', Argument::any())->willReturn(true); $normalizerManager->normalize(Argument::type(Person::class), 'docgen', Argument::any()) ->will(static function ($args) use ($normalizer) { return $normalizer->normalize($args[0], $args[1], $args[2]); }); $normalizerManager->normalize(Argument::any(), 'docgen', Argument::any())->will( static function ($args) { if (is_iterable($args[0])) { $r = []; foreach ($args[0] as $i) { $r[] = ['fake' => true, 'hash' => spl_object_hash($i)]; } return $r; } return ['fake' => true, 'hash' => null !== $args[0] ? spl_object_hash($args[0]) : null]; } ); $normalizer->setNormalizer($normalizerManager->reveal()); return $normalizer; } }