Abstract2Dot5ApiTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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\Component\Validator\Tests\Validator;
  11. use Symfony\Component\Validator\Constraints\Callback;
  12. use Symfony\Component\Validator\Constraints\Collection;
  13. use Symfony\Component\Validator\Constraints\Expression;
  14. use Symfony\Component\Validator\Constraints\GroupSequence;
  15. use Symfony\Component\Validator\Constraints\NotNull;
  16. use Symfony\Component\Validator\Constraints\Traverse;
  17. use Symfony\Component\Validator\Constraints\Valid;
  18. use Symfony\Component\Validator\ConstraintViolationInterface;
  19. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  20. use Symfony\Component\Validator\Mapping\ClassMetadata;
  21. use Symfony\Component\Validator\MetadataFactoryInterface;
  22. use Symfony\Component\Validator\Tests\Fixtures\Entity;
  23. use Symfony\Component\Validator\Tests\Fixtures\FailingConstraint;
  24. use Symfony\Component\Validator\Tests\Fixtures\FakeClassMetadata;
  25. use Symfony\Component\Validator\Tests\Fixtures\Reference;
  26. use Symfony\Component\Validator\Validator\ValidatorInterface;
  27. /**
  28. * Verifies that a validator satisfies the API of Symfony 2.5+.
  29. *
  30. * @author Bernhard Schussek <bschussek@gmail.com>
  31. */
  32. abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
  33. {
  34. /**
  35. * @var ValidatorInterface
  36. */
  37. protected $validator;
  38. /**
  39. * @return ValidatorInterface
  40. */
  41. abstract protected function createValidator(MetadataFactoryInterface $metadataFactory, array $objectInitializers = array());
  42. protected function setUp()
  43. {
  44. parent::setUp();
  45. $this->validator = $this->createValidator($this->metadataFactory);
  46. }
  47. protected function validate($value, $constraints = null, $groups = null)
  48. {
  49. return $this->validator->validate($value, $constraints, $groups);
  50. }
  51. protected function validateProperty($object, $propertyName, $groups = null)
  52. {
  53. return $this->validator->validateProperty($object, $propertyName, $groups);
  54. }
  55. protected function validatePropertyValue($object, $propertyName, $value, $groups = null)
  56. {
  57. return $this->validator->validatePropertyValue($object, $propertyName, $value, $groups);
  58. }
  59. public function testValidateConstraintWithoutGroup()
  60. {
  61. $violations = $this->validator->validate(null, new NotNull());
  62. $this->assertCount(1, $violations);
  63. }
  64. public function testValidateWithEmptyArrayAsConstraint()
  65. {
  66. $violations = $this->validator->validate('value', array());
  67. $this->assertCount(0, $violations);
  68. }
  69. public function testGroupSequenceAbortsAfterFailedGroup()
  70. {
  71. $entity = new Entity();
  72. $callback1 = function ($value, ExecutionContextInterface $context) {
  73. $context->addViolation('Message 1');
  74. };
  75. $callback2 = function ($value, ExecutionContextInterface $context) {
  76. $context->addViolation('Message 2');
  77. };
  78. $this->metadata->addConstraint(new Callback(array(
  79. 'callback' => function () {},
  80. 'groups' => 'Group 1',
  81. )));
  82. $this->metadata->addConstraint(new Callback(array(
  83. 'callback' => $callback1,
  84. 'groups' => 'Group 2',
  85. )));
  86. $this->metadata->addConstraint(new Callback(array(
  87. 'callback' => $callback2,
  88. 'groups' => 'Group 3',
  89. )));
  90. $sequence = new GroupSequence(array('Group 1', 'Group 2', 'Group 3'));
  91. $violations = $this->validator->validate($entity, new Valid(), $sequence);
  92. /* @var ConstraintViolationInterface[] $violations */
  93. $this->assertCount(1, $violations);
  94. $this->assertSame('Message 1', $violations[0]->getMessage());
  95. }
  96. public function testGroupSequenceIncludesReferences()
  97. {
  98. $entity = new Entity();
  99. $entity->reference = new Reference();
  100. $callback1 = function ($value, ExecutionContextInterface $context) {
  101. $context->addViolation('Reference violation 1');
  102. };
  103. $callback2 = function ($value, ExecutionContextInterface $context) {
  104. $context->addViolation('Reference violation 2');
  105. };
  106. $this->metadata->addPropertyConstraint('reference', new Valid());
  107. $this->referenceMetadata->addConstraint(new Callback(array(
  108. 'callback' => $callback1,
  109. 'groups' => 'Group 1',
  110. )));
  111. $this->referenceMetadata->addConstraint(new Callback(array(
  112. 'callback' => $callback2,
  113. 'groups' => 'Group 2',
  114. )));
  115. $sequence = new GroupSequence(array('Group 1', 'Entity'));
  116. $violations = $this->validator->validate($entity, new Valid(), $sequence);
  117. /* @var ConstraintViolationInterface[] $violations */
  118. $this->assertCount(1, $violations);
  119. $this->assertSame('Reference violation 1', $violations[0]->getMessage());
  120. }
  121. public function testValidateInSeparateContext()
  122. {
  123. $test = $this;
  124. $entity = new Entity();
  125. $entity->reference = new Reference();
  126. $callback1 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  127. $violations = $context
  128. ->getValidator()
  129. // Since the validator is not context aware, the group must
  130. // be passed explicitly
  131. ->validate($value->reference, new Valid(), 'Group')
  132. ;
  133. /* @var ConstraintViolationInterface[] $violations */
  134. $test->assertCount(1, $violations);
  135. $test->assertSame('Message value', $violations[0]->getMessage());
  136. $test->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  137. $test->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  138. $test->assertSame('', $violations[0]->getPropertyPath());
  139. // The root is different as we're in a new context
  140. $test->assertSame($entity->reference, $violations[0]->getRoot());
  141. $test->assertSame($entity->reference, $violations[0]->getInvalidValue());
  142. $test->assertNull($violations[0]->getPlural());
  143. $test->assertNull($violations[0]->getCode());
  144. // Verify that this method is called
  145. $context->addViolation('Separate violation');
  146. };
  147. $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  148. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  149. $test->assertNull($context->getPropertyName());
  150. $test->assertSame('', $context->getPropertyPath());
  151. $test->assertSame('Group', $context->getGroup());
  152. $test->assertSame($test->referenceMetadata, $context->getMetadata());
  153. $test->assertSame($entity->reference, $context->getRoot());
  154. $test->assertSame($entity->reference, $context->getValue());
  155. $test->assertSame($entity->reference, $value);
  156. $context->addViolation('Message %param%', array('%param%' => 'value'));
  157. };
  158. $this->metadata->addConstraint(new Callback(array(
  159. 'callback' => $callback1,
  160. 'groups' => 'Group',
  161. )));
  162. $this->referenceMetadata->addConstraint(new Callback(array(
  163. 'callback' => $callback2,
  164. 'groups' => 'Group',
  165. )));
  166. $violations = $this->validator->validate($entity, new Valid(), 'Group');
  167. /* @var ConstraintViolationInterface[] $violations */
  168. $this->assertCount(1, $violations);
  169. $test->assertSame('Separate violation', $violations[0]->getMessage());
  170. }
  171. public function testValidateInContext()
  172. {
  173. $test = $this;
  174. $entity = new Entity();
  175. $entity->reference = new Reference();
  176. $callback1 = function ($value, ExecutionContextInterface $context) use ($test) {
  177. $previousValue = $context->getValue();
  178. $previousObject = $context->getObject();
  179. $previousMetadata = $context->getMetadata();
  180. $previousPath = $context->getPropertyPath();
  181. $previousGroup = $context->getGroup();
  182. $context
  183. ->getValidator()
  184. ->inContext($context)
  185. ->atPath('subpath')
  186. ->validate($value->reference)
  187. ;
  188. // context changes shouldn't leak out of the validate() call
  189. $test->assertSame($previousValue, $context->getValue());
  190. $test->assertSame($previousObject, $context->getObject());
  191. $test->assertSame($previousMetadata, $context->getMetadata());
  192. $test->assertSame($previousPath, $context->getPropertyPath());
  193. $test->assertSame($previousGroup, $context->getGroup());
  194. };
  195. $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  196. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  197. $test->assertNull($context->getPropertyName());
  198. $test->assertSame('subpath', $context->getPropertyPath());
  199. $test->assertSame('Group', $context->getGroup());
  200. $test->assertSame($test->referenceMetadata, $context->getMetadata());
  201. $test->assertSame($entity, $context->getRoot());
  202. $test->assertSame($entity->reference, $context->getValue());
  203. $test->assertSame($entity->reference, $value);
  204. $context->addViolation('Message %param%', array('%param%' => 'value'));
  205. };
  206. $this->metadata->addConstraint(new Callback(array(
  207. 'callback' => $callback1,
  208. 'groups' => 'Group',
  209. )));
  210. $this->referenceMetadata->addConstraint(new Callback(array(
  211. 'callback' => $callback2,
  212. 'groups' => 'Group',
  213. )));
  214. $violations = $this->validator->validate($entity, new Valid(), 'Group');
  215. /* @var ConstraintViolationInterface[] $violations */
  216. $this->assertCount(1, $violations);
  217. $this->assertSame('Message value', $violations[0]->getMessage());
  218. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  219. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  220. $this->assertSame('subpath', $violations[0]->getPropertyPath());
  221. $this->assertSame($entity, $violations[0]->getRoot());
  222. $this->assertSame($entity->reference, $violations[0]->getInvalidValue());
  223. $this->assertNull($violations[0]->getPlural());
  224. $this->assertNull($violations[0]->getCode());
  225. }
  226. public function testValidateArrayInContext()
  227. {
  228. $test = $this;
  229. $entity = new Entity();
  230. $entity->reference = new Reference();
  231. $callback1 = function ($value, ExecutionContextInterface $context) use ($test) {
  232. $previousValue = $context->getValue();
  233. $previousObject = $context->getObject();
  234. $previousMetadata = $context->getMetadata();
  235. $previousPath = $context->getPropertyPath();
  236. $previousGroup = $context->getGroup();
  237. $context
  238. ->getValidator()
  239. ->inContext($context)
  240. ->atPath('subpath')
  241. ->validate(array('key' => $value->reference))
  242. ;
  243. // context changes shouldn't leak out of the validate() call
  244. $test->assertSame($previousValue, $context->getValue());
  245. $test->assertSame($previousObject, $context->getObject());
  246. $test->assertSame($previousMetadata, $context->getMetadata());
  247. $test->assertSame($previousPath, $context->getPropertyPath());
  248. $test->assertSame($previousGroup, $context->getGroup());
  249. };
  250. $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  251. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  252. $test->assertNull($context->getPropertyName());
  253. $test->assertSame('subpath[key]', $context->getPropertyPath());
  254. $test->assertSame('Group', $context->getGroup());
  255. $test->assertSame($test->referenceMetadata, $context->getMetadata());
  256. $test->assertSame($entity, $context->getRoot());
  257. $test->assertSame($entity->reference, $context->getValue());
  258. $test->assertSame($entity->reference, $value);
  259. $context->addViolation('Message %param%', array('%param%' => 'value'));
  260. };
  261. $this->metadata->addConstraint(new Callback(array(
  262. 'callback' => $callback1,
  263. 'groups' => 'Group',
  264. )));
  265. $this->referenceMetadata->addConstraint(new Callback(array(
  266. 'callback' => $callback2,
  267. 'groups' => 'Group',
  268. )));
  269. $violations = $this->validator->validate($entity, new Valid(), 'Group');
  270. /* @var ConstraintViolationInterface[] $violations */
  271. $this->assertCount(1, $violations);
  272. $this->assertSame('Message value', $violations[0]->getMessage());
  273. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  274. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  275. $this->assertSame('subpath[key]', $violations[0]->getPropertyPath());
  276. $this->assertSame($entity, $violations[0]->getRoot());
  277. $this->assertSame($entity->reference, $violations[0]->getInvalidValue());
  278. $this->assertNull($violations[0]->getPlural());
  279. $this->assertNull($violations[0]->getCode());
  280. }
  281. public function testTraverseTraversableByDefault()
  282. {
  283. $test = $this;
  284. $entity = new Entity();
  285. $traversable = new \ArrayIterator(array('key' => $entity));
  286. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, $traversable) {
  287. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  288. $test->assertNull($context->getPropertyName());
  289. $test->assertSame('[key]', $context->getPropertyPath());
  290. $test->assertSame('Group', $context->getGroup());
  291. $test->assertSame($test->metadata, $context->getMetadata());
  292. $test->assertSame($traversable, $context->getRoot());
  293. $test->assertSame($entity, $context->getValue());
  294. $test->assertSame($entity, $value);
  295. $context->addViolation('Message %param%', array('%param%' => 'value'));
  296. };
  297. $this->metadataFactory->addMetadata(new ClassMetadata('ArrayIterator'));
  298. $this->metadata->addConstraint(new Callback(array(
  299. 'callback' => $callback,
  300. 'groups' => 'Group',
  301. )));
  302. $violations = $this->validate($traversable, new Valid(), 'Group');
  303. /* @var ConstraintViolationInterface[] $violations */
  304. $this->assertCount(1, $violations);
  305. $this->assertSame('Message value', $violations[0]->getMessage());
  306. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  307. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  308. $this->assertSame('[key]', $violations[0]->getPropertyPath());
  309. $this->assertSame($traversable, $violations[0]->getRoot());
  310. $this->assertSame($entity, $violations[0]->getInvalidValue());
  311. $this->assertNull($violations[0]->getPlural());
  312. $this->assertNull($violations[0]->getCode());
  313. }
  314. public function testTraversalEnabledOnClass()
  315. {
  316. $entity = new Entity();
  317. $traversable = new \ArrayIterator(array('key' => $entity));
  318. $callback = function ($value, ExecutionContextInterface $context) {
  319. $context->addViolation('Message');
  320. };
  321. $traversableMetadata = new ClassMetadata('ArrayIterator');
  322. $traversableMetadata->addConstraint(new Traverse(true));
  323. $this->metadataFactory->addMetadata($traversableMetadata);
  324. $this->metadata->addConstraint(new Callback(array(
  325. 'callback' => $callback,
  326. 'groups' => 'Group',
  327. )));
  328. $violations = $this->validate($traversable, new Valid(), 'Group');
  329. /* @var ConstraintViolationInterface[] $violations */
  330. $this->assertCount(1, $violations);
  331. }
  332. public function testTraversalDisabledOnClass()
  333. {
  334. $test = $this;
  335. $entity = new Entity();
  336. $traversable = new \ArrayIterator(array('key' => $entity));
  337. $callback = function ($value, ExecutionContextInterface $context) use ($test) {
  338. $test->fail('Should not be called');
  339. };
  340. $traversableMetadata = new ClassMetadata('ArrayIterator');
  341. $traversableMetadata->addConstraint(new Traverse(false));
  342. $this->metadataFactory->addMetadata($traversableMetadata);
  343. $this->metadata->addConstraint(new Callback(array(
  344. 'callback' => $callback,
  345. 'groups' => 'Group',
  346. )));
  347. $violations = $this->validate($traversable, new Valid(), 'Group');
  348. /* @var ConstraintViolationInterface[] $violations */
  349. $this->assertCount(0, $violations);
  350. }
  351. /**
  352. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  353. */
  354. public function testExpectTraversableIfTraversalEnabledOnClass()
  355. {
  356. $entity = new Entity();
  357. $this->metadata->addConstraint(new Traverse(true));
  358. $this->validator->validate($entity);
  359. }
  360. public function testReferenceTraversalDisabledOnClass()
  361. {
  362. $test = $this;
  363. $entity = new Entity();
  364. $entity->reference = new \ArrayIterator(array('key' => new Reference()));
  365. $callback = function ($value, ExecutionContextInterface $context) use ($test) {
  366. $test->fail('Should not be called');
  367. };
  368. $traversableMetadata = new ClassMetadata('ArrayIterator');
  369. $traversableMetadata->addConstraint(new Traverse(false));
  370. $this->metadataFactory->addMetadata($traversableMetadata);
  371. $this->referenceMetadata->addConstraint(new Callback(array(
  372. 'callback' => $callback,
  373. 'groups' => 'Group',
  374. )));
  375. $this->metadata->addPropertyConstraint('reference', new Valid());
  376. $violations = $this->validate($entity, new Valid(), 'Group');
  377. /* @var ConstraintViolationInterface[] $violations */
  378. $this->assertCount(0, $violations);
  379. }
  380. public function testReferenceTraversalEnabledOnReferenceDisabledOnClass()
  381. {
  382. $test = $this;
  383. $entity = new Entity();
  384. $entity->reference = new \ArrayIterator(array('key' => new Reference()));
  385. $callback = function ($value, ExecutionContextInterface $context) use ($test) {
  386. $test->fail('Should not be called');
  387. };
  388. $traversableMetadata = new ClassMetadata('ArrayIterator');
  389. $traversableMetadata->addConstraint(new Traverse(false));
  390. $this->metadataFactory->addMetadata($traversableMetadata);
  391. $this->referenceMetadata->addConstraint(new Callback(array(
  392. 'callback' => $callback,
  393. 'groups' => 'Group',
  394. )));
  395. $this->metadata->addPropertyConstraint('reference', new Valid(array(
  396. 'traverse' => true,
  397. )));
  398. $violations = $this->validate($entity, new Valid(), 'Group');
  399. /* @var ConstraintViolationInterface[] $violations */
  400. $this->assertCount(0, $violations);
  401. }
  402. public function testReferenceTraversalDisabledOnReferenceEnabledOnClass()
  403. {
  404. $test = $this;
  405. $entity = new Entity();
  406. $entity->reference = new \ArrayIterator(array('key' => new Reference()));
  407. $callback = function ($value, ExecutionContextInterface $context) use ($test) {
  408. $test->fail('Should not be called');
  409. };
  410. $traversableMetadata = new ClassMetadata('ArrayIterator');
  411. $traversableMetadata->addConstraint(new Traverse(true));
  412. $this->metadataFactory->addMetadata($traversableMetadata);
  413. $this->referenceMetadata->addConstraint(new Callback(array(
  414. 'callback' => $callback,
  415. 'groups' => 'Group',
  416. )));
  417. $this->metadata->addPropertyConstraint('reference', new Valid(array(
  418. 'traverse' => false,
  419. )));
  420. $violations = $this->validate($entity, new Valid(), 'Group');
  421. /* @var ConstraintViolationInterface[] $violations */
  422. $this->assertCount(0, $violations);
  423. }
  424. public function testAddCustomizedViolation()
  425. {
  426. $entity = new Entity();
  427. $callback = function ($value, ExecutionContextInterface $context) {
  428. $context->buildViolation('Message %param%')
  429. ->setParameter('%param%', 'value')
  430. ->setInvalidValue('Invalid value')
  431. ->setPlural(2)
  432. ->setCode(42)
  433. ->addViolation();
  434. };
  435. $this->metadata->addConstraint(new Callback($callback));
  436. $violations = $this->validator->validate($entity);
  437. /* @var ConstraintViolationInterface[] $violations */
  438. $this->assertCount(1, $violations);
  439. $this->assertSame('Message value', $violations[0]->getMessage());
  440. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  441. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  442. $this->assertSame('', $violations[0]->getPropertyPath());
  443. $this->assertSame($entity, $violations[0]->getRoot());
  444. $this->assertSame('Invalid value', $violations[0]->getInvalidValue());
  445. $this->assertSame(2, $violations[0]->getPlural());
  446. $this->assertSame(42, $violations[0]->getCode());
  447. }
  448. /**
  449. * @expectedException \Symfony\Component\Validator\Exception\UnsupportedMetadataException
  450. * @group legacy
  451. */
  452. public function testMetadataMustImplementClassMetadataInterface()
  453. {
  454. $entity = new Entity();
  455. $metadata = $this->getMockBuilder('Symfony\Component\Validator\Tests\Fixtures\LegacyClassMetadata')->getMock();
  456. $metadata->expects($this->any())
  457. ->method('getClassName')
  458. ->will($this->returnValue(\get_class($entity)));
  459. $this->metadataFactory->addMetadata($metadata);
  460. $this->validator->validate($entity);
  461. }
  462. /**
  463. * @expectedException \Symfony\Component\Validator\Exception\UnsupportedMetadataException
  464. * @group legacy
  465. */
  466. public function testReferenceMetadataMustImplementClassMetadataInterface()
  467. {
  468. $entity = new Entity();
  469. $entity->reference = new Reference();
  470. $metadata = $this->getMockBuilder('Symfony\Component\Validator\Tests\Fixtures\LegacyClassMetadata')->getMock();
  471. $metadata->expects($this->any())
  472. ->method('getClassName')
  473. ->will($this->returnValue(\get_class($entity->reference)));
  474. $this->metadataFactory->addMetadata($metadata);
  475. $this->metadata->addPropertyConstraint('reference', new Valid());
  476. $this->validator->validate($entity);
  477. }
  478. /**
  479. * @expectedException \Symfony\Component\Validator\Exception\UnsupportedMetadataException
  480. * @group legacy
  481. */
  482. public function testLegacyPropertyMetadataMustImplementPropertyMetadataInterface()
  483. {
  484. $entity = new Entity();
  485. // Legacy interface
  486. $propertyMetadata = $this->getMockBuilder('Symfony\Component\Validator\MetadataInterface')->getMock();
  487. $metadata = new FakeClassMetadata(\get_class($entity));
  488. $metadata->addCustomPropertyMetadata('firstName', $propertyMetadata);
  489. $this->metadataFactory->addMetadata($metadata);
  490. $this->validator->validate($entity);
  491. }
  492. public function testNoDuplicateValidationIfClassConstraintInMultipleGroups()
  493. {
  494. $entity = new Entity();
  495. $callback = function ($value, ExecutionContextInterface $context) {
  496. $context->addViolation('Message');
  497. };
  498. $this->metadata->addConstraint(new Callback(array(
  499. 'callback' => $callback,
  500. 'groups' => array('Group 1', 'Group 2'),
  501. )));
  502. $violations = $this->validator->validate($entity, new Valid(), array('Group 1', 'Group 2'));
  503. /* @var ConstraintViolationInterface[] $violations */
  504. $this->assertCount(1, $violations);
  505. }
  506. public function testNoDuplicateValidationIfPropertyConstraintInMultipleGroups()
  507. {
  508. $entity = new Entity();
  509. $callback = function ($value, ExecutionContextInterface $context) {
  510. $context->addViolation('Message');
  511. };
  512. $this->metadata->addPropertyConstraint('firstName', new Callback(array(
  513. 'callback' => $callback,
  514. 'groups' => array('Group 1', 'Group 2'),
  515. )));
  516. $violations = $this->validator->validate($entity, new Valid(), array('Group 1', 'Group 2'));
  517. /* @var ConstraintViolationInterface[] $violations */
  518. $this->assertCount(1, $violations);
  519. }
  520. /**
  521. * @expectedException \Symfony\Component\Validator\Exception\RuntimeException
  522. */
  523. public function testValidateFailsIfNoConstraintsAndNoObjectOrArray()
  524. {
  525. $this->validate('Foobar');
  526. }
  527. public function testAccessCurrentObject()
  528. {
  529. $test = $this;
  530. $called = false;
  531. $entity = new Entity();
  532. $entity->firstName = 'Bernhard';
  533. $entity->data = array('firstName' => 'Bernhard');
  534. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, &$called) {
  535. $called = true;
  536. $test->assertSame($entity, $context->getObject());
  537. };
  538. $this->metadata->addConstraint(new Callback($callback));
  539. $this->metadata->addPropertyConstraint('firstName', new Callback($callback));
  540. $this->metadata->addPropertyConstraint('data', new Collection(array('firstName' => new Expression('value == this.firstName'))));
  541. $this->validator->validate($entity);
  542. $this->assertTrue($called);
  543. }
  544. public function testInitializeObjectsOnFirstValidation()
  545. {
  546. $test = $this;
  547. $entity = new Entity();
  548. $entity->initialized = false;
  549. // prepare initializers that set "initialized" to true
  550. $initializer1 = $this->getMockBuilder('Symfony\\Component\\Validator\\ObjectInitializerInterface')->getMock();
  551. $initializer2 = $this->getMockBuilder('Symfony\\Component\\Validator\\ObjectInitializerInterface')->getMock();
  552. $initializer1->expects($this->once())
  553. ->method('initialize')
  554. ->with($entity)
  555. ->will($this->returnCallback(function ($object) {
  556. $object->initialized = true;
  557. }));
  558. $initializer2->expects($this->once())
  559. ->method('initialize')
  560. ->with($entity);
  561. $this->validator = $this->createValidator($this->metadataFactory, array(
  562. $initializer1,
  563. $initializer2,
  564. ));
  565. // prepare constraint which
  566. // * checks that "initialized" is set to true
  567. // * validates the object again
  568. $callback = function ($object, ExecutionContextInterface $context) use ($test) {
  569. $test->assertTrue($object->initialized);
  570. // validate again in same group
  571. $validator = $context->getValidator()->inContext($context);
  572. $validator->validate($object);
  573. // validate again in other group
  574. $validator->validate($object, null, 'SomeGroup');
  575. };
  576. $this->metadata->addConstraint(new Callback($callback));
  577. $this->validate($entity);
  578. $this->assertTrue($entity->initialized);
  579. }
  580. public function testPassConstraintToViolation()
  581. {
  582. $constraint = new FailingConstraint();
  583. $violations = $this->validate('Foobar', $constraint);
  584. $this->assertCount(1, $violations);
  585. $this->assertSame($constraint, $violations[0]->getConstraint());
  586. }
  587. public function testCollectionConstraitViolationHasCorrectContext()
  588. {
  589. $data = array(
  590. 'foo' => 'fooValue',
  591. );
  592. // Missing field must not be the first in the collection validation
  593. $constraint = new Collection(array(
  594. 'foo' => new NotNull(),
  595. 'bar' => new NotNull(),
  596. ));
  597. $violations = $this->validate($data, $constraint);
  598. $this->assertCount(1, $violations);
  599. $this->assertSame($constraint, $violations[0]->getConstraint());
  600. }
  601. }