|
12 | 12 | use PHPUnit\Framework\TestCase;
|
13 | 13 | use Symfony\Component\Validator\Constraints\NotNull;
|
14 | 14 | use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
| 15 | +use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 16 | +use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; |
15 | 17 | use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
|
16 | 18 |
|
17 | 19 | class EntityExistValidatorTest extends TestCase
|
@@ -158,4 +160,46 @@ public function testValidateInvalidEntity(): void
|
158 | 160 |
|
159 | 161 | $this->validator->validate(1, $constraint);
|
160 | 162 | }
|
| 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; |
161 | 205 | }
|
0 commit comments