|
13 | 13 |
|
14 | 14 | use Symfony\Component\Validator\Constraints\Unique;
|
15 | 15 | use Symfony\Component\Validator\Constraints\UniqueValidator;
|
| 16 | +use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
16 | 17 | use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
17 | 18 | use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
18 | 19 |
|
19 | 20 | class UniqueValidatorTest extends ConstraintValidatorTestCase
|
20 | 21 | {
|
21 |
| - protected function createValidator() |
| 22 | + protected function createValidator(): UniqueValidator |
22 | 23 | {
|
23 | 24 | return new UniqueValidator();
|
24 | 25 | }
|
@@ -153,15 +154,15 @@ public function testExpectsNonUniqueObjects($callback)
|
153 | 154 | ->assertRaised();
|
154 | 155 | }
|
155 | 156 |
|
156 |
| - public function getCallback() |
| 157 | + public function getCallback(): array |
157 | 158 | {
|
158 | 159 | return [
|
159 |
| - yield 'static function' => [static function (\stdClass $object) { |
| 160 | + 'static function' => [static function (\stdClass $object) { |
160 | 161 | return [$object->name, $object->email];
|
161 | 162 | }],
|
162 |
| - yield 'callable with string notation' => ['Symfony\Component\Validator\Tests\Constraints\CallableClass::execute'], |
163 |
| - yield 'callable with static notation' => [[CallableClass::class, 'execute']], |
164 |
| - yield 'callable with object' => [[new CallableClass(), 'execute']], |
| 163 | + 'callable with string notation' => ['Symfony\Component\Validator\Tests\Constraints\CallableClass::execute'], |
| 164 | + 'callable with static notation' => [[CallableClass::class, 'execute']], |
| 165 | + 'callable with object' => [[new CallableClass(), 'execute']], |
165 | 166 | ];
|
166 | 167 | }
|
167 | 168 |
|
@@ -220,6 +221,67 @@ public function testExpectsValidCaseInsensitiveComparison()
|
220 | 221 |
|
221 | 222 | $this->assertNoViolation();
|
222 | 223 | }
|
| 224 | + |
| 225 | + public function testCollectionFieldsAreOptional() |
| 226 | + { |
| 227 | + $this->validator->validate([['value' => 5], ['id' => 1, 'value' => 6]], new Unique(fields: 'id')); |
| 228 | + |
| 229 | + $this->assertNoViolation(); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * @dataProvider getInvalidFieldNames |
| 234 | + */ |
| 235 | + public function testCollectionFieldNamesMustBeString(string $type, mixed $field) |
| 236 | + { |
| 237 | + $this->expectException(UnexpectedTypeException::class); |
| 238 | + $this->expectExceptionMessage(sprintf('Expected argument of type "string", "%s" given', $type)); |
| 239 | + |
| 240 | + $this->validator->validate([['value' => 5], ['id' => 1, 'value' => 6]], new Unique(fields: [$field])); |
| 241 | + } |
| 242 | + |
| 243 | + public function getInvalidFieldNames(): array |
| 244 | + { |
| 245 | + return [ |
| 246 | + ['stdClass', new \stdClass()], |
| 247 | + ['int', 2], |
| 248 | + ['bool', false], |
| 249 | + ]; |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * @dataProvider getInvalidCollectionValues |
| 254 | + */ |
| 255 | + public function testInvalidCollectionValues(array $value, array $fields) |
| 256 | + { |
| 257 | + $this->validator->validate($value, new Unique([ |
| 258 | + 'message' => 'myMessage', |
| 259 | + ], fields: $fields)); |
| 260 | + |
| 261 | + $this->buildViolation('myMessage') |
| 262 | + ->setParameter('{{ value }}', 'array') |
| 263 | + ->setCode(Unique::IS_NOT_UNIQUE) |
| 264 | + ->assertRaised(); |
| 265 | + } |
| 266 | + |
| 267 | + public function getInvalidCollectionValues(): array |
| 268 | + { |
| 269 | + return [ |
| 270 | + 'unique string' => [[ |
| 271 | + ['lang' => 'eng', 'translation' => 'hi'], |
| 272 | + ['lang' => 'eng', 'translation' => 'hello'], |
| 273 | + ], ['lang']], |
| 274 | + 'unique floats' => [[ |
| 275 | + ['latitude' => 51.509865, 'longitude' => -0.118092, 'poi' => 'capital'], |
| 276 | + ['latitude' => 52.520008, 'longitude' => 13.404954], |
| 277 | + ['latitude' => 51.509865, 'longitude' => -0.118092], |
| 278 | + ], ['latitude', 'longitude']], |
| 279 | + 'unique int' => [[ |
| 280 | + [ 'id' => 1, 'email' => '[email protected]'], |
| 281 | + [ 'id' => 1, 'email' => '[email protected]'], |
| 282 | + ], ['id']], |
| 283 | + ]; |
| 284 | + } |
223 | 285 | }
|
224 | 286 |
|
225 | 287 | class CallableClass
|
|
0 commit comments