1313
1414use Symfony \Component \Validator \Constraints \Unique ;
1515use Symfony \Component \Validator \Constraints \UniqueValidator ;
16+ use Symfony \Component \Validator \Exception \UnexpectedTypeException ;
1617use Symfony \Component \Validator \Exception \UnexpectedValueException ;
1718use Symfony \Component \Validator \Test \ConstraintValidatorTestCase ;
1819
@@ -228,10 +229,30 @@ public function testCollectionFieldsAreOptional()
228229 $ this ->assertNoViolation ();
229230 }
230231
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+
231252 /**
232253 * @dataProvider getInvalidCollectionValues
233254 */
234- public function testInvalidCollectionValues ($ value , $ fields )
255+ public function testInvalidCollectionValues (array $ value , array $ fields )
235256 {
236257 $ this ->validator ->validate ($ value , new Unique ($ fields , [
237258 'message ' => 'myMessage ' ,
@@ -243,18 +264,18 @@ public function testInvalidCollectionValues($value, $fields)
243264 ->assertRaised ();
244265 }
245266
246- public function getInvalidCollectionValues ()
267+ public function getInvalidCollectionValues (): \ Generator
247268 {
248269 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 ' ],
250271 ], ['lang ' ]],
251272 yield 'unique floats ' => [[
252273 ['latitude ' => 51.509865 , 'longitude ' => -0.118092 , 'poi ' => 'capital ' ],
253274 ['latitude ' => 52.520008 , 'longitude ' => 13.404954 ],
254275 ['latitude ' => 51.509865 , 'longitude ' => -0.118092 ],
255276 ], ['latitude ' , 'longitude ' ]],
256277 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 ' ],
258279 ], ['id ' ]],
259280 ];
260281 }
0 commit comments