DivisibleBy example


class DivisibleByValidatorTest extends AbstractComparisonValidatorTestCase
{
    protected function createValidator(): DivisibleByValidator
    {
        return new DivisibleByValidator();
    }

    protected static function createConstraint(array $options = null): Constraint
    {
        return new DivisibleBy($options);
    }

    protected function getErrorCode(): ?string
    {
        return DivisibleBy::NOT_DIVISIBLE_BY;
    }

    public static function provideValidComparisons(): array
    {
        return [
            [-7, 1],
            [
new EqualTo(['value' => 'symfony']),
            ]],
            [150, [
                new Range(['min' => 10, 'max' => 20]),
                new GreaterThanOrEqual(['value' => 100]),
            ]],
            [7, [
                new LessThan(['value' => 5]),
                new IdenticalTo(['value' => 7]),
            ]],
            [-3, [
                new DivisibleBy(['value' => 4]),
                new Negative(),
            ]],
            ['FOO', [
                new Choice(['choices' => ['bar', 'BAR']]),
                new Regex(['pattern' => '/foo/i']),
            ]],
            ['fr', [
                new Country(),
                new Language(),
            ]],
            [[1, 3, 5][
                
->setCode($exactlyOptionEnabled ? Count::NOT_EQUAL_COUNT_ERROR : Count::TOO_FEW_ERROR)
                ->addViolation();

            return;
        }

        if (null !== $constraint->divisibleBy) {
            $this->context
                ->getValidator()
                ->inContext($this->context)
                ->validate($count[
                    new DivisibleBy([
                        'value' => $constraint->divisibleBy,
                        'message' => $constraint->divisibleByMessage,
                    ]),
                ]$this->context->getGroup());
        }
    }
}


    // 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();
    }
}
->setCode($exactlyOptionEnabled ? Count::NOT_EQUAL_COUNT_ERROR : Count::TOO_FEW_ERROR)
                ->addViolation();

            return;
        }

        if (null !== $constraint->divisibleBy) {
            $this->context
                ->getValidator()
                ->inContext($this->context)
                ->validate($count[
                    new DivisibleBy([
                        'value' => $constraint->divisibleBy,
                        'message' => $constraint->divisibleByMessage,
                    ]),
                ]$this->context->getGroup());
        }
    }
}
Home | Imprint | This part of the site doesn't use cookies.