@@ -328,18 +328,208 @@ public function testTraverseTraversableByDefault()
328328 $ this ->assertNull ($ violations [0 ]->getCode ());
329329 }
330330
331+ public function testTraversalEnabledOnClass ()
332+ {
333+ $ entity = new Entity ();
334+ $ traversable = new \ArrayIterator (array ('key ' => $ entity ));
335+
336+ $ callback = function ($ value , ExecutionContextInterface $ context ) {
337+ $ context ->addViolation ('Message ' );
338+ };
339+
340+ $ traversableMetadata = new ClassMetadata ('ArrayIterator ' );
341+ $ traversableMetadata ->addConstraint (new Traverse (true ));
342+
343+ $ this ->metadataFactory ->addMetadata ($ traversableMetadata );
344+ $ this ->metadata ->addConstraint (new Callback (array (
345+ 'callback ' => $ callback ,
346+ 'groups ' => 'Group ' ,
347+ )));
348+
349+ $ violations = $ this ->validate ($ traversable , new Valid (), 'Group ' );
350+
351+ /** @var ConstraintViolationInterface[] $violations */
352+ $ this ->assertCount (1 , $ violations );
353+ }
354+
355+ public function testTraversalDisabledOnClass ()
356+ {
357+ $ test = $ this ;
358+ $ entity = new Entity ();
359+ $ traversable = new \ArrayIterator (array ('key ' => $ entity ));
360+
361+ $ callback = function ($ value , ExecutionContextInterface $ context ) use ($ test ) {
362+ $ test ->fail ('Should not be called ' );
363+ };
364+
365+ $ traversableMetadata = new ClassMetadata ('ArrayIterator ' );
366+ $ traversableMetadata ->addConstraint (new Traverse (false ));
367+
368+ $ this ->metadataFactory ->addMetadata ($ traversableMetadata );
369+ $ this ->metadata ->addConstraint (new Callback (array (
370+ 'callback ' => $ callback ,
371+ 'groups ' => 'Group ' ,
372+ )));
373+
374+ $ violations = $ this ->validate ($ traversable , new Valid (), 'Group ' );
375+
376+ /** @var ConstraintViolationInterface[] $violations */
377+ $ this ->assertCount (0 , $ violations );
378+ }
379+
331380 /**
332381 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
333382 */
334- public function testExpectTraversableIfTraverseOnClass ()
383+ public function testExpectTraversableIfTraversalEnabledOnClass ()
335384 {
336385 $ entity = new Entity ();
337386
338- $ this ->metadata ->addConstraint (new Traverse ());
387+ $ this ->metadata ->addConstraint (new Traverse (true ));
339388
340389 $ this ->validator ->validate ($ entity );
341390 }
342391
392+ public function testReferenceTraversalDisabledOnClass ()
393+ {
394+ $ test = $ this ;
395+ $ entity = new Entity ();
396+ $ entity ->reference = new \ArrayIterator (array ('key ' => new Reference ()));
397+
398+ $ callback = function ($ value , ExecutionContextInterface $ context ) use ($ test ) {
399+ $ test ->fail ('Should not be called ' );
400+ };
401+
402+ $ traversableMetadata = new ClassMetadata ('ArrayIterator ' );
403+ $ traversableMetadata ->addConstraint (new Traverse (false ));
404+
405+ $ this ->metadataFactory ->addMetadata ($ traversableMetadata );
406+ $ this ->referenceMetadata ->addConstraint (new Callback (array (
407+ 'callback ' => $ callback ,
408+ 'groups ' => 'Group ' ,
409+ )));
410+ $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid ());
411+
412+ $ violations = $ this ->validate ($ entity , new Valid (), 'Group ' );
413+
414+ /** @var ConstraintViolationInterface[] $violations */
415+ $ this ->assertCount (0 , $ violations );
416+ }
417+
418+ public function testReferenceTraversalEnabledOnReferenceDisabledOnClass ()
419+ {
420+ $ test = $ this ;
421+ $ entity = new Entity ();
422+ $ entity ->reference = new \ArrayIterator (array ('key ' => new Reference ()));
423+
424+ $ callback = function ($ value , ExecutionContextInterface $ context ) use ($ test ) {
425+ $ test ->fail ('Should not be called ' );
426+ };
427+
428+ $ traversableMetadata = new ClassMetadata ('ArrayIterator ' );
429+ $ traversableMetadata ->addConstraint (new Traverse (false ));
430+
431+ $ this ->metadataFactory ->addMetadata ($ traversableMetadata );
432+ $ this ->referenceMetadata ->addConstraint (new Callback (array (
433+ 'callback ' => $ callback ,
434+ 'groups ' => 'Group ' ,
435+ )));
436+ $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid (array (
437+ 'traverse ' => true ,
438+ )));
439+
440+ $ violations = $ this ->validate ($ entity , new Valid (), 'Group ' );
441+
442+ /** @var ConstraintViolationInterface[] $violations */
443+ $ this ->assertCount (0 , $ violations );
444+ }
445+
446+ public function testReferenceTraversalDisabledOnReferenceEnabledOnClass ()
447+ {
448+ $ test = $ this ;
449+ $ entity = new Entity ();
450+ $ entity ->reference = new \ArrayIterator (array ('key ' => new Reference ()));
451+
452+ $ callback = function ($ value , ExecutionContextInterface $ context ) use ($ test ) {
453+ $ test ->fail ('Should not be called ' );
454+ };
455+
456+ $ traversableMetadata = new ClassMetadata ('ArrayIterator ' );
457+ $ traversableMetadata ->addConstraint (new Traverse (true ));
458+
459+ $ this ->metadataFactory ->addMetadata ($ traversableMetadata );
460+ $ this ->referenceMetadata ->addConstraint (new Callback (array (
461+ 'callback ' => $ callback ,
462+ 'groups ' => 'Group ' ,
463+ )));
464+ $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid (array (
465+ 'traverse ' => false ,
466+ )));
467+
468+ $ violations = $ this ->validate ($ entity , new Valid (), 'Group ' );
469+
470+ /** @var ConstraintViolationInterface[] $violations */
471+ $ this ->assertCount (0 , $ violations );
472+ }
473+
474+ public function testReferenceTraversalRecursionEnabledOnReferenceTraversalEnabledOnClass ()
475+ {
476+ $ entity = new Entity ();
477+ $ entity ->reference = new \ArrayIterator (array (
478+ 2 => new \ArrayIterator (array ('key ' => new Reference ())),
479+ ));
480+
481+ $ callback = function ($ value , ExecutionContextInterface $ context ) {
482+ $ context ->addViolation ('Message ' );
483+ };
484+
485+ $ traversableMetadata = new ClassMetadata ('ArrayIterator ' );
486+ $ traversableMetadata ->addConstraint (new Traverse (true ));
487+
488+ $ this ->metadataFactory ->addMetadata ($ traversableMetadata );
489+ $ this ->referenceMetadata ->addConstraint (new Callback (array (
490+ 'callback ' => $ callback ,
491+ 'groups ' => 'Group ' ,
492+ )));
493+ $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid (array (
494+ 'deep ' => true ,
495+ )));
496+
497+ $ violations = $ this ->validate ($ entity , new Valid (), 'Group ' );
498+
499+ /** @var ConstraintViolationInterface[] $violations */
500+ $ this ->assertCount (1 , $ violations );
501+ }
502+
503+ public function testReferenceTraversalRecursionDisabledOnReferenceTraversalEnabledOnClass ()
504+ {
505+ $ test = $ this ;
506+ $ entity = new Entity ();
507+ $ entity ->reference = new \ArrayIterator (array (
508+ 2 => new \ArrayIterator (array ('key ' => new Reference ())),
509+ ));
510+
511+ $ callback = function ($ value , ExecutionContextInterface $ context ) use ($ test ) {
512+ $ test ->fail ('Should not be called ' );
513+ };
514+
515+ $ traversableMetadata = new ClassMetadata ('ArrayIterator ' );
516+ $ traversableMetadata ->addConstraint (new Traverse (true ));
517+
518+ $ this ->metadataFactory ->addMetadata ($ traversableMetadata );
519+ $ this ->referenceMetadata ->addConstraint (new Callback (array (
520+ 'callback ' => $ callback ,
521+ 'groups ' => 'Group ' ,
522+ )));
523+ $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid (array (
524+ 'deep ' => false ,
525+ )));
526+
527+ $ violations = $ this ->validate ($ entity , new Valid (), 'Group ' );
528+
529+ /** @var ConstraintViolationInterface[] $violations */
530+ $ this ->assertCount (0 , $ violations );
531+ }
532+
343533 public function testAddCustomizedViolation ()
344534 {
345535 $ entity = new Entity ();
0 commit comments