add docgen:normalization for relation

This commit is contained in:
Julien Fastré 2021-12-09 13:51:36 +01:00 committed by Julie Lenaerts
parent 2524229941
commit a25123ee38
3 changed files with 10 additions and 37 deletions

View File

@ -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(

View File

@ -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

View File

@ -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();