@@ -154,6 +154,119 @@ public function testInvalidMessage()
154
154
$ this ->assertEquals ('This value is not valid. ' , $ form ->getConfig ()->getOption ('invalid_message ' ));
155
155
}
156
156
157
+ public function testConstraintsFromEntityValid ()
158
+ {
159
+ // Maps firstName field to Author::firstName -> Length(3) constraint
160
+ $ form = $ this ->createFormForConstraintsFrom ();
161
+
162
+ $ form ->submit (['firstName ' => 'foo ' ]);
163
+
164
+ $ errors = $ form ->getErrors (true );
165
+
166
+ $ this ->assertCount (0 , $ errors );
167
+ }
168
+
169
+ public function testConstraintsFromEntityEmpty ()
170
+ {
171
+ // Maps firstName field to Author::firstName -> Length(3) constraint
172
+ $ form = $ this ->createFormForConstraintsFrom ();
173
+
174
+ $ form ->submit (['firstName ' => '' ]);
175
+
176
+ $ errors = $ form ->getErrors (true );
177
+
178
+ $ this ->assertCount (1 , $ errors );
179
+ }
180
+
181
+ public function testConstraintsFromEntityInvalid ()
182
+ {
183
+ // Maps firstName field to Author::firstName -> Length(3) constraint
184
+ $ form = $ this ->createFormForConstraintsFrom ();
185
+
186
+ $ form ->submit (['firstName ' => 'foobar ' ]);
187
+
188
+ $ errors = $ form ->getErrors (true );
189
+
190
+ $ this ->assertCount (1 , $ errors );
191
+ }
192
+
193
+ public function testConstraintsFromEntityCustomPropertyValid ()
194
+ {
195
+ // Maps firstName field to Author::lastName -> Length(min: 5) constraint
196
+ $ form = $ this ->createFormForConstraintsFrom ('lastName ' );
197
+
198
+ $ form ->submit (['firstName ' => 'foobar ' ]);
199
+
200
+ $ errors = $ form ->getErrors (true );
201
+
202
+ $ this ->assertCount (0 , $ errors );
203
+ }
204
+
205
+ public function testConstraintsFromEntityCustomPropertyEmpty ()
206
+ {
207
+ // Maps firstName field to Author::lastName -> Length(min: 5) constraint
208
+ $ form = $ this ->createFormForConstraintsFrom ('lastName ' );
209
+
210
+ $ form ->submit (['firstName ' => '' ]);
211
+
212
+ $ errors = $ form ->getErrors (true );
213
+
214
+ $ this ->assertCount (1 , $ errors );
215
+ }
216
+
217
+ public function testConstraintsFromEntityCustomPropertyInvalid ()
218
+ {
219
+ // Maps firstName field to Author::lastName -> Length(min: 5) constraint
220
+ $ form = $ this ->createFormForConstraintsFrom ('lastName ' );
221
+
222
+ $ form ->submit (['firstName ' => 'foo ' ]);
223
+
224
+ $ errors = $ form ->getErrors (true );
225
+
226
+ $ this ->assertCount (1 , $ errors );
227
+ }
228
+
229
+ protected function createFormForConstraintsFrom (string $ propertyName = null )
230
+ {
231
+ $ formMetadata = new ClassMetadata (Form::class);
232
+ $ authorMetadata = (new ClassMetadata (Author::class))
233
+ ->addPropertyConstraint ('firstName ' , new Length (3 ))
234
+ ->addPropertyConstraint ('lastName ' , new Length (min: 5 ))
235
+ ;
236
+ $ metadataFactory = $ this ->createMock (MetadataFactoryInterface::class);
237
+ $ metadataFactory ->expects ($ this ->any ())
238
+ ->method ('getMetadataFor ' )
239
+ ->willReturnCallback (static function ($ classOrObject ) use ($ formMetadata , $ authorMetadata ) {
240
+ if (Author::class === $ classOrObject || $ classOrObject instanceof Author) {
241
+ return $ authorMetadata ;
242
+ }
243
+
244
+ if (Form::class === $ classOrObject || $ classOrObject instanceof Form) {
245
+ return $ formMetadata ;
246
+ }
247
+
248
+ return new ClassMetadata (\is_string ($ classOrObject ) ? $ classOrObject : $ classOrObject ::class);
249
+ })
250
+ ;
251
+
252
+ $ validator = Validation::createValidatorBuilder ()
253
+ ->setMetadataFactory ($ metadataFactory )
254
+ ->getValidator ()
255
+ ;
256
+
257
+ $ form = Forms::createFormFactoryBuilder ()
258
+ ->addExtension (new ValidatorExtension ($ validator ))
259
+ ->getFormFactory ()
260
+ ->create (FormTypeTest::TESTED_TYPE )
261
+ ->add ('firstName ' , TextTypeTest::TESTED_TYPE , [
262
+ 'constraints_from_entity ' => Author::class,
263
+ 'constraints_from_property ' => $ propertyName ?? null ,
264
+ ])
265
+ ;
266
+
267
+ return $ form ;
268
+ }
269
+
157
270
protected function createForm (array $ options = [])
158
271
{
159
272
return $ this ->factory ->create (FormTypeTest::TESTED_TYPE , null , $ options );
0 commit comments