Skip to content

Commit 3c9cdc6

Browse files
bug #61117 [Validator] fix handling required options (xabbuh)
This PR was merged into the 6.4 branch. Discussion ---------- [Validator] fix handling required options | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT Commits ------- 43baeea fix handling required options
2 parents e5d6c57 + 43baeea commit 3c9cdc6

File tree

13 files changed

+166
-3
lines changed

13 files changed

+166
-3
lines changed

src/Symfony/Component/Validator/Constraints/CardScheme.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct(array|string|null $schemes, ?string $message = null,
5858
{
5959
if (\is_array($schemes) && \is_string(key($schemes))) {
6060
$options = array_merge($schemes, $options);
61-
} else {
61+
} elseif (null !== $schemes) {
6262
$options['value'] = $schemes;
6363
}
6464

src/Symfony/Component/Validator/Constraints/Expression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(
5757

5858
if (\is_array($expression)) {
5959
$options = array_merge($expression, $options);
60-
} else {
60+
} elseif (null !== $expression) {
6161
$options['value'] = $expression;
6262
}
6363

src/Symfony/Component/Validator/Constraints/When.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public function __construct(string|Expression|array $expression, array|Constrain
3737
$options = array_merge($expression, $options);
3838
} else {
3939
$options['expression'] = $expression;
40-
$options['constraints'] = $constraints;
40+
41+
if (null !== $constraints) {
42+
$options['constraints'] = $constraints;
43+
}
4144
}
4245

4346
if (isset($options['constraints']) && !\is_array($options['constraints'])) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\Constraints\All;
1616
use Symfony\Component\Validator\Constraints\Valid;
1717
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
18+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1819

1920
/**
2021
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -36,4 +37,20 @@ public function testRejectValidConstraint()
3637
new Valid(),
3738
]);
3839
}
40+
41+
public function testMissingConstraints()
42+
{
43+
$this->expectException(MissingOptionsException::class);
44+
$this->expectExceptionMessage(\sprintf('The options "constraints" must be set for constraint "%s".', All::class));
45+
46+
new All(null);
47+
}
48+
49+
public function testMissingConstraintsDoctrineStyle()
50+
{
51+
$this->expectException(MissingOptionsException::class);
52+
$this->expectExceptionMessage(\sprintf('The options "constraints" must be set for constraint "%s".', All::class));
53+
54+
new All([]);
55+
}
3956
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
1616
use Symfony\Component\Validator\Constraints\Valid;
1717
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
18+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1819

1920
/**
2021
* @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
@@ -36,4 +37,20 @@ public function testRejectValidConstraint()
3637
new Valid(),
3738
]);
3839
}
40+
41+
public function testMissingConstraints()
42+
{
43+
$this->expectException(MissingOptionsException::class);
44+
$this->expectExceptionMessage(\sprintf('The options "constraints" must be set for constraint "%s".', AtLeastOneOf::class));
45+
46+
new AtLeastOneOf(null);
47+
}
48+
49+
public function testMissingConstraintsDoctrineStyle()
50+
{
51+
$this->expectException(MissingOptionsException::class);
52+
$this->expectExceptionMessage(\sprintf('The options "constraints" must be set for constraint "%s".', AtLeastOneOf::class));
53+
54+
new AtLeastOneOf([]);
55+
}
3956
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Constraints\CardScheme;
16+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1617
use Symfony\Component\Validator\Mapping\ClassMetadata;
1718
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
1819

@@ -37,6 +38,14 @@ public function testAttributes()
3738
self::assertSame(['my_group'], $cConstraint->groups);
3839
self::assertSame('some attached data', $cConstraint->payload);
3940
}
41+
42+
public function testMissingSchemes()
43+
{
44+
$this->expectException(MissingOptionsException::class);
45+
$this->expectExceptionMessage(\sprintf('The options "schemes" must be set for constraint "%s".', CardScheme::class));
46+
47+
new CardScheme(null);
48+
}
4049
}
4150

4251
class CardSchemeDummy

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Validator\Constraints\Valid;
2020
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
2121
use Symfony\Component\Validator\Exception\InvalidOptionsException;
22+
use Symfony\Component\Validator\Exception\MissingOptionsException;
2223

2324
/**
2425
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -217,4 +218,12 @@ public function testEmptyConstraintListForFieldInOptions(?array $fieldConstraint
217218
$this->assertTrue($constraint->allowExtraFields);
218219
$this->assertSame('foo bar baz', $constraint->extraFieldsMessage);
219220
}
221+
222+
public function testMissingFields()
223+
{
224+
$this->expectException(MissingOptionsException::class);
225+
$this->expectExceptionMessage(\sprintf('The options "fields" must be set for constraint "%s".', Collection::class));
226+
227+
new Collection(null);
228+
}
220229
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,46 @@ public function testAttributes()
4040
self::assertSame(['my_group'], $cConstraint->groups);
4141
self::assertSame('some attached data', $cConstraint->payload);
4242
}
43+
44+
public function testMissingPattern()
45+
{
46+
$constraint = new CssColor(null);
47+
48+
$this->assertSame([
49+
CssColor::HEX_LONG,
50+
CssColor::HEX_LONG_WITH_ALPHA,
51+
CssColor::HEX_SHORT,
52+
CssColor::HEX_SHORT_WITH_ALPHA,
53+
CssColor::BASIC_NAMED_COLORS,
54+
CssColor::EXTENDED_NAMED_COLORS,
55+
CssColor::SYSTEM_COLORS,
56+
CssColor::KEYWORDS,
57+
CssColor::RGB,
58+
CssColor::RGBA,
59+
CssColor::HSL,
60+
CssColor::HSLA,
61+
], $constraint->formats);
62+
}
63+
64+
public function testMissingPatternDoctrineStyle()
65+
{
66+
$constraint = new CssColor([]);
67+
68+
$this->assertSame([
69+
CssColor::HEX_LONG,
70+
CssColor::HEX_LONG_WITH_ALPHA,
71+
CssColor::HEX_SHORT,
72+
CssColor::HEX_SHORT_WITH_ALPHA,
73+
CssColor::BASIC_NAMED_COLORS,
74+
CssColor::EXTENDED_NAMED_COLORS,
75+
CssColor::SYSTEM_COLORS,
76+
CssColor::KEYWORDS,
77+
CssColor::RGB,
78+
CssColor::RGBA,
79+
CssColor::HSL,
80+
CssColor::HSLA,
81+
], $constraint->formats);
82+
}
4383
}
4484

4585
class CssColorDummy

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Constraints\Expression;
16+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1617
use Symfony\Component\Validator\Mapping\ClassMetadata;
1718
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
1819

@@ -41,6 +42,22 @@ public function testAttributes()
4142
self::assertSame('some attached data', $cConstraint->payload);
4243
self::assertFalse($cConstraint->negate);
4344
}
45+
46+
public function testMissingPattern()
47+
{
48+
$this->expectException(MissingOptionsException::class);
49+
$this->expectExceptionMessage(\sprintf('The options "expression" must be set for constraint "%s".', Expression::class));
50+
51+
new Expression(null);
52+
}
53+
54+
public function testMissingPatternDoctrineStyle()
55+
{
56+
$this->expectException(MissingOptionsException::class);
57+
$this->expectExceptionMessage(\sprintf('The options "expression" must be set for constraint "%s".', Expression::class));
58+
59+
new Expression([]);
60+
}
4461
}
4562

4663
class ExpressionDummy

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Constraints\Regex;
1616
use Symfony\Component\Validator\Exception\InvalidArgumentException;
17+
use Symfony\Component\Validator\Exception\MissingOptionsException;
1718
use Symfony\Component\Validator\Mapping\ClassMetadata;
1819
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
1920

@@ -132,6 +133,22 @@ public function testAttributes()
132133
self::assertSame(['my_group'], $cConstraint->groups);
133134
self::assertSame('some attached data', $cConstraint->payload);
134135
}
136+
137+
public function testMissingPattern()
138+
{
139+
$this->expectException(MissingOptionsException::class);
140+
$this->expectExceptionMessage(\sprintf('The options "pattern" must be set for constraint "%s".', Regex::class));
141+
142+
new Regex(null);
143+
}
144+
145+
public function testMissingPatternDoctrineStyle()
146+
{
147+
$this->expectException(MissingOptionsException::class);
148+
$this->expectExceptionMessage(\sprintf('The options "pattern" must be set for constraint "%s".', Regex::class));
149+
150+
new Regex([]);
151+
}
135152
}
136153

137154
class RegexDummy

0 commit comments

Comments
 (0)