Problem/Motivation
I'm unable to get full coverage for Drupal\Component\Plugin\Context\Context
in
#2378311: Expand unit testing for Drupal\Component\Plugin\Context\Context β
, because the validate()
method depends on a static factory method.
It would be much easier to test behavior, and better in general, to depend on an optionally-injected instance of Symfony\Component\Validator\ValidatorInterface
.
Here's the method as it exists now:
public function validate() {
$validator = Validation::createValidatorBuilder()
->getValidator();
return $validator->validateValue($this->getContextValue(), $this->getConstraints());
}
I propose a change like this:
public function validate(ValidatorInterface $validator = NULL) {
if (NULL === $validator) {
$validator = Validation::createValidatorBuilder()->getValidator();
}
return $validator->validateValue($this->getContextValue(), $this->getConstraints());
}
This way the behavior of the method is isolated from the implementation of Validation::createValidatorBuilder()
, but still allows users of this interface to let Context build a validator for them.
Proposed resolution
Context::validate()
is part of Drupal\Component\Plugin\Context\ContextInterface
, so this change would be to the interface, not just Context
. Context
would then implement this change.
Remaining tasks
User interface changes
API changes
Change ContextInterface::validate()
's signature to this:
/**
* Validates the set context value.
*
* @param Symfony\Component\Validator\ValidatorInterface $validator
* (optional) A validator object. If none is supplied, one will be created.
*
* @return \Symfony\Component\Validator\ConstraintViolationListInterface
* A list of constraint violations. If the list is empty, validation
* succeeded.
*/
public function validate(ValidatorInterface $validator = NULL);