CollectionValidatorTest.php 5.8 KB

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