This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Validator/Constraints/MinLengthValidatorTest.php
Bernhard Schussek 9153f0e569 [Validator] Deprecated ConstraintValidator methods setMessage(), getMessageTemplate() and getMessageParameters()
Had to refactor the validation tests at the same time and fixed various small bugs while doing so.
2012-02-01 14:03:13 +01:00

128 lines
3.4 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MinLengthValidator;
class MinLengthValidatorTest extends \PHPUnit_Framework_TestCase
{
protected $context;
protected $validator;
protected function setUp()
{
$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
$this->validator = new MinLengthValidator();
$this->validator->initialize($this->context);
}
protected function tearDown()
{
$this->context = null;
$this->validator = null;
}
public function testNullIsValid()
{
$this->context->expects($this->never())
->method('addViolation');
$this->assertTrue($this->validator->isValid(null, new MinLength(array('limit' => 6))));
}
public function testEmptyStringIsValid()
{
$this->context->expects($this->never())
->method('addViolation');
$this->assertTrue($this->validator->isValid('', new MinLength(array('limit' => 6))));
}
/**
* @expectedException Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
public function testExpectsStringCompatibleType()
{
$this->validator->isValid(new \stdClass(), new MinLength(array('limit' => 5)));
}
/**
* @dataProvider getValidValues
*/
public function testValidValues($value, $mbOnly = false)
{
if ($mbOnly && !function_exists('mb_strlen')) {
return $this->markTestSkipped('mb_strlen does not exist');
}
$this->context->expects($this->never())
->method('addViolation');
$constraint = new MinLength(array('limit' => 6));
$this->assertTrue($this->validator->isValid($value, $constraint));
}
public function getValidValues()
{
return array(
array(123456),
array('123456'),
array('üüüüüü', true),
array('éééééé', true),
);
}
/**
* @dataProvider getInvalidValues
*/
public function testInvalidValues($value, $mbOnly = false)
{
if ($mbOnly && !function_exists('mb_strlen')) {
return $this->markTestSkipped('mb_strlen does not exist');
}
$constraint = new MinLength(array(
'limit' => 5,
'message' => 'myMessage'
));
$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => $value,
'{{ limit }}' => 5,
));
$this->assertFalse($this->validator->isValid($value, $constraint));
}
public function getInvalidValues()
{
return array(
array(1234),
array('1234'),
array('üüüü', true),
array('éééé', true),
);
}
public function testConstraintGetDefaultOption()
{
$constraint = new MinLength(array(
'limit' => 5,
));
$this->assertEquals('limit', $constraint->getDefaultOption());
}
}