[Validator] Renamed methods addViolationAtRelativePath() and getAbsolutePropertyPath() in ExecutionContext
This commit is contained in:
parent
9153f0e569
commit
2e4ebe444f
@ -102,7 +102,7 @@ class UniqueEntityValidator extends ConstraintValidator
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->context->addViolationAtRelativePath($fields[0], $constraint->message, array(), $criteria[$fields[0]]);
|
$this->context->addViolationAtSubPath($fields[0], $constraint->message, array(), $criteria[$fields[0]]);
|
||||||
|
|
||||||
return true; // all true, we added the violation already!
|
return true; // all true, we added the violation already!
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ class CollectionValidator extends ConstraintValidator
|
|||||||
$walker->walkConstraint($constr, $value[$field], $group, $propertyPath.'['.$field.']');
|
$walker->walkConstraint($constr, $value[$field], $group, $propertyPath.'['.$field.']');
|
||||||
}
|
}
|
||||||
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
|
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
|
||||||
$this->context->addViolationAtRelativePath('['.$field.']', $constraint->missingFieldsMessage, array(
|
$this->context->addViolationAtSubPath('['.$field.']', $constraint->missingFieldsMessage, array(
|
||||||
'{{ field }}' => $field
|
'{{ field }}' => $field
|
||||||
), null);
|
), null);
|
||||||
$valid = false;
|
$valid = false;
|
||||||
@ -78,7 +78,7 @@ class CollectionValidator extends ConstraintValidator
|
|||||||
if (!$constraint->allowExtraFields) {
|
if (!$constraint->allowExtraFields) {
|
||||||
foreach ($value as $field => $fieldValue) {
|
foreach ($value as $field => $fieldValue) {
|
||||||
if (!isset($constraint->fields[$field])) {
|
if (!isset($constraint->fields[$field])) {
|
||||||
$this->context->addViolationAtRelativePath('['.$field.']', $constraint->extraFieldsMessage, array(
|
$this->context->addViolationAtSubPath('['.$field.']', $constraint->extraFieldsMessage, array(
|
||||||
'{{ field }}' => $field
|
'{{ field }}' => $field
|
||||||
), $fieldValue);
|
), $fieldValue);
|
||||||
$valid = false;
|
$valid = false;
|
||||||
|
@ -96,18 +96,18 @@ class ExecutionContext
|
|||||||
* Adds a violation at the validation graph node with the given property
|
* Adds a violation at the validation graph node with the given property
|
||||||
* path relative to the current property path.
|
* path relative to the current property path.
|
||||||
*
|
*
|
||||||
* @param string $relativePath The relative property path for the violation.
|
* @param string $subPath The relative property path for the violation.
|
||||||
* @param string $message The error message.
|
* @param string $message The error message.
|
||||||
* @param array $params The parameters parsed into the error message.
|
* @param array $params The parameters parsed into the error message.
|
||||||
* @param mixed $invalidValue The invalid, validated value.
|
* @param mixed $invalidValue The invalid, validated value.
|
||||||
*/
|
*/
|
||||||
public function addViolationAtRelativePath($relativePath, $message, array $params = array(), $invalidValue = null)
|
public function addViolationAtSubPath($subPath, $message, array $params = array(), $invalidValue = null)
|
||||||
{
|
{
|
||||||
$this->globalContext->addViolation(new ConstraintViolation(
|
$this->globalContext->addViolation(new ConstraintViolation(
|
||||||
$message,
|
$message,
|
||||||
$params,
|
$params,
|
||||||
$this->globalContext->getRoot(),
|
$this->globalContext->getRoot(),
|
||||||
$this->getAbsolutePropertyPath($relativePath),
|
$this->getPropertyPath($subPath),
|
||||||
// check using func_num_args() to allow passing null values
|
// check using func_num_args() to allow passing null values
|
||||||
func_num_args() === 4 ? $invalidValue : $this->value
|
func_num_args() === 4 ? $invalidValue : $this->value
|
||||||
));
|
));
|
||||||
@ -128,18 +128,13 @@ class ExecutionContext
|
|||||||
return $this->globalContext->getRoot();
|
return $this->globalContext->getRoot();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPropertyPath()
|
public function getPropertyPath($subPath = null)
|
||||||
{
|
{
|
||||||
return $this->propertyPath;
|
if (null !== $subPath && '' !== $this->propertyPath && '' !== $subPath && '[' !== $subPath[0]) {
|
||||||
}
|
return $this->propertyPath . '.' . $subPath;
|
||||||
|
|
||||||
public function getAbsolutePropertyPath($relativePath)
|
|
||||||
{
|
|
||||||
if ('' !== $this->propertyPath && '' !== $relativePath && '[' !== $relativePath[0]) {
|
|
||||||
return $this->propertyPath . '.' . $relativePath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->propertyPath . $relativePath;
|
return $this->propertyPath . $subPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getCurrentClass()
|
public function getCurrentClass()
|
||||||
|
@ -59,7 +59,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testNullIsValid()
|
public function testNullIsValid()
|
||||||
{
|
{
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$this->assertTrue($this->validator->isValid(null, new Collection(array('fields' => array(
|
$this->assertTrue($this->validator->isValid(null, new Collection(array('fields' => array(
|
||||||
'foo' => new Min(4),
|
'foo' => new Min(4),
|
||||||
@ -71,7 +71,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$data = $this->prepareTestData(array('foo' => 'foobar'));
|
$data = $this->prepareTestData(array('foo' => 'foobar'));
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
||||||
'foo' => new Min(4),
|
'foo' => new Min(4),
|
||||||
@ -107,7 +107,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$data = $this->prepareTestData($array);
|
$data = $this->prepareTestData($array);
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
||||||
'fields' => array(
|
'fields' => array(
|
||||||
@ -141,7 +141,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$data = $this->prepareTestData($array);
|
$data = $this->prepareTestData($array);
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
||||||
'fields' => array(
|
'fields' => array(
|
||||||
@ -159,7 +159,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
));
|
));
|
||||||
|
|
||||||
$this->context->expects($this->once())
|
$this->context->expects($this->once())
|
||||||
->method('addViolationAtRelativePath')
|
->method('addViolationAtSubPath')
|
||||||
->with('[baz]', 'myMessage', array(
|
->with('[baz]', 'myMessage', array(
|
||||||
'{{ field }}' => 'baz'
|
'{{ field }}' => 'baz'
|
||||||
));
|
));
|
||||||
@ -186,7 +186,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
));
|
));
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$this->assertTrue($this->validator->isValid($data, $constraint));
|
$this->assertTrue($this->validator->isValid($data, $constraint));
|
||||||
}
|
}
|
||||||
@ -206,7 +206,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
));
|
));
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$this->assertTrue($this->validator->isValid($data, $constraint));
|
$this->assertTrue($this->validator->isValid($data, $constraint));
|
||||||
}
|
}
|
||||||
@ -223,7 +223,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
));
|
));
|
||||||
|
|
||||||
$this->context->expects($this->once())
|
$this->context->expects($this->once())
|
||||||
->method('addViolationAtRelativePath')
|
->method('addViolationAtSubPath')
|
||||||
->with('[foo]', 'myMessage', array(
|
->with('[foo]', 'myMessage', array(
|
||||||
'{{ field }}' => 'foo',
|
'{{ field }}' => 'foo',
|
||||||
));
|
));
|
||||||
@ -243,7 +243,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
));
|
));
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$this->assertTrue($this->validator->isValid($data, $constraint));
|
$this->assertTrue($this->validator->isValid($data, $constraint));
|
||||||
}
|
}
|
||||||
@ -255,7 +255,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
));
|
));
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
||||||
'foo' => new Optional(),
|
'foo' => new Optional(),
|
||||||
@ -267,7 +267,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$data = $this->prepareTestData(array());
|
$data = $this->prepareTestData(array());
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
||||||
'foo' => new Optional(),
|
'foo' => new Optional(),
|
||||||
@ -287,7 +287,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with($constraint, $array['foo'], 'MyGroup', 'foo.bar[foo]');
|
->with($constraint, $array['foo'], 'MyGroup', 'foo.bar[foo]');
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$data = $this->prepareTestData($array);
|
$data = $this->prepareTestData($array);
|
||||||
|
|
||||||
@ -314,7 +314,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$data = $this->prepareTestData($array);
|
$data = $this->prepareTestData($array);
|
||||||
|
|
||||||
@ -330,7 +330,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
));
|
));
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
$this->assertTrue($this->validator->isValid($data, new Collection(array(
|
||||||
'foo' => new Required(),
|
'foo' => new Required(),
|
||||||
@ -342,7 +342,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$data = $this->prepareTestData(array());
|
$data = $this->prepareTestData(array());
|
||||||
|
|
||||||
$this->context->expects($this->once())
|
$this->context->expects($this->once())
|
||||||
->method('addViolationAtRelativePath')
|
->method('addViolationAtSubPath')
|
||||||
->with('[foo]', 'myMessage', array(
|
->with('[foo]', 'myMessage', array(
|
||||||
'{{ field }}' => 'foo',
|
'{{ field }}' => 'foo',
|
||||||
));
|
));
|
||||||
@ -368,7 +368,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with($constraint, $array['foo'], 'MyGroup', 'foo.bar[foo]');
|
->with($constraint, $array['foo'], 'MyGroup', 'foo.bar[foo]');
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$data = $this->prepareTestData($array);
|
$data = $this->prepareTestData($array);
|
||||||
|
|
||||||
@ -395,7 +395,7 @@ abstract class CollectionValidatorTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->context->expects($this->never())
|
$this->context->expects($this->never())
|
||||||
->method('addViolationAtRelativePath');
|
->method('addViolationAtSubPath');
|
||||||
|
|
||||||
$data = $this->prepareTestData($array);
|
$data = $this->prepareTestData($array);
|
||||||
|
|
||||||
|
@ -152,10 +152,10 @@ class ExecutionContextTest extends \PHPUnit_Framework_TestCase
|
|||||||
)), $this->context->getViolations());
|
)), $this->context->getViolations());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddViolationAtRelativePath()
|
public function testAddViolationAtSubPath()
|
||||||
{
|
{
|
||||||
// override preconfigured property path
|
// override preconfigured property path
|
||||||
$this->context->addViolationAtRelativePath('bam.baz', 'Error', array('foo' => 'bar'), 'invalid');
|
$this->context->addViolationAtSubPath('bam.baz', 'Error', array('foo' => 'bar'), 'invalid');
|
||||||
|
|
||||||
$this->assertEquals(new ConstraintViolationList(array(
|
$this->assertEquals(new ConstraintViolationList(array(
|
||||||
new ConstraintViolation(
|
new ConstraintViolation(
|
||||||
@ -168,9 +168,9 @@ class ExecutionContextTest extends \PHPUnit_Framework_TestCase
|
|||||||
)), $this->context->getViolations());
|
)), $this->context->getViolations());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddViolationAtRelativePathUsesPreconfiguredValueIfNotPassed()
|
public function testAddViolationAtSubPathUsesPreconfiguredValueIfNotPassed()
|
||||||
{
|
{
|
||||||
$this->context->addViolationAtRelativePath('bam.baz', 'Error');
|
$this->context->addViolationAtSubPath('bam.baz', 'Error');
|
||||||
|
|
||||||
$this->assertEquals(new ConstraintViolationList(array(
|
$this->assertEquals(new ConstraintViolationList(array(
|
||||||
new ConstraintViolation(
|
new ConstraintViolation(
|
||||||
@ -183,10 +183,10 @@ class ExecutionContextTest extends \PHPUnit_Framework_TestCase
|
|||||||
)), $this->context->getViolations());
|
)), $this->context->getViolations());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAddViolationAtRelativePathUsesPassedNullValue()
|
public function testAddViolationAtSubPathUsesPassedNullValue()
|
||||||
{
|
{
|
||||||
// passed null value should override preconfigured value "invalid"
|
// passed null value should override preconfigured value "invalid"
|
||||||
$this->context->addViolationAtRelativePath('bam.baz', 'Error', array('foo' => 'bar'), null);
|
$this->context->addViolationAtSubPath('bam.baz', 'Error', array('foo' => 'bar'), null);
|
||||||
|
|
||||||
$this->assertEquals(new ConstraintViolationList(array(
|
$this->assertEquals(new ConstraintViolationList(array(
|
||||||
new ConstraintViolation(
|
new ConstraintViolation(
|
||||||
@ -199,20 +199,25 @@ class ExecutionContextTest extends \PHPUnit_Framework_TestCase
|
|||||||
)), $this->context->getViolations());
|
)), $this->context->getViolations());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetAbsolutePropertyPathWithIndexPath()
|
public function testGetPropertyPath()
|
||||||
{
|
{
|
||||||
$this->assertEquals('foo.bar[bam]', $this->context->getAbsolutePropertyPath('[bam]'));
|
$this->assertEquals('foo.bar', $this->context->getPropertyPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetAbsolutePropertyPathWithEmptyPath()
|
public function testGetPropertyPathWithIndexPath()
|
||||||
{
|
{
|
||||||
$this->assertEquals('foo.bar', $this->context->getAbsolutePropertyPath(''));
|
$this->assertEquals('foo.bar[bam]', $this->context->getPropertyPath('[bam]'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetAbsolutePropertyPathWithEmptyCurrentPropertyPath()
|
public function testGetPropertyPathWithEmptyPath()
|
||||||
|
{
|
||||||
|
$this->assertEquals('foo.bar', $this->context->getPropertyPath(''));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetPropertyPathWithEmptyCurrentPropertyPath()
|
||||||
{
|
{
|
||||||
$this->context = new ExecutionContext($this->globalContext, 'currentValue', '', 'Group', 'ClassName', 'propertyName');
|
$this->context = new ExecutionContext($this->globalContext, 'currentValue', '', 'Group', 'ClassName', 'propertyName');
|
||||||
|
|
||||||
$this->assertEquals('bam.baz', $this->context->getAbsolutePropertyPath('bam.baz'));
|
$this->assertEquals('bam.baz', $this->context->getPropertyPath('bam.baz'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user