class RangeFromGreaterToConstraintValidatorTest in Range 8
Tests the RangeFromGreaterToConstraintValidator validator.
@coversDefaultClass \Drupal\range\Plugin\Validation\Constraint\RangeFromGreaterToConstraintValidator @group range
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\range\Unit\Plugin\Validation\Constraint\RangeFromGreaterToConstraintValidatorTest
Expanded class hierarchy of RangeFromGreaterToConstraintValidatorTest
File
- tests/
src/ Unit/ Plugin/ Validation/ Constraint/ RangeFromGreaterToConstraintValidatorTest.php, line 18
Namespace
Drupal\Tests\range\Unit\Plugin\Validation\ConstraintView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
RangeFromGreaterToConstraintValidatorTest:: |
public | function | Data provider for testValidate(). | |
RangeFromGreaterToConstraintValidatorTest:: |
public | function | @covers ::validate | |
RangeFromGreaterToConstraintValidatorTest:: |
public | function | Tests the RangeFromGreaterToConstraintValidator::validate() method. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |