CallbackValidatorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. use Symfony\Component\Validator\Constraints\Callback;
  13. use Symfony\Component\Validator\Constraints\CallbackValidator;
  14. use Symfony\Component\Validator\ExecutionContextInterface;
  15. use Symfony\Component\Validator\Validation;
  16. class CallbackValidatorTest_Class
  17. {
  18. public static function validateCallback($object, ExecutionContextInterface $context)
  19. {
  20. $context->addViolation('Callback message', array('{{ value }}' => 'foobar'));
  21. return false;
  22. }
  23. }
  24. class CallbackValidatorTest_Object
  25. {
  26. public function validate(ExecutionContextInterface $context)
  27. {
  28. $context->addViolation('My message', array('{{ value }}' => 'foobar'));
  29. return false;
  30. }
  31. public static function validateStatic($object, ExecutionContextInterface $context)
  32. {
  33. $context->addViolation('Static message', array('{{ value }}' => 'baz'));
  34. return false;
  35. }
  36. }
  37. class CallbackValidatorTest extends AbstractConstraintValidatorTest
  38. {
  39. protected function getApiVersion()
  40. {
  41. return Validation::API_VERSION_2_5;
  42. }
  43. protected function createValidator()
  44. {
  45. return new CallbackValidator();
  46. }
  47. public function testNullIsValid()
  48. {
  49. $this->validator->validate(null, new Callback());
  50. $this->assertNoViolation();
  51. }
  52. public function testSingleMethod()
  53. {
  54. $object = new CallbackValidatorTest_Object();
  55. $constraint = new Callback('validate');
  56. $this->validator->validate($object, $constraint);
  57. $this->buildViolation('My message')
  58. ->setParameter('{{ value }}', 'foobar')
  59. ->assertRaised();
  60. }
  61. public function testSingleMethodExplicitName()
  62. {
  63. $object = new CallbackValidatorTest_Object();
  64. $constraint = new Callback(array('callback' => 'validate'));
  65. $this->validator->validate($object, $constraint);
  66. $this->buildViolation('My message')
  67. ->setParameter('{{ value }}', 'foobar')
  68. ->assertRaised();
  69. }
  70. public function testSingleStaticMethod()
  71. {
  72. $object = new CallbackValidatorTest_Object();
  73. $constraint = new Callback('validateStatic');
  74. $this->validator->validate($object, $constraint);
  75. $this->buildViolation('Static message')
  76. ->setParameter('{{ value }}', 'baz')
  77. ->assertRaised();
  78. }
  79. public function testClosure()
  80. {
  81. $object = new CallbackValidatorTest_Object();
  82. $constraint = new Callback(function ($object, ExecutionContextInterface $context) {
  83. $context->addViolation('My message', array('{{ value }}' => 'foobar'));
  84. return false;
  85. });
  86. $this->validator->validate($object, $constraint);
  87. $this->buildViolation('My message')
  88. ->setParameter('{{ value }}', 'foobar')
  89. ->assertRaised();
  90. }
  91. public function testClosureNullObject()
  92. {
  93. $constraint = new Callback(function ($object, ExecutionContextInterface $context) {
  94. $context->addViolation('My message', array('{{ value }}' => 'foobar'));
  95. return false;
  96. });
  97. $this->validator->validate(null, $constraint);
  98. $this->buildViolation('My message')
  99. ->setParameter('{{ value }}', 'foobar')
  100. ->assertRaised();
  101. }
  102. public function testClosureExplicitName()
  103. {
  104. $object = new CallbackValidatorTest_Object();
  105. $constraint = new Callback(array(
  106. 'callback' => function ($object, ExecutionContextInterface $context) {
  107. $context->addViolation('My message', array('{{ value }}' => 'foobar'));
  108. return false;
  109. },
  110. ));
  111. $this->validator->validate($object, $constraint);
  112. $this->buildViolation('My message')
  113. ->setParameter('{{ value }}', 'foobar')
  114. ->assertRaised();
  115. }
  116. public function testArrayCallable()
  117. {
  118. $object = new CallbackValidatorTest_Object();
  119. $constraint = new Callback(array(__CLASS__.'_Class', 'validateCallback'));
  120. $this->validator->validate($object, $constraint);
  121. $this->buildViolation('Callback message')
  122. ->setParameter('{{ value }}', 'foobar')
  123. ->assertRaised();
  124. }
  125. public function testArrayCallableNullObject()
  126. {
  127. $constraint = new Callback(array(__CLASS__.'_Class', 'validateCallback'));
  128. $this->validator->validate(null, $constraint);
  129. $this->buildViolation('Callback message')
  130. ->setParameter('{{ value }}', 'foobar')
  131. ->assertRaised();
  132. }
  133. public function testArrayCallableExplicitName()
  134. {
  135. $object = new CallbackValidatorTest_Object();
  136. $constraint = new Callback(array(
  137. 'callback' => array(__CLASS__.'_Class', 'validateCallback'),
  138. ));
  139. $this->validator->validate($object, $constraint);
  140. $this->buildViolation('Callback message')
  141. ->setParameter('{{ value }}', 'foobar')
  142. ->assertRaised();
  143. }
  144. /**
  145. * @group legacy
  146. */
  147. public function testLegacySingleMethodBc()
  148. {
  149. $object = new CallbackValidatorTest_Object();
  150. $constraint = new Callback(array('validate'));
  151. $this->validator->validate($object, $constraint);
  152. $this->buildViolation('My message')
  153. ->setParameter('{{ value }}', 'foobar')
  154. ->assertRaised();
  155. }
  156. /**
  157. * @group legacy
  158. */
  159. public function testLegacySingleMethodBcExplicitName()
  160. {
  161. $object = new CallbackValidatorTest_Object();
  162. $constraint = new Callback(array('methods' => array('validate')));
  163. $this->validator->validate($object, $constraint);
  164. $this->buildViolation('My message')
  165. ->setParameter('{{ value }}', 'foobar')
  166. ->assertRaised();
  167. }
  168. /**
  169. * @group legacy
  170. */
  171. public function testLegacyMultipleMethodsBc()
  172. {
  173. $object = new CallbackValidatorTest_Object();
  174. $constraint = new Callback(array('validate', 'validateStatic'));
  175. $this->validator->validate($object, $constraint);
  176. $this->buildViolation('My message')
  177. ->setParameter('{{ value }}', 'foobar')
  178. ->buildNextViolation('Static message')
  179. ->setParameter('{{ value }}', 'baz')
  180. ->assertRaised();
  181. }
  182. /**
  183. * @group legacy
  184. */
  185. public function testLegacyMultipleMethodsBcExplicitName()
  186. {
  187. $object = new CallbackValidatorTest_Object();
  188. $constraint = new Callback(array(
  189. 'methods' => array('validate', 'validateStatic'),
  190. ));
  191. $this->validator->validate($object, $constraint);
  192. $this->buildViolation('My message')
  193. ->setParameter('{{ value }}', 'foobar')
  194. ->buildNextViolation('Static message')
  195. ->setParameter('{{ value }}', 'baz')
  196. ->assertRaised();
  197. }
  198. /**
  199. * @group legacy
  200. */
  201. public function testLegacySingleStaticMethodBc()
  202. {
  203. $object = new CallbackValidatorTest_Object();
  204. $constraint = new Callback(array(
  205. array(__CLASS__.'_Class', 'validateCallback'),
  206. ));
  207. $this->validator->validate($object, $constraint);
  208. $this->buildViolation('Callback message')
  209. ->setParameter('{{ value }}', 'foobar')
  210. ->assertRaised();
  211. }
  212. /**
  213. * @group legacy
  214. */
  215. public function testLegacySingleStaticMethodBcExplicitName()
  216. {
  217. $object = new CallbackValidatorTest_Object();
  218. $constraint = new Callback(array(
  219. 'methods' => array(array(__CLASS__.'_Class', 'validateCallback')),
  220. ));
  221. $this->validator->validate($object, $constraint);
  222. $this->buildViolation('Callback message')
  223. ->setParameter('{{ value }}', 'foobar')
  224. ->assertRaised();
  225. }
  226. /**
  227. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  228. */
  229. public function testExpectValidMethods()
  230. {
  231. $object = new CallbackValidatorTest_Object();
  232. $this->validator->validate($object, new Callback(array('callback' => array('foobar'))));
  233. }
  234. /**
  235. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  236. */
  237. public function testExpectValidCallbacks()
  238. {
  239. $object = new CallbackValidatorTest_Object();
  240. $this->validator->validate($object, new Callback(array('callback' => array('foo', 'bar'))));
  241. }
  242. /**
  243. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  244. * @group legacy
  245. */
  246. public function testLegacyExpectEitherCallbackOrMethods()
  247. {
  248. $object = new CallbackValidatorTest_Object();
  249. $this->validator->validate($object, new Callback(array(
  250. 'callback' => 'validate',
  251. 'methods' => array('validateStatic'),
  252. )));
  253. }
  254. public function testConstraintGetTargets()
  255. {
  256. $constraint = new Callback(array());
  257. $targets = array(Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT);
  258. $this->assertEquals($targets, $constraint->getTargets());
  259. }
  260. // Should succeed. Needed when defining constraints as annotations.
  261. public function testNoConstructorArguments()
  262. {
  263. $constraint = new Callback();
  264. $this->assertSame(array(Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT), $constraint->getTargets());
  265. }
  266. public function testAnnotationInvocationSingleValued()
  267. {
  268. $constraint = new Callback(array('value' => 'validateStatic'));
  269. $this->assertEquals(new Callback('validateStatic'), $constraint);
  270. }
  271. public function testAnnotationInvocationMultiValued()
  272. {
  273. $constraint = new Callback(array('value' => array(__CLASS__.'_Class', 'validateCallback')));
  274. $this->assertEquals(new Callback(array(__CLASS__.'_Class', 'validateCallback')), $constraint);
  275. }
  276. }