MaxLengthValidatorTest.php 2.4 KB

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