/vendor/symfony/validator/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php

https://bitbucket.org/laborautonomo/laborautonomo-site · PHP · 233 lines · 166 code · 38 blank · 29 comment · 12 complexity · cd2e05c3024174f384f7034838eaa1f5 MD5 · raw file

  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. class LengthValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $context;
  16. protected $validator;
  17. protected function setUp()
  18. {
  19. $this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
  20. $this->validator = new LengthValidator();
  21. $this->validator->initialize($this->context);
  22. }
  23. protected function tearDown()
  24. {
  25. $this->context = null;
  26. $this->validator = null;
  27. }
  28. public function testNullIsValid()
  29. {
  30. $this->context->expects($this->never())
  31. ->method('addViolation');
  32. $this->validator->validate(null, new Length(6));
  33. }
  34. public function testEmptyStringIsValid()
  35. {
  36. $this->context->expects($this->never())
  37. ->method('addViolation');
  38. $this->validator->validate('', new Length(6));
  39. }
  40. /**
  41. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  42. */
  43. public function testExpectsStringCompatibleType()
  44. {
  45. $this->validator->validate(new \stdClass(), new Length(5));
  46. }
  47. public function getThreeOrLessCharacters()
  48. {
  49. return array(
  50. array(12),
  51. array('12'),
  52. array('üü', true),
  53. array('éé', true),
  54. array(123),
  55. array('123'),
  56. array('üüü', true),
  57. array('ééé', true),
  58. );
  59. }
  60. public function getFourCharacters()
  61. {
  62. return array(
  63. array(1234),
  64. array('1234'),
  65. array('üüüü', true),
  66. array('éééé', true),
  67. );
  68. }
  69. public function getNotFourCharacters()
  70. {
  71. return array_merge(
  72. $this->getThreeOrLessCharacters(),
  73. $this->getFiveOrMoreCharacters()
  74. );
  75. }
  76. public function getFiveOrMoreCharacters()
  77. {
  78. return array(
  79. array(12345),
  80. array('12345'),
  81. array('üüüüü', true),
  82. array('ééééé', true),
  83. array(123456),
  84. array('123456'),
  85. array('üüüüüü', true),
  86. array('éééééé', true),
  87. );
  88. }
  89. /**
  90. * @dataProvider getFiveOrMoreCharacters
  91. */
  92. public function testValidValuesMin($value, $mbOnly = false)
  93. {
  94. if ($mbOnly && !function_exists('mb_strlen')) {
  95. $this->markTestSkipped('mb_strlen does not exist');
  96. }
  97. $this->context->expects($this->never())
  98. ->method('addViolation');
  99. $constraint = new Length(array('min' => 5));
  100. $this->validator->validate($value, $constraint);
  101. }
  102. /**
  103. * @dataProvider getThreeOrLessCharacters
  104. */
  105. public function testValidValuesMax($value, $mbOnly = false)
  106. {
  107. if ($mbOnly && !function_exists('mb_strlen')) {
  108. $this->markTestSkipped('mb_strlen does not exist');
  109. }
  110. $this->context->expects($this->never())
  111. ->method('addViolation');
  112. $constraint = new Length(array('max' => 3));
  113. $this->validator->validate($value, $constraint);
  114. }
  115. /**
  116. * @dataProvider getFourCharacters
  117. */
  118. public function testValidValuesExact($value, $mbOnly = false)
  119. {
  120. if ($mbOnly && !function_exists('mb_strlen')) {
  121. $this->markTestSkipped('mb_strlen does not exist');
  122. }
  123. $this->context->expects($this->never())
  124. ->method('addViolation');
  125. $constraint = new Length(4);
  126. $this->validator->validate($value, $constraint);
  127. }
  128. /**
  129. * @dataProvider getThreeOrLessCharacters
  130. */
  131. public function testInvalidValuesMin($value, $mbOnly = false)
  132. {
  133. if ($mbOnly && !function_exists('mb_strlen')) {
  134. $this->markTestSkipped('mb_strlen does not exist');
  135. }
  136. $constraint = new Length(array(
  137. 'min' => 4,
  138. 'minMessage' => 'myMessage'
  139. ));
  140. $this->context->expects($this->once())
  141. ->method('addViolation')
  142. ->with('myMessage', $this->identicalTo(array(
  143. '{{ value }}' => (string) $value,
  144. '{{ limit }}' => 4,
  145. )), $this->identicalTo($value), 4);
  146. $this->validator->validate($value, $constraint);
  147. }
  148. /**
  149. * @dataProvider getFiveOrMoreCharacters
  150. */
  151. public function testInvalidValuesMax($value, $mbOnly = false)
  152. {
  153. if ($mbOnly && !function_exists('mb_strlen')) {
  154. $this->markTestSkipped('mb_strlen does not exist');
  155. }
  156. $constraint = new Length(array(
  157. 'max' => 4,
  158. 'maxMessage' => 'myMessage'
  159. ));
  160. $this->context->expects($this->once())
  161. ->method('addViolation')
  162. ->with('myMessage', $this->identicalTo(array(
  163. '{{ value }}' => (string) $value,
  164. '{{ limit }}' => 4,
  165. )), $this->identicalTo($value), 4);
  166. $this->validator->validate($value, $constraint);
  167. }
  168. /**
  169. * @dataProvider getNotFourCharacters
  170. */
  171. public function testInvalidValuesExact($value, $mbOnly = false)
  172. {
  173. if ($mbOnly && !function_exists('mb_strlen')) {
  174. $this->markTestSkipped('mb_strlen does not exist');
  175. }
  176. $constraint = new Length(array(
  177. 'min' => 4,
  178. 'max' => 4,
  179. 'exactMessage' => 'myMessage'
  180. ));
  181. $this->context->expects($this->once())
  182. ->method('addViolation')
  183. ->with('myMessage', $this->identicalTo(array(
  184. '{{ value }}' => (string) $value,
  185. '{{ limit }}' => 4,
  186. )), $this->identicalTo($value), 4);
  187. $this->validator->validate($value, $constraint);
  188. }
  189. public function testConstraintGetDefaultOption()
  190. {
  191. $constraint = new Length(5);
  192. $this->assertEquals(5, $constraint->min);
  193. $this->assertEquals(5, $constraint->max);
  194. }
  195. }