ExpressionValidatorTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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\PropertyAccess\PropertyAccess;
  12. use Symfony\Component\Validator\Constraints\Expression;
  13. use Symfony\Component\Validator\Constraints\ExpressionValidator;
  14. use Symfony\Component\Validator\Tests\Fixtures\Entity;
  15. use Symfony\Component\Validator\Tests\Fixtures\ToString;
  16. use Symfony\Component\Validator\Validation;
  17. class ExpressionValidatorTest extends AbstractConstraintValidatorTest
  18. {
  19. protected function getApiVersion()
  20. {
  21. return Validation::API_VERSION_2_5;
  22. }
  23. protected function createValidator()
  24. {
  25. return new ExpressionValidator(PropertyAccess::createPropertyAccessor());
  26. }
  27. public function testExpressionIsEvaluatedWithNullValue()
  28. {
  29. $constraint = new Expression(array(
  30. 'expression' => 'false',
  31. 'message' => 'myMessage',
  32. ));
  33. $this->validator->validate(null, $constraint);
  34. $this->buildViolation('myMessage')
  35. ->setParameter('{{ value }}', 'null')
  36. ->setCode(Expression::EXPRESSION_FAILED_ERROR)
  37. ->assertRaised();
  38. }
  39. public function testExpressionIsEvaluatedWithEmptyStringValue()
  40. {
  41. $constraint = new Expression(array(
  42. 'expression' => 'false',
  43. 'message' => 'myMessage',
  44. ));
  45. $this->validator->validate('', $constraint);
  46. $this->buildViolation('myMessage')
  47. ->setParameter('{{ value }}', '""')
  48. ->setCode(Expression::EXPRESSION_FAILED_ERROR)
  49. ->assertRaised();
  50. }
  51. public function testSucceedingExpressionAtObjectLevel()
  52. {
  53. $constraint = new Expression('this.data == 1');
  54. $object = new Entity();
  55. $object->data = '1';
  56. $this->setObject($object);
  57. $this->validator->validate($object, $constraint);
  58. $this->assertNoViolation();
  59. }
  60. public function testFailingExpressionAtObjectLevel()
  61. {
  62. $constraint = new Expression(array(
  63. 'expression' => 'this.data == 1',
  64. 'message' => 'myMessage',
  65. ));
  66. $object = new Entity();
  67. $object->data = '2';
  68. $this->setObject($object);
  69. $this->validator->validate($object, $constraint);
  70. $this->buildViolation('myMessage')
  71. ->setParameter('{{ value }}', 'object')
  72. ->setCode(Expression::EXPRESSION_FAILED_ERROR)
  73. ->assertRaised();
  74. }
  75. public function testSucceedingExpressionAtObjectLevelWithToString()
  76. {
  77. $constraint = new Expression('this.data == 1');
  78. $object = new ToString();
  79. $object->data = '1';
  80. $this->setObject($object);
  81. $this->validator->validate($object, $constraint);
  82. $this->assertNoViolation();
  83. }
  84. public function testFailingExpressionAtObjectLevelWithToString()
  85. {
  86. $constraint = new Expression(array(
  87. 'expression' => 'this.data == 1',
  88. 'message' => 'myMessage',
  89. ));
  90. $object = new ToString();
  91. $object->data = '2';
  92. $this->setObject($object);
  93. $this->validator->validate($object, $constraint);
  94. $this->buildViolation('myMessage')
  95. ->setParameter('{{ value }}', 'toString')
  96. ->setCode(Expression::EXPRESSION_FAILED_ERROR)
  97. ->assertRaised();
  98. }
  99. public function testSucceedingExpressionAtPropertyLevel()
  100. {
  101. $constraint = new Expression('value == this.data');
  102. $object = new Entity();
  103. $object->data = '1';
  104. $this->setRoot($object);
  105. $this->setPropertyPath('data');
  106. $this->setProperty($object, 'data');
  107. $this->validator->validate('1', $constraint);
  108. $this->assertNoViolation();
  109. }
  110. public function testFailingExpressionAtPropertyLevel()
  111. {
  112. $constraint = new Expression(array(
  113. 'expression' => 'value == this.data',
  114. 'message' => 'myMessage',
  115. ));
  116. $object = new Entity();
  117. $object->data = '1';
  118. $this->setRoot($object);
  119. $this->setPropertyPath('data');
  120. $this->setProperty($object, 'data');
  121. $this->validator->validate('2', $constraint);
  122. $this->buildViolation('myMessage')
  123. ->atPath('data')
  124. ->setParameter('{{ value }}', '"2"')
  125. ->setCode(Expression::EXPRESSION_FAILED_ERROR)
  126. ->assertRaised();
  127. }
  128. public function testSucceedingExpressionAtNestedPropertyLevel()
  129. {
  130. $constraint = new Expression('value == this.data');
  131. $object = new Entity();
  132. $object->data = '1';
  133. $root = new Entity();
  134. $root->reference = $object;
  135. $this->setRoot($root);
  136. $this->setPropertyPath('reference.data');
  137. $this->setProperty($object, 'data');
  138. $this->validator->validate('1', $constraint);
  139. $this->assertNoViolation();
  140. }
  141. public function testFailingExpressionAtNestedPropertyLevel()
  142. {
  143. $constraint = new Expression(array(
  144. 'expression' => 'value == this.data',
  145. 'message' => 'myMessage',
  146. ));
  147. $object = new Entity();
  148. $object->data = '1';
  149. $root = new Entity();
  150. $root->reference = $object;
  151. $this->setRoot($root);
  152. $this->setPropertyPath('reference.data');
  153. $this->setProperty($object, 'data');
  154. $this->validator->validate('2', $constraint);
  155. $this->buildViolation('myMessage')
  156. ->atPath('reference.data')
  157. ->setParameter('{{ value }}', '"2"')
  158. ->setCode(Expression::EXPRESSION_FAILED_ERROR)
  159. ->assertRaised();
  160. }
  161. /**
  162. * When validatePropertyValue() is called with a class name
  163. * https://github.com/symfony/symfony/pull/11498.
  164. */
  165. public function testSucceedingExpressionAtPropertyLevelWithoutRoot()
  166. {
  167. $constraint = new Expression('value == "1"');
  168. $this->setRoot('1');
  169. $this->setPropertyPath('');
  170. $this->setProperty(null, 'property');
  171. $this->validator->validate('1', $constraint);
  172. $this->assertNoViolation();
  173. }
  174. /**
  175. * When validatePropertyValue() is called with a class name
  176. * https://github.com/symfony/symfony/pull/11498.
  177. */
  178. public function testFailingExpressionAtPropertyLevelWithoutRoot()
  179. {
  180. $constraint = new Expression(array(
  181. 'expression' => 'value == "1"',
  182. 'message' => 'myMessage',
  183. ));
  184. $this->setRoot('2');
  185. $this->setPropertyPath('');
  186. $this->setProperty(null, 'property');
  187. $this->validator->validate('2', $constraint);
  188. $this->buildViolation('myMessage')
  189. ->atPath('')
  190. ->setParameter('{{ value }}', '"2"')
  191. ->setCode(Expression::EXPRESSION_FAILED_ERROR)
  192. ->assertRaised();
  193. }
  194. public function testExpressionLanguageUsage()
  195. {
  196. $constraint = new Expression(array(
  197. 'expression' => 'false',
  198. ));
  199. $expressionLanguage = $this->getMockBuilder('Symfony\Component\ExpressionLanguage\ExpressionLanguage')->getMock();
  200. $used = false;
  201. $expressionLanguage->method('evaluate')
  202. ->will($this->returnCallback(function () use (&$used) {
  203. $used = true;
  204. return true;
  205. }));
  206. $validator = new ExpressionValidator(null, $expressionLanguage);
  207. $validator->initialize($this->createContext());
  208. $validator->validate(null, $constraint);
  209. $this->assertTrue($used, 'Failed asserting that custom ExpressionLanguage instance is used.');
  210. }
  211. }