assertNoViolation example

class CallbackValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): CallbackValidator
    {
        return new CallbackValidator();
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new Callback());

        $this->assertNoViolation();
    }

    public function testSingleMethod()
    {
        $object = new CallbackValidatorTest_Object();
        $constraint = new Callback('validate');

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

        $this->buildViolation('My message')
            ->setParameter('{{ value }}', 'foobar')
            
public function testValidate()
    {
        $object = new \stdClass();
        $options = ['validation_groups' => ['group1', 'group2']];
        $form = $this->getCompoundForm($object$options);
        $form->submit([]);

        $this->expectValidateAt(0, 'data', $object['group1', 'group2']);

        $this->validator->validate($formnew Form());

        $this->assertNoViolation();
    }

    public function testValidateConstraints()
    {
        $object = new \stdClass();
        $constraint1 = new NotNull(['groups' => ['group1', 'group2']]);
        $constraint2 = new NotBlank(['groups' => 'group2']);
        $constraint3 = new Length(['groups' => 'group2', 'min' => 3]);

        $options = [
            'validation_groups' => ['group1', 'group2'],
            
protected function createValidator(): TypeValidator
    {
        return new TypeValidator();
    }

    public function testNullIsValid()
    {
        $constraint = new Type(['type' => 'integer']);

        $this->validator->validate(null, $constraint);

        $this->assertNoViolation();
    }

    public function testEmptyIsValidIfString()
    {
        $constraint = new Type(['type' => 'string']);

        $this->validator->validate('', $constraint);

        $this->assertNoViolation();
    }

    

    public function testPasswordIsValid(UserPassword $constraint)
    {
        $this->hasher->expects($this->once())
            ->method('isPasswordValid')
            ->with(static::PASSWORD, 'secret', static::SALT)
            ->willReturn(true);

        $this->validator->validate('secret', $constraint);

        $this->assertNoViolation();
    }

    /** * @dataProvider provideConstraints */
    public function testPasswordIsNotValid(UserPassword $constraint)
    {
        $this->hasher->expects($this->once())
            ->method('isPasswordValid')
            ->with(static::PASSWORD, 'secret', static::SALT)
            ->willReturn(false);

        

        return new NotNullValidator();
    }

    /** * @dataProvider getValidValues */
    public function testValidValues($value)
    {
        $this->validator->validate($valuenew NotNull());

        $this->assertNoViolation();
    }

    public static function getValidValues()
    {
        return [
            [0],
            [false],
            [true],
            [''],
        ];
    }

    
class UrlValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): UrlValidator
    {
        return new UrlValidator();
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new Url());

        $this->assertNoViolation();
    }

    public function testEmptyStringIsValid()
    {
        $this->validator->validate('', new Url());

        $this->assertNoViolation();
    }

    public function testEmptyStringFromObjectIsValid()
    {
        
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'),
            new Regex(['pattern' => '[a-z]']),
            new NotEqualTo('Foo'),
        ];

        $value = 'Foo';

        
if (file_exists($this->path)) {
            @unlink($this->path);
        }

        $this->file = null;
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new File());

        $this->assertNoViolation();
    }

    public function testEmptyStringIsValid()
    {
        $this->validator->validate('', new File());

        $this->assertNoViolation();
    }

    public function testExpectsStringCompatibleTypeOrFile()
    {
        
return new CollectionValidator();
    }

    abstract protected function prepareTestData(array $contents);

    public function testNullIsValid()
    {
        $this->validator->validate(null, new Collection(['fields' => [
            'foo' => new Range(['min' => 4]),
        ]]));

        $this->assertNoViolation();
    }

    public function testFieldsAsDefaultOption()
    {
        $constraint = new Range(['min' => 4]);

        $data = $this->prepareTestData(['foo' => 'foobar']);

        $this->expectValidateValueAt(0, '[foo]', $data['foo'][$constraint]);

        $this->validator->validate($datanew Collection([
            
class IbanValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): IbanValidator
    {
        return new IbanValidator();
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new Iban());

        $this->assertNoViolation();
    }

    public function testEmptyStringIsValid()
    {
        $this->validator->validate('', new Iban());

        $this->assertNoViolation();
    }

    /** * @dataProvider getValidIbans */
'1684-537X', Issn::CHECKSUM_FAILED_ERROR],
            ['1996-0795', Issn::CHECKSUM_FAILED_ERROR],
        ];
    }

    public function testNullIsValid()
    {
        $constraint = new Issn();

        $this->validator->validate(null, $constraint);

        $this->assertNoViolation();
    }

    public function testEmptyStringIsValid()
    {
        $constraint = new Issn();

        $this->validator->validate('', $constraint);

        $this->assertNoViolation();
    }

    
class BlankValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): BlankValidator
    {
        return new BlankValidator();
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new Blank());

        $this->assertNoViolation();
    }

    public function testBlankIsValid()
    {
        $this->validator->validate('', new Blank());

        $this->assertNoViolation();
    }

    /** * @dataProvider getInvalidValues */
protected function createValidator(): ConstraintValidatorInterface
    {
        // Pass HttpClient::create() instead of the mock to run the tests against the real API         return new NotCompromisedPasswordValidator($this->createHttpClientStub());
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new NotCompromisedPassword());

        $this->assertNoViolation();
    }

    public function testEmptyStringIsValid()
    {
        $this->validator->validate('', new NotCompromisedPassword());

        $this->assertNoViolation();
    }

    public function testInvalidPasswordButDisabled()
    {
        
class ExpressionSyntaxValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): ExpressionSyntaxValidator
    {
        return new ExpressionSyntaxValidator(new ExpressionLanguage());
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new ExpressionSyntax());

        $this->assertNoViolation();
    }

    public function testEmptyStringIsValid()
    {
        $this->validator->validate('', new ExpressionSyntax());

        $this->assertNoViolation();
    }

    public function testExpressionValid()
    {
        
class LengthValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): LengthValidator
    {
        return new LengthValidator();
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new Length(['value' => 6]));

        $this->assertNoViolation();
    }

    public function testEmptyStringIsInvalid()
    {
        $this->validator->validate('', new Length([
            'value' => $limit = 6,
            'exactMessage' => 'myMessage',
        ]));

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', '""')
            
Home | Imprint | This part of the site doesn't use cookies.