2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Tests\Components\Validator;
|
|
|
|
|
|
|
|
use Symfony\Components\Validator\Constraints\MinLength;
|
|
|
|
use Symfony\Components\Validator\Constraints\MinLengthValidator;
|
|
|
|
|
|
|
|
class MinLengthValidatorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $validator;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->validator = new MinLengthValidator();
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testNullIsValid()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->validator->isValid(null, new MinLength(array('limit' => 6))));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testExpectsStringCompatibleType()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('Symfony\Components\Validator\Exception\UnexpectedTypeException');
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->validator->isValid(new \stdClass(), new MinLength(array('limit' => 5)));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider getValidValues
|
|
|
|
*/
|
|
|
|
public function testValidValues($value, $skip = false)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!$skip) {
|
|
|
|
$constraint = new MinLength(array('limit' => 6));
|
|
|
|
$this->assertTrue($this->validator->isValid($value, $constraint));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getValidValues()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(123456),
|
|
|
|
array('123456'),
|
|
|
|
array('üüüüüü', !function_exists('mb_strlen')),
|
|
|
|
array('éééééé', !function_exists('mb_strlen')),
|
|
|
|
);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider getInvalidValues
|
|
|
|
*/
|
|
|
|
public function testInvalidValues($value, $skip = false)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
if (!$skip) {
|
|
|
|
$constraint = new MinLength(array('limit' => 6));
|
|
|
|
$this->assertFalse($this->validator->isValid($value, $constraint));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getInvalidValues()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(12345),
|
|
|
|
array('12345'),
|
|
|
|
array('üüüüü', !function_exists('mb_strlen')),
|
|
|
|
array('ééééé', !function_exists('mb_strlen')),
|
|
|
|
);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
public function testMessageIsSet()
|
|
|
|
{
|
|
|
|
$constraint = new MinLength(array(
|
|
|
|
'limit' => 5,
|
|
|
|
'message' => 'myMessage'
|
|
|
|
));
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->assertFalse($this->validator->isValid('1234', $constraint));
|
|
|
|
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
|
|
|
|
$this->assertEquals($this->validator->getMessageParameters(), array(
|
|
|
|
'value' => '1234',
|
|
|
|
'limit' => 5,
|
|
|
|
));
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|