Merge branch 'fix-tests-2022-03-30' into 'master'

Fix tests 2022 03 30

See merge request Chill-Projet/chill-bundles!400
This commit is contained in:
Julien Fastré 2022-03-30 20:20:03 +00:00
commit e027958c63
10 changed files with 135 additions and 58 deletions

View File

@ -460,8 +460,3 @@ parameters:
count: 1 count: 1
path: src/Bundle/ChillThirdPartyBundle/Search/ThirdPartySearch.php path: src/Bundle/ChillThirdPartyBundle/Search/ThirdPartySearch.php
-
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
count: 1
path: src/Bundle/ChillThirdPartyBundle/Templating/Entity/ThirdPartyRender.php

View File

@ -23,7 +23,7 @@ use function count;
/** /**
* Helps to find a summary of the budget: the sum of resources and charges. * Helps to find a summary of the budget: the sum of resources and charges.
*/ */
class SummaryBudget class SummaryBudget implements SummaryBudgetInterface
{ {
private const QUERY_CHARGE_BY_HOUSEHOLD = 'select SUM(amount) AS sum, type FROM chill_budget.charge WHERE (person_id IN (_ids_) OR household_id = ?) AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY type'; private const QUERY_CHARGE_BY_HOUSEHOLD = 'select SUM(amount) AS sum, type FROM chill_budget.charge WHERE (person_id IN (_ids_) OR household_id = ?) AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY type';
@ -52,26 +52,6 @@ class SummaryBudget
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
} }
public function getEmptyChargeArray(): array
{
$keys = $this->configRepository->getChargesKeys();
$labels = $this->chargeLabels;
return array_combine($keys, array_map(function ($i) use ($labels) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i])];
}, $keys));
}
public function getEmptyResourceArray(): array
{
$keys = $this->configRepository->getResourcesKeys();
$labels = $this->resourcesLabels;
return array_combine($keys, array_map(function ($i) use ($labels) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i])];
}, $keys));
}
public function getSummaryForHousehold(?Household $household): array public function getSummaryForHousehold(?Household $household): array
{ {
if (null === $household) { if (null === $household) {
@ -101,8 +81,15 @@ class SummaryBudget
]; ];
} }
public function getSummaryForPerson(Person $person): array public function getSummaryForPerson(?Person $person): array
{ {
if (null === $person) {
return [
'resources' => $this->getEmptyResourceArray(),
'charges' => $this->getEmptyChargeArray(),
];
}
$rsm = $this->buildRsm(); $rsm = $this->buildRsm();
$resources = $this->em->createNativeQuery(self::QUERY_RESOURCE_BY_PERSON, $rsm) $resources = $this->em->createNativeQuery(self::QUERY_RESOURCE_BY_PERSON, $rsm)
@ -128,6 +115,26 @@ class SummaryBudget
return $rsm; return $rsm;
} }
private function getEmptyChargeArray(): array
{
$keys = $this->configRepository->getChargesKeys();
$labels = $this->chargeLabels;
return array_combine($keys, array_map(function ($i) use ($labels) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i])];
}, $keys));
}
private function getEmptyResourceArray(): array
{
$keys = $this->configRepository->getResourcesKeys();
$labels = $this->resourcesLabels;
return array_combine($keys, array_map(function ($i) use ($labels) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i])];
}, $keys));
}
private function rowToArray(array $rows, string $kind): array private function rowToArray(array $rows, string $kind): array
{ {
switch ($kind) { switch ($kind) {

View File

@ -0,0 +1,25 @@
<?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\BudgetBundle\Service\Summary;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
/**
* Helps to find a summary of the budget: the sum of resources and charges.
*/
interface SummaryBudgetInterface
{
public function getSummaryForHousehold(?Household $household): array;
public function getSummaryForPerson(?Person $person): array;
}

View File

@ -49,7 +49,7 @@ class ApiController extends AbstractCRUDController
return $this->entityPut('_entity', $request, $id, $_format); return $this->entityPut('_entity', $request, $id, $_format);
case Request::METHOD_POST: case Request::METHOD_POST:
return $this->entityPostAction('_entity', $request, $id, $_format); return $this->entityPostAction('_entity', $request, $id);
case Request::METHOD_DELETE: case Request::METHOD_DELETE:
return $this->entityDelete('_entity', $request, $id, $_format); return $this->entityDelete('_entity', $request, $id, $_format);

View File

@ -276,25 +276,21 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
/** /**
* The person's first name. * The person's first name.
* *
* @var string
*
* @ORM\Column(type="string", length=255) * @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="The firstname cannot be empty") * @Assert\NotBlank(message="The firstname cannot be empty")
* @Assert\Length( * @Assert\Length(
* max=255, * max=255,
* ) * )
*/ */
private $firstName; private string $firstName = '';
/** /**
* fullname canonical. Read-only field, which is calculated by * fullname canonical. Read-only field, which is calculated by
* the database. * the database.
* *
* @var string
*
* @ORM\Column(type="text", nullable=true) * @ORM\Column(type="text", nullable=true)
*/ */
private $fullnameCanonical; private string $fullnameCanonical = '';
/** /**
* The person's gender. * The person's gender.
@ -345,15 +341,13 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
/** /**
* The person's last name. * The person's last name.
* *
* @var string
*
* @ORM\Column(type="string", length=255) * @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="The lastname cannot be empty") * @Assert\NotBlank(message="The lastname cannot be empty")
* @Assert\Length( * @Assert\Length(
* max=255, * max=255,
* ) * )
*/ */
private $lastName; private string $lastName = '';
/** /**
* The marital status of the person. * The marital status of the person.
@ -1583,9 +1577,9 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
return $this; return $this;
} }
public function setFirstName(string $firstName): self public function setFirstName(?string $firstName): self
{ {
$this->firstName = $firstName; $this->firstName = (string) $firstName;
return $this; return $this;
} }
@ -1611,9 +1605,9 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
return $this; return $this;
} }
public function setLastName(string $lastName): self public function setLastName(?string $lastName): self
{ {
$this->lastName = $lastName; $this->lastName = (string) $lastName;
return $this; return $this;
} }

View File

@ -29,8 +29,12 @@ use Symfony\Contracts\Translation\TranslatorInterface;
final class PersonResourceType extends AbstractType final class PersonResourceType extends AbstractType
{ {
private PersonRenderInterface $personRender;
private ResourceKindRender $resourceKindRender; private ResourceKindRender $resourceKindRender;
private ThirdPartyRender $thirdPartyRender;
private TranslatorInterface $translator; private TranslatorInterface $translator;
public function __construct(ResourceKindRender $resourceKindRender, PersonRenderInterface $personRender, ThirdPartyRender $thirdPartyRender, TranslatorInterface $translator) public function __construct(ResourceKindRender $resourceKindRender, PersonRenderInterface $personRender, ThirdPartyRender $thirdPartyRender, TranslatorInterface $translator)

View File

@ -11,7 +11,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer; namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\BudgetBundle\Service\Summary\SummaryBudget; use Chill\BudgetBundle\Service\Summary\SummaryBudgetInterface;
use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper; use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper;
use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Entity\Civility; use Chill\MainBundle\Entity\Civility;
@ -45,7 +45,7 @@ class PersonDocGenNormalizer implements
private RelationshipRepository $relationshipRepository; private RelationshipRepository $relationshipRepository;
private SummaryBudget $summaryBudget; private SummaryBudgetInterface $summaryBudget;
private TranslatableStringHelper $translatableStringHelper; private TranslatableStringHelper $translatableStringHelper;
@ -56,7 +56,7 @@ class PersonDocGenNormalizer implements
RelationshipRepository $relationshipRepository, RelationshipRepository $relationshipRepository,
TranslatorInterface $translator, TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper, TranslatableStringHelper $translatableStringHelper,
SummaryBudget $summaryBudget SummaryBudgetInterface $summaryBudget
) { ) {
$this->personRender = $personRender; $this->personRender = $personRender;
$this->relationshipRepository = $relationshipRepository; $this->relationshipRepository = $relationshipRepository;
@ -214,6 +214,14 @@ class PersonDocGenNormalizer implements
$data['relations'] = []; $data['relations'] = [];
} }
if ($context['docgen:person:with-budget'] ?? false) {
$data['budget']['person'] = $this->summaryBudget->getSummaryForPerson(null);
if ($context['docgen:person:with-household'] ?? false) {
$data['budget']['household'] = $this->summaryBudget->getSummaryForHousehold(null);
}
}
return $data; return $data;
} }
} }

View File

@ -65,7 +65,7 @@ final class HouseholdApiControllerTest extends WebTestCase
} }
$reference = $em->createQueryBuilder()->select('ar')->from(AddressReference::class, 'ar') $reference = $em->createQueryBuilder()->select('ar')->from(AddressReference::class, 'ar')
->setFirstResult(random_int(0, $nbReference)) ->setFirstResult(random_int(0, $nbReference - 1))
->setMaxResults(1) ->setMaxResults(1)
->getQuery()->getSingleResult(); ->getQuery()->getSingleResult();

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Serializer\Normalizer; namespace Serializer\Normalizer;
use Chill\BudgetBundle\Service\Summary\SummaryBudgetInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\Household;
@ -72,6 +73,17 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
$this->normalizer = self::$container->get(NormalizerInterface::class); $this->normalizer = self::$container->get(NormalizerInterface::class);
} }
public function dataGeneratorNormalizationNullOrNotNullHaveSameKeys(): iterable
{
yield [['docgen:expects' => Person::class, 'groups' => ['docgen:read']]];
yield [['docgen:expects' => Person::class, 'groups' => ['docgen:read'], 'docgen:person:with-household' => true]];
yield [['docgen:expects' => Person::class, 'groups' => ['docgen:read'], 'docgen:person:with-relations' => true]];
yield [['docgen:expects' => Person::class, 'groups' => ['docgen:read'], 'docgen:person:with-budget' => true]];
}
public function generateData() public function generateData()
{ {
$person = new Person(); $person = new Person();
@ -90,12 +102,16 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
yield [null, self::BLANK, 'normalization for a null person']; yield [null, self::BLANK, 'normalization for a null person'];
} }
public function testNormalizationNullOrNotNullHaveSameKeys() /**
* @dataProvider dataGeneratorNormalizationNullOrNotNullHaveSameKeys
*
* @param mixed $context
*/
public function testNormalizationNullOrNotNullHaveSameKeys($context)
{ {
$this->markTestSkipped();
$period = new Person(); $period = new Person();
$notNullData = $this->buildPersonNormalizer()->normalize($period, 'docgen', ['docgen:expects' => Person::class]); $notNullData = $this->buildPersonNormalizer()->normalize($period, 'docgen', $context);
$nullData = $this->buildPersonNormalizer()->normalize(null, 'docgen', ['docgen:expects' => Person::class]); $nullData = $this->buildPersonNormalizer()->normalize(null, 'docgen', $context);
$this->assertEqualsCanonicalizing( $this->assertEqualsCanonicalizing(
array_keys($notNullData), array_keys($notNullData),
@ -131,7 +147,6 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
public function testNormalizePersonWithHousehold() public function testNormalizePersonWithHousehold()
{ {
$this->markTestSkipped();
$household = new Household(); $household = new Household();
$person = new Person(); $person = new Person();
$person $person
@ -172,7 +187,6 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
public function testNormalizePersonWithRelationships() public function testNormalizePersonWithRelationships()
{ {
$this->markTestSkipped();
$person = (new Person())->setFirstName('Renaud')->setLastName('megane'); $person = (new Person())->setFirstName('Renaud')->setLastName('megane');
$father = (new Person())->setFirstName('Clément')->setLastName('megane'); $father = (new Person())->setFirstName('Clément')->setLastName('megane');
$mother = (new Person())->setFirstName('Mireille')->setLastName('Mathieu'); $mother = (new Person())->setFirstName('Mireille')->setLastName('Mathieu');
@ -235,13 +249,25 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
?RelationshipRepository $relationshipRepository = null, ?RelationshipRepository $relationshipRepository = null,
?TranslatorInterface $translator = null, ?TranslatorInterface $translator = null,
?TranslatableStringHelper $translatableStringHelper = null, ?TranslatableStringHelper $translatableStringHelper = null,
?NormalizerInterface $normalizer = null ?NormalizerInterface $normalizer = null,
?SummaryBudgetInterface $summaryBudget = null
): PersonDocGenNormalizer { ): PersonDocGenNormalizer {
if (null === $summaryBudget) {
$summaryBudget = $this->prophesize(SummaryBudgetInterface::class);
$summaryBudget->getSummaryForHousehold(Argument::any())->willReturn(
['resources' => [], 'charges' => []]
);
$summaryBudget->getSummaryForPerson(Argument::any())->willReturn(
['resources' => [], 'charges' => []]
);
}
$personDocGenNormalizer = new PersonDocGenNormalizer( $personDocGenNormalizer = new PersonDocGenNormalizer(
$personRender ?? self::$container->get(PersonRender::class), $personRender ?? self::$container->get(PersonRender::class),
$relationshipRepository ?? self::$container->get(RelationshipRepository::class), $relationshipRepository ?? self::$container->get(RelationshipRepository::class),
$translator ?? self::$container->get(TranslatorInterface::class), $translator ?? self::$container->get(TranslatorInterface::class),
$translatableStringHelper ?? self::$container->get(TranslatableStringHelperInterface::class) $translatableStringHelper ?? self::$container->get(TranslatableStringHelperInterface::class),
$summaryBudget->reveal(),
); );
if (null === $normalizer) { if (null === $normalizer) {
@ -259,13 +285,31 @@ final class PersonDocGenNormalizerTest extends KernelTestCase
?PersonRender $personRender = null, ?PersonRender $personRender = null,
?RelationshipRepository $relationshipRepository = null, ?RelationshipRepository $relationshipRepository = null,
?TranslatorInterface $translator = null, ?TranslatorInterface $translator = null,
?TranslatableStringHelper $translatableStringHelper = null ?TranslatableStringHelper $translatableStringHelper = null,
?SummaryBudgetInterface $summaryBudget = null
): PersonDocGenNormalizer { ): PersonDocGenNormalizer {
if (null === $relationshipRepository) {
$relationshipRepository = $this->prophesize(RelationshipRepository::class);
$relationshipRepository->findByPerson(Argument::type(Person::class))->willReturn([]);
$relationshipRepository = $relationshipRepository->reveal();
}
if (null === $summaryBudget) {
$summaryBudget = $this->prophesize(SummaryBudgetInterface::class);
$summaryBudget->getSummaryForHousehold(Argument::any())->willReturn(
['resources' => [], 'charges' => []]
);
$summaryBudget->getSummaryForPerson(Argument::any())->willReturn(
['resources' => [], 'charges' => []]
);
}
$normalizer = new PersonDocGenNormalizer( $normalizer = new PersonDocGenNormalizer(
$personRender ?? self::$container->get(PersonRender::class), $personRender ?? self::$container->get(PersonRender::class),
$relationshipRepository ?? self::$container->get(RelationshipRepository::class), $relationshipRepository,
$translator ?? self::$container->get(TranslatorInterface::class), $translator ?? self::$container->get(TranslatorInterface::class),
$translatableStringHelper ?? self::$container->get(TranslatableStringHelperInterface::class) $translatableStringHelper ?? self::$container->get(TranslatableStringHelperInterface::class),
$summaryBudget->reveal()
); );
$normalizerManager = $this->prophesize(NormalizerInterface::class); $normalizerManager = $this->prophesize(NormalizerInterface::class);
$normalizerManager->supportsNormalization(Argument::any(), 'docgen', Argument::any())->willReturn(true); $normalizerManager->supportsNormalization(Argument::any(), 'docgen', Argument::any())->willReturn(true);

View File

@ -71,13 +71,13 @@ class ThirdPartyRender extends AbstractChillEntityRender
$civility = ''; $civility = '';
} }
if (!empty($entity->getAcronym())) { if ('' !== (string) $entity->getAcronym()) {
$acronym = ' (' . $entity->getAcronym() . ')'; $acronym = ' (' . $entity->getAcronym() . ')';
} else { } else {
$acronym = ''; $acronym = '';
} }
$firstname = empty($entity->getFirstname()) ? '' : $entity->getFirstname(); $firstname = ('' === $entity->getFirstname()) ? '' : $entity->getFirstname();
return $civility . $firstname . ' ' . $entity->getName() . $acronym; return $civility . $firstname . ' ' . $entity->getName() . $acronym;
} }