GraphWalkerTest.php 15 KB

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