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\Validation;
19 * @requires extension fileinfo
21 class ImageValidatorTest extends AbstractConstraintValidatorTest
32 protected $imageLandscape;
33 protected $imagePortrait;
36 protected function getApiVersion()
38 return Validation::API_VERSION_2_5;
41 protected function createValidator()
43 return new ImageValidator();
46 protected function setUp()
50 $this->image = __DIR__.'/Fixtures/test.gif';
51 $this->imageLandscape = __DIR__.'/Fixtures/test_landscape.gif';
52 $this->imagePortrait = __DIR__.'/Fixtures/test_portrait.gif';
53 $this->image4By3 = __DIR__.'/Fixtures/test_4by3.gif';
56 public function testNullIsValid()
58 $this->validator->validate(null, new Image());
60 $this->assertNoViolation();
63 public function testEmptyStringIsValid()
65 $this->validator->validate('', new Image());
67 $this->assertNoViolation();
70 public function testValidImage()
72 $this->validator->validate($this->image, new Image());
74 $this->assertNoViolation();
77 public function testFileNotFound()
79 // Check that the logic from FileValidator still works
80 $constraint = new Image(array(
81 'notFoundMessage' => 'myMessage',
84 $this->validator->validate('foobar', $constraint);
86 $this->buildViolation('myMessage')
87 ->setParameter('{{ file }}', '"foobar"')
88 ->setCode(Image::NOT_FOUND_ERROR)
92 public function testValidSize()
94 $constraint = new Image(array(
101 $this->validator->validate($this->image, $constraint);
103 $this->assertNoViolation();
106 public function testWidthTooSmall()
108 $constraint = new Image(array(
110 'minWidthMessage' => 'myMessage',
113 $this->validator->validate($this->image, $constraint);
115 $this->buildViolation('myMessage')
116 ->setParameter('{{ width }}', '2')
117 ->setParameter('{{ min_width }}', '3')
118 ->setCode(Image::TOO_NARROW_ERROR)
122 public function testWidthTooBig()
124 $constraint = new Image(array(
126 'maxWidthMessage' => 'myMessage',
129 $this->validator->validate($this->image, $constraint);
131 $this->buildViolation('myMessage')
132 ->setParameter('{{ width }}', '2')
133 ->setParameter('{{ max_width }}', '1')
134 ->setCode(Image::TOO_WIDE_ERROR)
138 public function testHeightTooSmall()
140 $constraint = new Image(array(
142 'minHeightMessage' => 'myMessage',
145 $this->validator->validate($this->image, $constraint);
147 $this->buildViolation('myMessage')
148 ->setParameter('{{ height }}', '2')
149 ->setParameter('{{ min_height }}', '3')
150 ->setCode(Image::TOO_LOW_ERROR)
154 public function testHeightTooBig()
156 $constraint = new Image(array(
158 'maxHeightMessage' => 'myMessage',
161 $this->validator->validate($this->image, $constraint);
163 $this->buildViolation('myMessage')
164 ->setParameter('{{ height }}', '2')
165 ->setParameter('{{ max_height }}', '1')
166 ->setCode(Image::TOO_HIGH_ERROR)
171 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
173 public function testInvalidMinWidth()
175 $constraint = new Image(array(
176 'minWidth' => '1abc',
179 $this->validator->validate($this->image, $constraint);
183 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
185 public function testInvalidMaxWidth()
187 $constraint = new Image(array(
188 'maxWidth' => '1abc',
191 $this->validator->validate($this->image, $constraint);
195 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
197 public function testInvalidMinHeight()
199 $constraint = new Image(array(
200 'minHeight' => '1abc',
203 $this->validator->validate($this->image, $constraint);
207 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
209 public function testInvalidMaxHeight()
211 $constraint = new Image(array(
212 'maxHeight' => '1abc',
215 $this->validator->validate($this->image, $constraint);
218 public function testRatioTooSmall()
220 $constraint = new Image(array(
222 'minRatioMessage' => 'myMessage',
225 $this->validator->validate($this->image, $constraint);
227 $this->buildViolation('myMessage')
228 ->setParameter('{{ ratio }}', 1)
229 ->setParameter('{{ min_ratio }}', 2)
230 ->setCode(Image::RATIO_TOO_SMALL_ERROR)
234 public function testRatioTooBig()
236 $constraint = new Image(array(
238 'maxRatioMessage' => 'myMessage',
241 $this->validator->validate($this->image, $constraint);
243 $this->buildViolation('myMessage')
244 ->setParameter('{{ ratio }}', 1)
245 ->setParameter('{{ max_ratio }}', 0.5)
246 ->setCode(Image::RATIO_TOO_BIG_ERROR)
250 public function testMaxRatioUsesTwoDecimalsOnly()
252 $constraint = new Image(array(
256 $this->validator->validate($this->image4By3, $constraint);
258 $this->assertNoViolation();
262 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
264 public function testInvalidMinRatio()
266 $constraint = new Image(array(
267 'minRatio' => '1abc',
270 $this->validator->validate($this->image, $constraint);
274 * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
276 public function testInvalidMaxRatio()
278 $constraint = new Image(array(
279 'maxRatio' => '1abc',
282 $this->validator->validate($this->image, $constraint);
285 public function testSquareNotAllowed()
287 $constraint = new Image(array(
288 'allowSquare' => false,
289 'allowSquareMessage' => 'myMessage',
292 $this->validator->validate($this->image, $constraint);
294 $this->buildViolation('myMessage')
295 ->setParameter('{{ width }}', 2)
296 ->setParameter('{{ height }}', 2)
297 ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)
301 public function testLandscapeNotAllowed()
303 $constraint = new Image(array(
304 'allowLandscape' => false,
305 'allowLandscapeMessage' => 'myMessage',
308 $this->validator->validate($this->imageLandscape, $constraint);
310 $this->buildViolation('myMessage')
311 ->setParameter('{{ width }}', 2)
312 ->setParameter('{{ height }}', 1)
313 ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)
317 public function testPortraitNotAllowed()
319 $constraint = new Image(array(
320 'allowPortrait' => false,
321 'allowPortraitMessage' => 'myMessage',
324 $this->validator->validate($this->imagePortrait, $constraint);
326 $this->buildViolation('myMessage')
327 ->setParameter('{{ width }}', 1)
328 ->setParameter('{{ height }}', 2)
329 ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)