class RangeBothValuesRequiredConstraintValidatorTest in Range 8
Tests the RangeBothValuesRequiredConstraintValidator validator.
@coversDefaultClass \Drupal\range\Plugin\Validation\Constraint\RangeBothValuesRequiredConstraintValidator @group range
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\range\Unit\Plugin\Validation\Constraint\RangeBothValuesRequiredConstraintValidatorTest
Expanded class hierarchy of RangeBothValuesRequiredConstraintValidatorTest
File
- tests/
src/ Unit/ Plugin/ Validation/ Constraint/ RangeBothValuesRequiredConstraintValidatorTest.php, line 18
Namespace
Drupal\Tests\range\Unit\Plugin\Validation\ConstraintView source
class RangeBothValuesRequiredConstraintValidatorTest extends UnitTestCase {
/**
* Tests the RangeBothValuesRequiredConstraintValidator::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 RangeBothValuesRequiredConstraint();
$validate = new RangeBothValuesRequiredConstraintValidator();
$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' => '',
'to' => 10,
],
'valid' => FALSE,
],
[
'range' => [
'from' => 10,
'to' => '',
],
'valid' => FALSE,
],
[
'range' => [
'from' => '',
'to' => '',
],
'valid' => FALSE,
],
[
'range' => [
'from' => NULL,
'to' => 10,
],
'valid' => FALSE,
],
[
'range' => [
'from' => 10,
'to' => NULL,
],
'valid' => FALSE,
],
[
'range' => [
'from' => NULL,
'to' => NULL,
],
'valid' => FALSE,
],
[
'range' => [
'from' => 0,
'to' => 0,
],
'valid' => TRUE,
],
[
'range' => [
'from' => 0.0,
'to' => 0.0,
],
'valid' => TRUE,
],
[
'range' => [
'from' => 10,
'to' => 10,
],
'valid' => TRUE,
],
];
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 RangeBothValuesRequiredConstraint();
$validate = new RangeBothValuesRequiredConstraintValidator();
$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. | |
RangeBothValuesRequiredConstraintValidatorTest:: |
public | function | Data provider for testValidate(). | |
RangeBothValuesRequiredConstraintValidatorTest:: |
public | function | @covers ::validate | |
RangeBothValuesRequiredConstraintValidatorTest:: |
public | function | Tests the RangeBothValuesRequiredConstraintValidator::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 |