GraphWalkerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. 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. use Symfony\Component\Validator\Constraints\Collection;
  30. class GraphWalkerTest extends \PHPUnit_Framework_TestCase
  31. {
  32. const CLASSNAME = 'Symfony\Tests\Component\Validator\Fixtures\Entity';
  33. protected $factory;
  34. protected $walker;
  35. protected $metadata;
  36. protected function setUp()
  37. {
  38. $this->factory = new FakeClassMetadataFactory();
  39. $this->walker = new GraphWalker('Root', $this->factory, new ConstraintValidatorFactory());
  40. $this->metadata = new ClassMetadata(self::CLASSNAME);
  41. }
  42. protected function tearDown()
  43. {
  44. $this->factory = null;
  45. $this->walker = null;
  46. $this->metadata = null;
  47. }
  48. public function testWalkObjectValidatesConstraints()
  49. {
  50. $this->metadata->addConstraint(new ConstraintA());
  51. $this->walker->walkObject($this->metadata, new Entity(), 'Default', '');
  52. $this->assertEquals(1, count($this->walker->getViolations()));
  53. }
  54. public function testWalkObjectTwiceValidatesConstraintsOnce()
  55. {
  56. $this->metadata->addConstraint(new ConstraintA());
  57. $entity = new Entity();
  58. $this->walker->walkObject($this->metadata, $entity, 'Default', '');
  59. $this->walker->walkObject($this->metadata, $entity, 'Default', '');
  60. $this->assertEquals(1, count($this->walker->getViolations()));
  61. }
  62. public function testWalkDifferentObjectsValidatesTwice()
  63. {
  64. $this->metadata->addConstraint(new ConstraintA());
  65. $this->walker->walkObject($this->metadata, new Entity(), 'Default', '');
  66. $this->walker->walkObject($this->metadata, new Entity(), 'Default', '');
  67. $this->assertEquals(2, count($this->walker->getViolations()));
  68. }
  69. public function testWalkObjectTwiceInDifferentGroupsValidatesTwice()
  70. {
  71. $this->metadata->addConstraint(new ConstraintA());
  72. $this->metadata->addConstraint(new ConstraintA(array('groups' => 'Custom')));
  73. $entity = new Entity();
  74. $this->walker->walkObject($this->metadata, $entity, 'Default', '');
  75. $this->walker->walkObject($this->metadata, $entity, 'Custom', '');
  76. $this->assertEquals(2, count($this->walker->getViolations()));
  77. }
  78. public function testWalkObjectValidatesPropertyConstraints()
  79. {
  80. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  81. $this->walker->walkObject($this->metadata, new Entity(), 'Default', '');
  82. $this->assertEquals(1, count($this->walker->getViolations()));
  83. }
  84. public function testWalkObjectValidatesGetterConstraints()
  85. {
  86. $this->metadata->addGetterConstraint('lastName', new ConstraintA());
  87. $this->walker->walkObject($this->metadata, new Entity(), 'Default', '');
  88. $this->assertEquals(1, count($this->walker->getViolations()));
  89. }
  90. public function testWalkObjectInDefaultGroupTraversesGroupSequence()
  91. {
  92. $entity = new Entity();
  93. $this->metadata->addPropertyConstraint('firstName', new FailingConstraint(array(
  94. 'groups' => 'First',
  95. )));
  96. $this->metadata->addGetterConstraint('lastName', new FailingConstraint(array(
  97. 'groups' => 'Default',
  98. )));
  99. $this->metadata->setGroupSequence(array('First', $this->metadata->getDefaultGroup()));
  100. $this->walker->walkObject($this->metadata, $entity, 'Default', '');
  101. // After validation of group "First" failed, no more group was
  102. // validated
  103. $violations = new ConstraintViolationList();
  104. $violations->add(new ConstraintViolation(
  105. '',
  106. array(),
  107. 'Root',
  108. 'firstName',
  109. ''
  110. ));
  111. $this->assertEquals($violations, $this->walker->getViolations());
  112. }
  113. public function testWalkObjectInGroupSequencePropagatesDefaultGroup()
  114. {
  115. $entity = new Entity();
  116. $entity->reference = new Reference();
  117. $this->metadata->addPropertyConstraint('reference', new Valid());
  118. $this->metadata->setGroupSequence(array($this->metadata->getDefaultGroup()));
  119. $referenceMetadata = new ClassMetadata(get_class($entity->reference));
  120. $referenceMetadata->addConstraint(new FailingConstraint(array(
  121. // this constraint is only evaluated if group "Default" is
  122. // propagated to the reference
  123. 'groups' => 'Default',
  124. )));
  125. $this->factory->addClassMetadata($referenceMetadata);
  126. $this->walker->walkObject($this->metadata, $entity, 'Default', '');
  127. // The validation of the reference's FailingConstraint in group
  128. // "Default" was launched
  129. $violations = new ConstraintViolationList();
  130. $violations->add(new ConstraintViolation(
  131. '',
  132. array(),
  133. 'Root',
  134. 'reference',
  135. $entity->reference
  136. ));
  137. $this->assertEquals($violations, $this->walker->getViolations());
  138. }
  139. public function testWalkObjectInOtherGroupTraversesNoGroupSequence()
  140. {
  141. $entity = new Entity();
  142. $this->metadata->addPropertyConstraint('firstName', new FailingConstraint(array(
  143. 'groups' => 'First',
  144. )));
  145. $this->metadata->addGetterConstraint('lastName', new FailingConstraint(array(
  146. 'groups' => $this->metadata->getDefaultGroup(),
  147. )));
  148. $this->metadata->setGroupSequence(array('First', $this->metadata->getDefaultGroup()));
  149. $this->walker->walkObject($this->metadata, $entity, $this->metadata->getDefaultGroup(), '');
  150. // Only group "Second" was validated
  151. $violations = new ConstraintViolationList();
  152. $violations->add(new ConstraintViolation(
  153. '',
  154. array(),
  155. 'Root',
  156. 'lastName',
  157. ''
  158. ));
  159. $this->assertEquals($violations, $this->walker->getViolations());
  160. }
  161. public function testWalkPropertyValueValidatesConstraints()
  162. {
  163. $this->metadata->addPropertyConstraint('firstName', new ConstraintA());
  164. $this->walker->walkPropertyValue($this->metadata, 'firstName', 'value', 'Default', '');
  165. $this->assertEquals(1, count($this->walker->getViolations()));
  166. }
  167. public function testWalkCascadedPropertyValidatesReferences()
  168. {
  169. $entity = new Entity();
  170. $entityMetadata = new ClassMetadata(get_class($entity));
  171. $this->factory->addClassMetadata($entityMetadata);
  172. // add a constraint for the entity that always fails
  173. $entityMetadata->addConstraint(new FailingConstraint());
  174. // validate entity when validating the property "reference"
  175. $this->metadata->addPropertyConstraint('reference', new Valid());
  176. // invoke validation on an object
  177. $this->walker->walkPropertyValue(
  178. $this->metadata,
  179. 'reference',
  180. $entity, // object!
  181. 'Default',
  182. 'path'
  183. );
  184. $violations = new ConstraintViolationList();
  185. $violations->add(new ConstraintViolation(
  186. '',
  187. array(),
  188. 'Root',
  189. 'path',
  190. $entity
  191. ));
  192. $this->assertEquals($violations, $this->walker->getViolations());
  193. }
  194. public function testWalkCascadedPropertyValidatesArraysByDefault()
  195. {
  196. $entity = new Entity();
  197. $entityMetadata = new ClassMetadata(get_class($entity));
  198. $this->factory->addClassMetadata($entityMetadata);
  199. // add a constraint for the entity that always fails
  200. $entityMetadata->addConstraint(new FailingConstraint());
  201. // validate array when validating the property "reference"
  202. $this->metadata->addPropertyConstraint('reference', new Valid());
  203. $this->walker->walkPropertyValue(
  204. $this->metadata,
  205. 'reference',
  206. array('key' => $entity), // array!
  207. 'Default',
  208. 'path'
  209. );
  210. $violations = new ConstraintViolationList();
  211. $violations->add(new ConstraintViolation(
  212. '',
  213. array(),
  214. 'Root',
  215. 'path[key]',
  216. $entity
  217. ));
  218. $this->assertEquals($violations, $this->walker->getViolations());
  219. }
  220. public function testWalkCascadedPropertyValidatesTraversableByDefault()
  221. {
  222. $entity = new Entity();
  223. $entityMetadata = new ClassMetadata(get_class($entity));
  224. $this->factory->addClassMetadata($entityMetadata);
  225. $this->factory->addClassMetadata(new ClassMetadata('ArrayIterator'));
  226. // add a constraint for the entity that always fails
  227. $entityMetadata->addConstraint(new FailingConstraint());
  228. // validate array when validating the property "reference"
  229. $this->metadata->addPropertyConstraint('reference', new Valid());
  230. $this->walker->walkPropertyValue(
  231. $this->metadata,
  232. 'reference',
  233. new \ArrayIterator(array('key' => $entity)),
  234. 'Default',
  235. 'path'
  236. );
  237. $violations = new ConstraintViolationList();
  238. $violations->add(new ConstraintViolation(
  239. '',
  240. array(),
  241. 'Root',
  242. 'path[key]',
  243. $entity
  244. ));
  245. $this->assertEquals($violations, $this->walker->getViolations());
  246. }
  247. public function testWalkCascadedPropertyDoesNotValidateTraversableIfDisabled()
  248. {
  249. $entity = new Entity();
  250. $entityMetadata = new ClassMetadata(get_class($entity));
  251. $this->factory->addClassMetadata($entityMetadata);
  252. $this->factory->addClassMetadata(new ClassMetadata('ArrayIterator'));
  253. // add a constraint for the entity that always fails
  254. $entityMetadata->addConstraint(new FailingConstraint());
  255. // validate array when validating the property "reference"
  256. $this->metadata->addPropertyConstraint('reference', new Valid(array(
  257. 'traverse' => false,
  258. )));
  259. $this->walker->walkPropertyValue(
  260. $this->metadata,
  261. 'reference',
  262. new \ArrayIterator(array('key' => $entity)),
  263. 'Default',
  264. 'path'
  265. );
  266. $violations = new ConstraintViolationList();
  267. $this->assertEquals($violations, $this->walker->getViolations());
  268. }
  269. public function testWalkCascadedPropertyDoesNotValidateNestedScalarValues()
  270. {
  271. // validate array when validating the property "reference"
  272. $this->metadata->addPropertyConstraint('reference', new Valid());
  273. $this->walker->walkPropertyValue(
  274. $this->metadata,
  275. 'reference',
  276. array('scalar', 'values'),
  277. 'Default',
  278. 'path'
  279. );
  280. $violations = new ConstraintViolationList();
  281. $this->assertEquals($violations, $this->walker->getViolations());
  282. }
  283. public function testWalkCascadedPropertyDoesNotValidateNullValues()
  284. {
  285. $this->metadata->addPropertyConstraint('reference', new Valid());
  286. $this->walker->walkPropertyValue(
  287. $this->metadata,
  288. 'reference',
  289. null,
  290. 'Default',
  291. ''
  292. );
  293. $this->assertEquals(0, count($this->walker->getViolations()));
  294. }
  295. public function testWalkCascadedPropertyRequiresObjectOrArray()
  296. {
  297. $this->metadata->addPropertyConstraint('reference', new Valid());
  298. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  299. $this->walker->walkPropertyValue(
  300. $this->metadata,
  301. 'reference',
  302. 'no object',
  303. 'Default',
  304. ''
  305. );
  306. }
  307. public function testWalkConstraintBuildsAViolationIfFailed()
  308. {
  309. $constraint = new ConstraintA();
  310. $this->walker->walkConstraint($constraint, 'foobar', 'Default', 'firstName.path');
  311. $violations = new ConstraintViolationList();
  312. $violations->add(new ConstraintViolation(
  313. 'message',
  314. array('param' => 'value'),
  315. 'Root',
  316. 'firstName.path',
  317. 'foobar'
  318. ));
  319. $this->assertEquals($violations, $this->walker->getViolations());
  320. }
  321. public function testWalkConstraintBuildsNoViolationIfSuccessful()
  322. {
  323. $constraint = new ConstraintA();
  324. $this->walker->walkConstraint($constraint, 'VALID', 'Default', 'firstName.path');
  325. $this->assertEquals(0, count($this->walker->getViolations()));
  326. }
  327. public function testWalkObjectUsesCorrectPropertyPathInViolationsWhenUsingCollections()
  328. {
  329. $constraint = new Collection(array(
  330. 'foo' => new ConstraintA(),
  331. 'bar' => new ConstraintA(),
  332. ));
  333. $this->walker->walkConstraint($constraint, array('foo' => 'VALID'), 'Default', 'collection');
  334. $violations = $this->walker->getViolations();
  335. $this->assertEquals('collection', $violations[0]->getPropertyPath());
  336. }
  337. public function testWalkObjectUsesCorrectPropertyPathInViolationsWhenUsingNestedCollections()
  338. {
  339. $constraint = new Collection(array(
  340. 'foo' => new Collection(array(
  341. 'foo' => new ConstraintA(),
  342. 'bar' => new ConstraintA(),
  343. )),
  344. ));
  345. $this->walker->walkConstraint($constraint, array('foo' => array('foo' => 'VALID')), 'Default', 'collection');
  346. $violations = $this->walker->getViolations();
  347. $this->assertEquals('collection[foo]', $violations[0]->getPropertyPath());
  348. }
  349. }