You are here

public function FractionConstraintTest::testConstraints in Fraction 8

Same name and namespace in other branches
  1. 2.x tests/src/Kernel/Plugin/Constraints/FractionConstraintTest.php \Drupal\Tests\fraction\Kernel\Plugin\Constraints\FractionConstraintTest::testConstraints()

Tests constraints.

File

tests/src/Kernel/Plugin/Constraints/FractionConstraintTest.php, line 77

Class

FractionConstraintTest
Tests Constraint for Fraction.

Namespace

Drupal\Tests\fraction\Kernel\Plugin\Constraints

Code

public function testConstraints() {
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(0, $violations);
  $this
    ->assertViolations($violations);
  $this->entity->{self::FIELD_NAME} = [
    'denominator' => '0',
    'numerator' => '1',
  ];
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this->entity->{self::FIELD_NAME} = [
    'denominator' => '2147483649',
    'numerator' => '1',
  ];
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this
    ->assertViolations($violations);
  $this->entity->{self::FIELD_NAME} = [
    'denominator' => '123456',
    'numerator' => '1',
  ];
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(0, $violations);
  $this->entity->{self::FIELD_NAME} = [
    'denominator' => '1',
    'numerator' => '9223372036854775808',
  ];
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(2, $violations);
  $this
    ->assertViolations($violations);
  $this->entity->{self::FIELD_NAME} = [
    'denominator' => '1',
    'numerator' => '-9223372036854775809',
  ];
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(2, $violations);
  $this
    ->assertViolations($violations);
  $this->entity->{self::FIELD_NAME} = [
    'denominator' => '1',
    'numerator' => '92233720368547758',
  ];
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(0, $violations);
  $this->entity->{self::FIELD_NAME} = [
    'decimal' => '1.255',
  ];
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(0, $violations);
  $decimal = '9223372.993685481231231';
  $this->entity->{self::FIELD_NAME} = $this
    ->generateFraction($decimal);
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(2, $violations);
  $this
    ->assertViolations($violations);
  $decimal = '11.1234';
  $this->entity->{self::FIELD_NAME} = $this
    ->generateFraction($decimal);
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(0, $violations);
  $decimal = '8.1251251251';
  $this->entity->{self::FIELD_NAME} = $this
    ->generateFraction($decimal);
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(1, $violations);
  $this
    ->assertViolations($violations);
  $decimal = '8.12512512';
  $this->entity->{self::FIELD_NAME} = $this
    ->generateFraction($decimal);
  $violations = $this->entity
    ->validate();
  $this
    ->assertCount(0, $violations);
}