add docgen:normalization for relation

This commit is contained in:
Julien Fastré 2021-12-09 13:51:36 +01:00
parent 24a404964b
commit 8a9024de13
10 changed files with 173 additions and 148 deletions

View File

@ -221,6 +221,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
if (is_iterable($value)) { if (is_iterable($value)) {
$arr = []; $arr = [];
foreach ($value as $k => $v) { foreach ($value as $k => $v) {
$arr[$k] = $arr[$k] =
$this->normalizer->normalize($v, $format, array_merge( $this->normalizer->normalize($v, $format, array_merge(

View File

@ -14,7 +14,6 @@ namespace Chill\DocStoreBundle\Form;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
use Chill\DocStoreBundle\Entity\Document; use Chill\DocStoreBundle\Entity\Document;
use Chill\DocStoreBundle\Entity\DocumentCategory; use Chill\DocStoreBundle\Entity\DocumentCategory;
use Chill\DocStoreBundle\Entity\PersonDocument;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\ChillDateType; use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\MainBundle\Form\Type\ChillTextareaType; use Chill\MainBundle\Form\Type\ChillTextareaType;
@ -76,7 +75,7 @@ class AccompanyingCourseDocumentType extends AbstractType
'query_builder' => static function (EntityRepository $er) { 'query_builder' => static function (EntityRepository $er) {
return $er->createQueryBuilder('c') return $er->createQueryBuilder('c')
->where('c.documentClass = :docClass') ->where('c.documentClass = :docClass')
->setParameter('docClass', AccompanyingCourseDocument::class); ->setParameter('docClass', AccompanyingCourseDocument::class);
}, },
'choice_label' => function ($entity = null) { 'choice_label' => function ($entity = null) {
return $entity ? $this->translatableStringHelper->localize($entity->getName()) : ''; return $entity ? $this->translatableStringHelper->localize($entity->getName()) : '';

View File

@ -18,7 +18,6 @@ use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
use function array_values;
class RelationshipApiController extends ApiController class RelationshipApiController extends ApiController
{ {

View File

@ -19,6 +19,7 @@ use DateTimeImmutable;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\DiscriminatorColumn; use Doctrine\ORM\Mapping\DiscriminatorColumn;
use RuntimeException;
use Symfony\Component\Serializer\Annotation as Serializer; use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap; use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
@ -117,18 +118,17 @@ class Relationship implements TrackCreationInterface, TrackUpdateInterface
} }
/** /**
* Return the opposite person of the @link{counterpart} person. * Return the opposite person of the @see{counterpart} person.
* *
* this is the from person if the given is associated to the To, * this is the from person if the given is associated to the To,
* or the To person otherwise. * or the To person otherwise.
* *
* @param Person $counterpartthe counterpart
* @throw RuntimeException if the counterpart is neither in the from or to person * @throw RuntimeException if the counterpart is neither in the from or to person
*/ */
public function getOpposite(Person $counterpart): Person public function getOpposite(Person $counterpart): Person
{ {
if ($this->fromPerson !== $counterpart && $this->toPerson !== $counterpart) { if ($this->fromPerson !== $counterpart && $this->toPerson !== $counterpart) {
throw new \RuntimeException("the counterpart is neither the from nor to person for this relationship"); throw new RuntimeException('the counterpart is neither the from nor to person for this relationship');
} }
if ($this->fromPerson === $counterpart) { if ($this->fromPerson === $counterpart) {

View File

@ -12,7 +12,6 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Repository\Relationships; namespace Chill\PersonBundle\Repository\Relationships;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Relationships\Relation;
use Chill\PersonBundle\Entity\Relationships\Relationship; use Chill\PersonBundle\Entity\Relationships\Relationship;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
@ -21,16 +20,24 @@ use Doctrine\Persistence\ObjectRepository;
class RelationshipRepository implements ObjectRepository class RelationshipRepository implements ObjectRepository
{ {
private EntityRepository $repository;
private EntityManagerInterface $em; private EntityManagerInterface $em;
private EntityRepository $repository;
public function __construct(EntityManagerInterface $em) public function __construct(EntityManagerInterface $em)
{ {
$this->repository = $em->getRepository(Relationship::class); $this->repository = $em->getRepository(Relationship::class);
$this->em = $em; $this->em = $em;
} }
public function countByPerson(Person $person): int
{
return $this->buildQueryByPerson($person)
->select('COUNT(p)')
->getQuery()
->getSingleScalarResult();
}
public function find($id): ?Relationship public function find($id): ?Relationship
{ {
return $this->repository->find($id); return $this->repository->find($id);
@ -47,7 +54,6 @@ class RelationshipRepository implements ObjectRepository
} }
/** /**
* @param Person $person
* @return array|Relationship[] * @return array|Relationship[]
*/ */
public function findByPerson(Person $person): array public function findByPerson(Person $person): array
@ -58,12 +64,14 @@ class RelationshipRepository implements ObjectRepository
->getResult(); ->getResult();
} }
public function countByPerson(Person $person): int public function findOneBy(array $criteria): ?Relationship
{ {
return $this->buildQueryByPerson($person) return $this->findOneBy($criteria);
->select('COUNT(p)') }
->getQuery()
->getSingleScalarResult(); public function getClassName(): string
{
return Relationship::class;
} }
private function buildQueryByPerson(Person $person): QueryBuilder private function buildQueryByPerson(Person $person): QueryBuilder
@ -81,14 +89,4 @@ class RelationshipRepository implements ObjectRepository
return $qb; return $qb;
} }
public function findOneBy(array $criteria): ?Relationship
{
return $this->findOneBy($criteria);
}
public function getClassName(): string
{
return Relationship::class;
}
} }

View File

@ -16,7 +16,6 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\PersonAltName; use Chill\PersonBundle\Entity\PersonAltName;
use Chill\PersonBundle\Repository\Relationships\RelationRepository;
use Chill\PersonBundle\Repository\Relationships\RelationshipRepository; use Chill\PersonBundle\Repository\Relationships\RelationshipRepository;
use Chill\PersonBundle\Templating\Entity\PersonRender; use Chill\PersonBundle\Templating\Entity\PersonRender;
use DateTimeInterface; use DateTimeInterface;
@ -107,8 +106,8 @@ class PersonDocGenNormalizer implements
array_merge($context, [ array_merge($context, [
'docgen:person:with-household' => false, 'docgen:person:with-household' => false,
'docgen:person:with-relation' => false, 'docgen:person:with-relation' => false,
'docgen:relationship:counterpart' => $person 'docgen:relationship:counterpart' => $person,
]) ])
); );
} }

View File

@ -1,19 +1,22 @@
<?php <?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer; namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Relationships\Relation;
use Chill\PersonBundle\Entity\Relationships\Relationship; use Chill\PersonBundle\Entity\Relationships\Relationship;
use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class RelationshipDocGenNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface class RelationshipDocGenNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{ {
@ -29,7 +32,7 @@ class RelationshipDocGenNormalizer implements ContextAwareNormalizerInterface, N
/** /**
* @param Relationship $relation * @param Relationship $relation
*/ */
public function normalize($relation, string $format = null, array $context = []) public function normalize($relation, ?string $format = null, array $context = [])
{ {
$counterpart = $context['docgen:relationship:counterpart'] ?? null; $counterpart = $context['docgen:relationship:counterpart'] ?? null;
$contextPerson = array_merge($context, [ $contextPerson = array_merge($context, [
@ -46,8 +49,8 @@ class RelationshipDocGenNormalizer implements ContextAwareNormalizerInterface, N
if (null === $relation) { if (null === $relation) {
return [ return [
"id" => "", 'id' => '',
"fromPerson" => $nullPerson = $this->normalizer->normalize(null, $format, $contextPerson), 'fromPerson' => $nullPerson = $this->normalizer->normalize(null, $format, $contextPerson),
'toPerson' => $nullPerson, 'toPerson' => $nullPerson,
'opposite' => $nullPerson, 'opposite' => $nullPerson,
'text' => '', 'text' => '',
@ -75,13 +78,13 @@ class RelationshipDocGenNormalizer implements ContextAwareNormalizerInterface, N
]; ];
} }
public function supportsNormalization($data, string $format = null, array $context = []) public function supportsNormalization($data, ?string $format = null, array $context = [])
{ {
if ('docgen' !== $format) { if ('docgen' !== $format) {
return false; return false;
} }
return $data instanceof Relationship || (null === $data return $data instanceof Relationship || (null === $data
&& ($context['docgen:expects'] ?? null) === Relationship::class); && Relationship::class === ($context['docgen:expects'] ?? null));
} }
} }

View File

@ -67,7 +67,9 @@ class AccompanyingPeriodContext implements
} }
public function adminFormReverseTransform(array $data): array public function adminFormReverseTransform(array $data): array
{dump($data); {
dump($data);
if (array_key_exists('category', $data)) { if (array_key_exists('category', $data)) {
$data['category'] = [ $data['category'] = [
'idInsideBundle' => $data['category']->getIdInsideBundle(), 'idInsideBundle' => $data['category']->getIdInsideBundle(),

View File

@ -23,7 +23,6 @@ use Chill\PersonBundle\Repository\Relationships\RelationshipRepository;
use Chill\PersonBundle\Serializer\Normalizer\PersonDocGenNormalizer; use Chill\PersonBundle\Serializer\Normalizer\PersonDocGenNormalizer;
use Chill\PersonBundle\Templating\Entity\PersonRender; use Chill\PersonBundle\Templating\Entity\PersonRender;
use Prophecy\Argument; use Prophecy\Argument;
use Prophecy\Argument\Token\AnyValueToken;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
@ -57,38 +56,6 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
private NormalizerInterface $normalizer; private NormalizerInterface $normalizer;
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(function($args) use ($normalizer) {
return $normalizer->normalize($args[0], $args[1], $args[2]);
});
$normalizerManager->normalize(Argument::any(), 'docgen', Argument::any())->will(
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;
}
protected function setUp() protected function setUp()
{ {
self::bootKernel(); self::bootKernel();
@ -124,7 +91,7 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
{ {
$normalized = $this->normalizer->normalize($person, 'docgen', [ $normalized = $this->normalizer->normalize($person, 'docgen', [
'docgen:expects' => Person::class, 'docgen:expects' => Person::class,
'groups' => 'docgen:read' 'groups' => 'docgen:read',
]); ]);
$this->assertEquals($expected, $normalized, $msg); $this->assertEquals($expected, $normalized, $msg);
@ -136,28 +103,24 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
$person = new Person(); $person = new Person();
$person $person
->setFirstName('Renaud') ->setFirstName('Renaud')
->setLastName('Mégane') ->setLastName('Mégane');
;
$householdMember = new HouseholdMember(); $householdMember = new HouseholdMember();
$householdMember $householdMember
->setPosition((new Position())->setAllowHolder(true)->setLabel(['fr' => 'position']) ->setPosition((new Position())->setAllowHolder(true)->setLabel(['fr' => 'position'])
->setShareHousehold(true)) ->setShareHousehold(true))
->setHolder(true) ->setHolder(true);
;
$person->addHouseholdParticipation($householdMember); $person->addHouseholdParticipation($householdMember);
$household->addMember($householdMember); $household->addMember($householdMember);
$person = new Person(); $person = new Person();
$person $person
->setFirstName('Citroen') ->setFirstName('Citroen')
->setLastName('Xsara') ->setLastName('Xsara');
;
$householdMember = new HouseholdMember(); $householdMember = new HouseholdMember();
$householdMember $householdMember
->setPosition((new Position())->setAllowHolder(true)->setLabel(['fr' => 'position2']) ->setPosition((new Position())->setAllowHolder(true)->setLabel(['fr' => 'position2'])
->setShareHousehold(true)) ->setShareHousehold(true))
->setHolder(false) ->setHolder(false);
;
$person->addHouseholdParticipation($householdMember); $person->addHouseholdParticipation($householdMember);
$household->addMember($householdMember); $household->addMember($householdMember);
@ -165,7 +128,6 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
'groups' => 'docgen:read', 'groups' => 'docgen:read',
'docgen:expects' => Person::class, 'docgen:expects' => Person::class,
'docgen:person:with-household' => true, 'docgen:person:with-household' => true,
]); ]);
$this->assertCount(2, $household->getMembers()); $this->assertCount(2, $household->getMembers());
@ -188,10 +150,10 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
->setReverseTitle(['fr' => 'Fils'])), ->setReverseTitle(['fr' => 'Fils'])),
(new Relationship())->setFromPerson($person)->setToPerson($mother) (new Relationship())->setFromPerson($person)->setToPerson($mother)
->setReverse(false)->setRelation((new Relation())->setTitle(['fr' => 'Mère']) ->setReverse(false)->setRelation((new Relation())->setTitle(['fr' => 'Mère'])
->setReverseTitle(['fr' => 'Fils'])), ->setReverseTitle(['fr' => 'Fils'])),
(new Relationship())->setFromPerson($person)->setToPerson($sister) (new Relationship())->setFromPerson($person)->setToPerson($sister)
->setReverse(true)->setRelation((new Relation())->setTitle(['fr' => 'Frère']) ->setReverse(true)->setRelation((new Relation())->setTitle(['fr' => 'Frère'])
->setReverseTitle(['fr' => 'Soeur'])), ->setReverseTitle(['fr' => 'Soeur'])),
]; ];
$repository = $this->prophesize(RelationshipRepository::class); $repository = $this->prophesize(RelationshipRepository::class);
@ -209,4 +171,42 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
$this->assertArrayHasKey('relations', $actual); $this->assertArrayHasKey('relations', $actual);
$this->assertCount(3, $actual['relations']); $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;
}
} }

View File

@ -1,5 +1,14 @@
<?php <?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Serializer\Normalizer; namespace Serializer\Normalizer;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
@ -10,42 +19,36 @@ use Chill\PersonBundle\Serializer\Normalizer\RelationshipDocGenNormalizer;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\Argument; use Prophecy\Argument;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use function is_object;
class RelationshipDocGenNormalizerTest extends TestCase /**
* @internal
* @coversNothing
*/
final class RelationshipDocGenNormalizerTest extends TestCase
{ {
private function buildNormalizer(): RelationshipDocGenNormalizer public function testNormalizeRelationshipNull()
{ {
$translatableStringHelper = $this->prophesize(TranslatableStringHelperInterface::class); $relationship = null;
$translatableStringHelper->localize(Argument::type('array'))->will(
function ($args) { return $args[0][array_keys($args[0])[0]]; } $normalizer = $this->buildNormalizer();
$this->assertTrue($normalizer->supportsNormalization($relationship, 'docgen', [
'docgen:expects' => Relationship::class,
]));
$this->assertFalse($normalizer->supportsNormalization($relationship, 'docgen', [
'docgen:expects' => Person::class,
]));
$actual = $normalizer->normalize($relationship, 'docgen', [
'docgen:expects' => Relationship::class,
]);
$this->assertIsArray($actual);
$this->assertEqualsCanonicalizing(
['fromPerson', 'toPerson', 'id', 'relationId', 'text', 'opposite'],
array_keys($actual),
'check that the expected keys are present'
); );
$normalizer = new RelationshipDocGenNormalizer(
$translatableStringHelper->reveal()
);
$normalizerManager = $this->prophesize(NormalizerInterface::class);
$normalizerManager->supportsNormalization(Argument::any(), 'docgen', Argument::any())->willReturn(true);
$normalizerManager->normalize(Argument::type(Relationship::class), 'docgen', Argument::any())
->will(function($args) use ($normalizer) {
return $normalizer->normalize($args[0], $args[1], $args[2]);
});
$normalizerManager->normalize(Argument::any(), 'docgen', Argument::any())->will(
function ($args) {
if (null === $args[0]) {
return null;
} elseif (is_iterable($args[0])) {
$r = [];
foreach ($args[0] as $i) { $r[] = ['fake' => true, 'hash' => spl_object_hash($i)];}
return $r;
} elseif (is_object($args[0])) {
return ['fake' => true, 'hash' => null !== $args[0] ? spl_object_hash($args[0]) : null];
}
return $args[0];
});
$normalizer->setNormalizer($normalizerManager->reveal());
return $normalizer;
} }
public function testNormalizeRelationshipWithCounterPart() public function testNormalizeRelationshipWithCounterPart()
@ -54,11 +57,11 @@ class RelationshipDocGenNormalizerTest extends TestCase
$relationship $relationship
->setFromPerson($person1 = new Person()) ->setFromPerson($person1 = new Person())
->setToPerson($person2 = new Person()) ->setToPerson($person2 = new Person())
->setRelation((new Relation())->setTitle(['fr' => 'title']) ->setRelation(
->setReverseTitle(['fr' => 'reverse title']) (new Relation())->setTitle(['fr' => 'title'])
->setReverseTitle(['fr' => 'reverse title'])
) )
->setReverse(false) ->setReverse(false);
;
$normalizer = $this->buildNormalizer(); $normalizer = $this->buildNormalizer();
@ -67,7 +70,7 @@ class RelationshipDocGenNormalizerTest extends TestCase
$actual = $normalizer->normalize($relationship, 'docgen', [ $actual = $normalizer->normalize($relationship, 'docgen', [
'docgen:expects' => Relationship::class, 'docgen:expects' => Relationship::class,
'docgen:relationship:counterpart' => $person1 'docgen:relationship:counterpart' => $person1,
]); ]);
$this->assertIsArray($actual); $this->assertIsArray($actual);
@ -85,11 +88,11 @@ class RelationshipDocGenNormalizerTest extends TestCase
$relationship $relationship
->setFromPerson($person1 = new Person()) ->setFromPerson($person1 = new Person())
->setToPerson($person2 = new Person()) ->setToPerson($person2 = new Person())
->setRelation((new Relation())->setTitle(['fr' => 'title']) ->setRelation(
->setReverseTitle(['fr' => 'reverse title']) (new Relation())->setTitle(['fr' => 'title'])
->setReverseTitle(['fr' => 'reverse title'])
) )
->setReverse(false) ->setReverse(false);
;
$normalizer = $this->buildNormalizer(); $normalizer = $this->buildNormalizer();
@ -108,27 +111,48 @@ class RelationshipDocGenNormalizerTest extends TestCase
$this->assertEquals(null, $actual['opposite']); $this->assertEquals(null, $actual['opposite']);
} }
public function testNormalizeRelationshipNull() private function buildNormalizer(): RelationshipDocGenNormalizer
{ {
$relationship = null; $translatableStringHelper = $this->prophesize(TranslatableStringHelperInterface::class);
$translatableStringHelper->localize(Argument::type('array'))->will(
$normalizer = $this->buildNormalizer(); static function ($args) { return $args[0][array_keys($args[0])[0]]; }
$this->assertTrue($normalizer->supportsNormalization($relationship, 'docgen', [
'docgen:expects' => Relationship::class
]));
$this->assertFalse($normalizer->supportsNormalization($relationship, 'docgen', [
'docgen:expects' => Person::class
]));
$actual = $normalizer->normalize($relationship, 'docgen', [
'docgen:expects' => Relationship::class,
]);
$this->assertIsArray($actual);
$this->assertEqualsCanonicalizing(
['fromPerson', 'toPerson', 'id', 'relationId', 'text', 'opposite'],
array_keys($actual),
'check that the expected keys are present'
); );
$normalizer = new RelationshipDocGenNormalizer(
$translatableStringHelper->reveal()
);
$normalizerManager = $this->prophesize(NormalizerInterface::class);
$normalizerManager->supportsNormalization(Argument::any(), 'docgen', Argument::any())->willReturn(true);
$normalizerManager->normalize(Argument::type(Relationship::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 (null === $args[0]) {
return null;
}
if (is_iterable($args[0])) {
$r = [];
foreach ($args[0] as $i) {
$r[] = ['fake' => true, 'hash' => spl_object_hash($i)];
}
return $r;
}
if (is_object($args[0])) {
return ['fake' => true, 'hash' => null !== $args[0] ? spl_object_hash($args[0]) : null];
}
return $args[0];
}
);
$normalizer->setNormalizer($normalizerManager->reveal());
return $normalizer;
} }
} }