CdnSchemeConstraintValidatorTest.php in CDN 8.3
File
tests/src/Unit/Plugin/Validation/Constraint/CdnSchemeConstraintValidatorTest.php
View source
<?php
namespace Drupal\Tests\cdn\Unit\Plugin\Validation\Constraint;
use Drupal\cdn\Plugin\Validation\Constraint\CdnSchemeConstraint;
use Drupal\cdn\Plugin\Validation\Constraint\CdnSchemeConstraintValidator;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;
class CdnSchemeConstraintValidatorTest extends UnitTestCase {
public function testValidate($value, $valid) {
$constraint_violation_builder = $this
->prophesize(ConstraintViolationBuilderInterface::class);
$constraint_violation_builder
->setParameter('%scheme', $value)
->willReturn($constraint_violation_builder
->reveal());
$constraint_violation_builder
->setInvalidValue($value)
->willReturn($constraint_violation_builder
->reveal());
$constraint_violation_builder
->addViolation()
->willReturn($constraint_violation_builder
->reveal());
if ($valid) {
$constraint_violation_builder
->addViolation()
->shouldNotBeCalled();
}
else {
$constraint_violation_builder
->addViolation()
->shouldBeCalled();
}
$context = $this
->prophesize(ExecutionContextInterface::class);
$context
->buildViolation(Argument::type('string'))
->willReturn($constraint_violation_builder
->reveal());
$constraint = new CdnSchemeConstraint();
$validate = new CdnSchemeConstraintValidator();
$validate
->initialize($context
->reveal());
$validate
->validate($value, $constraint);
}
public function provideTestValidate() {
$data = [];
$data['http://'] = [
'http://',
TRUE,
];
$data['https://'] = [
'https://',
TRUE,
];
$data['//'] = [
'//',
TRUE,
];
$data['https'] = [
'https',
FALSE,
];
$data['https:'] = [
'https:',
FALSE,
];
$data['https:/'] = [
'https:/',
FALSE,
];
$data['ftp://'] = [
'ftp://',
FALSE,
];
$data['something://'] = [
'ftp://',
FALSE,
];
$data['/'] = [
'/',
FALSE,
];
$data[''] = [
'',
FALSE,
];
return $data;
}
}