AllValidatorTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. use Symfony\Component\Validator\ValidationContext;
  4. use Symfony\Component\Validator\Constraints\Min;
  5. use Symfony\Component\Validator\Constraints\All;
  6. use Symfony\Component\Validator\Constraints\AllValidator;
  7. class AllValidatorTest extends \PHPUnit_Framework_TestCase
  8. {
  9. protected $validator;
  10. protected $walker;
  11. protected $context;
  12. public function setUp()
  13. {
  14. $this->walker = $this->getMock('Symfony\Component\Validator\GraphWalker', array(), array(), '', false);
  15. $metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
  16. $messageInterpolator = $this->getMock('Symfony\Component\Validator\MessageInterpolator\MessageInterpolatorInterface');
  17. $this->context = new ValidationContext('Root', $this->walker, $metadataFactory, $messageInterpolator);
  18. $this->validator = new AllValidator();
  19. $this->validator->initialize($this->context);
  20. }
  21. public function testNullIsValid()
  22. {
  23. $this->assertTrue($this->validator->isValid(null, new All(new Min(4))));
  24. }
  25. public function testThrowsExceptionIfNotTraversable()
  26. {
  27. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  28. $this->validator->isValid('foobar', new All(new Min(4)));
  29. }
  30. /**
  31. * @dataProvider getValidArguments
  32. */
  33. public function testWalkSingleConstraint($array)
  34. {
  35. $this->context->setGroup('MyGroup');
  36. $this->context->setPropertyPath('foo');
  37. $constraint = new Min(4);
  38. foreach ($array as $key => $value) {
  39. $this->walker->expects($this->once())
  40. ->method('walkConstraint')
  41. ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  42. }
  43. $this->assertTrue($this->validator->isValid($array, new All($constraint)));
  44. }
  45. /**
  46. * @dataProvider getValidArguments
  47. */
  48. public function testWalkMultipleConstraints($array)
  49. {
  50. $this->context->setGroup('MyGroup');
  51. $this->context->setPropertyPath('foo');
  52. $constraint = new Min(4);
  53. // can only test for twice the same constraint because PHPUnits mocking
  54. // can't test method calls with different arguments
  55. $constraints = array($constraint, $constraint);
  56. foreach ($array as $key => $value) {
  57. $this->walker->expects($this->exactly(2))
  58. ->method('walkConstraint')
  59. ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  60. }
  61. $this->assertTrue($this->validator->isValid($array, new All($constraints)));
  62. }
  63. public function getValidArguments()
  64. {
  65. return array(
  66. // can only test for one entry, because PHPUnits mocking does not allow
  67. // to expect multiple method calls with different arguments
  68. array(array(1)),
  69. array(new \ArrayObject(array(1))),
  70. );
  71. }
  72. }