AllValidatorTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. $this->walker->expects($this->once())
  41. ->method('walkConstraint')
  42. ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  43. }
  44. $this->assertTrue($this->validator->isValid($array, new All($constraint)));
  45. }
  46. /**
  47. * @dataProvider getValidArguments
  48. */
  49. public function testWalkMultipleConstraints($array)
  50. {
  51. $this->context->setGroup('MyGroup');
  52. $this->context->setPropertyPath('foo');
  53. $constraint = new Min(4);
  54. // can only test for twice the same constraint because PHPUnits mocking
  55. // can't test method calls with different arguments
  56. $constraints = array($constraint, $constraint);
  57. foreach ($array as $key => $value) {
  58. $this->walker->expects($this->exactly(2))
  59. ->method('walkConstraint')
  60. ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  61. }
  62. $this->assertTrue($this->validator->isValid($array, new All($constraints)));
  63. }
  64. public function getValidArguments()
  65. {
  66. return array(
  67. // can only test for one entry, because PHPUnits mocking does not allow
  68. // to expect multiple method calls with different arguments
  69. array(array(1)),
  70. array(new \ArrayObject(array(1))),
  71. );
  72. }
  73. }