You are here

class CdnSchemeConstraintValidatorTest in CDN 8.3

@coversDefaultClass \Drupal\cdn\Plugin\Validation\Constraint\CdnSchemeConstraintValidator @group cdn

Hierarchy

Expanded class hierarchy of CdnSchemeConstraintValidatorTest

File

tests/src/Unit/Plugin/Validation/Constraint/CdnSchemeConstraintValidatorTest.php, line 16

Namespace

Drupal\Tests\cdn\Unit\Plugin\Validation\Constraint
View source
class CdnSchemeConstraintValidatorTest extends UnitTestCase {

  /**
   * @covers ::validate
   *
   * @dataProvider provideTestValidate
   */
  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 = [];

    // Valid schemes.
    $data['http://'] = [
      'http://',
      TRUE,
    ];
    $data['https://'] = [
      'https://',
      TRUE,
    ];
    $data['//'] = [
      '//',
      TRUE,
    ];

    // Scheme without `://`.
    $data['https'] = [
      'https',
      FALSE,
    ];
    $data['https:'] = [
      'https:',
      FALSE,
    ];
    $data['https:/'] = [
      'https:/',
      FALSE,
    ];

    // Disallowed schemes.
    $data['ftp://'] = [
      'ftp://',
      FALSE,
    ];
    $data['something://'] = [
      'ftp://',
      FALSE,
    ];

    // Non-scheme values.
    $data['/'] = [
      '/',
      FALSE,
    ];
    $data[''] = [
      '',
      FALSE,
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CdnSchemeConstraintValidatorTest::provideTestValidate public function
CdnSchemeConstraintValidatorTest::testValidate public function @covers ::validate
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340