DX: fix some phpstan issues and add test for ParticipationOverlapValidator.php

This commit is contained in:
Julien Fastré 2023-02-07 22:11:39 +01:00
parent 70871176fc
commit afd2235254
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
5 changed files with 118 additions and 4 deletions

View File

@ -0,0 +1,108 @@
<?php
namespace Validator\AccompanyingPeriod;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Templating\Entity\PersonRenderInterface;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ParticipationOverlap;
use Chill\PersonBundle\Validator\Constraints\AccompanyingPeriod\ParticipationOverlapValidator;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
class ParticipationOverlapValidatorTest extends ConstraintValidatorTestCase
{
use ProphecyTrait;
public function testNoViolation(): void
{
$constraint = $this->getConstraint();
$period = new AccompanyingPeriod();
$person1 = new Person();
$person2 = new Person();
$period->createParticipationFor($person1);
$period->createParticipationFor($person2);
$this->validator->validate($period->getParticipations(), $constraint);
$this->assertNoViolation();
}
public function testHasTwoParticipationsOverlaps(): void
{
$constraint = $this->getConstraint();
$period = new AccompanyingPeriod();
$person1 = new Person();
$reflectionPerson = new \ReflectionClass($person1);
$personId = $reflectionPerson->getProperty('id');
$personId->setAccessible(true);
$personId->setValue($person1, 1);
$person2 = new Person();
$period->createParticipationFor($person1);
$period->createParticipationFor($person1);
$period->createParticipationFor($person2);
$this->validator->validate($period->getParticipations(), $constraint);
$this->buildViolation('participation-overlaps')
->setParameters([
'{{ start }}' => (new \DateTimeImmutable('today'))->format('d-m-Y'),
'{{ end }}' => null,
'{{ ids }}' => [null, null],
'{{ name }}' => 'person'
])
->assertRaised();
}
public function testHasTwoParticipationsButDoNotOverlaps(): void
{
$constraint = $this->getConstraint();
$period = new AccompanyingPeriod();
$person1 = new Person();
$reflectionPerson = new \ReflectionClass($person1);
$personId = $reflectionPerson->getProperty('id');
$personId->setAccessible(true);
$personId->setValue($person1, 1);
$person2 = new Person();
$participation1 = $period->createParticipationFor($person1);
$period->createParticipationFor($person1);
$participation1->setEndDate(new \DateTime('now'));
$period->createParticipationFor($person2);
$this->validator->validate($period->getParticipations(), $constraint);
$this->assertNoViolation();
}
protected function createValidator(): ParticipationOverlapValidator
{
$personRender = $this->prophesize(PersonRenderInterface::class);
$personRender->renderString(Argument::type(Person::class), [])
->willReturn('person');
$thirdPartyRender = $this->prophesize(ThirdPartyRender::class);
$thirdPartyRender->renderString(Argument::type(ThirdParty::class), [])
->willReturn('third-party');
return new ParticipationOverlapValidator(
$personRender->reveal(),
$thirdPartyRender->reveal()
);
}
public function getConstraint(): ParticipationOverlap
{
return new ParticipationOverlap(['message' => 'participation-overlaps']);
}
}

View File

@ -61,9 +61,9 @@ class ParticipationOverlapValidator extends ConstraintValidator
throw new UnexpectedTypeException($participation, AccompanyingPeriodParticipation::class);
}
$personId = $participation->getPerson()->getId();
$hashPerson = spl_object_hash($participation->getPerson());
$participationList[$personId][] = $participation;
$participationList[$hashPerson][] = $participation;
}
foreach ($participationList as $group) {

View File

@ -23,6 +23,8 @@ use function in_array;
/**
* Lazy load third parties.
*
* @deprecated As the PickThirdPartyType is deprecated, this should not be in use
*/
class ThirdPartyChoiceLoader implements ChoiceLoaderInterface
{
@ -57,7 +59,7 @@ class ThirdPartyChoiceLoader implements ChoiceLoaderInterface
$choices = [];
foreach ($values as $value) {
if (empty($value)) {
if (null === $value || '' === $value) {
continue;
}

View File

@ -29,6 +29,10 @@ use function array_merge;
use function count;
use function is_array;
/**
* @deprecated use the @link{PickThirdPartyDynamicType::class}
* @note do remove ThirdPartyChoiceLoader if this class is removed
*/
class PickThirdPartyType extends AbstractType
{
/**

View File

@ -180,7 +180,7 @@ final class ThirdPartyRepository implements ObjectRepository
if (array_key_exists('name', $terms) || array_key_exists('_default', $terms)) {
$term = $terms['name'] ?? $terms['_default'];
if (empty($term)) {
if (null === $term || '' === $term) {
return;
}
$qb->andWhere($qb->expr()->like('UNACCENT(LOWER(tp.name))', 'UNACCENT(LOWER(:name))'));