You are here

class ImageValidatorTest in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/validator/Tests/Constraints/ImageValidatorTest.php \Symfony\Component\Validator\Tests\Constraints\ImageValidatorTest

@requires extension fileinfo

Hierarchy

Expanded class hierarchy of ImageValidatorTest

File

vendor/symfony/validator/Tests/Constraints/ImageValidatorTest.php, line 21

Namespace

Symfony\Component\Validator\Tests\Constraints
View source
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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractConstraintValidatorTest::$constraint protected property
AbstractConstraintValidatorTest::$defaultTimezone protected property
AbstractConstraintValidatorTest::$group protected property
AbstractConstraintValidatorTest::$metadata protected property
AbstractConstraintValidatorTest::$object protected property
AbstractConstraintValidatorTest::$propertyPath protected property
AbstractConstraintValidatorTest::$root protected property
AbstractConstraintValidatorTest::$value protected property
AbstractConstraintValidatorTest::assertNoViolation protected function
AbstractConstraintValidatorTest::assertViolation Deprecated protected function
AbstractConstraintValidatorTest::assertViolations Deprecated protected function
AbstractConstraintValidatorTest::buildViolation protected function
AbstractConstraintValidatorTest::createContext protected function
AbstractConstraintValidatorTest::createViolation Deprecated protected function
AbstractConstraintValidatorTest::expectNoValidate protected function
AbstractConstraintValidatorTest::expectValidateAt protected function
AbstractConstraintValidatorTest::expectValidateValueAt protected function
AbstractConstraintValidatorTest::restoreDefaultTimezone protected function
AbstractConstraintValidatorTest::setDefaultTimezone protected function
AbstractConstraintValidatorTest::setGroup protected function
AbstractConstraintValidatorTest::setObject protected function
AbstractConstraintValidatorTest::setProperty protected function
AbstractConstraintValidatorTest::setPropertyPath protected function
AbstractConstraintValidatorTest::setRoot protected function
AbstractConstraintValidatorTest::setValue protected function
AbstractConstraintValidatorTest::tearDown protected function 1
ImageValidatorTest::$context protected property Overrides AbstractConstraintValidatorTest::$context
ImageValidatorTest::$image protected property
ImageValidatorTest::$image4By3 protected property
ImageValidatorTest::$imageLandscape protected property
ImageValidatorTest::$imagePortrait protected property
ImageValidatorTest::$path protected property
ImageValidatorTest::$validator protected property Overrides AbstractConstraintValidatorTest::$validator
ImageValidatorTest::createValidator protected function Overrides AbstractConstraintValidatorTest::createValidator
ImageValidatorTest::getApiVersion protected function Overrides AbstractConstraintValidatorTest::getApiVersion
ImageValidatorTest::setUp protected function Overrides AbstractConstraintValidatorTest::setUp
ImageValidatorTest::testEmptyStringIsValid public function
ImageValidatorTest::testFileNotFound public function
ImageValidatorTest::testHeightTooBig public function
ImageValidatorTest::testHeightTooSmall public function
ImageValidatorTest::testInvalidMaxHeight public function @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
ImageValidatorTest::testInvalidMaxRatio public function @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
ImageValidatorTest::testInvalidMaxWidth public function @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
ImageValidatorTest::testInvalidMinHeight public function @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
ImageValidatorTest::testInvalidMinRatio public function @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
ImageValidatorTest::testInvalidMinWidth public function @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
ImageValidatorTest::testLandscapeNotAllowed public function
ImageValidatorTest::testMaxRatioUsesTwoDecimalsOnly public function
ImageValidatorTest::testNullIsValid public function
ImageValidatorTest::testPortraitNotAllowed public function
ImageValidatorTest::testRatioTooBig public function
ImageValidatorTest::testRatioTooSmall public function
ImageValidatorTest::testSquareNotAllowed public function
ImageValidatorTest::testValidImage public function
ImageValidatorTest::testValidSize public function
ImageValidatorTest::testWidthTooBig public function
ImageValidatorTest::testWidthTooSmall public function