Yaffs site version 1.1
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / ImageValidatorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\Tests\Constraints;
13
14 use Symfony\Component\Validator\Constraints\Image;
15 use Symfony\Component\Validator\Constraints\ImageValidator;
16 use Symfony\Component\Validator\Validation;
17
18 /**
19  * @requires extension fileinfo
20  */
21 class ImageValidatorTest extends AbstractConstraintValidatorTest
22 {
23     protected $context;
24
25     /**
26      * @var ImageValidator
27      */
28     protected $validator;
29
30     protected $path;
31     protected $image;
32     protected $imageLandscape;
33     protected $imagePortrait;
34     protected $image4By3;
35
36     protected function getApiVersion()
37     {
38         return Validation::API_VERSION_2_5;
39     }
40
41     protected function createValidator()
42     {
43         return new ImageValidator();
44     }
45
46     protected function setUp()
47     {
48         parent::setUp();
49
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';
54     }
55
56     public function testNullIsValid()
57     {
58         $this->validator->validate(null, new Image());
59
60         $this->assertNoViolation();
61     }
62
63     public function testEmptyStringIsValid()
64     {
65         $this->validator->validate('', new Image());
66
67         $this->assertNoViolation();
68     }
69
70     public function testValidImage()
71     {
72         $this->validator->validate($this->image, new Image());
73
74         $this->assertNoViolation();
75     }
76
77     public function testFileNotFound()
78     {
79         // Check that the logic from FileValidator still works
80         $constraint = new Image(array(
81             'notFoundMessage' => 'myMessage',
82         ));
83
84         $this->validator->validate('foobar', $constraint);
85
86         $this->buildViolation('myMessage')
87             ->setParameter('{{ file }}', '"foobar"')
88             ->setCode(Image::NOT_FOUND_ERROR)
89             ->assertRaised();
90     }
91
92     public function testValidSize()
93     {
94         $constraint = new Image(array(
95             'minWidth' => 1,
96             'maxWidth' => 2,
97             'minHeight' => 1,
98             'maxHeight' => 2,
99         ));
100
101         $this->validator->validate($this->image, $constraint);
102
103         $this->assertNoViolation();
104     }
105
106     public function testWidthTooSmall()
107     {
108         $constraint = new Image(array(
109             'minWidth' => 3,
110             'minWidthMessage' => 'myMessage',
111         ));
112
113         $this->validator->validate($this->image, $constraint);
114
115         $this->buildViolation('myMessage')
116             ->setParameter('{{ width }}', '2')
117             ->setParameter('{{ min_width }}', '3')
118             ->setCode(Image::TOO_NARROW_ERROR)
119             ->assertRaised();
120     }
121
122     public function testWidthTooBig()
123     {
124         $constraint = new Image(array(
125             'maxWidth' => 1,
126             'maxWidthMessage' => 'myMessage',
127         ));
128
129         $this->validator->validate($this->image, $constraint);
130
131         $this->buildViolation('myMessage')
132             ->setParameter('{{ width }}', '2')
133             ->setParameter('{{ max_width }}', '1')
134             ->setCode(Image::TOO_WIDE_ERROR)
135             ->assertRaised();
136     }
137
138     public function testHeightTooSmall()
139     {
140         $constraint = new Image(array(
141             'minHeight' => 3,
142             'minHeightMessage' => 'myMessage',
143         ));
144
145         $this->validator->validate($this->image, $constraint);
146
147         $this->buildViolation('myMessage')
148             ->setParameter('{{ height }}', '2')
149             ->setParameter('{{ min_height }}', '3')
150             ->setCode(Image::TOO_LOW_ERROR)
151             ->assertRaised();
152     }
153
154     public function testHeightTooBig()
155     {
156         $constraint = new Image(array(
157             'maxHeight' => 1,
158             'maxHeightMessage' => 'myMessage',
159         ));
160
161         $this->validator->validate($this->image, $constraint);
162
163         $this->buildViolation('myMessage')
164             ->setParameter('{{ height }}', '2')
165             ->setParameter('{{ max_height }}', '1')
166             ->setCode(Image::TOO_HIGH_ERROR)
167             ->assertRaised();
168     }
169
170     /**
171      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
172      */
173     public function testInvalidMinWidth()
174     {
175         $constraint = new Image(array(
176             'minWidth' => '1abc',
177         ));
178
179         $this->validator->validate($this->image, $constraint);
180     }
181
182     /**
183      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
184      */
185     public function testInvalidMaxWidth()
186     {
187         $constraint = new Image(array(
188             'maxWidth' => '1abc',
189         ));
190
191         $this->validator->validate($this->image, $constraint);
192     }
193
194     /**
195      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
196      */
197     public function testInvalidMinHeight()
198     {
199         $constraint = new Image(array(
200             'minHeight' => '1abc',
201         ));
202
203         $this->validator->validate($this->image, $constraint);
204     }
205
206     /**
207      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
208      */
209     public function testInvalidMaxHeight()
210     {
211         $constraint = new Image(array(
212             'maxHeight' => '1abc',
213         ));
214
215         $this->validator->validate($this->image, $constraint);
216     }
217
218     public function testRatioTooSmall()
219     {
220         $constraint = new Image(array(
221             'minRatio' => 2,
222             'minRatioMessage' => 'myMessage',
223         ));
224
225         $this->validator->validate($this->image, $constraint);
226
227         $this->buildViolation('myMessage')
228             ->setParameter('{{ ratio }}', 1)
229             ->setParameter('{{ min_ratio }}', 2)
230             ->setCode(Image::RATIO_TOO_SMALL_ERROR)
231             ->assertRaised();
232     }
233
234     public function testRatioTooBig()
235     {
236         $constraint = new Image(array(
237             'maxRatio' => 0.5,
238             'maxRatioMessage' => 'myMessage',
239         ));
240
241         $this->validator->validate($this->image, $constraint);
242
243         $this->buildViolation('myMessage')
244             ->setParameter('{{ ratio }}', 1)
245             ->setParameter('{{ max_ratio }}', 0.5)
246             ->setCode(Image::RATIO_TOO_BIG_ERROR)
247             ->assertRaised();
248     }
249
250     public function testMaxRatioUsesTwoDecimalsOnly()
251     {
252         $constraint = new Image(array(
253             'maxRatio' => 1.33,
254         ));
255
256         $this->validator->validate($this->image4By3, $constraint);
257
258         $this->assertNoViolation();
259     }
260
261     /**
262      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
263      */
264     public function testInvalidMinRatio()
265     {
266         $constraint = new Image(array(
267             'minRatio' => '1abc',
268         ));
269
270         $this->validator->validate($this->image, $constraint);
271     }
272
273     /**
274      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
275      */
276     public function testInvalidMaxRatio()
277     {
278         $constraint = new Image(array(
279             'maxRatio' => '1abc',
280         ));
281
282         $this->validator->validate($this->image, $constraint);
283     }
284
285     public function testSquareNotAllowed()
286     {
287         $constraint = new Image(array(
288             'allowSquare' => false,
289             'allowSquareMessage' => 'myMessage',
290         ));
291
292         $this->validator->validate($this->image, $constraint);
293
294         $this->buildViolation('myMessage')
295             ->setParameter('{{ width }}', 2)
296             ->setParameter('{{ height }}', 2)
297             ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)
298             ->assertRaised();
299     }
300
301     public function testLandscapeNotAllowed()
302     {
303         $constraint = new Image(array(
304             'allowLandscape' => false,
305             'allowLandscapeMessage' => 'myMessage',
306         ));
307
308         $this->validator->validate($this->imageLandscape, $constraint);
309
310         $this->buildViolation('myMessage')
311             ->setParameter('{{ width }}', 2)
312             ->setParameter('{{ height }}', 1)
313             ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)
314             ->assertRaised();
315     }
316
317     public function testPortraitNotAllowed()
318     {
319         $constraint = new Image(array(
320             'allowPortrait' => false,
321             'allowPortraitMessage' => 'myMessage',
322         ));
323
324         $this->validator->validate($this->imagePortrait, $constraint);
325
326         $this->buildViolation('myMessage')
327             ->setParameter('{{ width }}', 1)
328             ->setParameter('{{ height }}', 2)
329             ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)
330             ->assertRaised();
331     }
332 }