expectValidateValue example



    public function testWalkThroughConstraints()
    {
        $constraints = [
            new Type('number'),
            new Range(['min' => 4]),
        ];

        $value = 6;

        $this->expectValidateValue(0, $value[$constraints[0]]);
        $this->expectValidateValue(1, $value[$constraints[1]]);

        $this->validator->validate($valuenew Sequentially($constraints));

        $this->assertNoViolation();
    }

    public function testStopsAtFirstConstraintWithViolations()
    {
        $constraints = [
            new Type('string'),
            
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

final class WhenValidatorTest extends ConstraintValidatorTestCase
{
    public function testConstraintsAreExecuted()
    {
        $constraints = [
            new NotNull(),
            new NotBlank(),
        ];

        $this->expectValidateValue(0, 'Foo', $constraints);

        $this->validator->validate('Foo', new When([
            'expression' => 'true',
            'constraints' => $constraints,
        ]));
    }

    public function testConstraintIsExecuted()
    {
        $constraint = new NotNull();
        $this->expectValidateValue(0, 'Foo', [$constraint]);

        


    // Since the contextual validator is mocked, this test only asserts that it     // is called with the right DivisibleBy constraint.     public function testDivisibleBy()
    {
        $constraint = new Count([
            'divisibleBy' => 123,
            'divisibleByMessage' => 'foo {{ compared_value }}',
        ]);

        $this->expectValidateValue(0, 3, [new DivisibleBy([
            'value' => 123,
            'message' => 'foo {{ compared_value }}',
        ])]$this->group);

        $this->validator->validate(['foo', 'bar', 'ccc']$constraint);

        $this->assertNoViolation();
    }
}
$constraints = [new NotNull()new Range(['min' => 2])];

        $constraint = new Expression(
            ['expression' => 'is_valid(this.data, a)', 'values' => ['a' => $constraints]]
        );

        $object = new Entity();
        $object->data = 7;

        $this->setObject($object);

        $this->expectValidateValue(0, $object->data, $constraints);

        $this->validator->validate($object$constraint);

        $this->assertNoViolation();
    }

    public function testIsValidExpressionInvalid()
    {
        $constraints = [new Range(['min' => 2, 'max' => 5])];

        $constraint = new Expression(
            [

        $this->validator->validate('foo', new DummyCompoundConstraint());

        $this->assertNoViolation();
    }

    public function testValidateWithConstraints()
    {
        $value = 'foo';
        $constraint = new DummyCompoundConstraint();

        $this->expectValidateValue(0, $value$constraint->constraints);

        $this->validator->validate($value$constraint);

        $this->assertNoViolation();
    }
}

class DummyCompoundConstraint extends Compound
{
    protected function getConstraints(array $options): array
    {
        
Home | Imprint | This part of the site doesn't use cookies.