AllValidatorTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Constraints;
  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. protected function tearDown()
  29. {
  30. $this->validator = null;
  31. $this->walker = null;
  32. $this->context = null;
  33. }
  34. public function testNullIsValid()
  35. {
  36. $this->assertTrue($this->validator->isValid(null, new All(new Min(4))));
  37. }
  38. public function testThrowsExceptionIfNotTraversable()
  39. {
  40. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  41. $this->validator->isValid('foobar', new All(new Min(4)));
  42. }
  43. /**
  44. * @dataProvider getValidArguments
  45. */
  46. public function testWalkSingleConstraint($array)
  47. {
  48. $this->context->setGroup('MyGroup');
  49. $this->context->setPropertyPath('foo');
  50. $constraint = new Min(4);
  51. foreach ($array as $key => $value) {
  52. $this->walker->expects($this->once())
  53. ->method('walkConstraint')
  54. ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  55. }
  56. $this->assertTrue($this->validator->isValid($array, new All($constraint)));
  57. }
  58. /**
  59. * @dataProvider getValidArguments
  60. */
  61. public function testWalkMultipleConstraints($array)
  62. {
  63. $this->context->setGroup('MyGroup');
  64. $this->context->setPropertyPath('foo');
  65. $constraint = new Min(4);
  66. // can only test for twice the same constraint because PHPUnits mocking
  67. // can't test method calls with different arguments
  68. $constraints = array($constraint, $constraint);
  69. foreach ($array as $key => $value) {
  70. $this->walker->expects($this->exactly(2))
  71. ->method('walkConstraint')
  72. ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  73. }
  74. $this->assertTrue($this->validator->isValid($array, new All($constraints)));
  75. }
  76. public function getValidArguments()
  77. {
  78. return array(
  79. // can only test for one entry, because PHPUnits mocking does not allow
  80. // to expect multiple method calls with different arguments
  81. array(array(1)),
  82. array(new \ArrayObject(array(1))),
  83. );
  84. }
  85. }