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)) {
$arr = [];
foreach ($value as $k => $v) {
$arr[$k] =
$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\Document;
use Chill\DocStoreBundle\Entity\DocumentCategory;
use Chill\DocStoreBundle\Entity\PersonDocument;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\MainBundle\Form\Type\ChillTextareaType;
@ -76,7 +75,7 @@ class AccompanyingCourseDocumentType extends AbstractType
'query_builder' => static function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->where('c.documentClass = :docClass')
->setParameter('docClass', AccompanyingCourseDocument::class);
->setParameter('docClass', AccompanyingCourseDocument::class);
},
'choice_label' => function ($entity = null) {
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 Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use function array_values;
class RelationshipApiController extends ApiController
{

View File

@ -19,6 +19,7 @@ use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use RuntimeException;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
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,
* or the To person otherwise.
*
* @param Person $counterpartthe counterpart
* @throw RuntimeException if the counterpart is neither in the from or to person
*/
public function getOpposite(Person $counterpart): Person
{
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) {

View File

@ -12,7 +12,6 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Repository\Relationships;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Relationships\Relation;
use Chill\PersonBundle\Entity\Relationships\Relationship;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
@ -21,16 +20,24 @@ use Doctrine\Persistence\ObjectRepository;
class RelationshipRepository implements ObjectRepository
{
private EntityRepository $repository;
private EntityManagerInterface $em;
private EntityRepository $repository;
public function __construct(EntityManagerInterface $em)
{
$this->repository = $em->getRepository(Relationship::class);
$this->em = $em;
}
public function countByPerson(Person $person): int
{
return $this->buildQueryByPerson($person)
->select('COUNT(p)')
->getQuery()
->getSingleScalarResult();
}
public function find($id): ?Relationship
{
return $this->repository->find($id);
@ -47,7 +54,6 @@ class RelationshipRepository implements ObjectRepository
}
/**
* @param Person $person
* @return array|Relationship[]
*/
public function findByPerson(Person $person): array
@ -58,12 +64,14 @@ class RelationshipRepository implements ObjectRepository
->getResult();
}
public function countByPerson(Person $person): int
public function findOneBy(array $criteria): ?Relationship
{
return $this->buildQueryByPerson($person)
->select('COUNT(p)')
->getQuery()
->getSingleScalarResult();
return $this->findOneBy($criteria);
}
public function getClassName(): string
{
return Relationship::class;
}
private function buildQueryByPerson(Person $person): QueryBuilder
@ -81,14 +89,4 @@ class RelationshipRepository implements ObjectRepository
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\Person;
use Chill\PersonBundle\Entity\PersonAltName;
use Chill\PersonBundle\Repository\Relationships\RelationRepository;
use Chill\PersonBundle\Repository\Relationships\RelationshipRepository;
use Chill\PersonBundle\Templating\Entity\PersonRender;
use DateTimeInterface;
@ -107,8 +106,8 @@ class PersonDocGenNormalizer implements
array_merge($context, [
'docgen:person:with-household' => false,
'docgen:person:with-relation' => false,
'docgen:relationship:counterpart' => $person
])
'docgen:relationship:counterpart' => $person,
])
);
}

View File

@ -1,19 +1,22 @@
<?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;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Relationships\Relation;
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\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class RelationshipDocGenNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
@ -29,7 +32,7 @@ class RelationshipDocGenNormalizer implements ContextAwareNormalizerInterface, N
/**
* @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;
$contextPerson = array_merge($context, [
@ -46,8 +49,8 @@ class RelationshipDocGenNormalizer implements ContextAwareNormalizerInterface, N
if (null === $relation) {
return [
"id" => "",
"fromPerson" => $nullPerson = $this->normalizer->normalize(null, $format, $contextPerson),
'id' => '',
'fromPerson' => $nullPerson = $this->normalizer->normalize(null, $format, $contextPerson),
'toPerson' => $nullPerson,
'opposite' => $nullPerson,
'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) {
return false;
}
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
{dump($data);
{
dump($data);
if (array_key_exists('category', $data)) {
$data['category'] = [
'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\Templating\Entity\PersonRender;
use Prophecy\Argument;
use Prophecy\Argument\Token\AnyValueToken;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
@ -57,38 +56,6 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
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()
{
self::bootKernel();
@ -124,7 +91,7 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
{
$normalized = $this->normalizer->normalize($person, 'docgen', [
'docgen:expects' => Person::class,
'groups' => 'docgen:read'
'groups' => 'docgen:read',
]);
$this->assertEquals($expected, $normalized, $msg);
@ -136,28 +103,24 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
$person = new Person();
$person
->setFirstName('Renaud')
->setLastName('Mégane')
;
->setLastName('Mégane');
$householdMember = new HouseholdMember();
$householdMember
->setPosition((new Position())->setAllowHolder(true)->setLabel(['fr' => 'position'])
->setShareHousehold(true))
->setHolder(true)
;
->setShareHousehold(true))
->setHolder(true);
$person->addHouseholdParticipation($householdMember);
$household->addMember($householdMember);
$person = new Person();
$person
->setFirstName('Citroen')
->setLastName('Xsara')
;
->setLastName('Xsara');
$householdMember = new HouseholdMember();
$householdMember
->setPosition((new Position())->setAllowHolder(true)->setLabel(['fr' => 'position2'])
->setShareHousehold(true))
->setHolder(false)
;
->setShareHousehold(true))
->setHolder(false);
$person->addHouseholdParticipation($householdMember);
$household->addMember($householdMember);
@ -165,7 +128,6 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
'groups' => 'docgen:read',
'docgen:expects' => Person::class,
'docgen:person:with-household' => true,
]);
$this->assertCount(2, $household->getMembers());
@ -188,10 +150,10 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
->setReverseTitle(['fr' => 'Fils'])),
(new Relationship())->setFromPerson($person)->setToPerson($mother)
->setReverse(false)->setRelation((new Relation())->setTitle(['fr' => 'Mère'])
->setReverseTitle(['fr' => 'Fils'])),
->setReverseTitle(['fr' => 'Fils'])),
(new Relationship())->setFromPerson($person)->setToPerson($sister)
->setReverse(true)->setRelation((new Relation())->setTitle(['fr' => 'Frère'])
->setReverseTitle(['fr' => 'Soeur'])),
->setReverseTitle(['fr' => 'Soeur'])),
];
$repository = $this->prophesize(RelationshipRepository::class);
@ -209,4 +171,42 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
$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;
}
}

View File

@ -1,5 +1,14 @@
<?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;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
@ -10,42 +19,36 @@ use Chill\PersonBundle\Serializer\Normalizer\RelationshipDocGenNormalizer;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
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);
$translatableStringHelper->localize(Argument::type('array'))->will(
function ($args) { return $args[0][array_keys($args[0])[0]]; }
$relationship = null;
$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()
@ -54,11 +57,11 @@ class RelationshipDocGenNormalizerTest extends TestCase
$relationship
->setFromPerson($person1 = new Person())
->setToPerson($person2 = new Person())
->setRelation((new Relation())->setTitle(['fr' => 'title'])
->setReverseTitle(['fr' => 'reverse title'])
->setRelation(
(new Relation())->setTitle(['fr' => 'title'])
->setReverseTitle(['fr' => 'reverse title'])
)
->setReverse(false)
;
->setReverse(false);
$normalizer = $this->buildNormalizer();
@ -67,7 +70,7 @@ class RelationshipDocGenNormalizerTest extends TestCase
$actual = $normalizer->normalize($relationship, 'docgen', [
'docgen:expects' => Relationship::class,
'docgen:relationship:counterpart' => $person1
'docgen:relationship:counterpart' => $person1,
]);
$this->assertIsArray($actual);
@ -85,11 +88,11 @@ class RelationshipDocGenNormalizerTest extends TestCase
$relationship
->setFromPerson($person1 = new Person())
->setToPerson($person2 = new Person())
->setRelation((new Relation())->setTitle(['fr' => 'title'])
->setReverseTitle(['fr' => 'reverse title'])
->setRelation(
(new Relation())->setTitle(['fr' => 'title'])
->setReverseTitle(['fr' => 'reverse title'])
)
->setReverse(false)
;
->setReverse(false);
$normalizer = $this->buildNormalizer();
@ -108,27 +111,48 @@ class RelationshipDocGenNormalizerTest extends TestCase
$this->assertEquals(null, $actual['opposite']);
}
public function testNormalizeRelationshipNull()
private function buildNormalizer(): RelationshipDocGenNormalizer
{
$relationship = null;
$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'
$translatableStringHelper = $this->prophesize(TranslatableStringHelperInterface::class);
$translatableStringHelper->localize(Argument::type('array'))->will(
static function ($args) { return $args[0][array_keys($args[0])[0]]; }
);
$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;
}
}