CountryConstraintValidatorTest.php in Address 8
File
tests/src/Unit/Plugin/Validation/Constraint/CountryConstraintValidatorTest.php
View source
<?php
namespace Drupal\Tests\address\Unit\Plugin\Validation\Constraint;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use Drupal\address\Plugin\Validation\Constraint\CountryConstraint;
use Drupal\address\Plugin\Validation\Constraint\CountryConstraintValidator;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
class CountryConstraintValidatorTest extends UnitTestCase {
protected $constraint;
protected $validator;
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());
}
public function testValidate($country_code, $expected_violation) {
$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);
}
public function providerTestValidate() {
$constraint = new CountryConstraint();
$cases = [];
$cases[] = [
NULL,
FALSE,
];
$cases[] = [
'',
FALSE,
];
$cases[] = [
'FR',
FALSE,
];
$cases[] = [
'InvalidValue',
$constraint->invalidMessage,
];
$cases[] = [
'RS',
$constraint->notAvailableMessage,
];
return $cases;
}
}