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
@@ -228,10 +229,30 @@ public function testCollectionFieldsAreOptional()
228
229
$ this ->assertNoViolation ();
229
230
}
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 ([$ field ]));
241
+ }
242
+
243
+ public function getInvalidFieldNames (): \Generator
244
+ {
245
+ return [
246
+ yield ['stdClass ' , new \stdClass ()],
247
+ yield ['int ' , 2 ],
248
+ yield ['bool ' , false ],
249
+ ];
250
+ }
251
+
231
252
/**
232
253
* @dataProvider getInvalidCollectionValues
233
254
*/
234
- public function testInvalidCollectionValues ($ value , $ fields )
255
+ public function testInvalidCollectionValues (array $ value , array $ fields )
235
256
{
236
257
$ this ->validator ->validate ($ value , new Unique ($ fields , [
237
258
'message ' => 'myMessage ' ,
@@ -243,18 +264,18 @@ public function testInvalidCollectionValues($value, $fields)
243
264
->assertRaised ();
244
265
}
245
266
246
- public function getInvalidCollectionValues ()
267
+ public function getInvalidCollectionValues (): \ Generator
247
268
{
248
269
return [
249
- yield 'unique string ' => [[['lang ' => 'eng ' , 'translation ' => 'hi ' ], ['lang ' => 'eng ' , 'translation ' => 'hi ' ],
270
+ yield 'unique string ' => [[['lang ' => 'eng ' , 'translation ' => 'hi ' ], ['lang ' => 'eng ' , 'translation ' => 'hello ' ],
250
271
], ['lang ' ]],
251
272
yield 'unique floats ' => [[
252
273
['latitude ' => 51.509865 , 'longitude ' => -0.118092 , 'poi ' => 'capital ' ],
253
274
['latitude ' => 52.520008 , 'longitude ' => 13.404954 ],
254
275
['latitude ' => 51.509865 , 'longitude ' => -0.118092 ],
255
276
], ['latitude ' , 'longitude ' ]],
256
277
yield 'unique int ' => [[
257
- ['id ' => 1 , 'email ' => 'bar@email.com ' ], ['id ' => 1 , 'email ' => 'bar @email.com ' ],
278
+ ['id ' => 1 , 'email ' => 'bar@email.com ' ], ['id ' => 1 , 'email ' => 'foo @email.com ' ],
258
279
], ['id ' ]],
259
280
];
260
281
}
0 commit comments