diff --git a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php index 8361e1b59..f97d90e66 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php +++ b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php @@ -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( diff --git a/src/Bundle/ChillDocStoreBundle/Form/AccompanyingCourseDocumentType.php b/src/Bundle/ChillDocStoreBundle/Form/AccompanyingCourseDocumentType.php index 1b5edb966..64a81e9c7 100644 --- a/src/Bundle/ChillDocStoreBundle/Form/AccompanyingCourseDocumentType.php +++ b/src/Bundle/ChillDocStoreBundle/Form/AccompanyingCourseDocumentType.php @@ -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()) : ''; diff --git a/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php b/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php index 57a8992ab..271b0cff7 100644 --- a/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php @@ -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 { diff --git a/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php index 87f250e4e..d51881bb2 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php +++ b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php @@ -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) { diff --git a/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php b/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php index f82c061e9..9549a4564 100644 --- a/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php @@ -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; - } } diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php index 94fa8a652..169236104 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php @@ -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, + ]) ); } diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/RelationshipDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/RelationshipDocGenNormalizer.php index 948824b99..35e7f1056 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/RelationshipDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/RelationshipDocGenNormalizer.php @@ -1,19 +1,22 @@ "", - "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)); } } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 1b30d93fc..c6c4bf136 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -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(), diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php index 892d16211..fea2cdc72 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php @@ -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; + } } diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/RelationshipDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/RelationshipDocGenNormalizerTest.php index 5304eed7a..ea2da19a8 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/RelationshipDocGenNormalizerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/RelationshipDocGenNormalizerTest.php @@ -1,5 +1,14 @@ 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; } }