LengthValidatorTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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\Component\Validator\Tests\Constraints;
  11. use Symfony\Component\Validator\Constraints\Length;
  12. use Symfony\Component\Validator\Constraints\LengthValidator;
  13. use Symfony\Component\Validator\Validation;
  14. class LengthValidatorTest extends AbstractConstraintValidatorTest
  15. {
  16. protected function getApiVersion()
  17. {
  18. return Validation::API_VERSION_2_5;
  19. }
  20. protected function createValidator()
  21. {
  22. return new LengthValidator();
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->validator->validate(null, new Length(6));
  27. $this->assertNoViolation();
  28. }
  29. public function testEmptyStringIsValid()
  30. {
  31. $this->validator->validate('', new Length(6));
  32. $this->assertNoViolation();
  33. }
  34. /**
  35. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  36. */
  37. public function testExpectsStringCompatibleType()
  38. {
  39. $this->validator->validate(new \stdClass(), new Length(5));
  40. }
  41. public function getThreeOrLessCharacters()
  42. {
  43. return array(
  44. array(12),
  45. array('12'),
  46. array('üü'),
  47. array('éé'),
  48. array(123),
  49. array('123'),
  50. array('üüü'),
  51. array('ééé'),
  52. );
  53. }
  54. public function getFourCharacters()
  55. {
  56. return array(
  57. array(1234),
  58. array('1234'),
  59. array('üüüü'),
  60. array('éééé'),
  61. );
  62. }
  63. public function getFiveOrMoreCharacters()
  64. {
  65. return array(
  66. array(12345),
  67. array('12345'),
  68. array('üüüüü'),
  69. array('ééééé'),
  70. array(123456),
  71. array('123456'),
  72. array('üüüüüü'),
  73. array('éééééé'),
  74. );
  75. }
  76. public function getOneCharset()
  77. {
  78. return array(
  79. array('é', 'utf8', true),
  80. array("\xE9", 'CP1252', true),
  81. array("\xE9", 'XXX', false),
  82. array("\xE9", 'utf8', false),
  83. );
  84. }
  85. /**
  86. * @dataProvider getFiveOrMoreCharacters
  87. */
  88. public function testValidValuesMin($value)
  89. {
  90. $constraint = new Length(array('min' => 5));
  91. $this->validator->validate($value, $constraint);
  92. $this->assertNoViolation();
  93. }
  94. /**
  95. * @dataProvider getThreeOrLessCharacters
  96. */
  97. public function testValidValuesMax($value)
  98. {
  99. $constraint = new Length(array('max' => 3));
  100. $this->validator->validate($value, $constraint);
  101. $this->assertNoViolation();
  102. }
  103. /**
  104. * @dataProvider getFourCharacters
  105. */
  106. public function testValidValuesExact($value)
  107. {
  108. $constraint = new Length(4);
  109. $this->validator->validate($value, $constraint);
  110. $this->assertNoViolation();
  111. }
  112. /**
  113. * @dataProvider getThreeOrLessCharacters
  114. */
  115. public function testInvalidValuesMin($value)
  116. {
  117. $constraint = new Length(array(
  118. 'min' => 4,
  119. 'minMessage' => 'myMessage',
  120. ));
  121. $this->validator->validate($value, $constraint);
  122. $this->buildViolation('myMessage')
  123. ->setParameter('{{ value }}', '"'.$value.'"')
  124. ->setParameter('{{ limit }}', 4)
  125. ->setInvalidValue($value)
  126. ->setPlural(4)
  127. ->setCode(Length::TOO_SHORT_ERROR)
  128. ->assertRaised();
  129. }
  130. /**
  131. * @dataProvider getFiveOrMoreCharacters
  132. */
  133. public function testInvalidValuesMax($value)
  134. {
  135. $constraint = new Length(array(
  136. 'max' => 4,
  137. 'maxMessage' => 'myMessage',
  138. ));
  139. $this->validator->validate($value, $constraint);
  140. $this->buildViolation('myMessage')
  141. ->setParameter('{{ value }}', '"'.$value.'"')
  142. ->setParameter('{{ limit }}', 4)
  143. ->setInvalidValue($value)
  144. ->setPlural(4)
  145. ->setCode(Length::TOO_LONG_ERROR)
  146. ->assertRaised();
  147. }
  148. /**
  149. * @dataProvider getThreeOrLessCharacters
  150. */
  151. public function testInvalidValuesExactLessThanFour($value)
  152. {
  153. $constraint = new Length(array(
  154. 'min' => 4,
  155. 'max' => 4,
  156. 'exactMessage' => 'myMessage',
  157. ));
  158. $this->validator->validate($value, $constraint);
  159. $this->buildViolation('myMessage')
  160. ->setParameter('{{ value }}', '"'.$value.'"')
  161. ->setParameter('{{ limit }}', 4)
  162. ->setInvalidValue($value)
  163. ->setPlural(4)
  164. ->setCode(Length::TOO_SHORT_ERROR)
  165. ->assertRaised();
  166. }
  167. /**
  168. * @dataProvider getFiveOrMoreCharacters
  169. */
  170. public function testInvalidValuesExactMoreThanFour($value)
  171. {
  172. $constraint = new Length(array(
  173. 'min' => 4,
  174. 'max' => 4,
  175. 'exactMessage' => 'myMessage',
  176. ));
  177. $this->validator->validate($value, $constraint);
  178. $this->buildViolation('myMessage')
  179. ->setParameter('{{ value }}', '"'.$value.'"')
  180. ->setParameter('{{ limit }}', 4)
  181. ->setInvalidValue($value)
  182. ->setPlural(4)
  183. ->setCode(Length::TOO_LONG_ERROR)
  184. ->assertRaised();
  185. }
  186. /**
  187. * @dataProvider getOneCharset
  188. */
  189. public function testOneCharset($value, $charset, $isValid)
  190. {
  191. $constraint = new Length(array(
  192. 'min' => 1,
  193. 'max' => 1,
  194. 'charset' => $charset,
  195. 'charsetMessage' => 'myMessage',
  196. ));
  197. $this->validator->validate($value, $constraint);
  198. if ($isValid) {
  199. $this->assertNoViolation();
  200. } else {
  201. $this->buildViolation('myMessage')
  202. ->setParameter('{{ value }}', '"'.$value.'"')
  203. ->setParameter('{{ charset }}', $charset)
  204. ->setInvalidValue($value)
  205. ->setCode(Length::INVALID_CHARACTERS_ERROR)
  206. ->assertRaised();
  207. }
  208. }
  209. public function testConstraintGetDefaultOption()
  210. {
  211. $constraint = new Length(5);
  212. $this->assertEquals(5, $constraint->min);
  213. $this->assertEquals(5, $constraint->max);
  214. }
  215. }