Skip to content

Commit

Permalink
[Validator] Renamed methods addViolationAtRelativePath() and getAbsol…
Browse files Browse the repository at this point in the history
…utePropertyPath() in ExecutionContext
  • Loading branch information
webmozart committed Feb 2, 2012
1 parent 9153f0e commit 2e4ebe4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
Expand Up @@ -102,7 +102,7 @@ public function isValid($entity, Constraint $constraint)
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!
}
Expand Down
Expand Up @@ -68,7 +68,7 @@ public function isValid($value, Constraint $constraint)
$walker->walkConstraint($constr, $value[$field], $group, $propertyPath.'['.$field.']');
}
} elseif (!$fieldConstraint instanceof Optional && !$constraint->allowMissingFields) {
$this->context->addViolationAtRelativePath('['.$field.']', $constraint->missingFieldsMessage, array(
$this->context->addViolationAtSubPath('['.$field.']', $constraint->missingFieldsMessage, array(
'{{ field }}' => $field
), null);
$valid = false;
Expand All @@ -78,7 +78,7 @@ public function isValid($value, Constraint $constraint)
if (!$constraint->allowExtraFields) {
foreach ($value as $field => $fieldValue) {
if (!isset($constraint->fields[$field])) {
$this->context->addViolationAtRelativePath('['.$field.']', $constraint->extraFieldsMessage, array(
$this->context->addViolationAtSubPath('['.$field.']', $constraint->extraFieldsMessage, array(
'{{ field }}' => $field
), $fieldValue);
$valid = false;
Expand Down
19 changes: 7 additions & 12 deletions src/Symfony/Component/Validator/ExecutionContext.php
Expand Up @@ -96,18 +96,18 @@ public function addViolationAtPath($propertyPath, $message, array $params = arra
* Adds a violation at the validation graph node with the given property
* 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 array $params The parameters parsed into the error message.
* @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(
$message,
$params,
$this->globalContext->getRoot(),
$this->getAbsolutePropertyPath($relativePath),
$this->getPropertyPath($subPath),
// check using func_num_args() to allow passing null values
func_num_args() === 4 ? $invalidValue : $this->value
));
Expand All @@ -128,18 +128,13 @@ public function getRoot()
return $this->globalContext->getRoot();
}

public function getPropertyPath()
public function getPropertyPath($subPath = null)
{
return $this->propertyPath;
}

public function getAbsolutePropertyPath($relativePath)
{
if ('' !== $this->propertyPath && '' !== $relativePath && '[' !== $relativePath[0]) {
return $this->propertyPath . '.' . $relativePath;
if (null !== $subPath && '' !== $this->propertyPath && '' !== $subPath && '[' !== $subPath[0]) {
return $this->propertyPath . '.' . $subPath;
}

return $this->propertyPath . $relativePath;
return $this->propertyPath . $subPath;
}

public function getCurrentClass()
Expand Down
Expand Up @@ -59,7 +59,7 @@ abstract protected function prepareTestData(array $contents);
public function testNullIsValid()
{
$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$this->assertTrue($this->validator->isValid(null, new Collection(array('fields' => array(
'foo' => new Min(4),
Expand All @@ -71,7 +71,7 @@ public function testFieldsAsDefaultOption()
$data = $this->prepareTestData(array('foo' => 'foobar'));

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$this->assertTrue($this->validator->isValid($data, new Collection(array(
'foo' => new Min(4),
Expand Down Expand Up @@ -107,7 +107,7 @@ public function testWalkSingleConstraint()
$data = $this->prepareTestData($array);

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$this->assertTrue($this->validator->isValid($data, new Collection(array(
'fields' => array(
Expand Down Expand Up @@ -141,7 +141,7 @@ public function testWalkMultipleConstraints()
$data = $this->prepareTestData($array);

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$this->assertTrue($this->validator->isValid($data, new Collection(array(
'fields' => array(
Expand All @@ -159,7 +159,7 @@ public function testExtraFieldsDisallowed()
));

$this->context->expects($this->once())
->method('addViolationAtRelativePath')
->method('addViolationAtSubPath')
->with('[baz]', 'myMessage', array(
'{{ field }}' => 'baz'
));
Expand All @@ -186,7 +186,7 @@ public function testNullNotConsideredExtraField()
));

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$this->assertTrue($this->validator->isValid($data, $constraint));
}
Expand All @@ -206,7 +206,7 @@ public function testExtraFieldsAllowed()
));

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$this->assertTrue($this->validator->isValid($data, $constraint));
}
Expand All @@ -223,7 +223,7 @@ public function testMissingFieldsDisallowed()
));

$this->context->expects($this->once())
->method('addViolationAtRelativePath')
->method('addViolationAtSubPath')
->with('[foo]', 'myMessage', array(
'{{ field }}' => 'foo',
));
Expand All @@ -243,7 +243,7 @@ public function testMissingFieldsAllowed()
));

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$this->assertTrue($this->validator->isValid($data, $constraint));
}
Expand All @@ -255,7 +255,7 @@ public function testOptionalFieldPresent()
));

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$this->assertTrue($this->validator->isValid($data, new Collection(array(
'foo' => new Optional(),
Expand All @@ -267,7 +267,7 @@ public function testOptionalFieldNotPresent()
$data = $this->prepareTestData(array());

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$this->assertTrue($this->validator->isValid($data, new Collection(array(
'foo' => new Optional(),
Expand All @@ -287,7 +287,7 @@ public function testOptionalFieldSingleConstraint()
->with($constraint, $array['foo'], 'MyGroup', 'foo.bar[foo]');

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$data = $this->prepareTestData($array);

Expand All @@ -314,7 +314,7 @@ public function testOptionalFieldMultipleConstraints()
}

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$data = $this->prepareTestData($array);

Expand All @@ -330,7 +330,7 @@ public function testRequiredFieldPresent()
));

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$this->assertTrue($this->validator->isValid($data, new Collection(array(
'foo' => new Required(),
Expand All @@ -342,7 +342,7 @@ public function testRequiredFieldNotPresent()
$data = $this->prepareTestData(array());

$this->context->expects($this->once())
->method('addViolationAtRelativePath')
->method('addViolationAtSubPath')
->with('[foo]', 'myMessage', array(
'{{ field }}' => 'foo',
));
Expand All @@ -368,7 +368,7 @@ public function testRequiredFieldSingleConstraint()
->with($constraint, $array['foo'], 'MyGroup', 'foo.bar[foo]');

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$data = $this->prepareTestData($array);

Expand All @@ -395,7 +395,7 @@ public function testRequiredFieldMultipleConstraints()
}

$this->context->expects($this->never())
->method('addViolationAtRelativePath');
->method('addViolationAtSubPath');

$data = $this->prepareTestData($array);

Expand Down
29 changes: 17 additions & 12 deletions tests/Symfony/Tests/Component/Validator/ExecutionContextTest.php
Expand Up @@ -152,10 +152,10 @@ public function testAddViolationAtPathUsesPassedNullValue()
)), $this->context->getViolations());
}

public function testAddViolationAtRelativePath()
public function testAddViolationAtSubPath()
{
// 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(
new ConstraintViolation(
Expand All @@ -168,9 +168,9 @@ public function testAddViolationAtRelativePath()
)), $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(
new ConstraintViolation(
Expand All @@ -183,10 +183,10 @@ public function testAddViolationAtRelativePathUsesPreconfiguredValueIfNotPassed(
)), $this->context->getViolations());
}

public function testAddViolationAtRelativePathUsesPassedNullValue()
public function testAddViolationAtSubPathUsesPassedNullValue()
{
// 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(
new ConstraintViolation(
Expand All @@ -199,20 +199,25 @@ public function testAddViolationAtRelativePathUsesPassedNullValue()
)), $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->assertEquals('bam.baz', $this->context->getAbsolutePropertyPath('bam.baz'));
$this->assertEquals('bam.baz', $this->context->getPropertyPath('bam.baz'));
}
}

0 comments on commit 2e4ebe4

Please sign in to comment.