-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
verdet23
wants to merge
6
commits into
symfony:6.4
from
verdet23:feature/validator-expression-is_valid
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
536e2f5
Add is_valid function to Expression constraint
verdet23 4f48c40
rework compile, evaluate
verdet23 461b03b
remove hasFuncton method from ExprLanguage
verdet23 e3d85ec
fix class comment, add invalid expectation test
verdet23 91dafc8
fix remarks
verdet23 a45c2af
rework tests
verdet23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add is_valid function to Expression constraint
- Loading branch information
commit 536e2f56b668d19a226b5c80f3b9e150fcdaae28
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/Validator/Constraints/ExpressionLanguageProvider.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use LogicException; | ||
use Symfony\Component\ExpressionLanguage\ExpressionFunction; | ||
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
|
||
/** | ||
* Define some ExpressionLanguage functions. | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @author Ihor Khokhlov <eld2303@gmail.com> | ||
*/ | ||
class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface | ||
{ | ||
private ExecutionContextInterface $context; | ||
|
||
public function __construct(ExecutionContextInterface $context) | ||
{ | ||
$this->context = $context; | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new ExpressionFunction('is_valid', function () { | ||
throw new LogicException('The "is_valid" function cannot be compiled.'); | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, function (array $variables, ...$arguments): bool { | ||
$context = $this->context; | ||
|
||
$validator = $context->getValidator()->inContext($context); | ||
|
||
$violations = $validator->validate(...$arguments)->getViolations(); | ||
|
||
return 0 === $violations->count(); | ||
}), | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/Symfony/Component/Validator/Tests/Constraints/ExpressionLanguageProviderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Constraints; | ||
|
||
use LogicException; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
use Symfony\Component\Validator\Constraints\ExpressionLanguageProvider; | ||
use Symfony\Component\Validator\Constraints\NotNull; | ||
use Symfony\Component\Validator\Constraints\Range; | ||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Symfony\Component\Validator\Validator\ContextualValidatorInterface; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
class ExpressionLanguageProviderTest extends TestCase | ||
{ | ||
public function testCompile(): void | ||
{ | ||
$this->expectException(LogicException::class); | ||
$this->expectExceptionMessage('The "is_valid" function cannot be compiled.'); | ||
|
||
$context = $this->createMock(ExecutionContextInterface::class); | ||
|
||
$provider = new ExpressionLanguageProvider($context); | ||
|
||
$expressionLanguage = new ExpressionLanguage(); | ||
$expressionLanguage->registerProvider($provider); | ||
|
||
$expressionLanguage->compile('is_valid()'); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderEvaluate | ||
*/ | ||
public function testEvaluate(bool $expected, int $errorsCount): void | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$constraints = [new NotNull(), new Range(['min' => 2])]; | ||
|
||
$violationList = $this->getMockBuilder(ConstraintViolationListInterface::class) | ||
->onlyMethods(['count']) | ||
->getMockForAbstractClass(); | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$violationList->expects($this->once()) | ||
->method('count') | ||
->willReturn($errorsCount); | ||
|
||
$contextualValidator = $this->getMockBuilder(ContextualValidatorInterface::class) | ||
->onlyMethods(['getViolations', 'validate']) | ||
->getMockForAbstractClass(); | ||
$contextualValidator->expects($this->once()) | ||
->method('validate') | ||
->with('foo', $constraints) | ||
->willReturnSelf(); | ||
$contextualValidator->expects($this->once()) | ||
->method('getViolations') | ||
->willReturn($violationList); | ||
|
||
$validator = $this->getMockBuilder(ValidatorInterface::class) | ||
->onlyMethods(['inContext']) | ||
->getMockForAbstractClass(); | ||
|
||
$context = $this->getMockBuilder(ExecutionContextInterface::class) | ||
->onlyMethods(['getValidator']) | ||
->getMockForAbstractClass(); | ||
$context->expects($this->once()) | ||
->method('getValidator') | ||
->willReturn($validator); | ||
|
||
$validator->expects($this->once()) | ||
->method('inContext') | ||
->with($context) | ||
->willReturn($contextualValidator); | ||
|
||
$provider = new ExpressionLanguageProvider($context); | ||
|
||
$expressionLanguage = new ExpressionLanguage(); | ||
$expressionLanguage->registerProvider($provider); | ||
|
||
$this->assertSame($expected, $expressionLanguage->evaluate('is_valid("foo", a)', ['a' => $constraints])); | ||
} | ||
|
||
public function dataProviderEvaluate(): array | ||
{ | ||
return [ | ||
[true, 0], | ||
[false, 1], | ||
[false, 12], | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.