AbstractLegacyApiTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\Valid;
  13. use Symfony\Component\Validator\ConstraintViolationInterface;
  14. use Symfony\Component\Validator\ExecutionContextInterface;
  15. use Symfony\Component\Validator\MetadataFactoryInterface;
  16. use Symfony\Component\Validator\Tests\Fixtures\Entity;
  17. use Symfony\Component\Validator\Tests\Fixtures\Reference;
  18. use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
  19. /**
  20. * Verifies that a validator satisfies the API of Symfony < 2.5.
  21. *
  22. * @author Bernhard Schussek <bschussek@gmail.com>
  23. * @group legacy
  24. */
  25. abstract class AbstractLegacyApiTest extends AbstractValidatorTest
  26. {
  27. /**
  28. * @var LegacyValidatorInterface
  29. */
  30. protected $validator;
  31. /**
  32. * @param MetadataFactoryInterface $metadataFactory
  33. *
  34. * @return LegacyValidatorInterface
  35. */
  36. abstract protected function createValidator(MetadataFactoryInterface $metadataFactory, array $objectInitializers = array());
  37. protected function setUp()
  38. {
  39. parent::setUp();
  40. $this->validator = $this->createValidator($this->metadataFactory);
  41. }
  42. protected function validate($value, $constraints = null, $groups = null)
  43. {
  44. if (null === $constraints) {
  45. $constraints = new Valid();
  46. }
  47. if ($constraints instanceof Valid) {
  48. return $this->validator->validate($value, $groups, $constraints->traverse, $constraints->deep);
  49. }
  50. return $this->validator->validateValue($value, $constraints, $groups);
  51. }
  52. protected function validateProperty($object, $propertyName, $groups = null)
  53. {
  54. return $this->validator->validateProperty($object, $propertyName, $groups);
  55. }
  56. protected function validatePropertyValue($object, $propertyName, $value, $groups = null)
  57. {
  58. return $this->validator->validatePropertyValue($object, $propertyName, $value, $groups);
  59. }
  60. /**
  61. * @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
  62. */
  63. public function testTraversableTraverseDisabled()
  64. {
  65. $test = $this;
  66. $entity = new Entity();
  67. $traversable = new \ArrayIterator(array('key' => $entity));
  68. $callback = function () use ($test) {
  69. $test->fail('Should not be called');
  70. };
  71. $this->metadata->addConstraint(new Callback(array(
  72. 'callback' => $callback,
  73. 'groups' => 'Group',
  74. )));
  75. $this->validator->validate($traversable, 'Group');
  76. }
  77. /**
  78. * @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
  79. */
  80. public function testRecursiveTraversableRecursiveTraversalDisabled()
  81. {
  82. $test = $this;
  83. $entity = new Entity();
  84. $traversable = new \ArrayIterator(array(
  85. 2 => new \ArrayIterator(array('key' => $entity)),
  86. ));
  87. $callback = function () use ($test) {
  88. $test->fail('Should not be called');
  89. };
  90. $this->metadata->addConstraint(new Callback(array(
  91. 'callback' => $callback,
  92. 'groups' => 'Group',
  93. )));
  94. $this->validator->validate($traversable, 'Group');
  95. }
  96. public function testValidateInContext()
  97. {
  98. $test = $this;
  99. $entity = new Entity();
  100. $entity->reference = new Reference();
  101. $callback1 = function ($value, ExecutionContextInterface $context) use ($test) {
  102. $previousValue = $context->getValue();
  103. $previousMetadata = $context->getMetadata();
  104. $previousPath = $context->getPropertyPath();
  105. $previousGroup = $context->getGroup();
  106. $context->validate($value->reference, 'subpath');
  107. // context changes shouldn't leak out of the validate() call
  108. $test->assertSame($previousValue, $context->getValue());
  109. $test->assertSame($previousMetadata, $context->getMetadata());
  110. $test->assertSame($previousPath, $context->getPropertyPath());
  111. $test->assertSame($previousGroup, $context->getGroup());
  112. };
  113. $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  114. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  115. $test->assertNull($context->getPropertyName());
  116. $test->assertSame('subpath', $context->getPropertyPath());
  117. $test->assertSame('Group', $context->getGroup());
  118. $test->assertSame($test->referenceMetadata, $context->getMetadata());
  119. $test->assertSame($test->metadataFactory, $context->getMetadataFactory());
  120. $test->assertSame($entity, $context->getRoot());
  121. $test->assertSame($entity->reference, $context->getValue());
  122. $test->assertSame($entity->reference, $value);
  123. $context->addViolation('Message %param%', array('%param%' => 'value'));
  124. };
  125. $this->metadata->addConstraint(new Callback(array(
  126. 'callback' => $callback1,
  127. 'groups' => 'Group',
  128. )));
  129. $this->referenceMetadata->addConstraint(new Callback(array(
  130. 'callback' => $callback2,
  131. 'groups' => 'Group',
  132. )));
  133. $violations = $this->validator->validate($entity, 'Group');
  134. /* @var ConstraintViolationInterface[] $violations */
  135. $this->assertCount(1, $violations);
  136. $this->assertSame('Message value', $violations[0]->getMessage());
  137. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  138. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  139. $this->assertSame('subpath', $violations[0]->getPropertyPath());
  140. $this->assertSame($entity, $violations[0]->getRoot());
  141. $this->assertSame($entity->reference, $violations[0]->getInvalidValue());
  142. $this->assertNull($violations[0]->getPlural());
  143. $this->assertNull($violations[0]->getCode());
  144. }
  145. public function testValidateArrayInContext()
  146. {
  147. $test = $this;
  148. $entity = new Entity();
  149. $entity->reference = new Reference();
  150. $callback1 = function ($value, ExecutionContextInterface $context) use ($test) {
  151. $previousValue = $context->getValue();
  152. $previousMetadata = $context->getMetadata();
  153. $previousPath = $context->getPropertyPath();
  154. $previousGroup = $context->getGroup();
  155. $context->validate(array('key' => $value->reference), 'subpath');
  156. // context changes shouldn't leak out of the validate() call
  157. $test->assertSame($previousValue, $context->getValue());
  158. $test->assertSame($previousMetadata, $context->getMetadata());
  159. $test->assertSame($previousPath, $context->getPropertyPath());
  160. $test->assertSame($previousGroup, $context->getGroup());
  161. };
  162. $callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
  163. $test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
  164. $test->assertNull($context->getPropertyName());
  165. $test->assertSame('subpath[key]', $context->getPropertyPath());
  166. $test->assertSame('Group', $context->getGroup());
  167. $test->assertSame($test->referenceMetadata, $context->getMetadata());
  168. $test->assertSame($test->metadataFactory, $context->getMetadataFactory());
  169. $test->assertSame($entity, $context->getRoot());
  170. $test->assertSame($entity->reference, $context->getValue());
  171. $test->assertSame($entity->reference, $value);
  172. $context->addViolation('Message %param%', array('%param%' => 'value'));
  173. };
  174. $this->metadata->addConstraint(new Callback(array(
  175. 'callback' => $callback1,
  176. 'groups' => 'Group',
  177. )));
  178. $this->referenceMetadata->addConstraint(new Callback(array(
  179. 'callback' => $callback2,
  180. 'groups' => 'Group',
  181. )));
  182. $violations = $this->validator->validate($entity, 'Group');
  183. /* @var ConstraintViolationInterface[] $violations */
  184. $this->assertCount(1, $violations);
  185. $this->assertSame('Message value', $violations[0]->getMessage());
  186. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  187. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  188. $this->assertSame('subpath[key]', $violations[0]->getPropertyPath());
  189. $this->assertSame($entity, $violations[0]->getRoot());
  190. $this->assertSame($entity->reference, $violations[0]->getInvalidValue());
  191. $this->assertNull($violations[0]->getPlural());
  192. $this->assertNull($violations[0]->getCode());
  193. }
  194. public function testAddCustomizedViolation()
  195. {
  196. $entity = new Entity();
  197. $callback = function ($value, ExecutionContextInterface $context) {
  198. $context->addViolation(
  199. 'Message %param%',
  200. array('%param%' => 'value'),
  201. 'Invalid value',
  202. 2,
  203. 'Code'
  204. );
  205. };
  206. $this->metadata->addConstraint(new Callback($callback));
  207. $violations = $this->validator->validate($entity);
  208. /* @var ConstraintViolationInterface[] $violations */
  209. $this->assertCount(1, $violations);
  210. $this->assertSame('Message value', $violations[0]->getMessage());
  211. $this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
  212. $this->assertSame(array('%param%' => 'value'), $violations[0]->getParameters());
  213. $this->assertSame('', $violations[0]->getPropertyPath());
  214. $this->assertSame($entity, $violations[0]->getRoot());
  215. $this->assertSame('Invalid value', $violations[0]->getInvalidValue());
  216. $this->assertSame(2, $violations[0]->getPlural());
  217. $this->assertSame('Code', $violations[0]->getCode());
  218. }
  219. public function testInitializeObjectsOnFirstValidation()
  220. {
  221. $test = $this;
  222. $entity = new Entity();
  223. $entity->initialized = false;
  224. // prepare initializers that set "initialized" to true
  225. $initializer1 = $this->getMockBuilder('Symfony\\Component\\Validator\\ObjectInitializerInterface')->getMock();
  226. $initializer2 = $this->getMockBuilder('Symfony\\Component\\Validator\\ObjectInitializerInterface')->getMock();
  227. $initializer1->expects($this->once())
  228. ->method('initialize')
  229. ->with($entity)
  230. ->will($this->returnCallback(function ($object) {
  231. $object->initialized = true;
  232. }));
  233. $initializer2->expects($this->once())
  234. ->method('initialize')
  235. ->with($entity);
  236. $this->validator = $this->createValidator($this->metadataFactory, array(
  237. $initializer1,
  238. $initializer2,
  239. ));
  240. // prepare constraint which
  241. // * checks that "initialized" is set to true
  242. // * validates the object again
  243. $callback = function ($object, ExecutionContextInterface $context) use ($test) {
  244. $test->assertTrue($object->initialized);
  245. // validate again in same group
  246. $context->validate($object);
  247. // validate again in other group
  248. $context->validate($object, '', 'SomeGroup');
  249. };
  250. $this->metadata->addConstraint(new Callback($callback));
  251. $this->validate($entity);
  252. $this->assertTrue($entity->initialized);
  253. }
  254. public function testGetMetadataFactory()
  255. {
  256. $this->assertSame($this->metadataFactory, $this->validator->getMetadataFactory());
  257. }
  258. }