|
14 | 14 | use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
15 | 15 | use Symfony\Component\Validator\Constraints\Expression;
|
16 | 16 | use Symfony\Component\Validator\Constraints\ExpressionValidator;
|
| 17 | +use Symfony\Component\Validator\Constraints\NotNull; |
| 18 | +use Symfony\Component\Validator\Constraints\Range; |
| 19 | +use Symfony\Component\Validator\ConstraintViolation; |
17 | 20 | use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
18 | 21 | use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity;
|
19 | 22 | use Symfony\Component\Validator\Tests\Fixtures\ToString;
|
@@ -304,4 +307,81 @@ public function testViolationOnPass()
|
304 | 307 | ->setCode(Expression::EXPRESSION_FAILED_ERROR)
|
305 | 308 | ->assertRaised();
|
306 | 309 | }
|
| 310 | + |
| 311 | + public function testIsValidExpression() |
| 312 | + { |
| 313 | + $constraints = [new NotNull(), new Range(['min' => 2])]; |
| 314 | + |
| 315 | + $constraint = new Expression( |
| 316 | + ['expression' => 'is_valid(this.data, a)', 'values' => ['a' => $constraints]] |
| 317 | + ); |
| 318 | + |
| 319 | + $object = new Entity(); |
| 320 | + $object->data = 7; |
| 321 | + |
| 322 | + $this->setObject($object); |
| 323 | + |
| 324 | + $this->expectValidateValue(0, $object->data, $constraints); |
| 325 | + |
| 326 | + $this->validator->validate($object, $constraint); |
| 327 | + |
| 328 | + $this->assertNoViolation(); |
| 329 | + } |
| 330 | + |
| 331 | + public function testIsValidExpressionInvalid() |
| 332 | + { |
| 333 | + $constraints = [new Range(['min' => 2, 'max' => 5])]; |
| 334 | + |
| 335 | + $constraint = new Expression( |
| 336 | + ['expression' => 'is_valid(this.data, a)', 'values' => ['a' => $constraints]] |
| 337 | + ); |
| 338 | + |
| 339 | + $object = new Entity(); |
| 340 | + $object->data = 7; |
| 341 | + |
| 342 | + $this->setObject($object); |
| 343 | + |
| 344 | + $this->expectFailingValueValidation( |
| 345 | + 0, |
| 346 | + 7, |
| 347 | + $constraints, |
| 348 | + null, |
| 349 | + new ConstraintViolation('error_range', '', [], '', '', 7, null, 'range') |
| 350 | + ); |
| 351 | + |
| 352 | + $this->validator->validate($object, $constraint); |
| 353 | + |
| 354 | + $this->assertCount(2, $this->context->getViolations()); |
| 355 | + } |
| 356 | + |
| 357 | + /** |
| 358 | + * @dataProvider provideCompileIsValid |
| 359 | + */ |
| 360 | + public function testCompileIsValid(string $expression, array $names, string $expected) |
| 361 | + { |
| 362 | + $provider = new ExpressionValidator(); |
| 363 | + |
| 364 | + $expressionLanguage = new ExpressionLanguage(); |
| 365 | + $expressionLanguage->registerProvider($provider); |
| 366 | + |
| 367 | + $result = $expressionLanguage->compile($expression, $names); |
| 368 | + |
| 369 | + $this->assertSame($expected, $result); |
| 370 | + } |
| 371 | + |
| 372 | + public static function provideCompileIsValid(): array |
| 373 | + { |
| 374 | + return [ |
| 375 | + [ |
| 376 | + 'is_valid("foo", constraints)', |
| 377 | + ['constraints'], |
| 378 | + '0 === $context->getValidator()->inContext($context)->validate("foo", $constraints)->getViolations()->count()', |
| 379 | + ], |
| 380 | + [ |
| 381 | + 'is_valid(this.data, constraints, groups)', |
| 382 | + ['this', 'constraints', 'groups'], |
| 383 | + '0 === $context->getValidator()->inContext($context)->validate($this->data, $constraints, $groups)->getViolations()->count()', |
| 384 | + ], |
| 385 | + ]; |
| 386 | + } |
307 | 387 | }
|
0 commit comments