CollectionValidatorTest.php 5.5 KB

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