CollectionValidatorTest.php 5.4 KB

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