@@ -20,13 +20,17 @@ class ImageValidatorTest extends \PHPUnit_Framework_TestCase
20
20
protected $ validator ;
21
21
protected $ path ;
22
22
protected $ image ;
23
+ protected $ imageLandscape ;
24
+ protected $ imagePortrait ;
23
25
24
26
protected function setUp ()
25
27
{
26
28
$ this ->context = $ this ->getMock ('Symfony\Component\Validator\ExecutionContext ' , array (), array (), '' , false );
27
29
$ this ->validator = new ImageValidator ();
28
30
$ this ->validator ->initialize ($ this ->context );
29
31
$ this ->image = __DIR__ .'/Fixtures/test.gif ' ;
32
+ $ this ->imageLandscape = __DIR__ .'/Fixtures/test_landscape.gif ' ;
33
+ $ this ->imagePortrait = __DIR__ .'/Fixtures/test_portrait.gif ' ;
30
34
}
31
35
32
36
public function testNullIsValid ()
@@ -223,4 +227,141 @@ public function testInvalidMaxHeight()
223
227
224
228
$ this ->validator ->validate ($ this ->image , $ constraint );
225
229
}
230
+
231
+ public function testRatioTooSmall ()
232
+ {
233
+ if (!class_exists ('Symfony\Component\HttpFoundation\File\File ' )) {
234
+ $ this ->markTestSkipped ('The "HttpFoundation" component is not available ' );
235
+ }
236
+
237
+ $ constraint = new Image (array (
238
+ 'minRatio ' => 2 ,
239
+ 'minRatioMessage ' => 'myMessage ' ,
240
+ ));
241
+
242
+ $ this ->context ->expects ($ this ->once ())
243
+ ->method ('addViolation ' )
244
+ ->with ('myMessage ' , array (
245
+ '{{ ratio }} ' => 1 ,
246
+ '{{ min_ratio }} ' => 2 ,
247
+ ));
248
+
249
+ $ this ->validator ->validate ($ this ->image , $ constraint );
250
+ }
251
+
252
+ public function testRatioTooBig ()
253
+ {
254
+ if (!class_exists ('Symfony\Component\HttpFoundation\File\File ' )) {
255
+ $ this ->markTestSkipped ('The "HttpFoundation" component is not available ' );
256
+ }
257
+
258
+ $ constraint = new Image (array (
259
+ 'maxRatio ' => 0.5 ,
260
+ 'maxRatioMessage ' => 'myMessage ' ,
261
+ ));
262
+
263
+ $ this ->context ->expects ($ this ->once ())
264
+ ->method ('addViolation ' )
265
+ ->with ('myMessage ' , array (
266
+ '{{ ratio }} ' => 1 ,
267
+ '{{ max_ratio }} ' => 0.5 ,
268
+ ));
269
+
270
+ $ this ->validator ->validate ($ this ->image , $ constraint );
271
+ }
272
+
273
+ /**
274
+ * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
275
+ */
276
+ public function testInvalidMinRatio ()
277
+ {
278
+ if (!class_exists ('Symfony\Component\HttpFoundation\File\File ' )) {
279
+ $ this ->markTestSkipped ('The "HttpFoundation" component is not available ' );
280
+ }
281
+
282
+ $ constraint = new Image (array (
283
+ 'minRatio ' => '1abc ' ,
284
+ ));
285
+
286
+ $ this ->validator ->validate ($ this ->image , $ constraint );
287
+ }
288
+
289
+ /**
290
+ * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
291
+ */
292
+ public function testInvalidMaxRatio ()
293
+ {
294
+ if (!class_exists ('Symfony\Component\HttpFoundation\File\File ' )) {
295
+ $ this ->markTestSkipped ('The "HttpFoundation" component is not available ' );
296
+ }
297
+
298
+ $ constraint = new Image (array (
299
+ 'maxRatio ' => '1abc ' ,
300
+ ));
301
+
302
+ $ this ->validator ->validate ($ this ->image , $ constraint );
303
+ }
304
+
305
+ public function testSquareNotAllowed ()
306
+ {
307
+ if (!class_exists ('Symfony\Component\HttpFoundation\File\File ' )) {
308
+ $ this ->markTestSkipped ('The "HttpFoundation" component is not available ' );
309
+ }
310
+
311
+ $ constraint = new Image (array (
312
+ 'allowSquare ' => false ,
313
+ 'allowSquareMessage ' => 'myMessage ' ,
314
+ ));
315
+
316
+ $ this ->context ->expects ($ this ->once ())
317
+ ->method ('addViolation ' )
318
+ ->with ('myMessage ' , array (
319
+ '{{ width }} ' => 2 ,
320
+ '{{ height }} ' => 2 ,
321
+ ));
322
+
323
+ $ this ->validator ->validate ($ this ->image , $ constraint );
324
+ }
325
+
326
+ public function testLandscapeNotAllowed ()
327
+ {
328
+ if (!class_exists ('Symfony\Component\HttpFoundation\File\File ' )) {
329
+ $ this ->markTestSkipped ('The "HttpFoundation" component is not available ' );
330
+ }
331
+
332
+ $ constraint = new Image (array (
333
+ 'allowLandscape ' => false ,
334
+ 'allowLandscapeMessage ' => 'myMessage ' ,
335
+ ));
336
+
337
+ $ this ->context ->expects ($ this ->once ())
338
+ ->method ('addViolation ' )
339
+ ->with ('myMessage ' , array (
340
+ '{{ width }} ' => 2 ,
341
+ '{{ height }} ' => 1 ,
342
+ ));
343
+
344
+ $ this ->validator ->validate ($ this ->imageLandscape , $ constraint );
345
+ }
346
+
347
+ public function testPortraitNotAllowed ()
348
+ {
349
+ if (!class_exists ('Symfony\Component\HttpFoundation\File\File ' )) {
350
+ $ this ->markTestSkipped ('The "HttpFoundation" component is not available ' );
351
+ }
352
+
353
+ $ constraint = new Image (array (
354
+ 'allowPortrait ' => false ,
355
+ 'allowPortraitMessage ' => 'myMessage ' ,
356
+ ));
357
+
358
+ $ this ->context ->expects ($ this ->once ())
359
+ ->method ('addViolation ' )
360
+ ->with ('myMessage ' , array (
361
+ '{{ width }} ' => 1 ,
362
+ '{{ height }} ' => 2 ,
363
+ ));
364
+
365
+ $ this ->validator ->validate ($ this ->imagePortrait , $ constraint );
366
+ }
226
367
}
0 commit comments