MaxLengthValidatorTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraints\MaxLength;
  12. use Symfony\Component\Validator\Constraints\MaxLengthValidator;
  13. class MaxLengthValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $validator;
  16. protected function setUp()
  17. {
  18. $this->validator = new MaxLengthValidator();
  19. }
  20. protected function tearDown()
  21. {
  22. $this->validator = null;
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->assertTrue($this->validator->isValid(null, new MaxLength(array('limit' => 5))));
  27. }
  28. public function testEmptyStringIsValid()
  29. {
  30. $this->assertTrue($this->validator->isValid('', new MaxLength(array('limit' => 5))));
  31. }
  32. public function testExpectsStringCompatibleType()
  33. {
  34. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  35. $this->validator->isValid(new \stdClass(), new MaxLength(array('limit' => 5)));
  36. }
  37. /**
  38. * @dataProvider getValidValues
  39. */
  40. public function testValidValues($value, $skip = false)
  41. {
  42. if (!$skip) {
  43. $constraint = new MaxLength(array('limit' => 5));
  44. $this->assertTrue($this->validator->isValid($value, $constraint));
  45. }
  46. }
  47. public function getValidValues()
  48. {
  49. return array(
  50. array(12345),
  51. array('12345'),
  52. array('üüüüü', !function_exists('mb_strlen')),
  53. array('ééééé', !function_exists('mb_strlen')),
  54. );
  55. }
  56. /**
  57. * @dataProvider getInvalidValues
  58. */
  59. public function testInvalidValues($value, $skip = false)
  60. {
  61. if (!$skip) {
  62. $constraint = new MaxLength(array('limit' => 5));
  63. $this->assertFalse($this->validator->isValid($value, $constraint));
  64. }
  65. }
  66. public function getInvalidValues()
  67. {
  68. return array(
  69. array(123456),
  70. array('123456'),
  71. array('üüüüüü', !function_exists('mb_strlen')),
  72. array('éééééé', !function_exists('mb_strlen')),
  73. );
  74. }
  75. public function testMessageIsSet()
  76. {
  77. $constraint = new MaxLength(array(
  78. 'limit' => 5,
  79. 'message' => 'myMessage'
  80. ));
  81. $this->assertFalse($this->validator->isValid('123456', $constraint));
  82. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  83. $this->assertEquals($this->validator->getMessageParameters(), array(
  84. '{{ value }}' => '123456',
  85. '{{ limit }}' => 5,
  86. ));
  87. }
  88. public function testConstraintGetDefaultOption()
  89. {
  90. $constraint = new MaxLength(array(
  91. 'limit' => 5,
  92. ));
  93. $this->assertEquals('limit', $constraint->getDefaultOption());
  94. }
  95. }