AllValidatorTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. $this->context = new ValidationContext('Root', $this->walker, $metadataFactory);
  17. $this->validator = new AllValidator();
  18. $this->validator->initialize($this->context);
  19. }
  20. public function testNullIsValid()
  21. {
  22. $this->assertTrue($this->validator->isValid(null, new All(new Min(4))));
  23. }
  24. public function testThrowsExceptionIfNotTraversable()
  25. {
  26. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  27. $this->validator->isValid('foobar', new All(new Min(4)));
  28. }
  29. /**
  30. * @dataProvider getValidArguments
  31. */
  32. public function testWalkSingleConstraint($array)
  33. {
  34. $this->context->setGroup('MyGroup');
  35. $this->context->setPropertyPath('foo');
  36. $constraint = new Min(4);
  37. foreach ($array as $key => $value) {
  38. $this->walker->expects($this->once())
  39. ->method('walkConstraint')
  40. ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  41. }
  42. $this->assertTrue($this->validator->isValid($array, new All($constraint)));
  43. }
  44. /**
  45. * @dataProvider getValidArguments
  46. */
  47. public function testWalkMultipleConstraints($array)
  48. {
  49. $this->context->setGroup('MyGroup');
  50. $this->context->setPropertyPath('foo');
  51. $constraint = new Min(4);
  52. // can only test for twice the same constraint because PHPUnits mocking
  53. // can't test method calls with different arguments
  54. $constraints = array($constraint, $constraint);
  55. foreach ($array as $key => $value) {
  56. $this->walker->expects($this->exactly(2))
  57. ->method('walkConstraint')
  58. ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  59. }
  60. $this->assertTrue($this->validator->isValid($array, new All($constraints)));
  61. }
  62. public function getValidArguments()
  63. {
  64. return array(
  65. // can only test for one entry, because PHPUnits mocking does not allow
  66. // to expect multiple method calls with different arguments
  67. array(array(1)),
  68. array(new \ArrayObject(array(1))),
  69. );
  70. }
  71. }