|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Validator\Tests\Constraints; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Validator\Constraints\ExpressionSyntax; |
| 16 | +use Symfony\Component\Validator\Constraints\ExpressionSyntaxValidator; |
| 17 | +use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 18 | +use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; |
| 19 | + |
| 20 | +class ExpressionSyntaxTest extends TestCase |
| 21 | +{ |
| 22 | + public function testValidatedByStandardValidator() |
| 23 | + { |
| 24 | + $constraint = new ExpressionSyntax(); |
| 25 | + |
| 26 | + self::assertSame(ExpressionSyntaxValidator::class, $constraint->validatedBy()); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @dataProvider provideServiceValidatedConstraints |
| 31 | + */ |
| 32 | + public function testValidatedByService(ExpressionSyntax $constraint) |
| 33 | + { |
| 34 | + self::assertSame('my_service', $constraint->validatedBy()); |
| 35 | + } |
| 36 | + |
| 37 | + public function provideServiceValidatedConstraints(): iterable |
| 38 | + { |
| 39 | + yield 'Doctrine style' => [new ExpressionSyntax(['service' => 'my_service'])]; |
| 40 | + |
| 41 | + yield 'named arguments' => [new ExpressionSyntax(service: 'my_service')]; |
| 42 | + |
| 43 | + $metadata = new ClassMetadata(ExpressionSyntaxDummy::class); |
| 44 | + self::assertTrue((new AnnotationLoader())->loadClassMetadata($metadata)); |
| 45 | + |
| 46 | + yield 'attribute' => [$metadata->properties['b']->constraints[0]]; |
| 47 | + } |
| 48 | + |
| 49 | + public function testAttributes() |
| 50 | + { |
| 51 | + $metadata = new ClassMetadata(ExpressionSyntaxDummy::class); |
| 52 | + self::assertTrue((new AnnotationLoader())->loadClassMetadata($metadata)); |
| 53 | + |
| 54 | + [$aConstraint] = $metadata->properties['a']->getConstraints(); |
| 55 | + self::assertNull($aConstraint->service); |
| 56 | + self::assertNull($aConstraint->allowedVariables); |
| 57 | + |
| 58 | + [$bConstraint] = $metadata->properties['b']->getConstraints(); |
| 59 | + self::assertSame('my_service', $bConstraint->service); |
| 60 | + self::assertSame('myMessage', $bConstraint->message); |
| 61 | + self::assertSame(['Default', 'ExpressionSyntaxDummy'], $bConstraint->groups); |
| 62 | + |
| 63 | + [$cConstraint] = $metadata->properties['c']->getConstraints(); |
| 64 | + self::assertSame(['foo', 'bar'], $cConstraint->allowedVariables); |
| 65 | + self::assertSame(['my_group'], $cConstraint->groups); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +class ExpressionSyntaxDummy |
| 70 | +{ |
| 71 | + #[ExpressionSyntax] |
| 72 | + private $a; |
| 73 | + |
| 74 | + #[ExpressionSyntax(service: 'my_service', message: 'myMessage')] |
| 75 | + private $b; |
| 76 | + |
| 77 | + #[ExpressionSyntax(allowedVariables: ['foo', 'bar'], groups: ['my_group'])] |
| 78 | + private $c; |
| 79 | +} |
0 commit comments