You are here

class RangeFromGreaterToConstraintValidatorTest in Range 8

Tests the RangeFromGreaterToConstraintValidator validator.

@coversDefaultClass \Drupal\range\Plugin\Validation\Constraint\RangeFromGreaterToConstraintValidator @group range

Hierarchy

Expanded class hierarchy of RangeFromGreaterToConstraintValidatorTest

File

tests/src/Unit/Plugin/Validation/Constraint/RangeFromGreaterToConstraintValidatorTest.php, line 18

Namespace

Drupal\Tests\range\Unit\Plugin\Validation\Constraint
View source
class RangeFromGreaterToConstraintValidatorTest extends UnitTestCase {

  /**
   * Tests the RangeFromGreaterToConstraintValidator::validate() method.
   *
   * @param \Drupal\range\RangeItemInterface $value
   *   Range item.
   * @param bool $valid
   *   A boolean indicating if the combination is expected to be valid.
   *
   * @covers ::validate
   * @dataProvider providerValidate
   */
  public function testValidate(RangeItemInterface $value, $valid) {
    $context = $this
      ->createMock(ExecutionContextInterface::class);
    if ($valid) {
      $context
        ->expects($this
        ->never())
        ->method('addViolation');
    }
    else {
      $context
        ->expects($this
        ->once())
        ->method('addViolation');
    }
    $constraint = new RangeFromGreaterToConstraint();
    $validate = new RangeFromGreaterToConstraintValidator();
    $validate
      ->initialize($context);
    $validate
      ->validate($value, $constraint);
  }

  /**
   * Data provider for testValidate().
   *
   * @return array
   *   Nested arrays of values to check:
   *     - $item
   *     - $valid
   */
  public function providerValidate() {
    $data = [];
    $cases = [
      [
        'range' => [
          'from' => 5,
          'to' => 10,
        ],
        'valid' => TRUE,
      ],
      [
        'range' => [
          'from' => 10,
          'to' => 10,
        ],
        'valid' => TRUE,
      ],
      [
        'range' => [
          'from' => 10,
          'to' => 5,
        ],
        'valid' => FALSE,
      ],
    ];
    foreach ($cases as $case) {
      $item = $this
        ->createMock('Drupal\\range\\RangeItemInterface');
      $item
        ->expects($this
        ->any())
        ->method('getValue')
        ->willReturn($case['range']);
      $data[] = [
        $item,
        $case['valid'],
      ];
    }
    return $data;
  }

  /**
   * @covers ::validate
   */
  public function testInvalidValueType() {
    $context = $this
      ->createMock(ExecutionContextInterface::class);
    $constraint = new RangeFromGreaterToConstraint();
    $validate = new RangeFromGreaterToConstraintValidator();
    $validate
      ->initialize($context);
    $this
      ->expectException(UnexpectedTypeException::class);
    $validate
      ->validate(new \stdClass(), $constraint);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RangeFromGreaterToConstraintValidatorTest::providerValidate public function Data provider for testValidate().
RangeFromGreaterToConstraintValidatorTest::testInvalidValueType public function @covers ::validate
RangeFromGreaterToConstraintValidatorTest::testValidate public function Tests the RangeFromGreaterToConstraintValidator::validate() method.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340