function AllowedValuesConstraintValidatorTest::testValidationCallbackException

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php \Drupal\KernelTests\Core\TypedData\AllowedValuesConstraintValidatorTest::testValidationCallbackException()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php \Drupal\KernelTests\Core\TypedData\AllowedValuesConstraintValidatorTest::testValidationCallbackException()
  3. 10 core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php \Drupal\KernelTests\Core\TypedData\AllowedValuesConstraintValidatorTest::testValidationCallbackException()

Tests the AllowedValuesConstraintValidator with an invalid callback.

File

core/tests/Drupal/KernelTests/Core/TypedData/AllowedValuesConstraintValidatorTest.php, line 108

Class

AllowedValuesConstraintValidatorTest
Tests AllowedValues validation constraint with both valid and invalid values.

Namespace

Drupal\KernelTests\Core\TypedData

Code

public function testValidationCallbackException() : void {
    // Create a definition that specifies some AllowedValues and a callback.
    // This tests that callbacks have a higher priority than a supplied list of
    // values and can be used to coerce the value to the correct type.
    $definition = DataDefinition::create('string')->addConstraint('AllowedValues', [
        'choices' => [
            1,
            2,
            3,
        ],
        'callback' => [
            static::class,
            'doesNotExist',
        ],
    ]);
    $typed_data = $this->typedData
        ->create($definition, 1);
    $this->expectException(ConstraintDefinitionException::class);
    $this->expectExceptionMessage('The AllowedValuesConstraint constraint expects a valid callback');
    $typed_data->validate();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.