CollectionValidatorTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\Collection;
  6. use Symfony\Component\Validator\Constraints\CollectionValidator;
  7. class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
  8. {
  9. protected $validator;
  10. protected $walker;
  11. protected $context;
  12. protected 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 CollectionValidator();
  18. $this->validator->initialize($this->context);
  19. }
  20. public function testNullIsValid()
  21. {
  22. $this->assertTrue($this->validator->isValid(null, new Collection(array('fields' => array(
  23. 'foo' => new Min(4),
  24. )))));
  25. }
  26. public function testThrowsExceptionIfNotTraversable()
  27. {
  28. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  29. $this->validator->isValid('foobar', new Collection(array('fields' => array(
  30. 'foo' => new Min(4),
  31. ))));
  32. }
  33. /**
  34. * @dataProvider getValidArguments
  35. */
  36. public function testWalkSingleConstraint($array)
  37. {
  38. $this->context->setGroup('MyGroup');
  39. $this->context->setPropertyPath('foo');
  40. $constraint = new Min(4);
  41. foreach ($array as $key => $value) {
  42. $this->walker->expects($this->once())
  43. ->method('walkConstraint')
  44. ->with($this->equalTo($constraint), $this->equalTo($value), $this->equalTo('MyGroup'), $this->equalTo('foo['.$key.']'));
  45. }
  46. $this->assertTrue($this->validator->isValid($array, new Collection(array(
  47. 'fields' => array(
  48. 'foo' => $constraint,
  49. ),
  50. ))));
  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 Collection(array(
  69. 'fields' => array(
  70. 'foo' => $constraints,
  71. )
  72. ))));
  73. }
  74. public function testExtraFieldsDisallowed()
  75. {
  76. $array = array(
  77. 'foo' => 5,
  78. 'bar' => 6,
  79. );
  80. $this->assertFalse($this->validator->isValid($array, new Collection(array(
  81. 'fields' => array(
  82. 'foo' => new Min(4),
  83. ),
  84. ))));
  85. }
  86. // bug fix
  87. public function testNullNotConsideredExtraField()
  88. {
  89. $array = array(
  90. 'foo' => null,
  91. );
  92. $this->assertTrue($this->validator->isValid($array, new Collection(array(
  93. 'fields' => array(
  94. 'foo' => new Min(4),
  95. ),
  96. ))));
  97. }
  98. public function testExtraFieldsAllowed()
  99. {
  100. $array = array(
  101. 'foo' => 5,
  102. 'bar' => 6,
  103. );
  104. $this->assertTrue($this->validator->isValid($array, new Collection(array(
  105. 'fields' => array(
  106. 'foo' => new Min(4),
  107. ),
  108. 'allowExtraFields' => true,
  109. ))));
  110. }
  111. public function testMissingFieldsDisallowed()
  112. {
  113. $this->assertFalse($this->validator->isValid(array(), new Collection(array(
  114. 'fields' => array(
  115. 'foo' => new Min(4),
  116. ),
  117. ))));
  118. }
  119. public function testMissingFieldsAllowed()
  120. {
  121. $this->assertTrue($this->validator->isValid(array(), new Collection(array(
  122. 'fields' => array(
  123. 'foo' => new Min(4),
  124. ),
  125. 'allowMissingFields' => true,
  126. ))));
  127. }
  128. public function getValidArguments()
  129. {
  130. return array(
  131. // can only test for one entry, because PHPUnits mocking does not allow
  132. // to expect multiple method calls with different arguments
  133. array(array('foo' => 3)),
  134. array(new \ArrayObject(array('foo' => 3))),
  135. );
  136. }
  137. public function testObjectShouldBeLeftUnchanged()
  138. {
  139. $value = new \ArrayObject(array(
  140. 'foo' => 3
  141. ));
  142. $this->validator->isValid($value, new Collection(array(
  143. 'fields' => array(
  144. 'foo' => new Min(2),
  145. )
  146. )));
  147. $this->assertEquals(array(
  148. 'foo' => 3
  149. ), (array) $value);
  150. }
  151. }