AllValidatorTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\All;
  12. use Symfony\Component\Validator\Constraints\AllValidator;
  13. use Symfony\Component\Validator\Constraints\NotNull;
  14. use Symfony\Component\Validator\Constraints\Range;
  15. use Symfony\Component\Validator\Validation;
  16. class AllValidatorTest extends AbstractConstraintValidatorTest
  17. {
  18. protected function getApiVersion()
  19. {
  20. return Validation::API_VERSION_2_5;
  21. }
  22. protected function createValidator()
  23. {
  24. return new AllValidator();
  25. }
  26. public function testNullIsValid()
  27. {
  28. $this->validator->validate(null, new All(new Range(array('min' => 4))));
  29. $this->assertNoViolation();
  30. }
  31. /**
  32. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  33. */
  34. public function testThrowsExceptionIfNotTraversable()
  35. {
  36. $this->validator->validate('foo.barbar', new All(new Range(array('min' => 4))));
  37. }
  38. /**
  39. * @dataProvider getValidArguments
  40. */
  41. public function testWalkSingleConstraint($array)
  42. {
  43. $constraint = new Range(array('min' => 4));
  44. $i = 0;
  45. foreach ($array as $key => $value) {
  46. $this->expectValidateValueAt($i++, '['.$key.']', $value, array($constraint));
  47. }
  48. $this->validator->validate($array, new All($constraint));
  49. $this->assertNoViolation();
  50. }
  51. /**
  52. * @dataProvider getValidArguments
  53. */
  54. public function testWalkMultipleConstraints($array)
  55. {
  56. $constraint1 = new Range(array('min' => 4));
  57. $constraint2 = new NotNull();
  58. $constraints = array($constraint1, $constraint2);
  59. $i = 0;
  60. foreach ($array as $key => $value) {
  61. $this->expectValidateValueAt($i++, '['.$key.']', $value, array($constraint1, $constraint2));
  62. }
  63. $this->validator->validate($array, new All($constraints));
  64. $this->assertNoViolation();
  65. }
  66. public function getValidArguments()
  67. {
  68. return array(
  69. array(array(5, 6, 7)),
  70. array(new \ArrayObject(array(5, 6, 7))),
  71. );
  72. }
  73. }