ChoiceValidatorTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Symfony\Tests\Components\Validator;
  3. use Symfony\Components\Validator\ValidationContext;
  4. use Symfony\Components\Validator\Constraints\Choice;
  5. use Symfony\Components\Validator\Constraints\ChoiceValidator;
  6. function choice_callback()
  7. {
  8. return array('foo', 'bar');
  9. }
  10. class ChoiceValidatorTest extends \PHPUnit_Framework_TestCase
  11. {
  12. protected $validator;
  13. public static function staticCallback()
  14. {
  15. return array('foo', 'bar');
  16. }
  17. public function setUp()
  18. {
  19. $interpolator = $this->getMock('Symfony\Components\Validator\MessageInterpolator\MessageInterpolatorInterface');
  20. $walker = $this->getMock('Symfony\Components\Validator\GraphWalker', array(), array(), '', false);
  21. $factory = $this->getMock('Symfony\Components\Validator\Mapping\ClassMetadataFactoryInterface');
  22. $context = new ValidationContext('root', $walker, $factory, $interpolator);
  23. $context->setCurrentClass(__CLASS__);
  24. $this->validator = new ChoiceValidator();
  25. $this->validator->initialize($context);
  26. }
  27. public function testExpectArrayIfMultipleIsTrue()
  28. {
  29. $constraint = new Choice(array(
  30. 'choices' => array('foo', 'bar'),
  31. 'multiple' => true,
  32. ));
  33. $this->setExpectedException('Symfony\Components\Validator\Exception\UnexpectedTypeException');
  34. $this->validator->isValid('asdf', $constraint);
  35. }
  36. public function testNullIsValid()
  37. {
  38. $this->assertTrue($this->validator->isValid(null, new Choice(array('choices' => array('foo', 'bar')))));
  39. }
  40. public function testChoicesOrCallbackExpected()
  41. {
  42. $this->setExpectedException('Symfony\Components\Validator\Exception\ConstraintDefinitionException');
  43. $this->validator->isValid('foobar', new Choice());
  44. }
  45. public function testValidCallbackExpected()
  46. {
  47. $this->setExpectedException('Symfony\Components\Validator\Exception\ConstraintDefinitionException');
  48. $this->validator->isValid('foobar', new Choice(array('callback' => 'abcd')));
  49. }
  50. public function testValidChoiceArray()
  51. {
  52. $constraint = new Choice(array('choices' => array('foo', 'bar')));
  53. $this->assertTrue($this->validator->isValid('bar', $constraint));
  54. }
  55. public function testValidChoiceCallbackFunction()
  56. {
  57. $constraint = new Choice(array('callback' => __NAMESPACE__.'\choice_callback'));
  58. $this->assertTrue($this->validator->isValid('bar', $constraint));
  59. }
  60. public function testValidChoiceCallbackClosure()
  61. {
  62. $constraint = new Choice(array('callback' => function() {
  63. return array('foo', 'bar');
  64. }));
  65. $this->assertTrue($this->validator->isValid('bar', $constraint));
  66. }
  67. public function testValidChoiceCallbackStaticMethod()
  68. {
  69. $constraint = new Choice(array('callback' => array(__CLASS__, 'staticCallback')));
  70. $this->assertTrue($this->validator->isValid('bar', $constraint));
  71. }
  72. public function testValidChoiceCallbackContextMethod()
  73. {
  74. $constraint = new Choice(array('callback' => 'staticCallback'));
  75. $this->assertTrue($this->validator->isValid('bar', $constraint));
  76. }
  77. public function testMultipleChoices()
  78. {
  79. $constraint = new Choice(array(
  80. 'choices' => array('foo', 'bar', 'baz'),
  81. 'multiple' => true,
  82. ));
  83. $this->assertTrue($this->validator->isValid(array('baz', 'bar'), $constraint));
  84. }
  85. public function testInvalidChoice()
  86. {
  87. $constraint = new Choice(array(
  88. 'choices' => array('foo', 'bar'),
  89. 'message' => 'myMessage',
  90. ));
  91. $this->assertFalse($this->validator->isValid('baz', $constraint));
  92. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  93. $this->assertEquals($this->validator->getMessageParameters(), array(
  94. 'value' => 'baz',
  95. ));
  96. }
  97. public function testInvalidChoiceMultiple()
  98. {
  99. $constraint = new Choice(array(
  100. 'choices' => array('foo', 'bar'),
  101. 'message' => 'myMessage',
  102. 'multiple' => true,
  103. ));
  104. $this->assertFalse($this->validator->isValid(array('foo', 'baz'), $constraint));
  105. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  106. $this->assertEquals($this->validator->getMessageParameters(), array(
  107. 'value' => 'baz',
  108. ));
  109. }
  110. public function testTooFewChoices()
  111. {
  112. $constraint = new Choice(array(
  113. 'choices' => array('foo', 'bar', 'moo', 'maa'),
  114. 'multiple' => true,
  115. 'min' => 2,
  116. 'minMessage' => 'myMessage',
  117. ));
  118. $this->assertFalse($this->validator->isValid(array('foo'), $constraint));
  119. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  120. $this->assertEquals($this->validator->getMessageParameters(), array(
  121. 'limit' => 2,
  122. ));
  123. }
  124. public function testTooManyChoices()
  125. {
  126. $constraint = new Choice(array(
  127. 'choices' => array('foo', 'bar', 'moo', 'maa'),
  128. 'multiple' => true,
  129. 'max' => 2,
  130. 'maxMessage' => 'myMessage',
  131. ));
  132. $this->assertFalse($this->validator->isValid(array('foo', 'bar', 'moo'), $constraint));
  133. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  134. $this->assertEquals($this->validator->getMessageParameters(), array(
  135. 'limit' => 2,
  136. ));
  137. }
  138. }