@@ -32,6 +32,8 @@ abstract class AbstractValidatorTest extends TestCase
32
32
33
33
const REFERENCE_CLASS = 'Symfony\Component\Validator\Tests\Fixtures\Reference ' ;
34
34
35
+ const LAZY_PROPERTY = 'Symfony\Component\Validator\Validator\LazyProperty ' ;
36
+
35
37
/**
36
38
* @var FakeMetadataFactory
37
39
*/
@@ -54,6 +56,7 @@ protected function setUp(): void
54
56
$ this ->referenceMetadata = new ClassMetadata (self ::REFERENCE_CLASS );
55
57
$ this ->metadataFactory ->addMetadata ($ this ->metadata );
56
58
$ this ->metadataFactory ->addMetadata ($ this ->referenceMetadata );
59
+ $ this ->metadataFactory ->addMetadata (new ClassMetadata (self ::LAZY_PROPERTY ));
57
60
}
58
61
59
62
protected function tearDown (): void
@@ -510,7 +513,10 @@ public function testFailOnScalarReferences()
510
513
$ this ->validate ($ entity );
511
514
}
512
515
513
- public function testArrayReference ()
516
+ /**
517
+ * @dataProvider getConstraintMethods
518
+ */
519
+ public function testArrayReference ($ constraintMethod )
514
520
{
515
521
$ entity = new Entity ();
516
522
$ entity ->reference = ['key ' => new Reference ()];
@@ -528,7 +534,7 @@ public function testArrayReference()
528
534
$ context ->addViolation ('Message %param% ' , ['%param% ' => 'value ' ]);
529
535
};
530
536
531
- $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid ());
537
+ $ this ->metadata ->$ constraintMethod ('reference ' , new Valid ());
532
538
$ this ->referenceMetadata ->addConstraint (new Callback ([
533
539
'callback ' => $ callback ,
534
540
'groups ' => 'Group ' ,
@@ -548,8 +554,10 @@ public function testArrayReference()
548
554
$ this ->assertNull ($ violations [0 ]->getCode ());
549
555
}
550
556
551
- // https://github.com/symfony/symfony/issues/6246
552
- public function testRecursiveArrayReference ()
557
+ /**
558
+ * @dataProvider getConstraintMethods
559
+ */
560
+ public function testRecursiveArrayReference ($ constraintMethod )
553
561
{
554
562
$ entity = new Entity ();
555
563
$ entity ->reference = [2 => ['key ' => new Reference ()]];
@@ -567,7 +575,7 @@ public function testRecursiveArrayReference()
567
575
$ context ->addViolation ('Message %param% ' , ['%param% ' => 'value ' ]);
568
576
};
569
577
570
- $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid ());
578
+ $ this ->metadata ->$ constraintMethod ('reference ' , new Valid ());
571
579
$ this ->referenceMetadata ->addConstraint (new Callback ([
572
580
'callback ' => $ callback ,
573
581
'groups ' => 'Group ' ,
@@ -611,7 +619,10 @@ public function testOnlyCascadedArraysAreTraversed()
611
619
$ this ->assertCount (0 , $ violations );
612
620
}
613
621
614
- public function testArrayTraversalCannotBeDisabled ()
622
+ /**
623
+ * @dataProvider getConstraintMethods
624
+ */
625
+ public function testArrayTraversalCannotBeDisabled ($ constraintMethod )
615
626
{
616
627
$ entity = new Entity ();
617
628
$ entity ->reference = ['key ' => new Reference ()];
@@ -620,7 +631,7 @@ public function testArrayTraversalCannotBeDisabled()
620
631
$ context ->addViolation ('Message %param% ' , ['%param% ' => 'value ' ]);
621
632
};
622
633
623
- $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid ([
634
+ $ this ->metadata ->$ constraintMethod ('reference ' , new Valid ([
624
635
'traverse ' => false ,
625
636
]));
626
637
$ this ->referenceMetadata ->addConstraint (new Callback ($ callback ));
@@ -631,7 +642,10 @@ public function testArrayTraversalCannotBeDisabled()
631
642
$ this ->assertCount (1 , $ violations );
632
643
}
633
644
634
- public function testRecursiveArrayTraversalCannotBeDisabled ()
645
+ /**
646
+ * @dataProvider getConstraintMethods
647
+ */
648
+ public function testRecursiveArrayTraversalCannotBeDisabled ($ constraintMethod )
635
649
{
636
650
$ entity = new Entity ();
637
651
$ entity ->reference = [2 => ['key ' => new Reference ()]];
@@ -640,9 +654,10 @@ public function testRecursiveArrayTraversalCannotBeDisabled()
640
654
$ context ->addViolation ('Message %param% ' , ['%param% ' => 'value ' ]);
641
655
};
642
656
643
- $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid ([
657
+ $ this ->metadata ->$ constraintMethod ('reference ' , new Valid ([
644
658
'traverse ' => false ,
645
659
]));
660
+
646
661
$ this ->referenceMetadata ->addConstraint (new Callback ($ callback ));
647
662
648
663
$ violations = $ this ->validate ($ entity );
@@ -651,25 +666,31 @@ public function testRecursiveArrayTraversalCannotBeDisabled()
651
666
$ this ->assertCount (1 , $ violations );
652
667
}
653
668
654
- public function testIgnoreScalarsDuringArrayTraversal ()
669
+ /**
670
+ * @dataProvider getConstraintMethods
671
+ */
672
+ public function testIgnoreScalarsDuringArrayTraversal ($ constraintMethod )
655
673
{
656
674
$ entity = new Entity ();
657
675
$ entity ->reference = ['string ' , 1234 ];
658
676
659
- $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid ());
677
+ $ this ->metadata ->$ constraintMethod ('reference ' , new Valid ());
660
678
661
679
$ violations = $ this ->validate ($ entity );
662
680
663
681
/* @var ConstraintViolationInterface[] $violations */
664
682
$ this ->assertCount (0 , $ violations );
665
683
}
666
684
667
- public function testIgnoreNullDuringArrayTraversal ()
685
+ /**
686
+ * @dataProvider getConstraintMethods
687
+ */
688
+ public function testIgnoreNullDuringArrayTraversal ($ constraintMethod )
668
689
{
669
690
$ entity = new Entity ();
670
691
$ entity ->reference = [null ];
671
692
672
- $ this ->metadata ->addPropertyConstraint ('reference ' , new Valid ());
693
+ $ this ->metadata ->$ constraintMethod ('reference ' , new Valid ());
673
694
674
695
$ violations = $ this ->validate ($ entity );
675
696
@@ -1218,6 +1239,14 @@ public function testReplaceDefaultGroup($sequence, array $assertViolations)
1218
1239
}
1219
1240
}
1220
1241
1242
+ public function getConstraintMethods ()
1243
+ {
1244
+ return [
1245
+ ['addPropertyConstraint ' ],
1246
+ ['addGetterConstraint ' ],
1247
+ ];
1248
+ }
1249
+
1221
1250
public function getTestReplaceDefaultGroup ()
1222
1251
{
1223
1252
return [
0 commit comments