Skip to content

Commit eb888e9

Browse files
louisezetterlundker0xLouise Zetterlund
authored
Feature/php8 attribute (#11)
* 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]>
1 parent 6d96285 commit eb888e9

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "happyr/entity-exists-validation-constraint",
3-
"type": "library",
43
"description": "Verify that your entity exists",
54
"license": "MIT",
5+
"type": "library",
66
"authors": [
77
{
88
"name": "Radoje Albijanic",

src/EntityExist.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@
1111
*
1212
* @author Radoje Albijanic <[email protected]>
1313
*/
14+
#[\Attribute(\Attribute::TARGET_PROPERTY)]
1415
final class EntityExist extends Constraint
1516
{
1617
public $message = 'Entity "%entity%" with property "%property%": "%value%" does not exist.';
1718
public $property = 'id';
1819
public $entity;
20+
21+
public function __construct($entity = null, $property = null, $message = null, $options = null, array $groups = null, $payload = null)
22+
{
23+
parent::__construct($options, $groups, $payload);
24+
25+
$this->entity = $entity ?? $this->entity;
26+
$this->property = $property ?? $this->property;
27+
$this->message = $message ?? $this->message;
28+
}
1929
}

tests/EntityExistValidatorTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use PHPUnit\Framework\TestCase;
1313
use Symfony\Component\Validator\Constraints\NotNull;
1414
use Symfony\Component\Validator\Context\ExecutionContextInterface;
15+
use Symfony\Component\Validator\Mapping\ClassMetadata;
16+
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
1517
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
1618

1719
class EntityExistValidatorTest extends TestCase
@@ -158,4 +160,46 @@ public function testValidateInvalidEntity(): void
158160

159161
$this->validator->validate(1, $constraint);
160162
}
163+
164+
/**
165+
* @requires PHP 8
166+
*/
167+
public function testValidateFromAttribute()
168+
{
169+
$numRequired = (new \ReflectionMethod(AnnotationLoader::class, '__construct'))->getNumberOfRequiredParameters();
170+
if ($numRequired > 0) {
171+
$this->markTestSkipped('This test is skipped on Symfony <5.2');
172+
}
173+
174+
$this->context->expects($this->never())->method('buildViolation');
175+
176+
$classMetadata = new ClassMetadata(EntityDummy::class);
177+
(new AnnotationLoader())->loadClassMetadata($classMetadata);
178+
179+
[$constraint] = $classMetadata->properties['user']->constraints;
180+
181+
$repository = $this->getMockBuilder(EntityRepository::class)
182+
->disableOriginalConstructor()
183+
->getMock();
184+
185+
$repository
186+
->expects($this->once())
187+
->method('findOneBy')
188+
->with(['uuid' => 'foobar'])
189+
->willReturn('my_user');
190+
191+
$this->entityManager
192+
->expects($this->once())
193+
->method('getRepository')
194+
->with('App\Entity\User')
195+
->willReturn($repository);
196+
197+
$this->validator->validate('foobar', $constraint);
198+
}
199+
}
200+
201+
class EntityDummy
202+
{
203+
#[EntityExist(entity: 'App\Entity\User', property: 'uuid')]
204+
private $user;
161205
}

0 commit comments

Comments
 (0)