1313
1414use Symfony \Component \Validator \Constraints \Bic ;
1515use Symfony \Component \Validator \Constraints \BicValidator ;
16+ use Symfony \Component \Validator \Exception \ConstraintDefinitionException ;
1617use Symfony \Component \Validator \Test \ConstraintValidatorTestCase ;
1718
19+ class BicComparisonTest_Class
20+ {
21+ protected $ value ;
22+
23+ public function __construct ($ value )
24+ {
25+ $ this ->value = $ value ;
26+ }
27+
28+ public function __toString ()
29+ {
30+ return (string ) $ this ->value ;
31+ }
32+
33+ public function getValue ()
34+ {
35+ return $ this ->value ;
36+ }
37+ }
38+
1839class BicValidatorTest extends ConstraintValidatorTestCase
1940{
2041 protected function createValidator ()
@@ -36,6 +57,113 @@ public function testEmptyStringIsValid()
3657 $ this ->assertNoViolation ();
3758 }
3859
60+ public function testValidComparisonToPropertyPath ()
61+ {
62+ $ constraint = new Bic (array ('ibanPropertyPath ' => 'value ' ));
63+
64+ $ object = new BicComparisonTest_Class ('FR14 2004 1010 0505 0001 3M02 606 ' );
65+
66+ $ this ->setObject ($ object );
67+
68+ $ this ->validator ->validate ('SOGEFRPP ' , $ constraint );
69+
70+ $ this ->assertNoViolation ();
71+ }
72+
73+ public function testValidComparisonToPropertyPathOnArray ()
74+ {
75+ $ constraint = new Bic (array ('ibanPropertyPath ' => '[root][value] ' ));
76+
77+ $ this ->setObject (array ('root ' => array ('value ' => 'FR14 2004 1010 0505 0001 3M02 606 ' )));
78+
79+ $ this ->validator ->validate ('SOGEFRPP ' , $ constraint );
80+
81+ $ this ->assertNoViolation ();
82+ }
83+
84+ public function testInvalidComparisonToPropertyPath ()
85+ {
86+ $ constraint = new Bic (array ('ibanPropertyPath ' => 'value ' ));
87+ $ constraint ->ibanMessage = 'Constraint Message ' ;
88+
89+ $ object = new BicComparisonTest_Class ('FR14 2004 1010 0505 0001 3M02 606 ' );
90+
91+ $ this ->setObject ($ object );
92+
93+ $ this ->validator ->validate ('UNCRIT2B912 ' , $ constraint );
94+
95+ $ this ->buildViolation ('Constraint Message ' )
96+ ->setParameter ('{{ value }} ' , '"UNCRIT2B912" ' )
97+ ->setParameter ('{{ iban }} ' , 'FR14 2004 1010 0505 0001 3M02 606 ' )
98+ ->setCode (Bic::INVALID_IBAN_COUNTRY_CODE_ERROR )
99+ ->assertRaised ();
100+ }
101+
102+ public function testValidComparisonToValue ()
103+ {
104+ $ constraint = new Bic (array ('iban ' => 'FR14 2004 1010 0505 0001 3M02 606 ' ));
105+ $ constraint ->ibanMessage = 'Constraint Message ' ;
106+
107+ $ this ->validator ->validate ('SOGEFRPP ' , $ constraint );
108+
109+ $ this ->assertNoViolation ();
110+ }
111+
112+ public function testInvalidComparisonToValue ()
113+ {
114+ $ constraint = new Bic (array ('iban ' => 'FR14 2004 1010 0505 0001 3M02 606 ' ));
115+ $ constraint ->ibanMessage = 'Constraint Message ' ;
116+
117+ $ this ->validator ->validate ('UNCRIT2B912 ' , $ constraint );
118+
119+ $ this ->buildViolation ('Constraint Message ' )
120+ ->setParameter ('{{ value }} ' , '"UNCRIT2B912" ' )
121+ ->setParameter ('{{ iban }} ' , 'FR14 2004 1010 0505 0001 3M02 606 ' )
122+ ->setCode (Bic::INVALID_IBAN_COUNTRY_CODE_ERROR )
123+ ->assertRaised ();
124+ }
125+
126+ public function testNoViolationOnNullObjectWithPropertyPath ()
127+ {
128+ $ constraint = new Bic (array ('ibanPropertyPath ' => 'propertyPath ' ));
129+
130+ $ this ->setObject (null );
131+
132+ $ this ->validator ->validate ('UNCRIT2B912 ' , $ constraint );
133+
134+ $ this ->assertNoViolation ();
135+ }
136+
137+ /**
138+ * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
139+ * @expectedExceptionMessage requires only one of the "iban" or "ibanPropertyPath" options to be set, not both.
140+ */
141+ public function testThrowsConstraintExceptionIfBothValueAndPropertyPath ()
142+ {
143+ new Bic (array (
144+ 'iban ' => 'value ' ,
145+ 'ibanPropertyPath ' => 'propertyPath ' ,
146+ ));
147+ }
148+
149+ public function testInvalidValuePath ()
150+ {
151+ $ constraint = new Bic (array ('ibanPropertyPath ' => 'foo ' ));
152+
153+ if (method_exists ($ this , 'expectException ' )) {
154+ $ this ->expectException (ConstraintDefinitionException::class);
155+ $ this ->expectExceptionMessage (sprintf ('Invalid property path "foo" provided to "%s" constraint ' , \get_class ($ constraint )));
156+ } else {
157+ $ this ->setExpectedException (ConstraintDefinitionException::class, sprintf ('Invalid property path "foo" provided to "%s" constraint ' , \get_class ($ constraint )));
158+ }
159+
160+ $ object = new BicComparisonTest_Class (5 );
161+
162+ $ this ->setObject ($ object );
163+
164+ $ this ->validator ->validate ('UNCRIT2B912 ' , $ constraint );
165+ }
166+
39167 /**
40168 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
41169 */
0 commit comments