93b1d05bab7b2d623c78213866dcdc56f5a8ba84
[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\Test\ConstraintValidatorTestCase;
17
18 /**
19  * @requires extension fileinfo
20  */
21 class ImageValidatorTest extends ConstraintValidatorTestCase
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     protected $imageCorrupted;
36
37     protected function createValidator()
38     {
39         return new ImageValidator();
40     }
41
42     protected function setUp()
43     {
44         parent::setUp();
45
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';
51     }
52
53     public function testNullIsValid()
54     {
55         $this->validator->validate(null, new Image());
56
57         $this->assertNoViolation();
58     }
59
60     public function testEmptyStringIsValid()
61     {
62         $this->validator->validate('', new Image());
63
64         $this->assertNoViolation();
65     }
66
67     public function testValidImage()
68     {
69         $this->validator->validate($this->image, new Image());
70
71         $this->assertNoViolation();
72     }
73
74     public function testFileNotFound()
75     {
76         // Check that the logic from FileValidator still works
77         $constraint = new Image(array(
78             'notFoundMessage' => 'myMessage',
79         ));
80
81         $this->validator->validate('foobar', $constraint);
82
83         $this->buildViolation('myMessage')
84             ->setParameter('{{ file }}', '"foobar"')
85             ->setCode(Image::NOT_FOUND_ERROR)
86             ->assertRaised();
87     }
88
89     public function testValidSize()
90     {
91         $constraint = new Image(array(
92             'minWidth' => 1,
93             'maxWidth' => 2,
94             'minHeight' => 1,
95             'maxHeight' => 2,
96         ));
97
98         $this->validator->validate($this->image, $constraint);
99
100         $this->assertNoViolation();
101     }
102
103     public function testWidthTooSmall()
104     {
105         $constraint = new Image(array(
106             'minWidth' => 3,
107             'minWidthMessage' => 'myMessage',
108         ));
109
110         $this->validator->validate($this->image, $constraint);
111
112         $this->buildViolation('myMessage')
113             ->setParameter('{{ width }}', '2')
114             ->setParameter('{{ min_width }}', '3')
115             ->setCode(Image::TOO_NARROW_ERROR)
116             ->assertRaised();
117     }
118
119     public function testWidthTooBig()
120     {
121         $constraint = new Image(array(
122             'maxWidth' => 1,
123             'maxWidthMessage' => 'myMessage',
124         ));
125
126         $this->validator->validate($this->image, $constraint);
127
128         $this->buildViolation('myMessage')
129             ->setParameter('{{ width }}', '2')
130             ->setParameter('{{ max_width }}', '1')
131             ->setCode(Image::TOO_WIDE_ERROR)
132             ->assertRaised();
133     }
134
135     public function testHeightTooSmall()
136     {
137         $constraint = new Image(array(
138             'minHeight' => 3,
139             'minHeightMessage' => 'myMessage',
140         ));
141
142         $this->validator->validate($this->image, $constraint);
143
144         $this->buildViolation('myMessage')
145             ->setParameter('{{ height }}', '2')
146             ->setParameter('{{ min_height }}', '3')
147             ->setCode(Image::TOO_LOW_ERROR)
148             ->assertRaised();
149     }
150
151     public function testHeightTooBig()
152     {
153         $constraint = new Image(array(
154             'maxHeight' => 1,
155             'maxHeightMessage' => 'myMessage',
156         ));
157
158         $this->validator->validate($this->image, $constraint);
159
160         $this->buildViolation('myMessage')
161             ->setParameter('{{ height }}', '2')
162             ->setParameter('{{ max_height }}', '1')
163             ->setCode(Image::TOO_HIGH_ERROR)
164             ->assertRaised();
165     }
166
167     /**
168      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
169      */
170     public function testInvalidMinWidth()
171     {
172         $constraint = new Image(array(
173             'minWidth' => '1abc',
174         ));
175
176         $this->validator->validate($this->image, $constraint);
177     }
178
179     /**
180      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
181      */
182     public function testInvalidMaxWidth()
183     {
184         $constraint = new Image(array(
185             'maxWidth' => '1abc',
186         ));
187
188         $this->validator->validate($this->image, $constraint);
189     }
190
191     /**
192      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
193      */
194     public function testInvalidMinHeight()
195     {
196         $constraint = new Image(array(
197             'minHeight' => '1abc',
198         ));
199
200         $this->validator->validate($this->image, $constraint);
201     }
202
203     /**
204      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
205      */
206     public function testInvalidMaxHeight()
207     {
208         $constraint = new Image(array(
209             'maxHeight' => '1abc',
210         ));
211
212         $this->validator->validate($this->image, $constraint);
213     }
214
215     public function testRatioTooSmall()
216     {
217         $constraint = new Image(array(
218             'minRatio' => 2,
219             'minRatioMessage' => 'myMessage',
220         ));
221
222         $this->validator->validate($this->image, $constraint);
223
224         $this->buildViolation('myMessage')
225             ->setParameter('{{ ratio }}', 1)
226             ->setParameter('{{ min_ratio }}', 2)
227             ->setCode(Image::RATIO_TOO_SMALL_ERROR)
228             ->assertRaised();
229     }
230
231     public function testRatioTooBig()
232     {
233         $constraint = new Image(array(
234             'maxRatio' => 0.5,
235             'maxRatioMessage' => 'myMessage',
236         ));
237
238         $this->validator->validate($this->image, $constraint);
239
240         $this->buildViolation('myMessage')
241             ->setParameter('{{ ratio }}', 1)
242             ->setParameter('{{ max_ratio }}', 0.5)
243             ->setCode(Image::RATIO_TOO_BIG_ERROR)
244             ->assertRaised();
245     }
246
247     public function testMaxRatioUsesTwoDecimalsOnly()
248     {
249         $constraint = new Image(array(
250             'maxRatio' => 1.33,
251         ));
252
253         $this->validator->validate($this->image4By3, $constraint);
254
255         $this->assertNoViolation();
256     }
257
258     /**
259      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
260      */
261     public function testInvalidMinRatio()
262     {
263         $constraint = new Image(array(
264             'minRatio' => '1abc',
265         ));
266
267         $this->validator->validate($this->image, $constraint);
268     }
269
270     /**
271      * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
272      */
273     public function testInvalidMaxRatio()
274     {
275         $constraint = new Image(array(
276             'maxRatio' => '1abc',
277         ));
278
279         $this->validator->validate($this->image, $constraint);
280     }
281
282     public function testSquareNotAllowed()
283     {
284         $constraint = new Image(array(
285             'allowSquare' => false,
286             'allowSquareMessage' => 'myMessage',
287         ));
288
289         $this->validator->validate($this->image, $constraint);
290
291         $this->buildViolation('myMessage')
292             ->setParameter('{{ width }}', 2)
293             ->setParameter('{{ height }}', 2)
294             ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)
295             ->assertRaised();
296     }
297
298     public function testLandscapeNotAllowed()
299     {
300         $constraint = new Image(array(
301             'allowLandscape' => false,
302             'allowLandscapeMessage' => 'myMessage',
303         ));
304
305         $this->validator->validate($this->imageLandscape, $constraint);
306
307         $this->buildViolation('myMessage')
308             ->setParameter('{{ width }}', 2)
309             ->setParameter('{{ height }}', 1)
310             ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)
311             ->assertRaised();
312     }
313
314     public function testPortraitNotAllowed()
315     {
316         $constraint = new Image(array(
317             'allowPortrait' => false,
318             'allowPortraitMessage' => 'myMessage',
319         ));
320
321         $this->validator->validate($this->imagePortrait, $constraint);
322
323         $this->buildViolation('myMessage')
324             ->setParameter('{{ width }}', 1)
325             ->setParameter('{{ height }}', 2)
326             ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)
327             ->assertRaised();
328     }
329
330     public function testCorrupted()
331     {
332         if (!function_exists('imagecreatefromstring')) {
333             $this->markTestSkipped('This test require GD extension');
334         }
335
336         $constraint = new Image(array(
337             'detectCorrupted' => true,
338             'corruptedMessage' => 'myMessage',
339         ));
340
341         $this->validator->validate($this->image, $constraint);
342
343         $this->assertNoViolation();
344
345         $this->validator->validate($this->imageCorrupted, $constraint);
346
347         $this->buildViolation('myMessage')
348             ->setCode(Image::CORRUPTED_IMAGE_ERROR)
349             ->assertRaised();
350     }
351 }