AllValidatorTest.php 3.0 KB

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