15
15
use Symfony \Component \Translation \IdentityTranslator ;
16
16
use Symfony \Component \Validator \Constraints \Valid ;
17
17
use Symfony \Component \Validator \ConstraintViolation ;
18
+ use Symfony \Component \Validator \ConstraintViolationInterface ;
18
19
use Symfony \Component \Validator \ConstraintViolationList ;
20
+ use Symfony \Component \Validator \Util \PropertyPath ;
19
21
use Symfony \Component \Validator \Violation \ConstraintViolationBuilder ;
20
22
use Symfony \Contracts \Translation \TranslatorInterface ;
21
23
@@ -42,7 +44,7 @@ public function testAddViolation()
42
44
{
43
45
$ this ->builder ->addViolation ();
44
46
45
- $ this ->assertViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'data ' , 'foo ' , null , null , new Valid ()));
47
+ $ this ->assertBuiltViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'data ' , 'foo ' , null , null , new Valid ()));
46
48
}
47
49
48
50
public function testAppendPropertyPath ()
@@ -51,7 +53,7 @@ public function testAppendPropertyPath()
51
53
->atPath ('foo ' )
52
54
->addViolation ();
53
55
54
- $ this ->assertViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'data.foo ' , 'foo ' , null , null , new Valid ()));
56
+ $ this ->assertBuiltViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'data.foo ' , 'foo ' , null , null , new Valid ()));
55
57
}
56
58
57
59
public function testAppendMultiplePropertyPaths ()
@@ -61,7 +63,7 @@ public function testAppendMultiplePropertyPaths()
61
63
->atPath ('bar ' )
62
64
->addViolation ();
63
65
64
- $ this ->assertViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'data.foo.bar ' , 'foo ' , null , null , new Valid ()));
66
+ $ this ->assertBuiltViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'data.foo.bar ' , 'foo ' , null , null , new Valid ()));
65
67
}
66
68
67
69
public function testCodeCanBeSet ()
@@ -70,7 +72,7 @@ public function testCodeCanBeSet()
70
72
->setCode ('5 ' )
71
73
->addViolation ();
72
74
73
- $ this ->assertViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'data ' , 'foo ' , null , '5 ' , new Valid ()));
75
+ $ this ->assertBuiltViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'data ' , 'foo ' , null , '5 ' , new Valid ()));
74
76
}
75
77
76
78
public function testCauseCanBeSet ()
@@ -81,7 +83,7 @@ public function testCauseCanBeSet()
81
83
->setCause ($ cause )
82
84
->addViolation ();
83
85
84
- $ this ->assertViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'data ' , 'foo ' , null , null , new Valid (), $ cause ));
86
+ $ this ->assertBuiltViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'data ' , 'foo ' , null , null , new Valid (), $ cause ));
85
87
}
86
88
87
89
public function testTranslationDomainFalse ()
@@ -96,12 +98,31 @@ public function testTranslationDomainFalse()
96
98
$ builder ->addViolation ();
97
99
}
98
100
99
- private function assertViolationEquals (ConstraintViolation $ expectedViolation )
101
+ public function testBuildViolationFromExistingViolation ()
102
+ {
103
+ $ originalViolation = $ this ->builder ->getViolation ();
104
+
105
+ $ violation = ConstraintViolationBuilder::fromViolation ($ originalViolation )
106
+ ->setPath (PropertyPath::append ('top ' , $ originalViolation ->getPropertyPath ()))
107
+ ->setCause ($ cause = new \LogicException ())
108
+ ->getViolation ();
109
+
110
+ $ this ->assertCount (0 , $ this ->violations );
111
+
112
+ $ this ->assertViolationEquals (new ConstraintViolation ($ this ->messageTemplate , $ this ->messageTemplate , [], $ this ->root , 'top.data ' , 'foo ' , null , null , new Valid (), $ cause ), $ violation );
113
+ }
114
+
115
+ private function assertBuiltViolationEquals (ConstraintViolation $ expectedViolation ): void
100
116
{
101
117
$ this ->assertCount (1 , $ this ->violations );
102
118
103
119
$ violation = $ this ->violations ->get (0 );
104
120
121
+ $ this ->assertViolationEquals ($ expectedViolation , $ violation );
122
+ }
123
+
124
+ private function assertViolationEquals (ConstraintViolation $ expectedViolation , ConstraintViolationInterface $ violation ): void
125
+ {
105
126
$ this ->assertSame ($ expectedViolation ->getMessage (), $ violation ->getMessage ());
106
127
$ this ->assertSame ($ expectedViolation ->getMessageTemplate (), $ violation ->getMessageTemplate ());
107
128
$ this ->assertSame ($ expectedViolation ->getParameters (), $ violation ->getParameters ());
0 commit comments