GraphWalkerTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. require_once __DIR__.'/Fixtures/Entity.php';
  12. require_once __DIR__.'/Fixtures/Reference.php';
  13. require_once __DIR__.'/Fixtures/ConstraintA.php';
  14. require_once __DIR__.'/Fixtures/ConstraintAValidator.php';
  15. require_once __DIR__.'/Fixtures/FailingConstraint.php';
  16. require_once __DIR__.'/Fixtures/FailingConstraintValidator.php';
  17. require_once __DIR__.'/Fixtures/FakeClassMetadataFactory.php';
  18. use Symfony\Tests\Component\Validator\Fixtures\Entity;
  19. use Symfony\Tests\Component\Validator\Fixtures\Reference;
  20. use Symfony\Tests\Component\Validator\Fixtures\FakeClassMetadataFactory;
  21. use Symfony\Tests\Component\Validator\Fixtures\ConstraintA;
  22. use Symfony\Tests\Component\Validator\Fixtures\FailingConstraint;
  23. use Symfony\Component\Validator\GraphWalker;
  24. use Symfony\Component\Validator\ConstraintViolation;
  25. use Symfony\Component\Validator\ConstraintViolationList;
  26. use Symfony\Component\Validator\ConstraintValidatorFactory;
  27. use Symfony\Component\Validator\Mapping\ClassMetadata;
  28. use Symfony\Component\Validator\Constraints\Valid;
  29. class GraphWalkerTest extends \PHPUnit_Framework_TestCase
  30. {
  31. const CLASSNAME = 'Symfony\Tests\Component\Validator\Fixtures\Entity';
  32. protected $factory;
  33. protected $walker;
  34. protected $metadata;
  35. protected function setUp()
  36. {
  37. $this->factory = new FakeClassMetadataFactory();
  38. $this->walker = new GraphWalker('Root', $this->factory, new ConstraintValidatorFactory());
  39. $this->metadata = new ClassMetadata(self::CLASSNAME);
  40. }
  41. public function testWalkClassValidatesConstraints()
  42. {
  43. $this->metadata->addConstraint(new ConstraintA());
  44. $this->walker->walkClass($this->metadata, new Entity(), 'Default', '');
  45. $this->assertEquals(1, count($this->walker->getViolations()));
  46. }
  47. public function testWalkClassValidatesPropertyConstraints()
  48. {
  49. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  50. $this->walker->walkClass($this->metadata, new Entity(), 'Default', '');
  51. $this->assertEquals(1, count($this->walker->getViolations()));
  52. }
  53. public function testWalkClassValidatesGetterConstraints()
  54. {
  55. $this->metadata->addGetterConstraint('lastName', new ConstraintA());
  56. $this->walker->walkClass($this->metadata, new Entity(), 'Default', '');
  57. $this->assertEquals(1, count($this->walker->getViolations()));
  58. }
  59. public function testWalkClassInDefaultGroupTraversesGroupSequence()
  60. {
  61. $entity = new Entity();
  62. $this->metadata->addPropertyConstraint('firstName', new FailingConstraint(array(
  63. 'groups' => 'First',
  64. )));
  65. $this->metadata->addGetterConstraint('lastName', new FailingConstraint(array(
  66. 'groups' => 'Default',
  67. )));
  68. $this->metadata->setGroupSequence(array('First', $this->metadata->getDefaultGroup()));
  69. $this->walker->walkClass($this->metadata, $entity, 'Default', '');
  70. // After validation of group "First" failed, no more group was
  71. // validated
  72. $violations = new ConstraintViolationList();
  73. $violations->add(new ConstraintViolation(
  74. '',
  75. array(),
  76. 'Root',
  77. 'firstName',
  78. ''
  79. ));
  80. $this->assertEquals($violations, $this->walker->getViolations());
  81. }
  82. public function testWalkClassInGroupSequencePropagatesDefaultGroup()
  83. {
  84. $entity = new Entity();
  85. $entity->reference = new Reference();
  86. $this->metadata->addPropertyConstraint('reference', new Valid());
  87. $this->metadata->setGroupSequence(array($this->metadata->getDefaultGroup()));
  88. $referenceMetadata = new ClassMetadata(get_class($entity->reference));
  89. $referenceMetadata->addConstraint(new FailingConstraint(array(
  90. // this constraint is only evaluated if group "Default" is
  91. // propagated to the reference
  92. 'groups' => 'Default',
  93. )));
  94. $this->factory->addClassMetadata($referenceMetadata);
  95. $this->walker->walkClass($this->metadata, $entity, 'Default', '');
  96. // The validation of the reference's FailingConstraint in group
  97. // "Default" was launched
  98. $violations = new ConstraintViolationList();
  99. $violations->add(new ConstraintViolation(
  100. '',
  101. array(),
  102. 'Root',
  103. 'reference',
  104. $entity->reference
  105. ));
  106. $this->assertEquals($violations, $this->walker->getViolations());
  107. }
  108. public function testWalkClassInOtherGroupTraversesNoGroupSequence()
  109. {
  110. $entity = new Entity();
  111. $this->metadata->addPropertyConstraint('firstName', new FailingConstraint(array(
  112. 'groups' => 'First',
  113. )));
  114. $this->metadata->addGetterConstraint('lastName', new FailingConstraint(array(
  115. 'groups' => $this->metadata->getDefaultGroup(),
  116. )));
  117. $this->metadata->setGroupSequence(array('First', $this->metadata->getDefaultGroup()));
  118. $this->walker->walkClass($this->metadata, $entity, $this->metadata->getDefaultGroup(), '');
  119. // Only group "Second" was validated
  120. $violations = new ConstraintViolationList();
  121. $violations->add(new ConstraintViolation(
  122. '',
  123. array(),
  124. 'Root',
  125. 'lastName',
  126. ''
  127. ));
  128. $this->assertEquals($violations, $this->walker->getViolations());
  129. }
  130. public function testWalkPropertyValueValidatesConstraints()
  131. {
  132. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  133. $this->walker->walkPropertyValue($this->metadata, 'firstName', 'value', 'Default', '');
  134. $this->assertEquals(1, count($this->walker->getViolations()));
  135. }
  136. public function testWalkCascadedPropertyValidatesReferences()
  137. {
  138. $entity = new Entity();
  139. $entityMetadata = new ClassMetadata(get_class($entity));
  140. $this->factory->addClassMetadata($entityMetadata);
  141. // add a constraint for the entity that always fails
  142. $entityMetadata->addConstraint(new FailingConstraint());
  143. // validate entity when validating the property "reference"
  144. $this->metadata->addPropertyConstraint('reference', new Valid());
  145. // invoke validation on an object
  146. $this->walker->walkPropertyValue(
  147. $this->metadata,
  148. 'reference',
  149. $entity, // object!
  150. 'Default',
  151. 'path'
  152. );
  153. $violations = new ConstraintViolationList();
  154. $violations->add(new ConstraintViolation(
  155. '',
  156. array(),
  157. 'Root',
  158. 'path',
  159. $entity
  160. ));
  161. $this->assertEquals($violations, $this->walker->getViolations());
  162. }
  163. public function testWalkCascadedPropertyValidatesArrays()
  164. {
  165. $entity = new Entity();
  166. $entityMetadata = new ClassMetadata(get_class($entity));
  167. $this->factory->addClassMetadata($entityMetadata);
  168. // add a constraint for the entity that always fails
  169. $entityMetadata->addConstraint(new FailingConstraint());
  170. // validate array when validating the property "reference"
  171. $this->metadata->addPropertyConstraint('reference', new Valid());
  172. $this->walker->walkPropertyValue(
  173. $this->metadata,
  174. 'reference',
  175. array('key' => $entity), // array!
  176. 'Default',
  177. 'path'
  178. );
  179. $violations = new ConstraintViolationList();
  180. $violations->add(new ConstraintViolation(
  181. '',
  182. array(),
  183. 'Root',
  184. 'path[key]',
  185. $entity
  186. ));
  187. $this->assertEquals($violations, $this->walker->getViolations());
  188. }
  189. public function testWalkCascadedPropertyDoesNotValidateNullValues()
  190. {
  191. $this->metadata->addPropertyConstraint('reference', new Valid());
  192. $this->walker->walkPropertyValue(
  193. $this->metadata,
  194. 'reference',
  195. null,
  196. 'Default',
  197. ''
  198. );
  199. $this->assertEquals(0, count($this->walker->getViolations()));
  200. }
  201. public function testWalkCascadedPropertyRequiresObjectOrArray()
  202. {
  203. $this->metadata->addPropertyConstraint('reference', new Valid());
  204. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  205. $this->walker->walkPropertyValue(
  206. $this->metadata,
  207. 'reference',
  208. 'no object',
  209. 'Default',
  210. ''
  211. );
  212. }
  213. public function testWalkConstraintBuildsAViolationIfFailed()
  214. {
  215. $constraint = new ConstraintA();
  216. $this->walker->walkConstraint($constraint, 'foobar', 'Default', 'firstName.path');
  217. $violations = new ConstraintViolationList();
  218. $violations->add(new ConstraintViolation(
  219. 'message',
  220. array('param' => 'value'),
  221. 'Root',
  222. 'firstName.path',
  223. 'foobar'
  224. ));
  225. $this->assertEquals($violations, $this->walker->getViolations());
  226. }
  227. public function testWalkConstraintBuildsNoViolationIfSuccessful()
  228. {
  229. $constraint = new ConstraintA();
  230. $this->walker->walkConstraint($constraint, 'VALID', 'Default', 'firstName.path');
  231. $this->assertEquals(0, count($this->walker->getViolations()));
  232. }
  233. }