AllValidatorTest.php 3.2 KB

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