Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. CompoundConstraintTestCase.php

function CompoundConstraintTestCase::assertViolationsRaisedByCompound

File

vendor/symfony/validator/Test/CompoundConstraintTestCase.php, line 67

Class

CompoundConstraintTestCase
A test case to ease testing Compound Constraints.

Namespace

Symfony\Component\Validator\Test

Code

public function assertViolationsRaisedByCompound(Constraint|array $constraints) : void {
    if ($constraints instanceof Constraint) {
        $constraints = [
            $constraints,
        ];
    }
    $validator = new CompoundValidator();
    $context = $this->createContext();
    $validator->initialize($context);
    $validator->validate($this->validatedValue, new class ($constraints) extends Compound {
        public function __construct(array $testedConstraints) {
            parent::__construct();
        }
        protected function getConstraints(array $options) : array {
            return $this->testedConstraints;
        }

});
    $expectedViolations = iterator_to_array($context->getViolations());
    if (!$expectedViolations) {
        throw new ExpectationFailedException(\sprintf('Expected at least one violation for constraint(s) "%s", got none raised.', implode(', ', array_map(fn($constraint) => $constraint::class, $constraints))));
    }
    $failedToAssertViolations = [];
    reset($expectedViolations);
    foreach ($this->context
        ->getViolations() as $violation) {
        if ($violation != current($expectedViolations)) {
            $failedToAssertViolations[] = $violation;
        }
        next($expectedViolations);
    }
    $this->assertEmpty($failedToAssertViolations, \sprintf('Expected violation(s) for constraint(s) %s to be raised by compound.', implode(', ', array_map(fn($violation) => $violation->getConstraint()::class, $failedToAssertViolations))));
}
RSS feed
Powered by Drupal