4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
14 use Symfony\Component\Validator\Constraints\Image;
15 use Symfony\Component\Validator\Constraints\ImageValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
19 * @requires extension fileinfo
21 class ImageValidatorTest extends ConstraintValidatorTestCase
32 protected $imageLandscape;
33 protected $imagePortrait;
35 protected $imageCorrupted;
37 protected function createValidator()
39 return new ImageValidator();
42 protected function setUp()
46 $this->image = __DIR__.'/Fixtures/test.gif';
47 $this->imageLandscape = __DIR__.'/Fixtures/test_landscape.gif';
48 $this->imagePortrait = __DIR__.'/Fixtures/test_portrait.gif';
49 $this->image4By3 = __DIR__.'/Fixtures/test_4by3.gif';
50 $this->imageCorrupted = __DIR__.'/Fixtures/test_corrupted.gif';
53 public function testNullIsValid()
55 $this->validator->validate(null, new Image());
57 $this->assertNoViolation();
60 public function testEmptyStringIsValid()
62 $this->validator->validate('', new Image());
64 $this->assertNoViolation();
67 public function testValidImage()
69 $this->validator->validate($this->image, new Image());
71 $this->assertNoViolation();
74 public function testFileNotFound()
76 // Check that the logic from FileValidator still works
77 $constraint = new Image(array(
78 'notFoundMessage' => 'myMessage',
81 $this->validator->validate('foobar', $constraint);
83 $this->buildViolation('myMessage')
84 ->setParameter('{{ file }}', '"foobar"')
85 ->setCode(Image::NOT_FOUND_ERROR)
89 public function testValidSize()
91 $constraint = new Image(array(
98 $this->validator->validate($this->image, $constraint);
100 $this->assertNoViolation();
103 public function testWidthTooSmall()
105 $constraint = new Image(array(
107 'minWidthMessage' => 'myMessage',
110 $this->validator->validate($this->image, $constraint);
112 $this->buildViolation('myMessage')
113 ->setParameter('{{ width }}', '2')
114 ->setParameter('{{ min_width }}', '3')
115 ->setCode(Image::TOO_NARROW_ERROR)
119 public function testWidthTooBig()
121 $constraint = new Image(array(
123 'maxWidthMessage' => 'myMessage',
126 $this->validator->validate($this->image, $constraint);
128 $this->buildViolation('myMessage')
129 ->setParameter('{{ width }}', '2')
130 ->setParameter('{{ max_width }}', '1')
131 ->setCode(Image::TOO_WIDE_ERROR)
135 public function testHeightTooSmall()
137 $constraint = new Image(array(
139 'minHeightMessage' => 'myMessage',
142 $this->validator->validate($this->image, $constraint);
144 $this->buildViolation('myMessage')
145 ->setParameter('{{ height }}', '2')
146 ->setParameter('{{ min_height }}', '3')
147 ->setCode(Image::TOO_LOW_ERROR)
151 public function testHeightTooBig()
153 $constraint = new Image(array(
155 'maxHeightMessage' => 'myMessage',
158 $this->validator->validate($this->image, $constraint);
160 $this->buildViolation('myMessage')
161 ->setParameter('{{ height }}', '2')
162 ->setParameter('{{ max_height }}', '1')
163 ->setCode(Image::TOO_HIGH_ERROR)
168 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
170 public function testInvalidMinWidth()
172 $constraint = new Image(array(
173 'minWidth' => '1abc',
176 $this->validator->validate($this->image, $constraint);
180 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
182 public function testInvalidMaxWidth()
184 $constraint = new Image(array(
185 'maxWidth' => '1abc',
188 $this->validator->validate($this->image, $constraint);
192 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
194 public function testInvalidMinHeight()
196 $constraint = new Image(array(
197 'minHeight' => '1abc',
200 $this->validator->validate($this->image, $constraint);
204 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
206 public function testInvalidMaxHeight()
208 $constraint = new Image(array(
209 'maxHeight' => '1abc',
212 $this->validator->validate($this->image, $constraint);
215 public function testRatioTooSmall()
217 $constraint = new Image(array(
219 'minRatioMessage' => 'myMessage',
222 $this->validator->validate($this->image, $constraint);
224 $this->buildViolation('myMessage')
225 ->setParameter('{{ ratio }}', 1)
226 ->setParameter('{{ min_ratio }}', 2)
227 ->setCode(Image::RATIO_TOO_SMALL_ERROR)
231 public function testRatioTooBig()
233 $constraint = new Image(array(
235 'maxRatioMessage' => 'myMessage',
238 $this->validator->validate($this->image, $constraint);
240 $this->buildViolation('myMessage')
241 ->setParameter('{{ ratio }}', 1)
242 ->setParameter('{{ max_ratio }}', 0.5)
243 ->setCode(Image::RATIO_TOO_BIG_ERROR)
247 public function testMaxRatioUsesTwoDecimalsOnly()
249 $constraint = new Image(array(
253 $this->validator->validate($this->image4By3, $constraint);
255 $this->assertNoViolation();
259 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
261 public function testInvalidMinRatio()
263 $constraint = new Image(array(
264 'minRatio' => '1abc',
267 $this->validator->validate($this->image, $constraint);
271 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
273 public function testInvalidMaxRatio()
275 $constraint = new Image(array(
276 'maxRatio' => '1abc',
279 $this->validator->validate($this->image, $constraint);
282 public function testSquareNotAllowed()
284 $constraint = new Image(array(
285 'allowSquare' => false,
286 'allowSquareMessage' => 'myMessage',
289 $this->validator->validate($this->image, $constraint);
291 $this->buildViolation('myMessage')
292 ->setParameter('{{ width }}', 2)
293 ->setParameter('{{ height }}', 2)
294 ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)
298 public function testLandscapeNotAllowed()
300 $constraint = new Image(array(
301 'allowLandscape' => false,
302 'allowLandscapeMessage' => 'myMessage',
305 $this->validator->validate($this->imageLandscape, $constraint);
307 $this->buildViolation('myMessage')
308 ->setParameter('{{ width }}', 2)
309 ->setParameter('{{ height }}', 1)
310 ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)
314 public function testPortraitNotAllowed()
316 $constraint = new Image(array(
317 'allowPortrait' => false,
318 'allowPortraitMessage' => 'myMessage',
321 $this->validator->validate($this->imagePortrait, $constraint);
323 $this->buildViolation('myMessage')
324 ->setParameter('{{ width }}', 1)
325 ->setParameter('{{ height }}', 2)
326 ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)
330 public function testCorrupted()
332 if (!function_exists('imagecreatefromstring')) {
333 $this->markTestSkipped('This test require GD extension');
336 $constraint = new Image(array(
337 'detectCorrupted' => true,
338 'corruptedMessage' => 'myMessage',
341 $this->validator->validate($this->image, $constraint);
343 $this->assertNoViolation();
345 $this->validator->validate($this->imageCorrupted, $constraint);
347 $this->buildViolation('myMessage')
348 ->setCode(Image::CORRUPTED_IMAGE_ERROR)