123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Validator\Tests\Constraints;
- use Symfony\Component\Validator\Constraints\Image;
- use Symfony\Component\Validator\Constraints\ImageValidator;
- use Symfony\Component\Validator\Validation;
- /**
- * @requires extension fileinfo
- */
- class ImageValidatorTest extends AbstractConstraintValidatorTest
- {
- protected $context;
- /**
- * @var ImageValidator
- */
- protected $validator;
- protected $path;
- protected $image;
- protected $imageLandscape;
- protected $imagePortrait;
- protected $image4By3;
- protected function getApiVersion()
- {
- return Validation::API_VERSION_2_5;
- }
- protected function createValidator()
- {
- return new ImageValidator();
- }
- protected function setUp()
- {
- parent::setUp();
- $this->image = __DIR__.'/Fixtures/test.gif';
- $this->imageLandscape = __DIR__.'/Fixtures/test_landscape.gif';
- $this->imagePortrait = __DIR__.'/Fixtures/test_portrait.gif';
- $this->image4By3 = __DIR__.'/Fixtures/test_4by3.gif';
- }
- public function testNullIsValid()
- {
- $this->validator->validate(null, new Image());
- $this->assertNoViolation();
- }
- public function testEmptyStringIsValid()
- {
- $this->validator->validate('', new Image());
- $this->assertNoViolation();
- }
- public function testValidImage()
- {
- $this->validator->validate($this->image, new Image());
- $this->assertNoViolation();
- }
- public function testFileNotFound()
- {
- // Check that the logic from FileValidator still works
- $constraint = new Image(array(
- 'notFoundMessage' => 'myMessage',
- ));
- $this->validator->validate('foobar', $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ file }}', '"foobar"')
- ->setCode(Image::NOT_FOUND_ERROR)
- ->assertRaised();
- }
- public function testValidSize()
- {
- $constraint = new Image(array(
- 'minWidth' => 1,
- 'maxWidth' => 2,
- 'minHeight' => 1,
- 'maxHeight' => 2,
- ));
- $this->validator->validate($this->image, $constraint);
- $this->assertNoViolation();
- }
- public function testWidthTooSmall()
- {
- $constraint = new Image(array(
- 'minWidth' => 3,
- 'minWidthMessage' => 'myMessage',
- ));
- $this->validator->validate($this->image, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ width }}', '2')
- ->setParameter('{{ min_width }}', '3')
- ->setCode(Image::TOO_NARROW_ERROR)
- ->assertRaised();
- }
- public function testWidthTooBig()
- {
- $constraint = new Image(array(
- 'maxWidth' => 1,
- 'maxWidthMessage' => 'myMessage',
- ));
- $this->validator->validate($this->image, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ width }}', '2')
- ->setParameter('{{ max_width }}', '1')
- ->setCode(Image::TOO_WIDE_ERROR)
- ->assertRaised();
- }
- public function testHeightTooSmall()
- {
- $constraint = new Image(array(
- 'minHeight' => 3,
- 'minHeightMessage' => 'myMessage',
- ));
- $this->validator->validate($this->image, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ height }}', '2')
- ->setParameter('{{ min_height }}', '3')
- ->setCode(Image::TOO_LOW_ERROR)
- ->assertRaised();
- }
- public function testHeightTooBig()
- {
- $constraint = new Image(array(
- 'maxHeight' => 1,
- 'maxHeightMessage' => 'myMessage',
- ));
- $this->validator->validate($this->image, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ height }}', '2')
- ->setParameter('{{ max_height }}', '1')
- ->setCode(Image::TOO_HIGH_ERROR)
- ->assertRaised();
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- */
- public function testInvalidMinWidth()
- {
- $constraint = new Image(array(
- 'minWidth' => '1abc',
- ));
- $this->validator->validate($this->image, $constraint);
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- */
- public function testInvalidMaxWidth()
- {
- $constraint = new Image(array(
- 'maxWidth' => '1abc',
- ));
- $this->validator->validate($this->image, $constraint);
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- */
- public function testInvalidMinHeight()
- {
- $constraint = new Image(array(
- 'minHeight' => '1abc',
- ));
- $this->validator->validate($this->image, $constraint);
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- */
- public function testInvalidMaxHeight()
- {
- $constraint = new Image(array(
- 'maxHeight' => '1abc',
- ));
- $this->validator->validate($this->image, $constraint);
- }
- public function testRatioTooSmall()
- {
- $constraint = new Image(array(
- 'minRatio' => 2,
- 'minRatioMessage' => 'myMessage',
- ));
- $this->validator->validate($this->image, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ ratio }}', 1)
- ->setParameter('{{ min_ratio }}', 2)
- ->setCode(Image::RATIO_TOO_SMALL_ERROR)
- ->assertRaised();
- }
- public function testRatioTooBig()
- {
- $constraint = new Image(array(
- 'maxRatio' => 0.5,
- 'maxRatioMessage' => 'myMessage',
- ));
- $this->validator->validate($this->image, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ ratio }}', 1)
- ->setParameter('{{ max_ratio }}', 0.5)
- ->setCode(Image::RATIO_TOO_BIG_ERROR)
- ->assertRaised();
- }
- public function testMaxRatioUsesTwoDecimalsOnly()
- {
- $constraint = new Image(array(
- 'maxRatio' => 1.33,
- ));
- $this->validator->validate($this->image4By3, $constraint);
- $this->assertNoViolation();
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- */
- public function testInvalidMinRatio()
- {
- $constraint = new Image(array(
- 'minRatio' => '1abc',
- ));
- $this->validator->validate($this->image, $constraint);
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- */
- public function testInvalidMaxRatio()
- {
- $constraint = new Image(array(
- 'maxRatio' => '1abc',
- ));
- $this->validator->validate($this->image, $constraint);
- }
- public function testSquareNotAllowed()
- {
- $constraint = new Image(array(
- 'allowSquare' => false,
- 'allowSquareMessage' => 'myMessage',
- ));
- $this->validator->validate($this->image, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ width }}', 2)
- ->setParameter('{{ height }}', 2)
- ->setCode(Image::SQUARE_NOT_ALLOWED_ERROR)
- ->assertRaised();
- }
- public function testLandscapeNotAllowed()
- {
- $constraint = new Image(array(
- 'allowLandscape' => false,
- 'allowLandscapeMessage' => 'myMessage',
- ));
- $this->validator->validate($this->imageLandscape, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ width }}', 2)
- ->setParameter('{{ height }}', 1)
- ->setCode(Image::LANDSCAPE_NOT_ALLOWED_ERROR)
- ->assertRaised();
- }
- public function testPortraitNotAllowed()
- {
- $constraint = new Image(array(
- 'allowPortrait' => false,
- 'allowPortraitMessage' => 'myMessage',
- ));
- $this->validator->validate($this->imagePortrait, $constraint);
- $this->buildViolation('myMessage')
- ->setParameter('{{ width }}', 1)
- ->setParameter('{{ height }}', 2)
- ->setCode(Image::PORTRAIT_NOT_ALLOWED_ERROR)
- ->assertRaised();
- }
- }
|