8000 rework tests · symfony/symfony@7f3acc7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f3acc7

Browse files
committed
rework tests
1 parent 4815e23 commit 7f3acc7

File tree

1 file changed

+31
-52
lines changed

1 file changed

+31
-52
lines changed

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

Lines changed: 31 additions & 52 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@
1111

1212
namespace Constraints;
1313

14-
use PHPUnit\Framework\TestCase;
1514
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1615
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
2522
{
23+
protected function createValidator()
24+
{
25+
return new ExpressionValidator();
26+
}
27+
2628
/**
2729
* @dataProvider dataProviderCompile
2830
*/
@@ -54,56 +56,33 @@ public function dataProviderCompile(): array
5456
];
5557
}
5658

57-
/**
58-
* @dataProvider dataProviderEvaluate
59-
*/
60-
public function testEvaluate(bool $expected, int $errorsCount)
59+
public function testEvaluateValid()
6160
{
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])];
8962

90-
$provider = new ExpressionLanguageProvider();
63+
$this->expectValidateValue(0, 'foo', $constraints);
9164

9265
$expressionLanguage = new ExpressionLanguage();
93-
$expressionLanguage->registerProvider($provider);
66+
$expressionLanguage->registerProvider(new ExpressionLanguageProvider());
9467

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]));
9969
}
10070

101-
public function dataProviderEvaluate(): array
71+
public function testEvaluateInvalid()
10272
{
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]));
10887
}
10988
}

0 commit comments

Comments
 (0)
0