MinLengthValidatorTest.php 2.7 KB

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