CountValidatorTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\Component\Validator\Tests\Constraints;
  11. use Symfony\Component\Validator\Constraints\Count;
  12. use Symfony\Component\Validator\Constraints\CountValidator;
  13. use Symfony\Component\Validator\Validation;
  14. /**
  15. * @author Bernhard Schussek <bschussek@gmail.com>
  16. */
  17. abstract class CountValidatorTest extends AbstractConstraintValidatorTest
  18. {
  19. protected function getApiVersion()
  20. {
  21. return Validation::API_VERSION_2_5;
  22. }
  23. protected function createValidator()
  24. {
  25. return new CountValidator();
  26. }
  27. abstract protected function createCollection(array $content);
  28. public function testNullIsValid()
  29. {
  30. $this->validator->validate(null, new Count(6));
  31. $this->assertNoViolation();
  32. }
  33. /**
  34. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  35. */
  36. public function testExpectsCountableType()
  37. {
  38. $this->validator->validate(new \stdClass(), new Count(5));
  39. }
  40. public function getThreeOrLessElements()
  41. {
  42. return array(
  43. array($this->createCollection(array(1))),
  44. array($this->createCollection(array(1, 2))),
  45. array($this->createCollection(array(1, 2, 3))),
  46. array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3))),
  47. );
  48. }
  49. public function getFourElements()
  50. {
  51. return array(
  52. array($this->createCollection(array(1, 2, 3, 4))),
  53. array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4))),
  54. );
  55. }
  56. public function getFiveOrMoreElements()
  57. {
  58. return array(
  59. array($this->createCollection(array(1, 2, 3, 4, 5))),
  60. array($this->createCollection(array(1, 2, 3, 4, 5, 6))),
  61. array($this->createCollection(array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5))),
  62. );
  63. }
  64. /**
  65. * @dataProvider getThreeOrLessElements
  66. */
  67. public function testValidValuesMax($value)
  68. {
  69. $constraint = new Count(array('max' => 3));
  70. $this->validator->validate($value, $constraint);
  71. $this->assertNoViolation();
  72. }
  73. /**
  74. * @dataProvider getFiveOrMoreElements
  75. */
  76. public function testValidValuesMin($value)
  77. {
  78. $constraint = new Count(array('min' => 5));
  79. $this->validator->validate($value, $constraint);
  80. $this->assertNoViolation();
  81. }
  82. /**
  83. * @dataProvider getFourElements
  84. */
  85. public function testValidValuesExact($value)
  86. {
  87. $constraint = new Count(4);
  88. $this->validator->validate($value, $constraint);
  89. $this->assertNoViolation();
  90. }
  91. /**
  92. * @dataProvider getFiveOrMoreElements
  93. */
  94. public function testTooManyValues($value)
  95. {
  96. $constraint = new Count(array(
  97. 'max' => 4,
  98. 'maxMessage' => 'myMessage',
  99. ));
  100. $this->validator->validate($value, $constraint);
  101. $this->buildViolation('myMessage')
  102. ->setParameter('{{ count }}', \count($value))
  103. ->setParameter('{{ limit }}', 4)
  104. ->setInvalidValue($value)
  105. ->setPlural(4)
  106. ->setCode(Count::TOO_MANY_ERROR)
  107. ->assertRaised();
  108. }
  109. /**
  110. * @dataProvider getThreeOrLessElements
  111. */
  112. public function testTooFewValues($value)
  113. {
  114. $constraint = new Count(array(
  115. 'min' => 4,
  116. 'minMessage' => 'myMessage',
  117. ));
  118. $this->validator->validate($value, $constraint);
  119. $this->buildViolation('myMessage')
  120. ->setParameter('{{ count }}', \count($value))
  121. ->setParameter('{{ limit }}', 4)
  122. ->setInvalidValue($value)
  123. ->setPlural(4)
  124. ->setCode(Count::TOO_FEW_ERROR)
  125. ->assertRaised();
  126. }
  127. /**
  128. * @dataProvider getFiveOrMoreElements
  129. */
  130. public function testTooManyValuesExact($value)
  131. {
  132. $constraint = new Count(array(
  133. 'min' => 4,
  134. 'max' => 4,
  135. 'exactMessage' => 'myMessage',
  136. ));
  137. $this->validator->validate($value, $constraint);
  138. $this->buildViolation('myMessage')
  139. ->setParameter('{{ count }}', \count($value))
  140. ->setParameter('{{ limit }}', 4)
  141. ->setInvalidValue($value)
  142. ->setPlural(4)
  143. ->setCode(Count::TOO_MANY_ERROR)
  144. ->assertRaised();
  145. }
  146. /**
  147. * @dataProvider getThreeOrLessElements
  148. */
  149. public function testTooFewValuesExact($value)
  150. {
  151. $constraint = new Count(array(
  152. 'min' => 4,
  153. 'max' => 4,
  154. 'exactMessage' => 'myMessage',
  155. ));
  156. $this->validator->validate($value, $constraint);
  157. $this->buildViolation('myMessage')
  158. ->setParameter('{{ count }}', \count($value))
  159. ->setParameter('{{ limit }}', 4)
  160. ->setInvalidValue($value)
  161. ->setPlural(4)
  162. ->setCode(Count::TOO_FEW_ERROR)
  163. ->assertRaised();
  164. }
  165. public function testDefaultOption()
  166. {
  167. $constraint = new Count(5);
  168. $this->assertEquals(5, $constraint->min);
  169. $this->assertEquals(5, $constraint->max);
  170. }
  171. }