10000 [Validator] Add is_valid function to Expression constraint by verdet23 · Pull Request #47153 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Add is_valid function to Expression constraint #47153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix remarks
  • Loading branch information
verdet23 committed Aug 11, 2022
commit 91dafc8bad98fcb9e921b5b37c451815ab8e7b2f
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CHANGELOG

* Deprecate the "loose" e-mail validation mode, use "html5" instead
* Add the `negate` option to the `Expression` constraint, to inverse the logic of the violation's creation
* Add `is_valid` function to the `Expression` constraint, api the same as `ValidatorInterface::validate`
* Add `is_valid` function to the `Expression` constraint, its behavior is the same as `ValidatorInterface::validate`

6.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class ExpressionValidator extends ConstraintValidator
public function __construct(ExpressionLanguage $expressionLanguage = null)
{
$this->expressionLanguage = $expressionLanguage;

$this->expressionLanguage?->registerProvider(new ExpressionLanguageProvider());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't mutate the expression language we receive in the constructor, it would lead to unexpected behavior if the provided expression language is used elsewhere.

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ExpressionLanguageProviderTest extends TestCase
/**
* @dataProvider dataProviderCompile
*/
public function testCompile(string $expression, array $names, string $expected): void
public function testCompile(string $expression, array $names, string $expected)
{
$provider = new ExpressionLanguageProvider();

Expand All @@ -50,27 +50,23 @@ public function dataProviderCompile(): array
'is_valid(this.data, constraints, groups)',
['this', 'constraints', 'groups'],
'0 === $context->getValidator()->inContext($context)->validate($this->data, $constraints, $groups)->getViolations()->count()',
]
],
];
}

/**
* @dataProvider dataProviderEvaluate
*/
public function testEvaluate(bool $expected, int $errorsCount): void
public function testEvaluate(bool $expected, int $errorsCount)
{
$constraints = [new NotNull(), new Range(['min' => 2])];

$violationList = $this->getMockBuilder(ConstraintViolationListInterface::class)
->onlyMethods(['count'])
->getMockForAbstractClass();
$violationList = $this->createMock(ConstraintViolationListInterface::class);
$violationList->expects($this->once())
->method('count')
->willReturn($errorsCount);

$contextualValidator = $this->getMockBuilder(ContextualValidatorInterface::class)
->onlyMethods(['getViolations', 'validate'])
->getMockForAbstractClass();
$contextualValidator = $this->createMock(ContextualValidatorInterface::class);
$contextualValidator->expects($this->once())
->method('validate')
->with('foo', $constraints)
Expand All @@ -79,13 +75,9 @@ public function testEvaluate(bool $expected, int $errorsCount): void
->method('getViolations')
->willReturn($violationList);

$validator = $this->getMockBuilder(ValidatorInterface::class)
->onlyMethods(['inContext'])
->getMockForAbstractClass();
$validator = $this->createMock(ValidatorInterface::class);

$context = $this->getMockBuilder(ExecutionContextInterface::class)
->onlyMethods(['getValidator'])
->getMockForAbstractClass();
$context = $this->createMock(ExecutionContextInterface::class);
$context->expects($this->once())
->method('getValidator')
->willReturn($validator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function testViolationOnPass()
->assertRaised();
}

public function testIsValidExpression(): void
public function testIsValidExpression()
{
$constraints = [new NotNull(), new Range(['min' => 2])];

Expand All @@ -328,7 +328,7 @@ public function testIsValidExpression(): void
$this->assertNoViolation();
}

public function testIsValidExpressionInvalid(): void
public function testIsValidExpressionInvalid()
{
$constraints = [new Range(['min' => 2, 'max' => 5])];

Expand All @@ -346,15 +346,7 @@ public function testIsValidExpressionInvalid(): void
7,
$constraints,
null,
new ConstraintViolation(
'error_range',
null, [],
null,
'',
null,
null,
'range'
)
new ConstraintViolation('error_range', '', [], '', '', 7, null, 'range')
);

$this->validator->validate($object, $constraint);
Expand Down
0