8000 [Validator] Add AtLeastOne constraint and validator by przemyslaw-bogusz · Pull Request #35744 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Add AtLeastOne constraint and validator #35744

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

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 47 additions & 0 deletions src/Symfony/Component/Validator/Constraints/AtLeastOneOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
*/
class AtLeastOneOf extends Composite
{
public const AT_LEAST_ONE_ERROR = 'f27e6d6c-261a-4056-b391-6673a623531c';

protected static $errorNames = [
self::AT_LEAST_ONE_ERROR => 'AT_LEAST_ONE_ERROR',
];

public $constraints = [];
public $message = 'This value should satisfy at least one of the following constraints:';
public $messageCollection = 'Each element of this collection should satisfy its own set of constraints.';
public $includeInternalMessages = true;

public function getDefaultOption()
{
return 'constraints';
}

public function getRequiredOptions()
{
return ['constraints'];
}

protected function getCompositeOption()
{
return 'constraints';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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 Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
*/
class AtLeastOneOfValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof AtLeastOneOf) {
throw new UnexpectedTypeException($constraint, AtLeastOneOf::class);
}

$validator = $this->context->getValidator();

$messages = [$constraint->message];

foreach ($constraint->constraints as $key => $item) {
$violations = $validator->validate($value, $item);

if (0 === \count($violations)) {
return;
}

if ($constraint->includeInternalMessages) {
$message = ' ['.($key + 1).'] ';

if ($item instanceof All || $item instanceof Collection) {
$message .= $constraint->messageCollection;
} else {
$message .= $violations->get(0)->getMessage();
}

$messages[] = $message;
}
}

$this->context->buildViolation(implode('', $messages))
->setCode(AtLeastOneOf::AT_LEAST_ONE_ERROR)
->addViolation()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,25 @@ protected function expectValidateValueAt($i, $propertyPath, $value, $constraints
->willReturn($contextualValidator);
}

protected function expectViolationsAt($i, $value, Constraint $constraint)
{
$context = $this->createContext();

$validatorClassname = $constraint->validatedBy();

$validator = new $validatorClassname();
$validator->initialize($context);
$validator->validate($value, $constraint);

$this->context->getValidator()
->expects($this->at($i))
->method('validate')
->willReturn($context->getViolations())
;

return $context->getViolations();
}

protected function assertNoViolation()
{
$this->assertSame(0, $violationsCount = \count($this->context->getViolations()), sprintf('0 violation expected. Got %u.', $violationsCount));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\Tests\Constraints;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
use Symfony\Component\Validator\Constraints\Valid;

/**
* @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
*/
class AtLeastOneOfTest extends TestCase
{
public function testRejectNonConstraints()
{
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
new AtLeastOneOf([
'foo',
]);
}

public function testRejectValidConstraint()
{
$this->expectException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
new AtLeastOneOf([
new Valid(),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?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\Tests\Constraints;

use Symfony\Component\Validator\Constraints\AtLeastOneOf;
use Symfony\Component\Validator\Constraints\AtLeastOneOfValidator;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Constraints\Country;
use Symfony\Component\Validator\Constraints\DivisibleBy;
use Symfony\Component\Validator\Constraints\EqualTo;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\IdenticalTo;
use Symfony\Component\Validator\Constraints\Language;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\LessThan;
use Symfony\Component\Validator\Constraints\Negative;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\Unique;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/**
* @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
*/
class AtLeastOneOfValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
{
return new AtLeastOneOfValidator();
}

/**
* @dataProvider getValidCombinations
*/
public function testValidCombinations($value, $constraints)
{
$i = 0;

foreach ($constraints as $constraint) {
$this->expectViolationsAt($i++, $value, $constraint);
}

$this->validator->validate($value, new AtLeastOneOf($constraints));

$this->assertNoViolation();
}

public function getValidCombinations()
{
return [
['symfony', [
new Length(['min' => 10]),
new EqualTo(['value' => 'symfony']),
]],
[150, [
new Range(['min' => 10, 'max' => 20]),
new GreaterThanOrEqual(['value' => 100]),
]],
[7, [
new LessThan(['value' => 5]),
new IdenticalTo(['value' => 7]),
]],
[-3, [
new DivisibleBy(['value' => 4]),
new Negative(),
]],
['FOO', [
new Choice(['choices' => ['bar', 'BAR']]),
new Regex(['pattern' => '/foo/i']),
]],
['fr', [
new Country(),
new Language(),
]],
[[1, 3, 5], [
new Count(['min' => 5]),
new Unique(),
]],
];
}

/**
* @dataProvider getInvalidCombinations
*/
public function testInvalidCombinationsWithDefaultMessage($value, $constraints)
{
$atLeastOneOf = new AtLeastOneOf(['constraints' => $constraints]);

$message = [$atLeastOneOf->message];

$i = 0;

foreach ($constraints as $constraint) {
$message[] = ' ['.($i + 1).'] '.$this->expectViolationsAt($i++, $value, $constraint)->get(0)->getMessage();
}

$this->validator->validate($value, $atLeastOneOf);

$this->buildViolation(implode('', $message))->setCode(AtLeastOneOf::AT_LEAST_ONE_ERROR)->assertRaised();
}

/**
* @dataProvider getInvalidCombinations
*/
public function testInvalidCombinationsWithCustomMessage($value, $constraints)
{
$atLeastOneOf = new AtLeastOneOf(['constraints' => $constraints, 'message' => 'foo', 'includeInternalMessages' => false]);

$i = 0;

foreach ($constraints as $constraint) {
$this->expectViolationsAt($i++, $value, $constraint);
}

$this->validator->validate($value, $atLeastOneOf);

$this->buildViolation('foo')->setCode(AtLeastOneOf::AT_LEAST_ONE_ERROR)->assertRaised();
}

public function getInvalidCombinations()
{
return [
['symphony', [
new Length(['min' => 10]),
new EqualTo(['value' => 'symfony']),
]],
[70, [
new Range(['min' => 10, 'max' => 20]),
new GreaterThanOrEqual(['value' => 100]),
]],
[8, [
new LessThan(['value' => 5]),
new IdenticalTo(['value' => 7]),
]],
[3, [
new DivisibleBy(['value' => 4]),
new Negative(),
]],
['F_O_O', [
new Choice(['choices' => ['bar', 'BAR']]),
new Regex(['pattern' => '/foo/i']),
]],
['f_r', [
new Country(),
new Language(),
]],
[[1, 3, 3], [
new Count(['min' => 5]),
new Unique(),
]],
];
}
}
0