ChoiceValidatorTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. public function testExpectArrayIfMultipleIsTrue()
  35. {
  36. $constraint = new Choice(array(
  37. 'choices' => array('foo', 'bar'),
  38. 'multiple' => true,
  39. ));
  40. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  41. $this->validator->isValid('asdf', $constraint);
  42. }
  43. public function testNullIsValid()
  44. {
  45. $this->assertTrue($this->validator->isValid(null, new Choice(array('choices' => array('foo', 'bar')))));
  46. }
  47. public function testChoicesOrCallbackExpected()
  48. {
  49. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  50. $this->validator->isValid('foobar', new Choice());
  51. }
  52. public function testValidCallbackExpected()
  53. {
  54. $this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  55. $this->validator->isValid('foobar', new Choice(array('callback' => 'abcd')));
  56. }
  57. public function testValidChoiceArray()
  58. {
  59. $constraint = new Choice(array('choices' => array('foo', 'bar')));
  60. $this->assertTrue($this->validator->isValid('bar', $constraint));
  61. }
  62. public function testValidChoiceCallbackFunction()
  63. {
  64. $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback'));
  65. $this->assertTrue($this->validator->isValid('bar', $constraint));
  66. }
  67. public function testValidChoiceCallbackClosure()
  68. {
  69. $constraint = new Choice(array('callback' => function() {
  70. return array('foo', 'bar');
  71. }));
  72. $this->assertTrue($this->validator->isValid('bar', $constraint));
  73. }
  74. public function testValidChoiceCallbackStaticMethod()
  75. {
  76. $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback')));
  77. $this->assertTrue($this->validator->isValid('bar', $constraint));
  78. }
  79. public function testValidChoiceCallbackContextMethod()
  80. {
  81. $constraint = new Choice(array('callback' => 'staticCallback'));
  82. $this->assertTrue($this->validator->isValid('bar', $constraint));
  83. }
  84. public function testMultipleChoices()
  85. {
  86. $constraint = new Choice(array(
  87. 'choices' => array('foo', 'bar', 'baz'),
  88. 'multiple' => true,
  89. ));
  90. $this->assertTrue($this->validator->isValid(array('baz', 'bar'), $constraint));
  91. }
  92. public function testInvalidChoice()
  93. {
  94. $constraint = new Choice(array(
  95. 'choices' => array('foo', 'bar'),
  96. 'message' => 'myMessage',
  97. ));
  98. $this->assertFalse($this->validator->isValid('baz', $constraint));
  99. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  100. $this->assertEquals($this->validator->getMessageParameters(), array(
  101. '{{ value }}' => 'baz',
  102. ));
  103. }
  104. public function testInvalidChoiceMultiple()
  105. {
  106. $constraint = new Choice(array(
  107. 'choices' => array('foo', 'bar'),
  108. 'multipleMessage' => 'myMessage',
  109. 'multiple' => true,
  110. ));
  111. $this->assertFalse($this->validator->isValid(array('foo', 'baz'), $constraint));
  112. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  113. $this->assertEquals($this->validator->getMessageParameters(), array(
  114. '{{ value }}' => 'baz',
  115. ));
  116. }
  117. public function testTooFewChoices()
  118. {
  119. $constraint = new Choice(array(
  120. 'choices' => array('foo', 'bar', 'moo', 'maa'),
  121. 'multiple' => true,
  122. 'min' => 2,
  123. 'minMessage' => 'myMessage',
  124. ));
  125. $this->assertFalse($this->validator->isValid(array('foo'), $constraint));
  126. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  127. $this->assertEquals($this->validator->getMessageParameters(), array(
  128. '{{ limit }}' => 2,
  129. ));
  130. }
  131. public function testTooManyChoices()
  132. {
  133. $constraint = new Choice(array(
  134. 'choices' => array('foo', 'bar', 'moo', 'maa'),
  135. 'multiple' => true,
  136. 'max' => 2,
  137. 'maxMessage' => 'myMessage',
  138. ));
  139. $this->assertFalse($this->validator->isValid(array('foo', 'bar', 'moo'), $constraint));
  140. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  141. $this->assertEquals($this->validator->getMessageParameters(), array(
  142. '{{ limit }}' => 2,
  143. ));
  144. }
  145. }