Trim PersonIdentifier values during denormalization, implement RequiredIdentifierConstraint and validator, and add tests for empty value validation.

This commit is contained in:
2025-09-18 14:01:34 +02:00
parent 4207efd6bf
commit 52404956d2
4 changed files with 216 additions and 2 deletions

View File

@@ -26,7 +26,7 @@ final readonly class StringIdentifier implements PersonIdentifierEngineInterface
public function canonicalizeValue(array $value, PersonIdentifierDefinition $definition): ?string
{
return $value['content'] ?? '';
return trim($value['content'] ?? '');
}
public function buildForm(FormBuilderInterface $builder, PersonIdentifierDefinition $personIdentifierDefinition): void
@@ -36,7 +36,7 @@ final readonly class StringIdentifier implements PersonIdentifierEngineInterface
public function renderAsString(?PersonIdentifier $identifier, PersonIdentifierDefinition $definition): string
{
return $identifier?->getValue()['content'] ?? '';
return trim($identifier?->getValue()['content'] ?? '');
}
public function isEmpty(PersonIdentifier $identifier): bool

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\PersonBundle\PersonIdentifier\Validator;
use Symfony\Component\Validator\Constraint;
/**
* Test that the required constraints are present.
*/
class RequiredIdentifierConstraint extends Constraint
{
public string $message = 'This identifier must be set';
public function getTargets(): string
{
return self::PROPERTY_CONSTRAINT;
}
}

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\PersonBundle\PersonIdentifier\Validator;
use Chill\PersonBundle\Entity\Identifier\IdentifierPresenceEnum;
use Chill\PersonBundle\Entity\Identifier\PersonIdentifier;
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierManagerInterface;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
final class RequiredIdentifierConstraintValidator extends ConstraintValidator
{
public function __construct(private readonly PersonIdentifierManagerInterface $identifierManager) {}
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof RequiredIdentifierConstraint) {
throw new UnexpectedTypeException($constraint, RequiredIdentifierConstraint::class);
}
if (!$value instanceof Collection) {
throw new UnexpectedValueException($value, Collection::class);
}
foreach ($this->identifierManager->getWorkers() as $worker) {
if (IdentifierPresenceEnum::REQUIRED !== $worker->getDefinition()->getPresence()) {
continue;
}
$identifier = $value->findFirst(fn (int $key, PersonIdentifier $identifier) => $identifier->getDefinition() === $worker->getDefinition());
if (null === $identifier || $worker->isEmpty($identifier)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $worker->renderAsString($identifier))
->setParameter('definition_id', (string) $worker->getDefinition()->getId())
->atPath('identifiers')
->setCode('c08b7b32-947f-11f0-8608-9b8560e9bf05')
->addViolation();
}
}
}
}

View File

@@ -0,0 +1,133 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\PersonBundle\Tests\PersonIdentifier\Validator;
use Chill\PersonBundle\Entity\Identifier\IdentifierPresenceEnum;
use Chill\PersonBundle\Entity\Identifier\PersonIdentifier;
use Chill\PersonBundle\Entity\Identifier\PersonIdentifierDefinition;
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierEngineInterface;
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierManagerInterface;
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker;
use Chill\PersonBundle\PersonIdentifier\Validator\RequiredIdentifierConstraint;
use Chill\PersonBundle\PersonIdentifier\Validator\RequiredIdentifierConstraintValidator;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Argument;
/**
* @internal
*/
#[CoversClass(RequiredIdentifierConstraintValidator::class)]
final class RequiredIdentifierConstraintValidatorTest extends ConstraintValidatorTestCase
{
use ProphecyTrait;
private PersonIdentifierDefinition $requiredDefinition;
protected function createValidator(): RequiredIdentifierConstraintValidator
{
$this->requiredDefinition = new PersonIdentifierDefinition(
label: ['fr' => 'Identifiant requis'],
engine: 'test.engine',
);
$this->requiredDefinition->setPresence(IdentifierPresenceEnum::REQUIRED);
$reflection = new \ReflectionClass($this->requiredDefinition);
$id = $reflection->getProperty('id');
$id->setValue($this->requiredDefinition, 1);
// Mock only the required methods of the engine used by the validator through the worker
$engineProphecy = $this->prophesize(PersonIdentifierEngineInterface::class);
$engineProphecy->isEmpty(Argument::type(PersonIdentifier::class))
->will(function (array $args): bool {
/** @var PersonIdentifier $identifier */
$identifier = $args[0];
return '' === trim($identifier->getValue()['content'] ?? '');
});
$engineProphecy->renderAsString(Argument::any(), Argument::any())
->will(function (array $args): string {
/** @var PersonIdentifier|null $identifier */
$identifier = $args[0] ?? null;
return $identifier?->getValue()['content'] ?? '';
});
$worker = new PersonIdentifierWorker($engineProphecy->reveal(), $this->requiredDefinition);
// Mock only the required method used by the validator
$managerProphecy = $this->prophesize(PersonIdentifierManagerInterface::class);
$managerProphecy->getWorkers()->willReturn([$worker]);
return new RequiredIdentifierConstraintValidator($managerProphecy->reveal());
}
public function testThrowsOnNonCollectionValue(): void
{
$this->expectException(UnexpectedValueException::class);
$this->validator->validate(new \stdClass(), new RequiredIdentifierConstraint());
}
public function testThrowsOnInvalidConstraintType(): void
{
$this->expectException(UnexpectedTypeException::class);
// Provide a valid Collection value so the type check reaches the constraint check
$this->validator->validate(new ArrayCollection(), new NotBlank());
}
public function testNoViolationWhenRequiredIdentifierPresentAndNotEmpty(): void
{
$identifier = new PersonIdentifier($this->requiredDefinition);
$identifier->setValue(['content' => 'ABC']);
$collection = new ArrayCollection([$identifier]);
$this->validator->validate($collection, new RequiredIdentifierConstraint());
$this->assertNoViolation();
}
public function testViolationWhenRequiredIdentifierMissing(): void
{
$collection = new ArrayCollection();
$this->validator->validate($collection, new RequiredIdentifierConstraint());
$this->buildViolation('This identifier must be set')
->atPath('property.path.identifiers')
->setParameter('{{ value }}', '')
->setParameter('definition_id', '1')
->setCode('c08b7b32-947f-11f0-8608-9b8560e9bf05')
->assertRaised();
}
public function testViolationWhenRequiredIdentifierIsEmpty(): void
{
$identifier = new PersonIdentifier($this->requiredDefinition);
$identifier->setValue(['content' => ' ']);
$collection = new ArrayCollection([$identifier]);
$this->validator->validate($collection, new RequiredIdentifierConstraint());
$this->buildViolation('This identifier must be set')
->atPath('property.path.identifiers')
->setParameter('{{ value }}', ' ')
->setParameter('definition_id', '1')
->setCode('c08b7b32-947f-11f0-8608-9b8560e9bf05')
->assertRaised();
}
}