-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add errorPath
to Unique constraint
#57436
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
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ public static function getValidValues() | |
/** | ||
* @dataProvider getInvalidValues | ||
*/ | ||
public function testInvalidValues($value) | ||
public function testInvalidValues($value, string $expectedErrorPath) | ||
{ | ||
$constraint = new Unique([ | ||
'message' => 'myMessage', | ||
|
@@ -71,6 +71,7 @@ public function testInvalidValues($value) | |
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', 'array') | ||
->setCode(Unique::IS_NOT_UNIQUE) | ||
->atPath($expectedErrorPath) | ||
->assertRaised(); | ||
} | ||
|
||
|
@@ -79,12 +80,12 @@ public static function getInvalidValues() | |
$object = new \stdClass(); | ||
|
||
return [ | ||
yield 'not unique booleans' => [[true, true]], | ||
yield 'not unique integers' => [[1, 2, 3, 3]], | ||
yield 'not unique floats' => [[0.1, 0.2, 0.1]], | ||
yield 'not unique string' => [['a', 'b', 'a']], | ||
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]]], | ||
yield 'not unique objects' => [[$object, $object]], | ||
yield 'not unique booleans' => [[true, true], 'property.path[1]'], | ||
yield 'not unique integers' => [[1, 2, 3, 3], 'property.path[3]'], | ||
yield 'not unique floats' => [[0.1, 0.2, 0.1], 'property.path[2]'], | ||
yield 'not unique string' => [['a', 'b', 'a'], 'property.path[2]'], | ||
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]], 'property.path[2]'], | ||
yield 'not unique objects' => [[$object, $object], 'property.path[1]'], | ||
]; | ||
} | ||
|
||
|
@@ -96,6 +97,7 @@ public function testInvalidValueNamed() | |
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', 'array') | ||
->setCode(Unique::IS_NOT_UNIQUE) | ||
->atPath('property.path[3]') | ||
->assertRaised(); | ||
} | ||
|
||
|
@@ -152,6 +154,7 @@ public function testExpectsNonUniqueObjects($callback) | |
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', 'array') | ||
->setCode(Unique::IS_NOT_UNIQUE) | ||
->atPath('property.path[2]') | ||
->assertRaised(); | ||
} | ||
|
||
|
@@ -176,6 +179,7 @@ public function testExpectsInvalidNonStrictComparison() | |
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', 'array') | ||
->setCode(Unique::IS_NOT_UNIQUE) | ||
->atPath('property.path[1]') | ||
->assertRaised(); | ||
} | ||
|
||
|
@@ -202,6 +206,7 @@ public function testExpectsInvalidCaseInsensitiveComparison() | |
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', 'array') | ||
->setCode(Unique::IS_NOT_UNIQUE) | ||
->atPath('property.path[1]') | ||
->assertRaised(); | ||
} | ||
|
||
|
@@ -246,7 +251,7 @@ public static function getInvalidFieldNames(): array | |
/** | ||
* @dataProvider getInvalidCollectionValues | ||
*/ | ||
public function testInvalidCollectionValues(array $value, array $fields) | ||
public function testInvalidCollectionValues(array $value, array $fields, string $expectedErrorPath) | ||
{ | ||
$this->validator->validate($value, new Unique([ | ||
'message' => 'myMessage', | ||
|
@@ -255,6 +260,7 @@ public function testInvalidCollectionValues(array $value, array $fields) | |
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', 'array') | ||
->setCode(Unique::IS_NOT_UNIQUE) | ||
->atPath($expectedErrorPath) | ||
->assertRaised(); | ||
} | ||
|
||
|
@@ -264,23 +270,25 @@ public static function getInvalidCollectionValues(): array | |
'unique string' => [[ | ||
['lang' => 'eng', 'translation' => 'hi'], | ||
['lang' => 'eng', 'translation' => 'hello'], | ||
], ['lang']], | ||
], ['lang'], 'property.path[1]'], | ||
'unique floats' => [[ | ||
['latitude' => 51.509865, 'longitude' => -0.118092, 'poi' => 'capital'], | ||
['latitude' => 52.520008, 'longitude' => 13.404954], | ||
['latitude' => 51.509865, 'longitude' => -0.118092], | ||
], ['latitude', 'longitude']], | ||
], ['latitude', 'longitude'], 'property.path[2]'], | ||
'unique int' => [[ | ||
['id' => 1, 'email' => '[email protected]'], | ||
['id' => 1, 'email' => '[email protected]'], | ||
], ['id']], | ||
], ['id'], 'property.path[1]'], | ||
'unique null' => [ | ||
[null, null], | ||
[], | ||
'property.path[1]', | ||
], | ||
'unique field null' => [ | ||
[['nullField' => null], ['nullField' => null]], | ||
['nullField'], | ||
'property.path[1]', | ||
], | ||
]; | ||
} | ||
|
@@ -308,6 +316,90 @@ public function testArrayOfObjectsUnique() | |
$this->assertNoViolation(); | ||
} | ||
|
||
public function testErrorPath() | ||
{ | ||
$array = [ | ||
new DummyClassOne(), | ||
new DummyClassOne(), | ||
new DummyClassOne(), | ||
]; | ||
|
||
$array[0]->code = 'a1'; | ||
$array[1]->code = 'a2'; | ||
$array[2]->code = 'a1'; | ||
|
||
$this->validator->validate( | ||
$array, | ||
new Unique( | ||
normalizer: [self::class, 'normalizeDummyClassOne'], | ||
fields: 'code', | ||
errorPath: 'code', | ||
) | ||
); | ||
|
||
$this->buildViolation('This collection should contain only unique elements.') | ||
->setParameter('{{ value }}', 'array') | ||
->setCode(Unique::IS_NOT_UNIQUE) | ||
->atPath('property.path[2].code') | ||
->assertRaised(); | ||
} | ||
|
||
public function testErrorPathWithIteratorAggregate() | ||
{ | ||
$array = new \ArrayObject([ | ||
new DummyClassOne(), | ||
new DummyClassOne(), | ||
new DummyClassOne(), | ||
]); | ||
|
||
$array[0]->code = 'a1'; | ||
$array[1]->code = 'a2'; | ||
$array[2]->code = 'a1'; | ||
|
||
$this->validator->validate( | ||
$array, | ||
new Unique( | ||
normalizer: [self::class, 'normalizeDummyClassOne'], | ||
fields: 'code', | ||
errorPath: 'code', | ||
) | ||
); | ||
|
||
$this->buildViolation('This collection should contain only unique elements.') | ||
->setParameter('{{ value }}', 'object') | ||
->setCode(Unique::IS_NOT_UNIQUE) | ||
->atPath('property.path[2].code') | ||
->assertRaised(); | ||
} | ||
|
||
public function testErrorPathWithNonList() | ||
{ | ||
$array = [ | ||
'a' => new DummyClassOne(), | ||
'b' => new DummyClassOne(), | ||
'c' => new DummyClassOne(), | ||
]; | ||
|
||
$array['a']->code = 'a1'; | ||
$array['b']->code = 'a2'; | ||
$array['c']->code = 'a1'; | ||
|
||
$this->validator->validate( | ||
$array, | ||
new Unique( | ||
normalizer: [self::class, 'normalizeDummyClassOne'], | ||
fields: 'code', | ||
errorPath: 'code', | ||
) | ||
); | ||
|
||
$this->buildViolation('This collection should contain only unique elements.') | ||
->setParameter('{{ value }}', 'array') | ||
->setCode(Unique::IS_NOT_UNIQUE) | ||
->atPath('property.path[c].code') | ||
->assertRaised(); | ||
} | ||
|
||
public static function normalizeDummyClassOne(DummyClassOne $obj): array | ||
{ | ||
return [ | ||
|
Oops, something went wrong.
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.