CollectionValidatorTest.php 5.4 KB

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