You are here

abstract class AbstractComparisonValidatorTestCase in Zircon Profile 8

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

@author Daniel Holmes <daniel@danielholmes.org>

Hierarchy

Expanded class hierarchy of AbstractComparisonValidatorTestCase

File

vendor/symfony/validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php, line 35

Namespace

Symfony\Component\Validator\Tests\Constraints
View source
abstract class AbstractComparisonValidatorTestCase extends AbstractConstraintValidatorTest {
  protected static function addPhp5Dot5Comparisons(array $comparisons) {
    if (PHP_VERSION_ID < 50500) {
      return $comparisons;
    }
    $result = $comparisons;

    // Duplicate all tests involving DateTime objects to be tested with
    // DateTimeImmutable objects as well
    foreach ($comparisons as $comparison) {
      $add = false;
      foreach ($comparison as $i => $value) {
        if ($value instanceof \DateTime) {
          $comparison[$i] = new \DateTimeImmutable($value
            ->format('Y-m-d H:i:s.u e'), $value
            ->getTimezone());
          $add = true;
        }
        elseif ('DateTime' === $value) {
          $comparison[$i] = 'DateTimeImmutable';
          $add = true;
        }
      }
      if ($add) {
        $result[] = $comparison;
      }
    }
    return $result;
  }

  /**
   * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
   */
  public function testThrowsConstraintExceptionIfNoValueOrProperty() {
    $comparison = $this
      ->createConstraint(array());
    $this->validator
      ->validate('some value', $comparison);
  }

  /**
   * @dataProvider provideAllValidComparisons
   *
   * @param mixed $dirtyValue
   * @param mixed $comparisonValue
   */
  public function testValidComparisonToValue($dirtyValue, $comparisonValue) {
    $constraint = $this
      ->createConstraint(array(
      'value' => $comparisonValue,
    ));
    $this->validator
      ->validate($dirtyValue, $constraint);
    $this
      ->assertNoViolation();
  }

  /**
   * @return array
   */
  public function provideAllValidComparisons() {

    // The provider runs before setUp(), so we need to manually fix
    // the default timezone
    $this
      ->setDefaultTimezone('UTC');
    $comparisons = self::addPhp5Dot5Comparisons($this
      ->provideValidComparisons());
    $this
      ->restoreDefaultTimezone();
    return $comparisons;
  }

  /**
   * @return array
   */
  public abstract function provideValidComparisons();

  /**
   * @dataProvider provideAllInvalidComparisons
   *
   * @param mixed  $dirtyValue
   * @param mixed  $dirtyValueAsString
   * @param mixed  $comparedValue
   * @param mixed  $comparedValueString
   * @param string $comparedValueType
   */
  public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType) {

    // Conversion of dates to string differs between ICU versions
    // Make sure we have the correct version loaded
    if ($dirtyValue instanceof \DateTime || $dirtyValue instanceof \DateTimeInterface) {
      IntlTestHelper::requireIntl($this);
      if (PHP_VERSION_ID < 50304 && !(extension_loaded('intl') && method_exists('IntlDateFormatter', 'setTimeZone'))) {
        $this
          ->markTestSkipped('Intl supports formatting DateTime objects since 5.3.4');
      }
    }
    $constraint = $this
      ->createConstraint(array(
      'value' => $comparedValue,
    ));
    $constraint->message = 'Constraint Message';
    $this->validator
      ->validate($dirtyValue, $constraint);
    $this
      ->buildViolation('Constraint Message')
      ->setParameter('{{ value }}', $dirtyValueAsString)
      ->setParameter('{{ compared_value }}', $comparedValueString)
      ->setParameter('{{ compared_value_type }}', $comparedValueType)
      ->assertRaised();
  }

  /**
   * @return array
   */
  public function provideAllInvalidComparisons() {

    // The provider runs before setUp(), so we need to manually fix
    // the default timezone
    $this
      ->setDefaultTimezone('UTC');
    $comparisons = self::addPhp5Dot5Comparisons($this
      ->provideInvalidComparisons());
    $this
      ->restoreDefaultTimezone();
    return $comparisons;
  }

  /**
   * @return array
   */
  public abstract function provideInvalidComparisons();

  /**
   * @param array $options Options for the constraint
   *
   * @return Constraint
   */
  protected abstract function createConstraint(array $options);

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractComparisonValidatorTestCase::addPhp5Dot5Comparisons protected static function
AbstractComparisonValidatorTestCase::createConstraint abstract protected function 8
AbstractComparisonValidatorTestCase::provideAllInvalidComparisons public function 1
AbstractComparisonValidatorTestCase::provideAllValidComparisons public function 1
AbstractComparisonValidatorTestCase::provideInvalidComparisons abstract public function 8
AbstractComparisonValidatorTestCase::provideValidComparisons abstract public function 8
AbstractComparisonValidatorTestCase::testInvalidComparisonToValue public function @dataProvider provideAllInvalidComparisons
AbstractComparisonValidatorTestCase::testThrowsConstraintExceptionIfNoValueOrProperty public function @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
AbstractComparisonValidatorTestCase::testValidComparisonToValue public function @dataProvider provideAllValidComparisons
AbstractConstraintValidatorTest::$constraint protected property
AbstractConstraintValidatorTest::$context protected property 1
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::$validator protected property 1
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::createValidator abstract protected function 42
AbstractConstraintValidatorTest::createViolation Deprecated protected function
AbstractConstraintValidatorTest::expectNoValidate protected function
AbstractConstraintValidatorTest::expectValidateAt protected function
AbstractConstraintValidatorTest::expectValidateValueAt protected function
AbstractConstraintValidatorTest::getApiVersion protected function 42
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::setUp protected function 2
AbstractConstraintValidatorTest::setValue protected function
AbstractConstraintValidatorTest::tearDown protected function 1