|
| 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 Doctrine\Common\Annotations\AnnotationReader; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Component\Validator\Constraints\Callback; |
| 17 | +use Symfony\Component\Validator\Constraints\NotBlank; |
| 18 | +use Symfony\Component\Validator\Constraints\NotNull; |
| 19 | +use Symfony\Component\Validator\Constraints\When; |
| 20 | +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
| 21 | +use Symfony\Component\Validator\Exception\MissingOptionsException; |
| 22 | +use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 23 | +use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader; |
| 24 | +use Symfony\Component\Validator\Tests\Constraints\Fixtures\WhenTestWithAttributes; |
| 25 | + |
| 26 | +final class WhenTest extends TestCase |
| 27 | +{ |
| 28 | + public function testMissingOptionsExceptionIsThrown() |
| 29 | + { |
| 30 | + $this->expectException(MissingOptionsException::class); |
| 31 | + $this->expectExceptionMessage('The options "expression", "constraints" must be set for constraint "Symfony\Component\Validator\Constraints\When".'); |
| 32 | + |
| 33 | + new When([]); |
| 34 | + } |
| 35 | + |
| 36 | + public function testNonConstraintsAreRejected() |
| 37 | + { |
| 38 | + $this->expectException(ConstraintDefinitionException::class); |
| 39 | + $this->expectExceptionMessage('The value "foo" is not an instance of Constraint in constraint "Symfony\Component\Validator\Constraints\When"'); |
| 40 | + new When('true', [ |
| 41 | + 'foo', |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + public function testAnnotations() |
| 46 | + { |
| 47 | + $loader = new AnnotationLoader(new AnnotationReader()); |
| 48 | + $metadata = new ClassMetadata(WhenTestWithAnnotations::class); |
| 49 | + |
| 50 | + self::assertTrue($loader->loadClassMetadata($metadata)); |
| 51 | + |
| 52 | + [$classConstraint] = $metadata->getConstraints(); |
| 53 | + |
| 54 | + self::assertInstanceOf(When::class, $classConstraint); |
| 55 | + self::assertSame('true', $classConstraint->expression); |
| 56 | + self::assertEquals([ |
| 57 | + new Callback([ |
| 58 | + 'callback' => 'callback', |
| 59 | + 'groups' => ['Default', 'WhenTestWithAnnotations'], |
| 60 | + ]), |
| 61 | + ], $classConstraint->constraints); |
| 62 | + |
| 63 | + [$fooConstraint] = $metadata->properties['foo']->getConstraints(); |
| 64 | + |
| 65 | + self::assertInstanceOf(When::class, $fooConstraint); |
| 66 | + self::assertSame('true', $fooConstraint->expression); |
| 67 | + self::assertEquals([ |
| 68 | + new NotNull([ |
| 69 | + 'groups' => ['Default', 'WhenTestWithAnnotations'], |
| 70 | + ]), |
| 71 | + new NotBlank([ |
| 72 | + 'groups' => ['Default', 'WhenTestWithAnnotations'], |
| 73 | + ]), |
| 74 | + ], $fooConstraint->constraints); |
| 75 | + self::assertSame(['Default', 'WhenTestWithAnnotations'], $fooConstraint->groups); |
| 76 | + |
| 77 | + [$barConstraint] = $metadata->properties['bar']->getConstraints(); |
| 78 | + |
| 79 | + self::assertInstanceOf(When::class, $fooConstraint); |
| 80 | + self::assertSame('false', $barConstraint->expression); |
| 81 | + self::assertEquals([ |
| 82 | + new NotNull([ |
| 83 | + 'groups' => ['foo'], |
| 84 | + ]), |
| 85 | + new NotBlank([ |
| 86 | + 'groups' => ['foo'], |
| 87 | + ]), |
| 88 | + ], $barConstraint->constraints); |
| 89 | + self::assertSame(['foo'], $barConstraint->groups); |
| 90 | + |
| 91 | + [$bazConstraint] = $metadata->getters['baz']->getConstraints(); |
| 92 | + |
| 93 | + self::assertInstanceOf(When::class, $bazConstraint); |
| 94 | + self::assertSame('true', $bazConstraint->expression); |
| 95 | + self::assertEquals([ |
| 96 | + new NotNull([ |
| 97 | + 'groups' => ['Default', 'WhenTestWithAnnotations'], |
| 98 | + ]), |
| 99 | + new NotBlank([ |
| 100 | + 'groups' => ['Default', 'WhenTestWithAnnotations'], |
| 101 | + ]), |
| 102 | + ], $bazConstraint->constraints); |
| 103 | + self::assertSame(['Default', 'WhenTestWithAnnotations'], $bazConstraint->groups); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @requires PHP 8.1 |
| 108 | + */ |
| 109 | + public function testAttributes() |
| 110 | + { |
| 111 | + $loader = new AnnotationLoader(new AnnotationReader()); |
| 112 | + $metadata = new ClassMetadata(WhenTestWithAttributes::class); |
| 113 | + |
| 114 | + self::assertTrue($loader->loadClassMetadata($metadata)); |
| 115 | + |
| 116 | + [$classConstraint] = $metadata->getConstraints(); |
| 117 | + |
| 118 | + self::assertInstanceOf(When::class, $classConstraint); |
| 119 | + self::assertSame('true', $classConstraint->expression); |
| 120 | + self::assertEquals([ |
| 121 | + new Callback([ |
| 122 | + 'callback' => 'callback', |
| 123 | + 'groups' => ['Default', 'WhenTestWithAttributes'], |
| 124 | + ]), |
| 125 | + ], $classConstraint->constraints); |
| 126 | + |
| 127 | + [$fooConstraint] = $metadata->properties['foo']->getConstraints(); |
| 128 | + |
| 129 | + self::assertInstanceOf(When::class, $fooConstraint); |
| 130 | + self::assertSame('true', $fooConstraint->expression); |
| 131 | + self::assertEquals([ |
| 132 | + new NotNull([ |
| 133 | + 'groups' => ['Default', 'WhenTestWithAttributes'], |
| 134 | + ]), |
| 135 | + new NotBlank([ |
| 136 | + 'groups' => ['Default', 'WhenTestWithAttributes'], |
| 137 | + ]), |
| 138 | + ], $fooConstraint->constraints); |
| 139 | + self::assertSame(['Default', 'WhenTestWithAttributes'], $fooConstraint->groups); |
| 140 | + |
| 141 | + [$barConstraint] = $metadata->properties['bar']->getConstraints(); |
| 142 | + |
| 143 | + self::assertInstanceOf(When::class, $fooConstraint); |
| 144 | + self::assertSame('false', $barConstraint->expression); |
| 145 | + self::assertEquals([ |
| 146 | + new NotNull([ |
| 147 | + 'groups' => ['foo'], |
| 148 | + ]), |
| 149 | + new NotBlank([ |
| 150 | + 'groups' => ['foo'], |
| 151 | + ]), |
| 152 | + ], $barConstraint->constraints); |
| 153 | + self::assertSame(['foo'], $barConstraint->groups); |
| 154 | + |
| 155 | + [$bazConstraint] = $metadata->getters['baz']->getConstraints(); |
| 156 | + |
| 157 | + self::assertInstanceOf(When::class, $bazConstraint); |
| 158 | + self::assertSame('true', $bazConstraint->expression); |
| 159 | + self::assertEquals([ |
| 160 | + new NotNull([ |
| 161 | + 'groups' => ['Default', 'WhenTestWithAttributes'], |
| 162 | + ]), |
| 163 | + new NotBlank([ |
| 164 | + 'groups' => ['Default', 'WhenTestWithAttributes'], |
| 165 | + ]), |
| 166 | + ], $bazConstraint->constraints); |
| 167 | + self::assertSame(['Default', 'WhenTestWithAttributes'], $bazConstraint->groups); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * @When(expression="true", constraints={@Callback("callback")}) |
| 173 | + */ |
| 174 | +class WhenTestWithAnnotations |
| 175 | +{ |
| 176 | + /** |
| 177 | + * @When(expression="true", constraints={@NotNull, @NotBlank}) |
| 178 | + */ |
| 179 | + private $foo; |
| 180 | + |
| 181 | + /** |
| 182 | + * @When(expression="false", constraints={@NotNull, @NotBlank}, groups={"foo"}) |
| 183 | + */ |
| 184 | + private $bar; |
| 185 | + |
| 186 | + /** |
| 187 | + * @When(expression="true", constraints={@NotNull, @NotBlank}) |
| 188 | + */ |
| 189 | + public function getBaz() |
| 190 | + { |
| 191 | + return null; |
| 192 | + } |
| 193 | + |
| 194 | + public function callback() |
| 195 | + { |
| 196 | + } |
| 197 | +} |
0 commit comments