MinLengthValidatorTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. use Symfony\Component\Validator\Constraints\MinLength;
  4. use Symfony\Component\Validator\Constraints\MinLengthValidator;
  5. class MinLengthValidatorTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $validator;
  8. public function setUp()
  9. {
  10. $this->validator = new MinLengthValidator();
  11. }
  12. public function testNullIsValid()
  13. {
  14. $this->assertTrue($this->validator->isValid(null, new MinLength(array('limit' => 6))));
  15. }
  16. public function testExpectsStringCompatibleType()
  17. {
  18. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  19. $this->validator->isValid(new \stdClass(), new MinLength(array('limit' => 5)));
  20. }
  21. /**
  22. * @dataProvider getValidValues
  23. */
  24. public function testValidValues($value, $skip = false)
  25. {
  26. if (!$skip) {
  27. $constraint = new MinLength(array('limit' => 6));
  28. $this->assertTrue($this->validator->isValid($value, $constraint));
  29. }
  30. }
  31. public function getValidValues()
  32. {
  33. return array(
  34. array(123456),
  35. array('123456'),
  36. array('üüüüüü', !function_exists('mb_strlen')),
  37. array('éééééé', !function_exists('mb_strlen')),
  38. );
  39. }
  40. /**
  41. * @dataProvider getInvalidValues
  42. */
  43. public function testInvalidValues($value, $skip = false)
  44. {
  45. if (!$skip) {
  46. $constraint = new MinLength(array('limit' => 6));
  47. $this->assertFalse($this->validator->isValid($value, $constraint));
  48. }
  49. }
  50. public function getInvalidValues()
  51. {
  52. return array(
  53. array(12345),
  54. array('12345'),
  55. array('üüüüü', !function_exists('mb_strlen')),
  56. array('ééééé', !function_exists('mb_strlen')),
  57. );
  58. }
  59. public function testMessageIsSet()
  60. {
  61. $constraint = new MinLength(array(
  62. 'limit' => 5,
  63. 'message' => 'myMessage'
  64. ));
  65. $this->assertFalse($this->validator->isValid('1234', $constraint));
  66. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  67. $this->assertEquals($this->validator->getMessageParameters(), array(
  68. '{{ value }}' => '1234',
  69. '{{ limit }}' => 5,
  70. ));
  71. }
  72. }