Skip to content

Commit

Permalink
Feature/php8 attribute (#11)
Browse files Browse the repository at this point in the history
* Add support for PHP8 attribute

* fix/skip attribute test for symfony <5.2

---------

Co-authored-by: Romain Monteil <[email protected]>
Co-authored-by: Louise Zetterlund <[email protected]>
  • Loading branch information
3 people authored Jun 21, 2023
1 parent 6d96285 commit eb888e9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 10 additions & 0 deletions src/EntityExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@
*
* @author Radoje Albijanic <[email protected]>
*/
#[\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;
}
}
44 changes: 44 additions & 0 deletions tests/EntityExistValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

0 comments on commit eb888e9

Please sign in to comment.