ChoiceValidatorTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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\Choice;
  12. use Symfony\Component\Validator\Constraints\ChoiceValidator;
  13. use Symfony\Component\Validator\Validation;
  14. function choice_callback()
  15. {
  16. return array('foo', 'bar');
  17. }
  18. class ChoiceValidatorTest extends AbstractConstraintValidatorTest
  19. {
  20. protected function getApiVersion()
  21. {
  22. return Validation::API_VERSION_2_5;
  23. }
  24. protected function createValidator()
  25. {
  26. return new ChoiceValidator();
  27. }
  28. public static function staticCallback()
  29. {
  30. return array('foo', 'bar');
  31. }
  32. /**
  33. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  34. */
  35. public function testExpectArrayIfMultipleIsTrue()
  36. {
  37. $constraint = new Choice(array(
  38. 'choices' => array('foo', 'bar'),
  39. 'multiple' => true,
  40. ));
  41. $this->validator->validate('asdf', $constraint);
  42. }
  43. public function testNullIsValid()
  44. {
  45. $this->validator->validate(null, new Choice(array('choices' => array('foo', 'bar'))));
  46. $this->assertNoViolation();
  47. }
  48. /**
  49. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  50. */
  51. public function testChoicesOrCallbackExpected()
  52. {
  53. $this->validator->validate('foobar', new Choice());
  54. }
  55. /**
  56. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  57. */
  58. public function testValidCallbackExpected()
  59. {
  60. $this->validator->validate('foobar', new Choice(array('callback' => 'abcd')));
  61. }
  62. public function testValidChoiceArray()
  63. {
  64. $constraint = new Choice(array('choices' => array('foo', 'bar')));
  65. $this->validator->validate('bar', $constraint);
  66. $this->assertNoViolation();
  67. }
  68. public function testValidChoiceCallbackFunction()
  69. {
  70. $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback'));
  71. $this->validator->validate('bar', $constraint);
  72. $this->assertNoViolation();
  73. }
  74. public function testValidChoiceCallbackClosure()
  75. {
  76. $constraint = new Choice(array('callback' => function () {
  77. return array('foo', 'bar');
  78. }));
  79. $this->validator->validate('bar', $constraint);
  80. $this->assertNoViolation();
  81. }
  82. public function testValidChoiceCallbackStaticMethod()
  83. {
  84. $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback')));
  85. $this->validator->validate('bar', $constraint);
  86. $this->assertNoViolation();
  87. }
  88. public function testValidChoiceCallbackContextMethod()
  89. {
  90. // search $this for "staticCallback"
  91. $this->setObject($this);
  92. $constraint = new Choice(array('callback' => 'staticCallback'));
  93. $this->validator->validate('bar', $constraint);
  94. $this->assertNoViolation();
  95. }
  96. public function testMultipleChoices()
  97. {
  98. $constraint = new Choice(array(
  99. 'choices' => array('foo', 'bar', 'baz'),
  100. 'multiple' => true,
  101. ));
  102. $this->validator->validate(array('baz', 'bar'), $constraint);
  103. $this->assertNoViolation();
  104. }
  105. public function testInvalidChoice()
  106. {
  107. $constraint = new Choice(array(
  108. 'choices' => array('foo', 'bar'),
  109. 'message' => 'myMessage',
  110. ));
  111. $this->validator->validate('baz', $constraint);
  112. $this->buildViolation('myMessage')
  113. ->setParameter('{{ value }}', '"baz"')
  114. ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
  115. ->assertRaised();
  116. }
  117. public function testInvalidChoiceEmptyChoices()
  118. {
  119. $constraint = new Choice(array(
  120. // May happen when the choices are provided dynamically, e.g. from
  121. // the DB or the model
  122. 'choices' => array(),
  123. 'message' => 'myMessage',
  124. ));
  125. $this->validator->validate('baz', $constraint);
  126. $this->buildViolation('myMessage')
  127. ->setParameter('{{ value }}', '"baz"')
  128. ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
  129. ->assertRaised();
  130. }
  131. public function testInvalidChoiceMultiple()
  132. {
  133. $constraint = new Choice(array(
  134. 'choices' => array('foo', 'bar'),
  135. 'multipleMessage' => 'myMessage',
  136. 'multiple' => true,
  137. ));
  138. $this->validator->validate(array('foo', 'baz'), $constraint);
  139. $this->buildViolation('myMessage')
  140. ->setParameter('{{ value }}', '"baz"')
  141. ->setInvalidValue('baz')
  142. ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
  143. ->assertRaised();
  144. }
  145. public function testTooFewChoices()
  146. {
  147. $constraint = new Choice(array(
  148. 'choices' => array('foo', 'bar', 'moo', 'maa'),
  149. 'multiple' => true,
  150. 'min' => 2,
  151. 'minMessage' => 'myMessage',
  152. ));
  153. $value = array('foo');
  154. $this->setValue($value);
  155. $this->validator->validate($value, $constraint);
  156. $this->buildViolation('myMessage')
  157. ->setParameter('{{ limit }}', 2)
  158. ->setInvalidValue($value)
  159. ->setPlural(2)
  160. ->setCode(Choice::TOO_FEW_ERROR)
  161. ->assertRaised();
  162. }
  163. public function testTooManyChoices()
  164. {
  165. $constraint = new Choice(array(
  166. 'choices' => array('foo', 'bar', 'moo', 'maa'),
  167. 'multiple' => true,
  168. 'max' => 2,
  169. 'maxMessage' => 'myMessage',
  170. ));
  171. $value = array('foo', 'bar', 'moo');
  172. $this->setValue($value);
  173. $this->validator->validate($value, $constraint);
  174. $this->buildViolation('myMessage')
  175. ->setParameter('{{ limit }}', 2)
  176. ->setInvalidValue($value)
  177. ->setPlural(2)
  178. ->setCode(Choice::TOO_MANY_ERROR)
  179. ->assertRaised();
  180. }
  181. public function testNonStrict()
  182. {
  183. $constraint = new Choice(array(
  184. 'choices' => array(1, 2),
  185. 'strict' => false,
  186. ));
  187. $this->validator->validate('2', $constraint);
  188. $this->validator->validate(2, $constraint);
  189. $this->assertNoViolation();
  190. }
  191. public function testStrictAllowsExactValue()
  192. {
  193. $constraint = new Choice(array(
  194. 'choices' => array(1, 2),
  195. 'strict' => true,
  196. ));
  197. $this->validator->validate(2, $constraint);
  198. $this->assertNoViolation();
  199. }
  200. public function testStrictDisallowsDifferentType()
  201. {
  202. $constraint = new Choice(array(
  203. 'choices' => array(1, 2),
  204. 'strict' => true,
  205. 'message' => 'myMessage',
  206. ));
  207. $this->validator->validate('2', $constraint);
  208. $this->buildViolation('myMessage')
  209. ->setParameter('{{ value }}', '"2"')
  210. ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
  211. ->assertRaised();
  212. }
  213. public function testNonStrictWithMultipleChoices()
  214. {
  215. $constraint = new Choice(array(
  216. 'choices' => array(1, 2, 3),
  217. 'multiple' => true,
  218. 'strict' => false,
  219. ));
  220. $this->validator->validate(array('2', 3), $constraint);
  221. $this->assertNoViolation();
  222. }
  223. public function testStrictWithMultipleChoices()
  224. {
  225. $constraint = new Choice(array(
  226. 'choices' => array(1, 2, 3),
  227. 'multiple' => true,
  228. 'strict' => true,
  229. 'multipleMessage' => 'myMessage',
  230. ));
  231. $this->validator->validate(array(2, '3'), $constraint);
  232. $this->buildViolation('myMessage')
  233. ->setParameter('{{ value }}', '"3"')
  234. ->setInvalidValue('3')
  235. ->setCode(Choice::NO_SUCH_CHOICE_ERROR)
  236. ->assertRaised();
  237. }
  238. }