@@ -99,4 +99,136 @@ public function testInvalidValueNamed()
99
99
->setCode (Unique::IS_NOT_UNIQUE )
100
100
->assertRaised ();
101
101
}
102
+
103
+ /**
104
+ * @dataProvider getCallback
105
+ */
106
+ public function testExpectsUniqueObjects ($ callback )
107
+ {
108
+ $ object1 = new \stdClass ();
109
+ $ object1 ->name = 'Foo ' ;
110
+ $ object1->
email =
'[email protected] ' ;
111
+
112
+ $ object2 = new \stdClass ();
113
+ $ object2 ->name = 'Foo ' ;
114
+ $ object2->
email =
'[email protected] ' ;
115
+
116
+ $ object3 = new \stdClass ();
117
+ $ object3 ->name = 'Bar ' ;
118
+ $ object3->
email =
'[email protected] ' ;
119
+
120
+ $ value = [$ object1 , $ object2 , $ object3 ];
121
+
122
+ $ this ->validator ->validate ($ value , new Unique ([
123
+ 'normalizer ' => $ callback ,
124
+ ]));
125
+
126
+ $ this ->assertNoViolation ();
127
+ }
128
+
129
+ /**
130
+ * @dataProvider getCallback
131
+ */
132
+ public function testExpectsNonUniqueObjects ($ callback )
133
+ {
134
+ $ object1 = new \stdClass ();
135
+ $ object1 ->name = 'Foo ' ;
136
+ $ object1->
email =
'[email protected] ' ;
137
+
138
+ $ object2 = new \stdClass ();
139
+ $ object2 ->name = 'Foo ' ;
140
+ $ object2->
email =
'[email protected] ' ;
141
+
142
+ $ object3 = new \stdClass ();
143
+ $ object3 ->name = 'Foo ' ;
144
+ $ object3->
email =
'[email protected] ' ;
145
+
146
+ $ value = [$ object1 , $ object2 , $ object3 ];
147
+
148
+ $ this ->validator ->validate ($ value , new Unique ([
149
+ 'message ' => 'myMessage ' ,
150
+ 'normalizer ' => $ callback ,
151
+ ]));
152
+
153
+ $ this ->buildViolation ('myMessage ' )
154
+ ->setParameter ('{{ value }} ' , 'array ' )
155
+ ->setCode (Unique::IS_NOT_UNIQUE )
156
+ ->assertRaised ();
157
+ }
158
+
159
+ public function getCallback ()
160
+ {
161
+ return [
162
+ yield 'static function ' => [static function (\stdClass $ object ) {
163
+ return [$ object ->name , $ object ->email ];
164
+ }],
165
+ yield 'callable with string notation ' => ['Symfony\Component\Validator\Tests\Constraints\CallableClass::execute ' ],
166
+ yield 'callable with static notation ' => [[CallableClass::class, 'execute ' ]],
167
+ yield 'callable with object ' => [[new CallableClass (), 'execute ' ]],
168
+ ];
169
+ }
170
+
171
+ public function testExpectsInvalidNonStrictComparison ()
172
+ {
173
+ $ this ->validator ->validate ([1 , '1 ' , 1.0 , '1.0 ' ], new Unique ([
174
+ 'message ' => 'myMessage ' ,
175
+ 'normalizer ' => 'intval ' ,
176
+ ]));
177
+
178
+ $ this ->buildViolation ('myMessage ' )
179
+ ->setParameter ('{{ value }} ' , 'array ' )
180
+ ->setCode (Unique::IS_NOT_UNIQUE )
181
+ ->assertRaised ();
182
+ }
183
+
184
+ public function testExpectsValidNonStrictComparison ()
185
+ {
186
+ $ callback = static function ($ item ) {
187
+ return (int ) $ item ;
188
+ };
189
+
190
+ $ this ->validator ->validate ([1 , '2 ' , 3 , '4.0 ' ], new Unique ([
191
+ 'normalizer ' => $ callback ,
192
+ ]));
193
+
194
+ $ this ->assertNoViolation ();
195
+ }
196
+
197
+ public function testExpectsInvalidCaseInsensitiveComparison ()
198
+ {
199
+ $ callback = static function ($ item ) {
200
+ return mb_strtolower ($ item );
201
+ };
202
+
203
+ $ this ->validator ->validate (['Hello ' , 'hello ' , 'HELLO ' , 'hellO ' ], new Unique ([
204
+ 'message ' => 'myMessage ' ,
205
+ 'normalizer ' => $ callback ,
206
+ ]));
207
+
208
+ $ this ->buildViolation ('myMessage ' )
209
+ ->setParameter ('{{ value }} ' , 'array ' )
210
+ ->setCode (Unique::IS_NOT_UNIQUE )
211
+ ->assertRaised ();
212
+ }
213
+
214
+ public function testExpectsValidCaseInsensitiveComparison ()
215
+ {
216
+ $ callback = static function ($ item ) {
217
+ return mb_strtolower ($ item );
218
+ };
219
+
220
+ $ this ->validator ->validate (['Hello ' , 'World ' ], new Unique ([
221
+ 'normalizer ' => $ callback ,
222
+ ]));
223
+
224
+ $ this ->assertNoViolation ();
225
+ }
226
+ }
227
+
228
+ class CallableClass
229
+ {
230
+ public static function execute (\stdClass $ object )
231
+ {
232
+ return [$ object ->name , $ object ->email ];
233
+ }
102
234
}
0 commit comments