Skip to content

Commit

Permalink
[Validator] [UniqueValidator] Use correct variable as parameter in (c…
Browse files Browse the repository at this point in the history
…ustom) error message
  • Loading branch information
seho-nl authored and fabpot committed Jun 9, 2024
1 parent c6b5f82 commit 49bfa54
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Constraints/UniqueValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function validate($value, Constraint $constraint)

if (\in_array($element, $collectionElements, true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ value }}', $this->formatValue($element))
->setCode(Unique::IS_NOT_UNIQUE)
->addViolation();

Expand Down
22 changes: 11 additions & 11 deletions Tests/Constraints/UniqueValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public static function getValidValues()
/**
* @dataProvider getInvalidValues
*/
public function testInvalidValues($value)
public function testInvalidValues($value, $expectedMessageParam)
{
$constraint = new Unique([
'message' => 'myMessage',
]);
$this->validator->validate($value, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setParameter('{{ value }}', $expectedMessageParam)
->setCode(Unique::IS_NOT_UNIQUE)
->assertRaised();
}
Expand All @@ -77,12 +77,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], 'true'],
yield 'not unique integers' => [[1, 2, 3, 3], 3],
yield 'not unique floats' => [[0.1, 0.2, 0.1], 0.1],
yield 'not unique string' => [['a', 'b', 'a'], '"a"'],
yield 'not unique arrays' => [[[1, 1], [2, 3], [1, 1]], 'array'],
yield 'not unique objects' => [[$object, $object], 'object'],
];
}

Expand All @@ -95,7 +95,7 @@ public function testInvalidValueNamed()
$this->validator->validate([1, 2, 3, 3], $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setParameter('{{ value }}', '3')
->setCode(Unique::IS_NOT_UNIQUE)
->assertRaised();
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public function testExpectsInvalidNonStrictComparison()
]));

$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setParameter('{{ value }}', '1')
->setCode(Unique::IS_NOT_UNIQUE)
->assertRaised();
}
Expand Down Expand Up @@ -206,7 +206,7 @@ public function testExpectsInvalidCaseInsensitiveComparison()
]));

$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setParameter('{{ value }}', '"hello"')
->setCode(Unique::IS_NOT_UNIQUE)
->assertRaised();
}
Expand Down

0 comments on commit 49bfa54

Please sign in to comment.