MinLengthValidatorTest.php 2.2 KB

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