From a25123ee38c4ea73d0ef7a891c86363ec7316dcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 9 Dec 2021 13:51:36 +0100 Subject: [PATCH] add docgen:normalization for relation --- .../Normalizer/DocGenObjectNormalizer.php | 1 + .../Relationships/RelationshipRepository.php | 14 +++++--- .../Normalizer/PersonDocGenNormalizerTest.php | 32 ------------------- 3 files changed, 10 insertions(+), 37 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php index 3ec11e380..35c4729bb 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php +++ b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php @@ -268,6 +268,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte ->localize($value); } elseif (is_iterable($value)) { $arr = []; + foreach ($value as $k => $v) { $arr[$k] = $this->normalizer->normalize($v, $format, array_merge( diff --git a/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php b/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php index edb87ae0d..e5c1d8f4e 100644 --- a/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php @@ -26,6 +26,8 @@ class RelationshipRepository implements ObjectRepository private EntityManagerInterface $em; + private EntityRepository $repository; + public function __construct(EntityManagerInterface $em) { $this->repository = $em->getRepository(Relationship::class); @@ -66,12 +68,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 diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php index 63649ab7e..6c9d8de5c 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php @@ -60,38 +60,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();