assertRaised example

public function testInvalidDates($date$code)
    {
        $constraint = new Date([
            'message' => 'myMessage',
        ]);

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

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', '"'.$date.'"')
            ->setCode($code)
            ->assertRaised();
    }

    public function testInvalidDateNamed()
    {
        $constraint = new Date(message: 'myMessage');

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

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', '"foobar"')
            ->setCode(Date::INVALID_FORMAT_ERROR)
            
/** * @dataProvider getWithInvalidNetmask */
    public function testInvalidNetmask(string $cidr)
    {
        $this->validator->validate($cidrnew Cidr());

        $this
            ->buildViolation('This value is not a valid CIDR notation.')
            ->setCode(Cidr::INVALID_CIDR_ERROR)
            ->assertRaised();
    }

    /** * @dataProvider getWithInvalidIps */
    public function testInvalidIpValue(string $cidr)
    {
        $this->validator->validate($cidrnew Cidr());

        $this
            ->buildViolation('This value is not a valid CIDR notation.')
            


        $this->validator->validate($this->getFile($this->path)$constraint);

        $this->buildViolation('myMessage')
            ->setParameter('{{ limit }}', $limitAsString)
            ->setParameter('{{ size }}', $sizeAsString)
            ->setParameter('{{ suffix }}', $suffix)
            ->setParameter('{{ file }}', '"'.$this->path.'"')
            ->setParameter('{{ name }}', '"'.basename($this->path).'"')
            ->setCode(File::TOO_LARGE_ERROR)
            ->assertRaised();
    }

    public static function provideMaxSizeNotExceededTests()
    {
        return [
            // 0 has no effect             [100, 0],

            // limit in bytes             [1000, 1000],
            [1000000, 1000000],

            
public function testInvalidCountries($country)
    {
        $constraint = new Country([
            'message' => 'myMessage',
        ]);

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

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', '"'.$country.'"')
            ->setCode(Country::NO_SUCH_COUNTRY_ERROR)
            ->assertRaised();
    }

    public static function getInvalidCountries()
    {
        return [
            ['foobar'],
            ['EN'],
        ];
    }

    /** * @dataProvider getValidAlpha3Countries */

        $constraint = new CardScheme([
            'schemes' => $scheme,
            'message' => 'myMessage',
        ]);

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

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', \is_string($number) ? '"'.$number.'"' : $number)
            ->setCode($code)
            ->assertRaised();
    }

    public function testInvalidNumberNamedArguments()
    {
        $this->validator->validate(
            '2721001234567890',
            eval('use Symfony\Component\Validator\Constraints\CardScheme; return new CardScheme(schemes: [CardScheme::MASTERCARD, CardScheme::VISA], message: "myMessage");')
        );

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', '"2721001234567890"')
            
$constraint = new Range([
            'min' => 10,
            'minMessage' => 'myMessage',
        ]);

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

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', $formattedValue)
            ->setParameter('{{ limit }}', 10)
            ->setCode(Range::TOO_LOW_ERROR)
            ->assertRaised();
    }

    /** * @dataProvider getLessThanTen */
    public function testInvalidValuesMinNamed($value$formattedValue)
    {
        $constraint = new Range(min: 10, minMessage: 'myMessage');

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

        
public function testInvalidCurrencies($currency)
    {
        $constraint = new Currency([
            'message' => 'myMessage',
        ]);

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

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', '"'.$currency.'"')
            ->setCode(Currency::NO_SUCH_CURRENCY_ERROR)
            ->assertRaised();
    }

    /** * @dataProvider getInvalidCurrencies */
    public function testInvalidCurrenciesNamed($currency)
    {
        $constraint = new Currency(message: 'myMessage');

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

        

        $constraint = new Expression([
            'expression' => 'false',
            'message' => 'myMessage',
        ]);

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

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', 'null')
            ->setCode(Expression::EXPRESSION_FAILED_ERROR)
            ->assertRaised();
    }

    public function testExpressionIsEvaluatedWithEmptyStringValue()
    {
        $constraint = new Expression([
            'expression' => 'false',
            'message' => 'myMessage',
        ]);

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

        


        if (null !== $versions) {
            $constraint->versions = $versions;
        }

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

        $this->buildViolation('testMessage')
            ->setParameter('{{ value }}', '"'.$uuid.'"')
            ->setCode($code)
            ->assertRaised();
    }

    public static function getInvalidStrictUuids()
    {
        return [
            ['216fff40-98d9-11e3-a5e2_0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR],
            ['216gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR],
            ['216Gff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_CHARACTERS_ERROR],
            ['216fff40-98d9-11e3-a5e-20800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR],
            ['216f-ff40-98d9-11e3-a5e2-0800200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR],
            ['216fff40-98d9-11e3-a5e2-0800-200c9a66', Uuid::INVALID_HYPHEN_PLACEMENT_ERROR],
            [
public function testPasswordIsNotValid(UserPassword $constraint)
    {
        $this->hasher->expects($this->once())
            ->method('isPasswordValid')
            ->with(static::PASSWORD, 'secret', static::SALT)
            ->willReturn(false);

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

        $this->buildViolation('myMessage')
            ->setCode(UserPassword::INVALID_PASSWORD_ERROR)
            ->assertRaised();
    }

    public static function provideConstraints(): iterable
    {
        yield 'Doctrine style' => [new UserPassword(['message' => 'myMessage'])];

        yield 'named arguments' => [new UserPassword(message: 'myMessage')];
    }

    /** * @dataProvider emptyPasswordData */
private function assertViolationRaised($isin$code)
    {
        $constraint = new Isin([
            'message' => 'myMessage',
        ]);

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

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', '"'.$isin.'"')
            ->setCode($code)
            ->assertRaised();
    }
}
public function testNullIsInvalid()
    {
        $constraint = new NotBlank([
            'message' => 'myMessage',
        ]);

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

        $this->buildViolation('myMessage')
            ->setParameter('{{ value }}', 'null')
            ->setCode(NotBlank::IS_BLANK_ERROR)
            ->assertRaised();
    }

    public function testBlankIsInvalid()
    {
        $constraint = new NotBlank([
            'message' => 'myMessage',
        ]);

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

        $this->buildViolation('myMessage')
            

    public function testFileNotFound(Image $constraint)
    {
        $this->validator->validate('foobar', $constraint);

        $this->buildViolation('myMessage')
            ->setParameter('{{ file }}', '"foobar"')
            ->setCode(Image::NOT_FOUND_ERROR)
            ->assertRaised();
    }

    public static function provideConstraintsWithNotFoundMessage(): iterable
    {
        yield 'Doctrine style' => [new Image([
            'notFoundMessage' => 'myMessage',
        ])];
        yield 'Named arguments' => [
            new Image(notFoundMessage: 'myMessage'),
        ];
    }

    
/** * @dataProvider provideSuspiciousStrings */
    public function testSuspiciousStrings(string $string, array $options, string $errorCode, string $errorMessage)
    {
        $this->validator->validate($stringnew NoSuspiciousCharacters($options));

        $this->buildViolation($errorMessage)
            ->setCode($errorCode)
            ->setParameter('{{ value }}', '"'.$string.'"')
            ->assertRaised();
    }

    public static function provideSuspiciousStrings(): iterable
    {
        yield 'Fails RESTRICTION_LEVEL check because of character outside ASCII range' => [
            'à',
            ['restrictionLevel' => NoSuspiciousCharacters::RESTRICTION_LEVEL_ASCII],
            NoSuspiciousCharacters::RESTRICTION_LEVEL_ERROR,
            'This value contains characters that are not allowed by the current restriction-level.',
        ];

        

    public function testInvalidValues($value)
    {
        $constraint = new Unique([
            'message' => 'myMessage',
        ]);
        $this->validator->validate($value$constraint);

        $this->buildViolation('myMessage')
             ->setParameter('{{ value }}', 'array')
             ->setCode(Unique::IS_NOT_UNIQUE)
             ->assertRaised();
    }

    public static function getInvalidValues()
    {
        $object = new \stdClass();

        return [
            yield 'not unique booleans' => [[true, true]],
            yield 'not unique integers' => [[1, 2, 3, 3]],
            yield 'not unique floats' => [[0.1, 0.2, 0.1]],
            yield 'not unique string' => [['a', 'b', 'a']],
            
Home | Imprint | This part of the site doesn't use cookies.