ChoiceValidatorTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\Constraints;
  11. use Symfony\Component\Validator\ExecutionContext;
  12. use Symfony\Component\Validator\Constraints\Choice;
  13. use Symfony\Component\Validator\Constraints\ChoiceValidator;
  14. function choice_callback()
  15. {
  16. return array('foo', 'bar');
  17. }
  18. class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
  19. {
  20. protected $validator;
  21. public static function staticCallback()
  22. {
  23. return array('foo', 'bar');
  24. }
  25. protected function setUp()
  26. {
  27. $walker = $this->getMock('Symfony\Component\Validator\GraphWalker', array(), array(), '', false);
  28. $factory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
  29. $context = new ExecutionContext('root', $walker, $factory);
  30. $context->setCurrentClass(__CLASS__);
  31. $this->validator = new ChoiceValidator();
  32. $this->validator->initialize($context);
  33. }
  34. protected function tearDown()
  35. {
  36. $this->validator = null;
  37. }
  38. public function testExpectArrayIfMultipleIsTrue()
  39. {
  40. $constraint = new Choice(array(
  41. 'choices' => array('foo', 'bar'),
  42. 'multiple' => true,
  43. ));
  44. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  45. $this->validator->isValid('asdf', $constraint);
  46. }
  47. public function testNullIsValid()
  48. {
  49. $this->assertTrue($this->validator->isValid(null, new Choice(array('choices' => array('foo', 'bar')))));
  50. }
  51. public function testChoicesOrCallbackExpected()
  52. {
  53. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  54. $this->validator->isValid('foobar', new Choice());
  55. }
  56. public function testValidCallbackExpected()
  57. {
  58. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  59. $this->validator->isValid('foobar', new Choice(array('callback' => 'abcd')));
  60. }
  61. public function testValidChoiceArray()
  62. {
  63. $constraint = new Choice(array('choices' => array('foo', 'bar')));
  64. $this->assertTrue($this->validator->isValid('bar', $constraint));
  65. }
  66. public function testValidChoiceCallbackFunction()
  67. {
  68. $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback'));
  69. $this->assertTrue($this->validator->isValid('bar', $constraint));
  70. }
  71. public function testValidChoiceCallbackClosure()
  72. {
  73. $constraint = new Choice(array('callback' => function() {
  74. return array('foo', 'bar');
  75. }));
  76. $this->assertTrue($this->validator->isValid('bar', $constraint));
  77. }
  78. public function testValidChoiceCallbackStaticMethod()
  79. {
  80. $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback')));
  81. $this->assertTrue($this->validator->isValid('bar', $constraint));
  82. }
  83. public function testValidChoiceCallbackContextMethod()
  84. {
  85. $constraint = new Choice(array('callback' => 'staticCallback'));
  86. $this->assertTrue($this->validator->isValid('bar', $constraint));
  87. }
  88. public function testMultipleChoices()
  89. {
  90. $constraint = new Choice(array(
  91. 'choices' => array('foo', 'bar', 'baz'),
  92. 'multiple' => true,
  93. ));
  94. $this->assertTrue($this->validator->isValid(array('baz', 'bar'), $constraint));
  95. }
  96. public function testInvalidChoice()
  97. {
  98. $constraint = new Choice(array(
  99. 'choices' => array('foo', 'bar'),
  100. 'message' => 'myMessage',
  101. ));
  102. $this->assertFalse($this->validator->isValid('baz', $constraint));
  103. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  104. $this->assertEquals($this->validator->getMessageParameters(), array(
  105. '{{ value }}' => 'baz',
  106. ));
  107. }
  108. public function testInvalidChoiceMultiple()
  109. {
  110. $constraint = new Choice(array(
  111. 'choices' => array('foo', 'bar'),
  112. 'multipleMessage' => 'myMessage',
  113. 'multiple' => true,
  114. ));
  115. $this->assertFalse($this->validator->isValid(array('foo', 'baz'), $constraint));
  116. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  117. $this->assertEquals($this->validator->getMessageParameters(), array(
  118. '{{ value }}' => 'baz',
  119. ));
  120. }
  121. public function testTooFewChoices()
  122. {
  123. $constraint = new Choice(array(
  124. 'choices' => array('foo', 'bar', 'moo', 'maa'),
  125. 'multiple' => true,
  126. 'min' => 2,
  127. 'minMessage' => 'myMessage',
  128. ));
  129. $this->assertFalse($this->validator->isValid(array('foo'), $constraint));
  130. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  131. $this->assertEquals($this->validator->getMessageParameters(), array(
  132. '{{ limit }}' => 2,
  133. ));
  134. }
  135. public function testTooManyChoices()
  136. {
  137. $constraint = new Choice(array(
  138. 'choices' => array('foo', 'bar', 'moo', 'maa'),
  139. 'multiple' => true,
  140. 'max' => 2,
  141. 'maxMessage' => 'myMessage',
  142. ));
  143. $this->assertFalse($this->validator->isValid(array('foo', 'bar', 'moo'), $constraint));
  144. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  145. $this->assertEquals($this->validator->getMessageParameters(), array(
  146. '{{ limit }}' => 2,
  147. ));
  148. }
  149. public function testStrictIsFalse()
  150. {
  151. $constraint = new Choice(array(
  152. 'choices' => array(1, 2),
  153. 'strict' => false,
  154. ));
  155. $this->assertTrue($this->validator->isValid('2', $constraint));
  156. $this->assertTrue($this->validator->isValid(2, $constraint));
  157. }
  158. public function testStrictIsTrue()
  159. {
  160. $constraint = new Choice(array(
  161. 'choices' => array(1, 2),
  162. 'strict' => true,
  163. ));
  164. $this->assertTrue($this->validator->isValid(2, $constraint));
  165. $this->assertFalse($this->validator->isValid('2', $constraint));
  166. }
  167. public function testStrictIsFalseWhenMultipleChoices()
  168. {
  169. $constraint = new Choice(array(
  170. 'choices' => array(1, 2, 3),
  171. 'multiple' => true,
  172. 'strict' => false
  173. ));
  174. $this->assertTrue($this->validator->isValid(array('2', 3), $constraint));
  175. }
  176. public function testStrictIsTrueWhenMultipleChoices()
  177. {
  178. $constraint = new Choice(array(
  179. 'choices' => array(1, 2, 3),
  180. 'multiple' => true,
  181. 'strict' => true
  182. ));
  183. $this->assertFalse($this->validator->isValid(array(2, '3'), $constraint));
  184. }
  185. }