|
8000
11 | 11 |
|
12 | 12 | namespace Constraints;
|
13 | 13 |
|
14 |
| -use PHPUnit\Framework\TestCase; |
15 | 14 | use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
|
16 | 15 | use Symfony\Component\Validator\Constraints\ExpressionLanguageProvider;
|
17 |
| -use Symfony\Component\Validator\Constraints\NotNull; |
18 |
| -use Symfony\Component\Validator\Constraints\Range; |
19 |
| -use Symfony\Component\Validator\ConstraintViolationListInterface; |
20 |
| -use Symfony\Component\Validator\Context\ExecutionContextInterface; |
21 |
| -use Symfony\Component\Validator\Validator\ContextualValidatorInterface; |
22 |
| -use Symfony\Component\Validator\Validator\ValidatorInterface; |
23 |
| - |
24 |
| -class ExpressionLanguageProviderTest extends TestCase |
| 16 | +use Symfony\Component\Validator\Constraints\ExpressionValidator; |
| 17 | +use Symfony\Component\Validator\Constraints\Length; |
| 18 | +use Symfony\Component\Validator\ConstraintViolation; |
| 19 | +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; |
| 20 | + |
| 21 | +class ExpressionLanguageProviderTest extends ConstraintValidatorTestCase |
25 | 22 | {
|
| 23 | + protected function createValidator() |
| 24 | + { |
| 25 | + return new ExpressionValidator(); |
| 26 | + } |
| 27 | + |
26 | 28 | /**
|
27 | 29 | * @dataProvider dataProviderCompile
|
28 | 30 | */
|
@@ -54,56 +56,33 @@ public function dataProviderCompile(): array
|
54 | 56 | ];
|
55 | 57 | }
|
56 | 58 |
|
57 |
| - /** |
58 |
| - * @dataProvider dataProviderEvaluate |
59 |
| - */ |
60 |
| - public function testEvaluate(bool $expected, int $errorsCount) |
| 59 | + public function testEvaluateValid() |
61 | 60 | {
|
62 |
| - $constraints = [new NotNull(), new Range(['min' => 2])]; |
63 |
| - |
64 |
| - $violationList = $this->createMock(ConstraintViolationListInterface::class); |
65 |
| - $violationList->expects($this->once()) |
66 |
| - ->method('count') |
67 |
| - ->willReturn($errorsCount); |
68 |
| - |
69 |
| - $contextualValidator = $this->createMock(ContextualValidatorInterface::class); |
70 |
| - $contextualValidator->expects($this->once()) |
71 |
| - ->method('validate') |
72 |
| - ->with('foo', $constraints) |
73 |
| - ->willReturnSelf(); |
74 |
| - $contextualValidator->expects($this->once()) |
75 |
| - ->method('getViolations') |
76 |
| - ->willReturn($violationList); |
77 |
| - |
78 |
| - $validator = $this->createMock(ValidatorInterface::class); |
79 |
| - |
80 |
| - $context = $this->createMock(ExecutionContextInterface::class); |
81 |
| - $context->expects($this->once()) |
82 |
| - ->method('getValidator') |
83 |
| - ->willReturn($validator); |
84 |
| - |
85 |
| - $validator->expects($this->once()) |
86 |
| - ->method('inContext') |
87 |
| - ->with($context) |
88 |
| -
8000
->willReturn($contextualValidator); |
| 61 | + $constraints = [new Length(['min' => 2, 'max' => 12])]; |
89 | 62 |
|
90 |
| - $provider = new ExpressionLanguageProvider(); |
| 63 | + $this->expectValidateValue(0, 'foo', $constraints); |
91 | 64 |
|
92 | 65 | $expressionLanguage = new ExpressionLanguage();
|
93 |
| - $expressionLanguage->registerProvider($provider); |
| 66 | + $expressionLanguage->registerProvider(new ExpressionLanguageProvider()); |
94 | 67 |
|
95 |
| - $this->assertSame( |
96 |
| - $expected, |
97 |
| - $expressionLanguage->evaluate('is_valid("foo", a)', ['a' => $constraints, 'context' => $context]) |
98 |
| - ); |
| 68 | + $this->assertTrue($expressionLanguage->evaluate('is_valid("foo", a)', ['a' => $constraints, 'context' => $this->context])); |
99 | 69 | }
|
100 | 70 |
|
101 |
| - public function dataProviderEvaluate(): array |
| 71 | + public function testEvaluateInvalid() |
102 | 72 | {
|
103 |
| - return [ |
104 |
| - [true, 0], |
105 |
| - [false, 1], |
106 |
| - [false, 12], |
107 |
| - ]; |
| 73 | + $constraints = [new Length(['min' => 7, 'max' => 12])]; |
| 74 | + |
| 75 | + $this->expectFailingValueValidation( |
| 76 | + 0, |
| 77 | + 'foo', |
| 78 | + $constraints, |
| 79 | + null, |
| 80 | + new ConstraintViolation('error_length', '', [], '', '', 'foo', null, 'range') |
| 81 | + ); |
| 82 | + |
| 83 | + $expressionLanguage = new ExpressionLanguage(); |
| 84 | + $expressionLanguage->registerProvider(new ExpressionLanguageProvider()); |
| 85 | + |
| 86 | + $this->assertFalse($expressionLanguage->evaluate('is_valid("foo", a)', ['a' => $constraints, 'context' => $this->context])); |
108 | 87 | }
|
109 | 88 | }
|
0 commit comments