class CountryConstraintValidatorTest in Address 8
@coversDefaultClass \Drupal\address\Plugin\Validation\Constraint\CountryConstraintValidator @group address
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\address\Unit\Plugin\Validation\Constraint\CountryConstraintValidatorTest
Expanded class hierarchy of CountryConstraintValidatorTest
File
- tests/
src/ Unit/ Plugin/ Validation/ Constraint/ CountryConstraintValidatorTest.php, line 17
Namespace
Drupal\Tests\address\Unit\Plugin\Validation\ConstraintView source
class CountryConstraintValidatorTest extends UnitTestCase {
/**
* The constraint.
*
* @var \Drupal\address\Plugin\Validation\Constraint\CountryConstraint
*/
protected $constraint;
/**
* The validator.
*
* @var \Drupal\address\Plugin\Validation\Constraint\CountryConstraintValidator
*/
protected $validator;
/**
* {@inheritdoc}
*/
public function setUp() : void {
$country_repository = $this
->prophesize(CountryRepositoryInterface::class);
$country_repository
->getList()
->willReturn([
'RS' => 'Serbia',
'FR' => 'France',
]);
$this->constraint = new CountryConstraint([
'availableCountries' => [
'FR',
],
]);
$this->validator = new CountryConstraintValidator($country_repository
->reveal());
}
/**
* @covers ::validate
*
* @dataProvider providerTestValidate
*/
public function testValidate($country_code, $expected_violation) {
// If a violation is expected, then the context's buildViolation method
// will be called, otherwise it should not be called.
$context = $this
->prophesize(ExecutionContextInterface::class);
if ($expected_violation) {
$violation_builder = $this
->prophesize(ConstraintViolationBuilderInterface::class);
$violation_builder
->setParameter('%value', Argument::any())
->willReturn($violation_builder);
$violation_builder
->addViolation()
->willReturn($violation_builder);
$context
->buildViolation($expected_violation)
->willReturn($violation_builder
->reveal())
->shouldBeCalled();
}
else {
$context
->buildViolation(Argument::any())
->shouldNotBeCalled();
}
$this->validator
->initialize($context
->reveal());
$this->validator
->validate($country_code, $this->constraint);
}
/**
* Data provider for ::testValidate().
*/
public function providerTestValidate() {
// Data provides run before setUp, so $this->constraint is not available.
$constraint = new CountryConstraint();
$cases = [];
// Case 1: Empty values.
$cases[] = [
NULL,
FALSE,
];
$cases[] = [
'',
FALSE,
];
// Case 2: Valid country.
$cases[] = [
'FR',
FALSE,
];
// Case 3: Invalid country.
$cases[] = [
'InvalidValue',
$constraint->invalidMessage,
];
// Case 4: Valid, but unavailable country.
$cases[] = [
'RS',
$constraint->notAvailableMessage,
];
return $cases;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CountryConstraintValidatorTest:: |
protected | property | The constraint. | |
CountryConstraintValidatorTest:: |
protected | property | The validator. | |
CountryConstraintValidatorTest:: |
public | function | Data provider for ::testValidate(). | |
CountryConstraintValidatorTest:: |
public | function |
Overrides UnitTestCase:: |
|
CountryConstraintValidatorTest:: |
public | function | @covers ::validate | |
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. | |
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. |