AbstractValidatorTest.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Validator\Constraints\Callback;
  13. use Symfony\Component\Validator\Constraints\GroupSequence;
  14. use Symfony\Component\Validator\Constraints\Valid;
  15. use Symfony\Component\Validator\ConstraintViolationInterface;
  16. use Symfony\Component\Validator\ExecutionContextInterface;
  17. use Symfony\Component\Validator\Mapping\ClassMetadata;
  18. use Symfony\Component\Validator\Tests\Fixtures\Entity;
  19. use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory;
  20. use Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity;
  21. use Symfony\Component\Validator\Tests\Fixtures\Reference;
  22. /**
  23. * @author Bernhard Schussek <bschussek@gmail.com>
  24. */
  25. abstract class AbstractValidatorTest extends TestCase
  26. {
  27. const ENTITY_CLASS = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
  28. const REFERENCE_CLASS = 'Symfony\Component\Validator\Tests\Fixtures\Reference';
  29. /**
  30. * @var FakeMetadataFactory
  31. */
  32. public $metadataFactory;
  33. /**
  34. * @var ClassMetadata
  35. */
  36. public $metadata;
  37. /**
  38. * @var ClassMetadata
  39. */
  40. public $referenceMetadata;
  41. protected function setUp()
  42. {
  43. $this->metadataFactory = new FakeMetadataFactory();
  44. $this->metadata = new ClassMetadata(self::ENTITY_CLASS);
  45. $this->referenceMetadata = new ClassMetadata(self::REFERENCE_CLASS);
  46. $this->metadataFactory->addMetadata($this->metadata);
  47. $this->metadataFactory->addMetadata($this->referenceMetadata);
  48. }
  49. protected function tearDown()
  50. {
  51. $this->metadataFactory = null;
  52. $this->metadata = null;
  53. $this->referenceMetadata = null;
  54. }
  55. abstract protected function validate($value, $constraints = null, $groups = null);
  56. abstract protected function validateProperty($object, $propertyName, $groups = null);
  57. abstract protected function validatePropertyValue($object, $propertyName, $value, $groups = null);
  58. public function testValidate()
  59. {
  60. $test = $this;
  61. $callback = function ($value, ExecutionContextInterface $context) use ($test) {
  62. $test->assertNull($context->getClassName());
  63. $test->assertNull($context->getPropertyName());
  64. $test->assertSame('', $context->getPropertyPath());
  65. $test->assertSame('Group', $context->getGroup());
  66. $test->assertSame('Bernhard', $context->getRoot());
  67. $test->assertSame('Bernhard', $context->getValue());
  68. $test->assertSame('Bernhard', $value);
  69. $context->addViolation('Message %param%', array('%param%' => 'value'));
  70. };
  71. $constraint = new Callback(array(
  72. 'callback' => $callback,
  73. 'groups' => 'Group',
  74. ));
  75. $violations = $this->validate('Bernhard', $constraint, 'Group');
  76. /* @var ConstraintViolationInterface[] $violations */
  77. $this->assertCount(1, $violations);
  78. $this->assertSame('Message value', $violations[0]->getMessage());
  79. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  80. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  81. $this->assertSame('', $violations[0]->getPropertyPath());
  82. $this->assertSame('Bernhard', $violations[0]->getRoot());
  83. $this->assertSame('Bernhard', $violations[0]->getInvalidValue());
  84. $this->assertNull($violations[0]->getPlural());
  85. $this->assertNull($violations[0]->getCode());
  86. }
  87. public function testClassConstraint()
  88. {
  89. $test = $this;
  90. $entity = new Entity();
  91. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  92. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  93. $test->assertNull($context->getPropertyName());
  94. $test->assertSame('', $context->getPropertyPath());
  95. $test->assertSame('Group', $context->getGroup());
  96. $test->assertSame($test->metadata, $context->getMetadata());
  97. $test->assertSame($entity, $context->getRoot());
  98. $test->assertSame($entity, $context->getValue());
  99. $test->assertSame($entity, $value);
  100. $context->addViolation('Message %param%', array('%param%' => 'value'));
  101. };
  102. $this->metadata->addConstraint(new Callback(array(
  103. 'callback' => $callback,
  104. 'groups' => 'Group',
  105. )));
  106. $violations = $this->validate($entity, null, 'Group');
  107. /* @var ConstraintViolationInterface[] $violations */
  108. $this->assertCount(1, $violations);
  109. $this->assertSame('Message value', $violations[0]->getMessage());
  110. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  111. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  112. $this->assertSame('', $violations[0]->getPropertyPath());
  113. $this->assertSame($entity, $violations[0]->getRoot());
  114. $this->assertSame($entity, $violations[0]->getInvalidValue());
  115. $this->assertNull($violations[0]->getPlural());
  116. $this->assertNull($violations[0]->getCode());
  117. }
  118. public function testPropertyConstraint()
  119. {
  120. $test = $this;
  121. $entity = new Entity();
  122. $entity->firstName = 'Bernhard';
  123. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  124. $propertyMetadatas = $test->metadata->getPropertyMetadata('firstName');
  125. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  126. $test->assertSame('firstName', $context->getPropertyName());
  127. $test->assertSame('firstName', $context->getPropertyPath());
  128. $test->assertSame('Group', $context->getGroup());
  129. $test->assertSame($propertyMetadatas[0], $context->getMetadata());
  130. $test->assertSame($entity, $context->getRoot());
  131. $test->assertSame('Bernhard', $context->getValue());
  132. $test->assertSame('Bernhard', $value);
  133. $context->addViolation('Message %param%', array('%param%' => 'value'));
  134. };
  135. $this->metadata->addPropertyConstraint('firstName', new Callback(array(
  136. 'callback' => $callback,
  137. 'groups' => 'Group',
  138. )));
  139. $violations = $this->validate($entity, null, 'Group');
  140. /* @var ConstraintViolationInterface[] $violations */
  141. $this->assertCount(1, $violations);
  142. $this->assertSame('Message value', $violations[0]->getMessage());
  143. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  144. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  145. $this->assertSame('firstName', $violations[0]->getPropertyPath());
  146. $this->assertSame($entity, $violations[0]->getRoot());
  147. $this->assertSame('Bernhard', $violations[0]->getInvalidValue());
  148. $this->assertNull($violations[0]->getPlural());
  149. $this->assertNull($violations[0]->getCode());
  150. }
  151. public function testGetterConstraint()
  152. {
  153. $test = $this;
  154. $entity = new Entity();
  155. $entity->setLastName('Schussek');
  156. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  157. $propertyMetadatas = $test->metadata->getPropertyMetadata('lastName');
  158. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  159. $test->assertSame('lastName', $context->getPropertyName());
  160. $test->assertSame('lastName', $context->getPropertyPath());
  161. $test->assertSame('Group', $context->getGroup());
  162. $test->assertSame($propertyMetadatas[0], $context->getMetadata());
  163. $test->assertSame($entity, $context->getRoot());
  164. $test->assertSame('Schussek', $context->getValue());
  165. $test->assertSame('Schussek', $value);
  166. $context->addViolation('Message %param%', array('%param%' => 'value'));
  167. };
  168. $this->metadata->addGetterConstraint('lastName', new Callback(array(
  169. 'callback' => $callback,
  170. 'groups' => 'Group',
  171. )));
  172. $violations = $this->validate($entity, null, 'Group');
  173. /* @var ConstraintViolationInterface[] $violations */
  174. $this->assertCount(1, $violations);
  175. $this->assertSame('Message value', $violations[0]->getMessage());
  176. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  177. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  178. $this->assertSame('lastName', $violations[0]->getPropertyPath());
  179. $this->assertSame($entity, $violations[0]->getRoot());
  180. $this->assertSame('Schussek', $violations[0]->getInvalidValue());
  181. $this->assertNull($violations[0]->getPlural());
  182. $this->assertNull($violations[0]->getCode());
  183. }
  184. public function testArray()
  185. {
  186. $test = $this;
  187. $entity = new Entity();
  188. $array = array('key' => $entity);
  189. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, $array) {
  190. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  191. $test->assertNull($context->getPropertyName());
  192. $test->assertSame('[key]', $context->getPropertyPath());
  193. $test->assertSame('Group', $context->getGroup());
  194. $test->assertSame($test->metadata, $context->getMetadata());
  195. $test->assertSame($array, $context->getRoot());
  196. $test->assertSame($entity, $context->getValue());
  197. $test->assertSame($entity, $value);
  198. $context->addViolation('Message %param%', array('%param%' => 'value'));
  199. };
  200. $this->metadata->addConstraint(new Callback(array(
  201. 'callback' => $callback,
  202. 'groups' => 'Group',
  203. )));
  204. $violations = $this->validate($array, null, 'Group');
  205. /* @var ConstraintViolationInterface[] $violations */
  206. $this->assertCount(1, $violations);
  207. $this->assertSame('Message value', $violations[0]->getMessage());
  208. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  209. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  210. $this->assertSame('[key]', $violations[0]->getPropertyPath());
  211. $this->assertSame($array, $violations[0]->getRoot());
  212. $this->assertSame($entity, $violations[0]->getInvalidValue());
  213. $this->assertNull($violations[0]->getPlural());
  214. $this->assertNull($violations[0]->getCode());
  215. }
  216. public function testRecursiveArray()
  217. {
  218. $test = $this;
  219. $entity = new Entity();
  220. $array = array(2 => array('key' => $entity));
  221. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, $array) {
  222. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  223. $test->assertNull($context->getPropertyName());
  224. $test->assertSame('[2][key]', $context->getPropertyPath());
  225. $test->assertSame('Group', $context->getGroup());
  226. $test->assertSame($test->metadata, $context->getMetadata());
  227. $test->assertSame($array, $context->getRoot());
  228. $test->assertSame($entity, $context->getValue());
  229. $test->assertSame($entity, $value);
  230. $context->addViolation('Message %param%', array('%param%' => 'value'));
  231. };
  232. $this->metadata->addConstraint(new Callback(array(
  233. 'callback' => $callback,
  234. 'groups' => 'Group',
  235. )));
  236. $violations = $this->validate($array, null, 'Group');
  237. /* @var ConstraintViolationInterface[] $violations */
  238. $this->assertCount(1, $violations);
  239. $this->assertSame('Message value', $violations[0]->getMessage());
  240. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  241. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  242. $this->assertSame('[2][key]', $violations[0]->getPropertyPath());
  243. $this->assertSame($array, $violations[0]->getRoot());
  244. $this->assertSame($entity, $violations[0]->getInvalidValue());
  245. $this->assertNull($violations[0]->getPlural());
  246. $this->assertNull($violations[0]->getCode());
  247. }
  248. public function testTraversable()
  249. {
  250. $test = $this;
  251. $entity = new Entity();
  252. $traversable = new \ArrayIterator(array('key' => $entity));
  253. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, $traversable) {
  254. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  255. $test->assertNull($context->getPropertyName());
  256. $test->assertSame('[key]', $context->getPropertyPath());
  257. $test->assertSame('Group', $context->getGroup());
  258. $test->assertSame($test->metadata, $context->getMetadata());
  259. $test->assertSame($traversable, $context->getRoot());
  260. $test->assertSame($entity, $context->getValue());
  261. $test->assertSame($entity, $value);
  262. $context->addViolation('Message %param%', array('%param%' => 'value'));
  263. };
  264. $this->metadata->addConstraint(new Callback(array(
  265. 'callback' => $callback,
  266. 'groups' => 'Group',
  267. )));
  268. $violations = $this->validate($traversable, null, 'Group');
  269. /* @var ConstraintViolationInterface[] $violations */
  270. $this->assertCount(1, $violations);
  271. $this->assertSame('Message value', $violations[0]->getMessage());
  272. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  273. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  274. $this->assertSame('[key]', $violations[0]->getPropertyPath());
  275. $this->assertSame($traversable, $violations[0]->getRoot());
  276. $this->assertSame($entity, $violations[0]->getInvalidValue());
  277. $this->assertNull($violations[0]->getPlural());
  278. $this->assertNull($violations[0]->getCode());
  279. }
  280. public function testRecursiveTraversable()
  281. {
  282. $test = $this;
  283. $entity = new Entity();
  284. $traversable = new \ArrayIterator(array(
  285. 2 => new \ArrayIterator(array('key' => $entity)),
  286. ));
  287. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, $traversable) {
  288. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  289. $test->assertNull($context->getPropertyName());
  290. $test->assertSame('[2][key]', $context->getPropertyPath());
  291. $test->assertSame('Group', $context->getGroup());
  292. $test->assertSame($test->metadata, $context->getMetadata());
  293. $test->assertSame($traversable, $context->getRoot());
  294. $test->assertSame($entity, $context->getValue());
  295. $test->assertSame($entity, $value);
  296. $context->addViolation('Message %param%', array('%param%' => 'value'));
  297. };
  298. $this->metadata->addConstraint(new Callback(array(
  299. 'callback' => $callback,
  300. 'groups' => 'Group',
  301. )));
  302. $violations = $this->validate($traversable, null, '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('[2][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 testReferenceClassConstraint()
  315. {
  316. $test = $this;
  317. $entity = new Entity();
  318. $entity->reference = new Reference();
  319. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  320. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  321. $test->assertNull($context->getPropertyName());
  322. $test->assertSame('reference', $context->getPropertyPath());
  323. $test->assertSame('Group', $context->getGroup());
  324. $test->assertSame($test->referenceMetadata, $context->getMetadata());
  325. $test->assertSame($entity, $context->getRoot());
  326. $test->assertSame($entity->reference, $context->getValue());
  327. $test->assertSame($entity->reference, $value);
  328. $context->addViolation('Message %param%', array('%param%' => 'value'));
  329. };
  330. $this->metadata->addPropertyConstraint('reference', new Valid());
  331. $this->referenceMetadata->addConstraint(new Callback(array(
  332. 'callback' => $callback,
  333. 'groups' => 'Group',
  334. )));
  335. $violations = $this->validate($entity, null, 'Group');
  336. /* @var ConstraintViolationInterface[] $violations */
  337. $this->assertCount(1, $violations);
  338. $this->assertSame('Message value', $violations[0]->getMessage());
  339. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  340. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  341. $this->assertSame('reference', $violations[0]->getPropertyPath());
  342. $this->assertSame($entity, $violations[0]->getRoot());
  343. $this->assertSame($entity->reference, $violations[0]->getInvalidValue());
  344. $this->assertNull($violations[0]->getPlural());
  345. $this->assertNull($violations[0]->getCode());
  346. }
  347. public function testReferencePropertyConstraint()
  348. {
  349. $test = $this;
  350. $entity = new Entity();
  351. $entity->reference = new Reference();
  352. $entity->reference->value = 'Foobar';
  353. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  354. $propertyMetadatas = $test->referenceMetadata->getPropertyMetadata('value');
  355. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  356. $test->assertSame('value', $context->getPropertyName());
  357. $test->assertSame('reference.value', $context->getPropertyPath());
  358. $test->assertSame('Group', $context->getGroup());
  359. $test->assertSame($propertyMetadatas[0], $context->getMetadata());
  360. $test->assertSame($entity, $context->getRoot());
  361. $test->assertSame('Foobar', $context->getValue());
  362. $test->assertSame('Foobar', $value);
  363. $context->addViolation('Message %param%', array('%param%' => 'value'));
  364. };
  365. $this->metadata->addPropertyConstraint('reference', new Valid());
  366. $this->referenceMetadata->addPropertyConstraint('value', new Callback(array(
  367. 'callback' => $callback,
  368. 'groups' => 'Group',
  369. )));
  370. $violations = $this->validate($entity, null, 'Group');
  371. /* @var ConstraintViolationInterface[] $violations */
  372. $this->assertCount(1, $violations);
  373. $this->assertSame('Message value', $violations[0]->getMessage());
  374. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  375. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  376. $this->assertSame('reference.value', $violations[0]->getPropertyPath());
  377. $this->assertSame($entity, $violations[0]->getRoot());
  378. $this->assertSame('Foobar', $violations[0]->getInvalidValue());
  379. $this->assertNull($violations[0]->getPlural());
  380. $this->assertNull($violations[0]->getCode());
  381. }
  382. public function testReferenceGetterConstraint()
  383. {
  384. $test = $this;
  385. $entity = new Entity();
  386. $entity->reference = new Reference();
  387. $entity->reference->setPrivateValue('Bamboo');
  388. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  389. $propertyMetadatas = $test->referenceMetadata->getPropertyMetadata('privateValue');
  390. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  391. $test->assertSame('privateValue', $context->getPropertyName());
  392. $test->assertSame('reference.privateValue', $context->getPropertyPath());
  393. $test->assertSame('Group', $context->getGroup());
  394. $test->assertSame($propertyMetadatas[0], $context->getMetadata());
  395. $test->assertSame($entity, $context->getRoot());
  396. $test->assertSame('Bamboo', $context->getValue());
  397. $test->assertSame('Bamboo', $value);
  398. $context->addViolation('Message %param%', array('%param%' => 'value'));
  399. };
  400. $this->metadata->addPropertyConstraint('reference', new Valid());
  401. $this->referenceMetadata->addPropertyConstraint('privateValue', new Callback(array(
  402. 'callback' => $callback,
  403. 'groups' => 'Group',
  404. )));
  405. $violations = $this->validate($entity, null, 'Group');
  406. /* @var ConstraintViolationInterface[] $violations */
  407. $this->assertCount(1, $violations);
  408. $this->assertSame('Message value', $violations[0]->getMessage());
  409. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  410. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  411. $this->assertSame('reference.privateValue', $violations[0]->getPropertyPath());
  412. $this->assertSame($entity, $violations[0]->getRoot());
  413. $this->assertSame('Bamboo', $violations[0]->getInvalidValue());
  414. $this->assertNull($violations[0]->getPlural());
  415. $this->assertNull($violations[0]->getCode());
  416. }
  417. public function testsIgnoreNullReference()
  418. {
  419. $entity = new Entity();
  420. $entity->reference = null;
  421. $this->metadata->addPropertyConstraint('reference', new Valid());
  422. $violations = $this->validate($entity);
  423. /* @var ConstraintViolationInterface[] $violations */
  424. $this->assertCount(0, $violations);
  425. }
  426. /**
  427. * @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
  428. */
  429. public function testFailOnScalarReferences()
  430. {
  431. $entity = new Entity();
  432. $entity->reference = 'string';
  433. $this->metadata->addPropertyConstraint('reference', new Valid());
  434. $this->validate($entity);
  435. }
  436. public function testArrayReference()
  437. {
  438. $test = $this;
  439. $entity = new Entity();
  440. $entity->reference = array('key' => new Reference());
  441. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  442. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  443. $test->assertNull($context->getPropertyName());
  444. $test->assertSame('reference[key]', $context->getPropertyPath());
  445. $test->assertSame('Group', $context->getGroup());
  446. $test->assertSame($test->referenceMetadata, $context->getMetadata());
  447. $test->assertSame($entity, $context->getRoot());
  448. $test->assertSame($entity->reference['key'], $context->getValue());
  449. $test->assertSame($entity->reference['key'], $value);
  450. $context->addViolation('Message %param%', array('%param%' => 'value'));
  451. };
  452. $this->metadata->addPropertyConstraint('reference', new Valid());
  453. $this->referenceMetadata->addConstraint(new Callback(array(
  454. 'callback' => $callback,
  455. 'groups' => 'Group',
  456. )));
  457. $violations = $this->validate($entity, null, 'Group');
  458. /* @var ConstraintViolationInterface[] $violations */
  459. $this->assertCount(1, $violations);
  460. $this->assertSame('Message value', $violations[0]->getMessage());
  461. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  462. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  463. $this->assertSame('reference[key]', $violations[0]->getPropertyPath());
  464. $this->assertSame($entity, $violations[0]->getRoot());
  465. $this->assertSame($entity->reference['key'], $violations[0]->getInvalidValue());
  466. $this->assertNull($violations[0]->getPlural());
  467. $this->assertNull($violations[0]->getCode());
  468. }
  469. // https://github.com/symfony/symfony/issues/6246
  470. public function testRecursiveArrayReference()
  471. {
  472. $test = $this;
  473. $entity = new Entity();
  474. $entity->reference = array(2 => array('key' => new Reference()));
  475. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  476. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  477. $test->assertNull($context->getPropertyName());
  478. $test->assertSame('reference[2][key]', $context->getPropertyPath());
  479. $test->assertSame('Group', $context->getGroup());
  480. $test->assertSame($test->referenceMetadata, $context->getMetadata());
  481. $test->assertSame($entity, $context->getRoot());
  482. $test->assertSame($entity->reference[2]['key'], $context->getValue());
  483. $test->assertSame($entity->reference[2]['key'], $value);
  484. $context->addViolation('Message %param%', array('%param%' => 'value'));
  485. };
  486. $this->metadata->addPropertyConstraint('reference', new Valid());
  487. $this->referenceMetadata->addConstraint(new Callback(array(
  488. 'callback' => $callback,
  489. 'groups' => 'Group',
  490. )));
  491. $violations = $this->validate($entity, null, 'Group');
  492. /* @var ConstraintViolationInterface[] $violations */
  493. $this->assertCount(1, $violations);
  494. $this->assertSame('Message value', $violations[0]->getMessage());
  495. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  496. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  497. $this->assertSame('reference[2][key]', $violations[0]->getPropertyPath());
  498. $this->assertSame($entity, $violations[0]->getRoot());
  499. $this->assertSame($entity->reference[2]['key'], $violations[0]->getInvalidValue());
  500. $this->assertNull($violations[0]->getPlural());
  501. $this->assertNull($violations[0]->getCode());
  502. }
  503. public function testArrayTraversalCannotBeDisabled()
  504. {
  505. $entity = new Entity();
  506. $entity->reference = array('key' => new Reference());
  507. $callback = function ($value, ExecutionContextInterface $context) {
  508. $context->addViolation('Message %param%', array('%param%' => 'value'));
  509. };
  510. $this->metadata->addPropertyConstraint('reference', new Valid(array(
  511. 'traverse' => false,
  512. )));
  513. $this->referenceMetadata->addConstraint(new Callback($callback));
  514. $violations = $this->validate($entity);
  515. /* @var ConstraintViolationInterface[] $violations */
  516. $this->assertCount(1, $violations);
  517. }
  518. public function testRecursiveArrayTraversalCannotBeDisabled()
  519. {
  520. $entity = new Entity();
  521. $entity->reference = array(2 => array('key' => new Reference()));
  522. $callback = function ($value, ExecutionContextInterface $context) {
  523. $context->addViolation('Message %param%', array('%param%' => 'value'));
  524. };
  525. $this->metadata->addPropertyConstraint('reference', new Valid(array(
  526. 'traverse' => false,
  527. )));
  528. $this->referenceMetadata->addConstraint(new Callback($callback));
  529. $violations = $this->validate($entity);
  530. /* @var ConstraintViolationInterface[] $violations */
  531. $this->assertCount(1, $violations);
  532. }
  533. public function testIgnoreScalarsDuringArrayTraversal()
  534. {
  535. $entity = new Entity();
  536. $entity->reference = array('string', 1234);
  537. $this->metadata->addPropertyConstraint('reference', new Valid());
  538. $violations = $this->validate($entity);
  539. /* @var ConstraintViolationInterface[] $violations */
  540. $this->assertCount(0, $violations);
  541. }
  542. public function testIgnoreNullDuringArrayTraversal()
  543. {
  544. $entity = new Entity();
  545. $entity->reference = array(null);
  546. $this->metadata->addPropertyConstraint('reference', new Valid());
  547. $violations = $this->validate($entity);
  548. /* @var ConstraintViolationInterface[] $violations */
  549. $this->assertCount(0, $violations);
  550. }
  551. public function testTraversableReference()
  552. {
  553. $test = $this;
  554. $entity = new Entity();
  555. $entity->reference = new \ArrayIterator(array('key' => new Reference()));
  556. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  557. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  558. $test->assertNull($context->getPropertyName());
  559. $test->assertSame('reference[key]', $context->getPropertyPath());
  560. $test->assertSame('Group', $context->getGroup());
  561. $test->assertSame($test->referenceMetadata, $context->getMetadata());
  562. $test->assertSame($entity, $context->getRoot());
  563. $test->assertSame($entity->reference['key'], $context->getValue());
  564. $test->assertSame($entity->reference['key'], $value);
  565. $context->addViolation('Message %param%', array('%param%' => 'value'));
  566. };
  567. $this->metadata->addPropertyConstraint('reference', new Valid());
  568. $this->referenceMetadata->addConstraint(new Callback(array(
  569. 'callback' => $callback,
  570. 'groups' => 'Group',
  571. )));
  572. $violations = $this->validate($entity, null, 'Group');
  573. /* @var ConstraintViolationInterface[] $violations */
  574. $this->assertCount(1, $violations);
  575. $this->assertSame('Message value', $violations[0]->getMessage());
  576. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  577. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  578. $this->assertSame('reference[key]', $violations[0]->getPropertyPath());
  579. $this->assertSame($entity, $violations[0]->getRoot());
  580. $this->assertSame($entity->reference['key'], $violations[0]->getInvalidValue());
  581. $this->assertNull($violations[0]->getPlural());
  582. $this->assertNull($violations[0]->getCode());
  583. }
  584. public function testDisableTraversableTraversal()
  585. {
  586. $entity = new Entity();
  587. $entity->reference = new \ArrayIterator(array('key' => new Reference()));
  588. $callback = function ($value, ExecutionContextInterface $context) {
  589. $context->addViolation('Message %param%', array('%param%' => 'value'));
  590. };
  591. $this->metadataFactory->addMetadata(new ClassMetadata('ArrayIterator'));
  592. $this->metadata->addPropertyConstraint('reference', new Valid(array(
  593. 'traverse' => false,
  594. )));
  595. $this->referenceMetadata->addConstraint(new Callback($callback));
  596. $violations = $this->validate($entity);
  597. /* @var ConstraintViolationInterface[] $violations */
  598. $this->assertCount(0, $violations);
  599. }
  600. /**
  601. * @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
  602. */
  603. public function testMetadataMustExistIfTraversalIsDisabled()
  604. {
  605. $entity = new Entity();
  606. $entity->reference = new \ArrayIterator();
  607. $this->metadata->addPropertyConstraint('reference', new Valid(array(
  608. 'traverse' => false,
  609. )));
  610. $this->validate($entity);
  611. }
  612. public function testEnableRecursiveTraversableTraversal()
  613. {
  614. $test = $this;
  615. $entity = new Entity();
  616. $entity->reference = new \ArrayIterator(array(
  617. 2 => new \ArrayIterator(array('key' => new Reference())),
  618. ));
  619. $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  620. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  621. $test->assertNull($context->getPropertyName());
  622. $test->assertSame('reference[2][key]', $context->getPropertyPath());
  623. $test->assertSame('Group', $context->getGroup());
  624. $test->assertSame($test->referenceMetadata, $context->getMetadata());
  625. $test->assertSame($entity, $context->getRoot());
  626. $test->assertSame($entity->reference[2]['key'], $context->getValue());
  627. $test->assertSame($entity->reference[2]['key'], $value);
  628. $context->addViolation('Message %param%', array('%param%' => 'value'));
  629. };
  630. $this->metadata->addPropertyConstraint('reference', new Valid(array(
  631. 'traverse' => true,
  632. )));
  633. $this->referenceMetadata->addConstraint(new Callback(array(
  634. 'callback' => $callback,
  635. 'groups' => 'Group',
  636. )));
  637. $violations = $this->validate($entity, null, 'Group');
  638. /* @var ConstraintViolationInterface[] $violations */
  639. $this->assertCount(1, $violations);
  640. $this->assertSame('Message value', $violations[0]->getMessage());
  641. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  642. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  643. $this->assertSame('reference[2][key]', $violations[0]->getPropertyPath());
  644. $this->assertSame($entity, $violations[0]->getRoot());
  645. $this->assertSame($entity->reference[2]['key'], $violations[0]->getInvalidValue());
  646. $this->assertNull($violations[0]->getPlural());
  647. $this->assertNull($violations[0]->getCode());
  648. }
  649. public function testValidateProperty()
  650. {
  651. $test = $this;
  652. $entity = new Entity();
  653. $entity->firstName = 'Bernhard';
  654. $entity->setLastName('Schussek');
  655. $callback1 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  656. $propertyMetadatas = $test->metadata->getPropertyMetadata('firstName');
  657. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  658. $test->assertSame('firstName', $context->getPropertyName());
  659. $test->assertSame('firstName', $context->getPropertyPath());
  660. $test->assertSame('Group', $context->getGroup());
  661. $test->assertSame($propertyMetadatas[0], $context->getMetadata());
  662. $test->assertSame($entity, $context->getRoot());
  663. $test->assertSame('Bernhard', $context->getValue());
  664. $test->assertSame('Bernhard', $value);
  665. $context->addViolation('Message %param%', array('%param%' => 'value'));
  666. };
  667. $callback2 = function ($value, ExecutionContextInterface $context) {
  668. $context->addViolation('Other violation');
  669. };
  670. $this->metadata->addPropertyConstraint('firstName', new Callback(array(
  671. 'callback' => $callback1,
  672. 'groups' => 'Group',
  673. )));
  674. $this->metadata->addPropertyConstraint('lastName', new Callback(array(
  675. 'callback' => $callback2,
  676. 'groups' => 'Group',
  677. )));
  678. $violations = $this->validateProperty($entity, 'firstName', 'Group');
  679. /* @var ConstraintViolationInterface[] $violations */
  680. $this->assertCount(1, $violations);
  681. $this->assertSame('Message value', $violations[0]->getMessage());
  682. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  683. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  684. $this->assertSame('firstName', $violations[0]->getPropertyPath());
  685. $this->assertSame($entity, $violations[0]->getRoot());
  686. $this->assertSame('Bernhard', $violations[0]->getInvalidValue());
  687. $this->assertNull($violations[0]->getPlural());
  688. $this->assertNull($violations[0]->getCode());
  689. }
  690. /**
  691. * Cannot be UnsupportedMetadataException for BC with Symfony < 2.5.
  692. *
  693. * @expectedException \Symfony\Component\Validator\Exception\ValidatorException
  694. * @group legacy
  695. */
  696. public function testLegacyValidatePropertyFailsIfPropertiesNotSupported()
  697. {
  698. // $metadata does not implement PropertyMetadataContainerInterface
  699. $metadata = $this->getMockBuilder('Symfony\Component\Validator\MetadataInterface')->getMock();
  700. $this->metadataFactory->addMetadataForValue('VALUE', $metadata);
  701. $this->validateProperty('VALUE', 'someProperty');
  702. }
  703. /**
  704. * https://github.com/symfony/symfony/issues/11604.
  705. */
  706. public function testValidatePropertyWithoutConstraints()
  707. {
  708. $entity = new Entity();
  709. $violations = $this->validateProperty($entity, 'lastName');
  710. $this->assertCount(0, $violations, '->validateProperty() returns no violations if no constraints have been configured for the property being validated');
  711. }
  712. public function testValidatePropertyValue()
  713. {
  714. $test = $this;
  715. $entity = new Entity();
  716. $entity->setLastName('Schussek');
  717. $callback1 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  718. $propertyMetadatas = $test->metadata->getPropertyMetadata('firstName');
  719. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  720. $test->assertSame('firstName', $context->getPropertyName());
  721. $test->assertSame('firstName', $context->getPropertyPath());
  722. $test->assertSame('Group', $context->getGroup());
  723. $test->assertSame($propertyMetadatas[0], $context->getMetadata());
  724. $test->assertSame($entity, $context->getRoot());
  725. $test->assertSame('Bernhard', $context->getValue());
  726. $test->assertSame('Bernhard', $value);
  727. $context->addViolation('Message %param%', array('%param%' => 'value'));
  728. };
  729. $callback2 = function ($value, ExecutionContextInterface $context) {
  730. $context->addViolation('Other violation');
  731. };
  732. $this->metadata->addPropertyConstraint('firstName', new Callback(array(
  733. 'callback' => $callback1,
  734. 'groups' => 'Group',
  735. )));
  736. $this->metadata->addPropertyConstraint('lastName', new Callback(array(
  737. 'callback' => $callback2,
  738. 'groups' => 'Group',
  739. )));
  740. $violations = $this->validatePropertyValue(
  741. $entity,
  742. 'firstName',
  743. 'Bernhard',
  744. 'Group'
  745. );
  746. /* @var ConstraintViolationInterface[] $violations */
  747. $this->assertCount(1, $violations);
  748. $this->assertSame('Message value', $violations[0]->getMessage());
  749. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  750. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  751. $this->assertSame('firstName', $violations[0]->getPropertyPath());
  752. $this->assertSame($entity, $violations[0]->getRoot());
  753. $this->assertSame('Bernhard', $violations[0]->getInvalidValue());
  754. $this->assertNull($violations[0]->getPlural());
  755. $this->assertNull($violations[0]->getCode());
  756. }
  757. public function testValidatePropertyValueWithClassName()
  758. {
  759. $test = $this;
  760. $callback1 = function ($value, ExecutionContextInterface $context) use ($test) {
  761. $propertyMetadatas = $test->metadata->getPropertyMetadata('firstName');
  762. $test->assertSame($test::ENTITY_CLASS, $context->getClassName());
  763. $test->assertSame('firstName', $context->getPropertyName());
  764. $test->assertSame('', $context->getPropertyPath());
  765. $test->assertSame('Group', $context->getGroup());
  766. $test->assertSame($propertyMetadatas[0], $context->getMetadata());
  767. $test->assertSame('Bernhard', $context->getRoot());
  768. $test->assertSame('Bernhard', $context->getValue());
  769. $test->assertSame('Bernhard', $value);
  770. $context->addViolation('Message %param%', array('%param%' => 'value'));
  771. };
  772. $callback2 = function ($value, ExecutionContextInterface $context) {
  773. $context->addViolation('Other violation');
  774. };
  775. $this->metadata->addPropertyConstraint('firstName', new Callback(array(
  776. 'callback' => $callback1,
  777. 'groups' => 'Group',
  778. )));
  779. $this->metadata->addPropertyConstraint('lastName', new Callback(array(
  780. 'callback' => $callback2,
  781. 'groups' => 'Group',
  782. )));
  783. $violations = $this->validatePropertyValue(
  784. self::ENTITY_CLASS,
  785. 'firstName',
  786. 'Bernhard',
  787. 'Group'
  788. );
  789. /* @var ConstraintViolationInterface[] $violations */
  790. $this->assertCount(1, $violations);
  791. $this->assertSame('Message value', $violations[0]->getMessage());
  792. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  793. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  794. $this->assertSame('', $violations[0]->getPropertyPath());
  795. $this->assertSame('Bernhard', $violations[0]->getRoot());
  796. $this->assertSame('Bernhard', $violations[0]->getInvalidValue());
  797. $this->assertNull($violations[0]->getPlural());
  798. $this->assertNull($violations[0]->getCode());
  799. }
  800. /**
  801. * Cannot be UnsupportedMetadataException for BC with Symfony < 2.5.
  802. *
  803. * @expectedException \Symfony\Component\Validator\Exception\ValidatorException
  804. * @group legacy
  805. */
  806. public function testLegacyValidatePropertyValueFailsIfPropertiesNotSupported()
  807. {
  808. // $metadata does not implement PropertyMetadataContainerInterface
  809. $metadata = $this->getMockBuilder('Symfony\Component\Validator\MetadataInterface')->getMock();
  810. $this->metadataFactory->addMetadataForValue('VALUE', $metadata);
  811. $this->validatePropertyValue('VALUE', 'someProperty', 'someValue');
  812. }
  813. /**
  814. * https://github.com/symfony/symfony/issues/11604.
  815. */
  816. public function testValidatePropertyValueWithoutConstraints()
  817. {
  818. $entity = new Entity();
  819. $violations = $this->validatePropertyValue($entity, 'lastName', 'foo');
  820. $this->assertCount(0, $violations, '->validatePropertyValue() returns no violations if no constraints have been configured for the property being validated');
  821. }
  822. public function testValidateObjectOnlyOncePerGroup()
  823. {
  824. $entity = new Entity();
  825. $entity->reference = new Reference();
  826. $entity->reference2 = $entity->reference;
  827. $callback = function ($value, ExecutionContextInterface $context) {
  828. $context->addViolation('Message');
  829. };
  830. $this->metadata->addPropertyConstraint('reference', new Valid());
  831. $this->metadata->addPropertyConstraint('reference2', new Valid());
  832. $this->referenceMetadata->addConstraint(new Callback($callback));
  833. $violations = $this->validate($entity);
  834. /* @var ConstraintViolationInterface[] $violations */
  835. $this->assertCount(1, $violations);
  836. }
  837. public function testValidateDifferentObjectsSeparately()
  838. {
  839. $entity = new Entity();
  840. $entity->reference = new Reference();
  841. $entity->reference2 = new Reference();
  842. $callback = function ($value, ExecutionContextInterface $context) {
  843. $context->addViolation('Message');
  844. };
  845. $this->metadata->addPropertyConstraint('reference', new Valid());
  846. $this->metadata->addPropertyConstraint('reference2', new Valid());
  847. $this->referenceMetadata->addConstraint(new Callback($callback));
  848. $violations = $this->validate($entity);
  849. /* @var ConstraintViolationInterface[] $violations */
  850. $this->assertCount(2, $violations);
  851. }
  852. public function testValidateSingleGroup()
  853. {
  854. $entity = new Entity();
  855. $callback = function ($value, ExecutionContextInterface $context) {
  856. $context->addViolation('Message');
  857. };
  858. $this->metadata->addConstraint(new Callback(array(
  859. 'callback' => $callback,
  860. 'groups' => 'Group 1',
  861. )));
  862. $this->metadata->addConstraint(new Callback(array(
  863. 'callback' => $callback,
  864. 'groups' => 'Group 2',
  865. )));
  866. $violations = $this->validate($entity, null, 'Group 2');
  867. /* @var ConstraintViolationInterface[] $violations */
  868. $this->assertCount(1, $violations);
  869. }
  870. public function testValidateMultipleGroups()
  871. {
  872. $entity = new Entity();
  873. $callback = function ($value, ExecutionContextInterface $context) {
  874. $context->addViolation('Message');
  875. };
  876. $this->metadata->addConstraint(new Callback(array(
  877. 'callback' => $callback,
  878. 'groups' => 'Group 1',
  879. )));
  880. $this->metadata->addConstraint(new Callback(array(
  881. 'callback' => $callback,
  882. 'groups' => 'Group 2',
  883. )));
  884. $violations = $this->validate($entity, null, array('Group 1', 'Group 2'));
  885. /* @var ConstraintViolationInterface[] $violations */
  886. $this->assertCount(2, $violations);
  887. }
  888. public function testReplaceDefaultGroupByGroupSequenceObject()
  889. {
  890. $entity = new Entity();
  891. $callback1 = function ($value, ExecutionContextInterface $context) {
  892. $context->addViolation('Violation in Group 2');
  893. };
  894. $callback2 = function ($value, ExecutionContextInterface $context) {
  895. $context->addViolation('Violation in Group 3');
  896. };
  897. $this->metadata->addConstraint(new Callback(array(
  898. 'callback' => function () {},
  899. 'groups' => 'Group 1',
  900. )));
  901. $this->metadata->addConstraint(new Callback(array(
  902. 'callback' => $callback1,
  903. 'groups' => 'Group 2',
  904. )));
  905. $this->metadata->addConstraint(new Callback(array(
  906. 'callback' => $callback2,
  907. 'groups' => 'Group 3',
  908. )));
  909. $sequence = new GroupSequence(array('Group 1', 'Group 2', 'Group 3', 'Entity'));
  910. $this->metadata->setGroupSequence($sequence);
  911. $violations = $this->validate($entity, null, 'Default');
  912. /* @var ConstraintViolationInterface[] $violations */
  913. $this->assertCount(1, $violations);
  914. $this->assertSame('Violation in Group 2', $violations[0]->getMessage());
  915. }
  916. public function testReplaceDefaultGroupByGroupSequenceArray()
  917. {
  918. $entity = new Entity();
  919. $callback1 = function ($value, ExecutionContextInterface $context) {
  920. $context->addViolation('Violation in Group 2');
  921. };
  922. $callback2 = function ($value, ExecutionContextInterface $context) {
  923. $context->addViolation('Violation in Group 3');
  924. };
  925. $this->metadata->addConstraint(new Callback(array(
  926. 'callback' => function () {},
  927. 'groups' => 'Group 1',
  928. )));
  929. $this->metadata->addConstraint(new Callback(array(
  930. 'callback' => $callback1,
  931. 'groups' => 'Group 2',
  932. )));
  933. $this->metadata->addConstraint(new Callback(array(
  934. 'callback' => $callback2,
  935. 'groups' => 'Group 3',
  936. )));
  937. $sequence = array('Group 1', 'Group 2', 'Group 3', 'Entity');
  938. $this->metadata->setGroupSequence($sequence);
  939. $violations = $this->validate($entity, null, 'Default');
  940. /* @var ConstraintViolationInterface[] $violations */
  941. $this->assertCount(1, $violations);
  942. $this->assertSame('Violation in Group 2', $violations[0]->getMessage());
  943. }
  944. public function testPropagateDefaultGroupToReferenceWhenReplacingDefaultGroup()
  945. {
  946. $entity = new Entity();
  947. $entity->reference = new Reference();
  948. $callback1 = function ($value, ExecutionContextInterface $context) {
  949. $context->addViolation('Violation in Default group');
  950. };
  951. $callback2 = function ($value, ExecutionContextInterface $context) {
  952. $context->addViolation('Violation in group sequence');
  953. };
  954. $this->metadata->addPropertyConstraint('reference', new Valid());
  955. $this->referenceMetadata->addConstraint(new Callback(array(
  956. 'callback' => $callback1,
  957. 'groups' => 'Default',
  958. )));
  959. $this->referenceMetadata->addConstraint(new Callback(array(
  960. 'callback' => $callback2,
  961. 'groups' => 'Group 1',
  962. )));
  963. $sequence = new GroupSequence(array('Group 1', 'Entity'));
  964. $this->metadata->setGroupSequence($sequence);
  965. $violations = $this->validate($entity, null, 'Default');
  966. /* @var ConstraintViolationInterface[] $violations */
  967. $this->assertCount(1, $violations);
  968. $this->assertSame('Violation in Default group', $violations[0]->getMessage());
  969. }
  970. public function testValidateCustomGroupWhenDefaultGroupWasReplaced()
  971. {
  972. $entity = new Entity();
  973. $callback1 = function ($value, ExecutionContextInterface $context) {
  974. $context->addViolation('Violation in other group');
  975. };
  976. $callback2 = function ($value, ExecutionContextInterface $context) {
  977. $context->addViolation('Violation in group sequence');
  978. };
  979. $this->metadata->addConstraint(new Callback(array(
  980. 'callback' => $callback1,
  981. 'groups' => 'Other Group',
  982. )));
  983. $this->metadata->addConstraint(new Callback(array(
  984. 'callback' => $callback2,
  985. 'groups' => 'Group 1',
  986. )));
  987. $sequence = new GroupSequence(array('Group 1', 'Entity'));
  988. $this->metadata->setGroupSequence($sequence);
  989. $violations = $this->validate($entity, null, 'Other Group');
  990. /* @var ConstraintViolationInterface[] $violations */
  991. $this->assertCount(1, $violations);
  992. $this->assertSame('Violation in other group', $violations[0]->getMessage());
  993. }
  994. public function testReplaceDefaultGroupWithObjectFromGroupSequenceProvider()
  995. {
  996. $sequence = new GroupSequence(array('Group 1', 'Group 2', 'Group 3', 'Entity'));
  997. $entity = new GroupSequenceProviderEntity($sequence);
  998. $callback1 = function ($value, ExecutionContextInterface $context) {
  999. $context->addViolation('Violation in Group 2');
  1000. };
  1001. $callback2 = function ($value, ExecutionContextInterface $context) {
  1002. $context->addViolation('Violation in Group 3');
  1003. };
  1004. $metadata = new ClassMetadata(\get_class($entity));
  1005. $metadata->addConstraint(new Callback(array(
  1006. 'callback' => function () {},
  1007. 'groups' => 'Group 1',
  1008. )));
  1009. $metadata->addConstraint(new Callback(array(
  1010. 'callback' => $callback1,
  1011. 'groups' => 'Group 2',
  1012. )));
  1013. $metadata->addConstraint(new Callback(array(
  1014. 'callback' => $callback2,
  1015. 'groups' => 'Group 3',
  1016. )));
  1017. $metadata->setGroupSequenceProvider(true);
  1018. $this->metadataFactory->addMetadata($metadata);
  1019. $violations = $this->validate($entity, null, 'Default');
  1020. /* @var ConstraintViolationInterface[] $violations */
  1021. $this->assertCount(1, $violations);
  1022. $this->assertSame('Violation in Group 2', $violations[0]->getMessage());
  1023. }
  1024. public function testReplaceDefaultGroupWithArrayFromGroupSequenceProvider()
  1025. {
  1026. $sequence = array('Group 1', 'Group 2', 'Group 3', 'Entity');
  1027. $entity = new GroupSequenceProviderEntity($sequence);
  1028. $callback1 = function ($value, ExecutionContextInterface $context) {
  1029. $context->addViolation('Violation in Group 2');
  1030. };
  1031. $callback2 = function ($value, ExecutionContextInterface $context) {
  1032. $context->addViolation('Violation in Group 3');
  1033. };
  1034. $metadata = new ClassMetadata(\get_class($entity));
  1035. $metadata->addConstraint(new Callback(array(
  1036. 'callback' => function () {},
  1037. 'groups' => 'Group 1',
  1038. )));
  1039. $metadata->addConstraint(new Callback(array(
  1040. 'callback' => $callback1,
  1041. 'groups' => 'Group 2',
  1042. )));
  1043. $metadata->addConstraint(new Callback(array(
  1044. 'callback' => $callback2,
  1045. 'groups' => 'Group 3',
  1046. )));
  1047. $metadata->setGroupSequenceProvider(true);
  1048. $this->metadataFactory->addMetadata($metadata);
  1049. $violations = $this->validate($entity, null, 'Default');
  1050. /* @var ConstraintViolationInterface[] $violations */
  1051. $this->assertCount(1, $violations);
  1052. $this->assertSame('Violation in Group 2', $violations[0]->getMessage());
  1053. }
  1054. }