CollectionValidatorTest.php 5.7 KB

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