ChoiceValidatorTest.php 5.5 KB

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