From eb888e90e3743ee4e1b17752eb5658ce03ce3ce5 Mon Sep 17 00:00:00 2001 From: Louise Zetterlund <51154961+louisezetterlund@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:25:17 +0200 Subject: [PATCH] Feature/php8 attribute (#11) * Add support for PHP8 attribute * fix/skip attribute test for symfony <5.2 --------- Co-authored-by: Romain Monteil Co-authored-by: Louise Zetterlund --- composer.json | 2 +- src/EntityExist.php | 10 +++++++ tests/EntityExistValidatorTest.php | 44 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fb4d563..7d437e6 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { "name": "happyr/entity-exists-validation-constraint", - "type": "library", "description": "Verify that your entity exists", "license": "MIT", + "type": "library", "authors": [ { "name": "Radoje Albijanic", diff --git a/src/EntityExist.php b/src/EntityExist.php index e8bbc60..5657554 100644 --- a/src/EntityExist.php +++ b/src/EntityExist.php @@ -11,9 +11,19 @@ * * @author Radoje Albijanic */ +#[\Attribute(\Attribute::TARGET_PROPERTY)] final class EntityExist extends Constraint { public $message = 'Entity "%entity%" with property "%property%": "%value%" does not exist.'; public $property = 'id'; public $entity; + + public function __construct($entity = null, $property = null, $message = null, $options = null, array $groups = null, $payload = null) + { + parent::__construct($options, $groups, $payload); + + $this->entity = $entity ?? $this->entity; + $this->property = $property ?? $this->property; + $this->message = $message ?? $this->message; + } } diff --git a/tests/EntityExistValidatorTest.php b/tests/EntityExistValidatorTest.php index 4f0e345..1e951cd 100644 --- a/tests/EntityExistValidatorTest.php +++ b/tests/EntityExistValidatorTest.php @@ -12,6 +12,8 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Validator\Constraints\NotNull; use Symfony\Component\Validator\Context\ExecutionContextInterface; +use Symfony\Component\Validator\Mapping\ClassMetadata; +use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface; class EntityExistValidatorTest extends TestCase @@ -158,4 +160,46 @@ public function testValidateInvalidEntity(): void $this->validator->validate(1, $constraint); } + + /** + * @requires PHP 8 + */ + public function testValidateFromAttribute() + { + $numRequired = (new \ReflectionMethod(AnnotationLoader::class, '__construct'))->getNumberOfRequiredParameters(); + if ($numRequired > 0) { + $this->markTestSkipped('This test is skipped on Symfony <5.2'); + } + + $this->context->expects($this->never())->method('buildViolation'); + + $classMetadata = new ClassMetadata(EntityDummy::class); + (new AnnotationLoader())->loadClassMetadata($classMetadata); + + [$constraint] = $classMetadata->properties['user']->constraints; + + $repository = $this->getMockBuilder(EntityRepository::class) + ->disableOriginalConstructor() + ->getMock(); + + $repository + ->expects($this->once()) + ->method('findOneBy') + ->with(['uuid' => 'foobar']) + ->willReturn('my_user'); + + $this->entityManager + ->expects($this->once()) + ->method('getRepository') + ->with('App\Entity\User') + ->willReturn($repository); + + $this->validator->validate('foobar', $constraint); + } +} + +class EntityDummy +{ + #[EntityExist(entity: 'App\Entity\User', property: 'uuid')] + private $user; }