CollectionValidatorTest.php 4.8 KB

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