@@ -168,51 +168,33 @@ public function testAllConstraintValidateAllGroupsForNestedConstraints()
168
168
/**
169
169
* @requires PHP 7.4
170
170
*/
171
- public function testValidateDoNotCascadeNestedObjectsByDefault ()
172
- {
173
- $ this ->metadataFactory ->addMetadata (new ClassMetadata (CascadingEntity::class));
174
- $ this ->metadataFactory ->addMetadata ((new ClassMetadata (CascadedChild::class))
175
- ->addPropertyConstraint ('name ' , new NotNull ())
176
- );
177
-
178
- $ entity = new CascadingEntity ();
179
- $ entity ->childOne = new CascadedChild ();
180
- CascadingEntity::$ childTwo = new CascadedChild ();
181
-
182
- $ violations = $ this ->validator ->validate ($ entity );
183
-
184
- $ this ->assertCount (0 , $ violations );
185
-
186
- CascadingEntity::$ childTwo = null ;
187
- }
188
-
189
- /**
190
- * @requires PHP 7.4
191
- */
192
- public function testValidateDoNotCascadeNestedArrayByDefault ()
171
+ public function testValidateDoNotCascadeNestedObjectsAndArraysByDefault ()
193
172
{
194
173
$ this ->metadataFactory ->addMetadata (new ClassMetadata (CascadingEntity::class));
195
174
$ this ->metadataFactory ->addMetadata ((new ClassMetadata (CascadedChild::class))
196
175
->addPropertyConstraint ('name ' , new NotNull ())
197
176
);
198
177
199
178
$ entity = new CascadingEntity ();
179
+ $ entity ->requiredChild = new CascadedChild ();
180
+ $ entity ->optionalChild = new CascadedChild ();
200
181
$ entity ->children [] = new CascadedChild ();
201
- $ entity -> children [] = new CascadedChild ();
182
+ CascadingEntity:: $ staticChild = new CascadedChild ();
202
183
203
184
$ violations = $ this ->validator ->validate ($ entity );
204
185
205
186
$ this ->assertCount (0 , $ violations );
187
+
188
+ CascadingEntity::$ staticChild = null ;
206
189
}
207
190
208
191
/**
209
192
* @requires PHP 7.4
210
193
*/
211
- public function testValidateCascadeNestedArrayByDefaultIfConstrained ()
194
+ public function testValidateTraverseNestedArrayByDefaultIfConstrainedWithoutCascading ()
212
195
{
213
196
$ this ->metadataFactory ->addMetadata ((new ClassMetadata (CascadingEntity::class))
214
197
->addPropertyConstraint ('children ' , new All ([
215
- new NotNull (),
216
198
new Type (CascadedChild::class),
217
199
]))
218
200
);
@@ -222,6 +204,7 @@ public function testValidateCascadeNestedArrayByDefaultIfConstrained()
222
204
223
205
$ entity = new CascadingEntity ();
224
206
$ entity ->children [] = new \stdClass ();
207
+ $ entity ->children [] = new CascadedChild ();
225
208
226
209
$ violations = $ this ->validator ->validate ($ entity );
227
210
@@ -235,24 +218,32 @@ public function testValidateCascadeNestedArrayByDefaultIfConstrained()
235
218
public function testValidateCascadeWithValid ()
236
219
{
237
220
$ this ->metadataFactory ->addMetadata ((new ClassMetadata (CascadingEntity::class))
238
- ->addPropertyConstraint ('childOne ' , new Valid ())
239
- ->addPropertyConstraint ('childTwo ' , new Valid ())
221
+ ->addPropertyConstraint ('requiredChild ' , new Valid ())
222
+ ->addPropertyConstraint ('optionalChild ' , new Valid ())
223
+ ->addPropertyConstraint ('staticChild ' , new Valid ())
224
+ ->addPropertyConstraint ('children ' , new Valid ())
240
225
);
241
226
$ this ->metadataFactory ->addMetadata ((new ClassMetadata (CascadedChild::class))
242
227
->addPropertyConstraint ('name ' , new NotNull ())
243
228
);
244
229
245
230
$ entity = new CascadingEntity ();
246
- $ entity ->childOne = new CascadedChild ();
247
- CascadingEntity::$ childTwo = new CascadedChild ();
231
+ $ entity ->requiredChild = new CascadedChild ();
232
+ $ entity ->children [] = new CascadedChild ();
233
+ $ entity ->children [] = null ;
234
+ CascadingEntity::$ staticChild = new CascadedChild ();
248
235
249
236
$ violations = $ this ->validator ->validate ($ entity );
250
237
251
- $ this ->assertCount (2 , $ violations );
238
+ $ this ->assertCount (3 , $ violations );
252
239
$ this ->assertInstanceOf (NotNull::class, $ violations ->get (0 )->getConstraint ());
253
240
$ this ->assertInstanceOf (NotNull::class, $ violations ->get (1 )->getConstraint ());
241
+ $ this ->assertInstanceOf (NotNull::class, $ violations ->get (2 )->getConstraint ());
242
+ $ this ->assertSame ('requiredChild.name ' , $ violations ->get (0 )->getPropertyPath ());
243
+ $ this ->assertSame ('staticChild.name ' , $ violations ->get (1 )->getPropertyPath ());
244
+ $ this ->assertSame ('children[0].name ' , $ violations ->get (2 )->getPropertyPath ());
254
245
255
- CascadingEntity::$ childTwo = null ;
246
+ CascadingEntity::$ staticChild = null ;
256
247
}
257
248
258
249
/**
@@ -268,15 +259,21 @@ public function testValidateWithExplicitCascade()
268
259
);
269
260
270
261
$ entity = new CascadingEntity ();
271
- $ entity ->childOne = new CascadedChild ();
272
- CascadingEntity::$ childTwo = new CascadedChild ();
262
+ $ entity ->requiredChild = new CascadedChild ();
263
+ $ entity ->children [] = new CascadedChild ();
264
+ $ entity ->children [] = null ;
265
+ CascadingEntity::$ staticChild = new CascadedChild ();
273
266
274
267
$ violations = $ this ->validator ->validate ($ entity );
275
268
276
- $ this ->assertCount (2 , $ violations );
269
+ $ this ->assertCount (3 , $ violations );
277
270
$ this ->assertInstanceOf (NotNull::class, $ violations ->get (0 )->getConstraint ());
278
271
$ this ->assertInstanceOf (NotNull::class, $ violations ->get (1 )->getConstraint ());
272
+ $ this ->assertInstanceOf (NotNull::class, $ violations ->get (2 )->getConstraint ());
273
+ $ this ->assertSame ('requiredChild.name ' , $ violations ->get (0 )->getPropertyPath ());
274
+ $ this ->assertSame ('staticChild.name ' , $ violations ->get (1 )->getPropertyPath ());
275
+ $ this ->assertSame ('children[0].name ' , $ violations ->get (2 )->getPropertyPath ());
279
276
280
- CascadingEntity::$ childTwo = null ;
277
+ CascadingEntity::$ staticChild = null ;
281
278
}
282
279
}
0 commit comments