ChoiceValidatorTest.php 5.4 KB

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