MaxLengthValidatorTest.php 2.5 KB

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