PageRenderTime 57ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php

http://github.com/symfony/symfony
PHP | 252 lines | 186 code | 57 blank | 9 comment | 0 complexity | b1cc6ade03748e96b16fe62df5bc8020 MD5 | raw file
  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\Context\ExecutionContextInterface;
  15. use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
  16. class CallbackValidatorTest_Class
  17. {
  18. public static function validateCallback($object, ExecutionContextInterface $context)
  19. {
  20. $context->addViolation('Callback message', ['{{ value }}' => 'foobar']);
  21. return false;
  22. }
  23. }
  24. class CallbackValidatorTest_Object
  25. {
  26. public function validate(ExecutionContextInterface $context)
  27. {
  28. $context->addViolation('My message', ['{{ value }}' => 'foobar']);
  29. return false;
  30. }
  31. public static function validateStatic($object, ExecutionContextInterface $context)
  32. {
  33. $context->addViolation('Static message', ['{{ value }}' => 'baz']);
  34. return false;
  35. }
  36. }
  37. class CallbackValidatorTest extends ConstraintValidatorTestCase
  38. {
  39. protected function createValidator()
  40. {
  41. return new CallbackValidator();
  42. }
  43. public function testNullIsValid()
  44. {
  45. $this->validator->validate(null, new Callback());
  46. $this->assertNoViolation();
  47. }
  48. public function testSingleMethod()
  49. {
  50. $object = new CallbackValidatorTest_Object();
  51. $constraint = new Callback('validate');
  52. $this->validator->validate($object, $constraint);
  53. $this->buildViolation('My message')
  54. ->setParameter('{{ value }}', 'foobar')
  55. ->assertRaised();
  56. }
  57. public function testSingleMethodExplicitName()
  58. {
  59. $object = new CallbackValidatorTest_Object();
  60. $constraint = new Callback(['callback' => 'validate']);
  61. $this->validator->validate($object, $constraint);
  62. $this->buildViolation('My message')
  63. ->setParameter('{{ value }}', 'foobar')
  64. ->assertRaised();
  65. }
  66. public function testSingleStaticMethod()
  67. {
  68. $object = new CallbackValidatorTest_Object();
  69. $constraint = new Callback('validateStatic');
  70. $this->validator->validate($object, $constraint);
  71. $this->buildViolation('Static message')
  72. ->setParameter('{{ value }}', 'baz')
  73. ->assertRaised();
  74. }
  75. public function testClosure()
  76. {
  77. $object = new CallbackValidatorTest_Object();
  78. $constraint = new Callback(function ($object, ExecutionContextInterface $context) {
  79. $context->addViolation('My message', ['{{ value }}' => 'foobar']);
  80. return false;
  81. });
  82. $this->validator->validate($object, $constraint);
  83. $this->buildViolation('My message')
  84. ->setParameter('{{ value }}', 'foobar')
  85. ->assertRaised();
  86. }
  87. public function testClosureNullObject()
  88. {
  89. $constraint = new Callback(function ($object, ExecutionContextInterface $context) {
  90. $context->addViolation('My message', ['{{ value }}' => 'foobar']);
  91. return false;
  92. });
  93. $this->validator->validate(null, $constraint);
  94. $this->buildViolation('My message')
  95. ->setParameter('{{ value }}', 'foobar')
  96. ->assertRaised();
  97. }
  98. public function testClosureExplicitName()
  99. {
  100. $object = new CallbackValidatorTest_Object();
  101. $constraint = new Callback([
  102. 'callback' => function ($object, ExecutionContextInterface $context) {
  103. $context->addViolation('My message', ['{{ value }}' => 'foobar']);
  104. return false;
  105. },
  106. ]);
  107. $this->validator->validate($object, $constraint);
  108. $this->buildViolation('My message')
  109. ->setParameter('{{ value }}', 'foobar')
  110. ->assertRaised();
  111. }
  112. public function testArrayCallable()
  113. {
  114. $object = new CallbackValidatorTest_Object();
  115. $constraint = new Callback([__CLASS__.'_Class', 'validateCallback']);
  116. $this->validator->validate($object, $constraint);
  117. $this->buildViolation('Callback message')
  118. ->setParameter('{{ value }}', 'foobar')
  119. ->assertRaised();
  120. }
  121. public function testArrayCallableNullObject()
  122. {
  123. $constraint = new Callback([__CLASS__.'_Class', 'validateCallback']);
  124. $this->validator->validate(null, $constraint);
  125. $this->buildViolation('Callback message')
  126. ->setParameter('{{ value }}', 'foobar')
  127. ->assertRaised();
  128. }
  129. public function testArrayCallableExplicitName()
  130. {
  131. $object = new CallbackValidatorTest_Object();
  132. $constraint = new Callback([
  133. 'callback' => [__CLASS__.'_Class', 'validateCallback'],
  134. ]);
  135. $this->validator->validate($object, $constraint);
  136. $this->buildViolation('Callback message')
  137. ->setParameter('{{ value }}', 'foobar')
  138. ->assertRaised();
  139. }
  140. public function testExpectValidMethods()
  141. {
  142. $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  143. $object = new CallbackValidatorTest_Object();
  144. $this->validator->validate($object, new Callback(['callback' => ['foobar']]));
  145. }
  146. public function testExpectValidCallbacks()
  147. {
  148. $this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
  149. $object = new CallbackValidatorTest_Object();
  150. $this->validator->validate($object, new Callback(['callback' => ['foo', 'bar']]));
  151. }
  152. public function testConstraintGetTargets()
  153. {
  154. $constraint = new Callback([]);
  155. $targets = [Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT];
  156. $this->assertEquals($targets, $constraint->getTargets());
  157. }
  158. // Should succeed. Needed when defining constraints as annotations.
  159. public function testNoConstructorArguments()
  160. {
  161. $constraint = new Callback();
  162. $this->assertSame([Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT], $constraint->getTargets());
  163. }
  164. public function testAnnotationInvocationSingleValued()
  165. {
  166. $constraint = new Callback(['value' => 'validateStatic']);
  167. $this->assertEquals(new Callback('validateStatic'), $constraint);
  168. }
  169. public function testAnnotationInvocationMultiValued()
  170. {
  171. $constraint = new Callback(['value' => [__CLASS__.'_Class', 'validateCallback']]);
  172. $this->assertEquals(new Callback([__CLASS__.'_Class', 'validateCallback']), $constraint);
  173. }
  174. public function testPayloadIsPassedToCallback()
  175. {
  176. $object = new \stdClass();
  177. $payloadCopy = null;
  178. $constraint = new Callback([
  179. 'callback' => function ($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
  180. $payloadCopy = $payload;
  181. },
  182. 'payload' => 'Hello world!',
  183. ]);
  184. $this->validator->validate($object, $constraint);
  185. $this->assertEquals('Hello world!', $payloadCopy);
  186. $payloadCopy = null;
  187. $constraint = new Callback([
  188. 'callback' => function ($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
  189. $payloadCopy = $payload;
  190. },
  191. ]);
  192. $this->validator->validate($object, $constraint);
  193. $this->assertNull($payloadCopy);
  194. }
  195. }