[Validator] Clearly separated classes supporting the API <2.5/2.5+
This commit is contained in:
parent
a3555fbd99
commit
bc29591935
|
@ -13,10 +13,14 @@ namespace Symfony\Component\Validator\Context;
|
|||
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Validator\ClassBasedInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
use Symfony\Component\Validator\Exception\BadMethodCallException;
|
||||
use Symfony\Component\Validator\ExecutionContextInterface as LegacyExecutionContextInterface;
|
||||
use Symfony\Component\Validator\Group\GroupManagerInterface;
|
||||
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface;
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\Node\Node;
|
||||
use Symfony\Component\Validator\Util\PropertyPath;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
@ -30,7 +34,7 @@ use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;
|
|||
*
|
||||
* @see ExecutionContextInterface
|
||||
*/
|
||||
class ExecutionContext implements ExecutionContextInterface
|
||||
class ExecutionContext implements ExecutionContextInterface, LegacyExecutionContextInterface
|
||||
{
|
||||
/**
|
||||
* The root value of the validated object graph.
|
||||
|
@ -151,8 +155,12 @@ class ExecutionContext implements ExecutionContextInterface
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addViolation($message, array $parameters = array())
|
||||
public function addViolation($message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
|
||||
{
|
||||
// The parameters $invalidValue and following are ignored by the new
|
||||
// API, as they are not present in the new interface anymore.
|
||||
// You should use buildViolation() instead.
|
||||
|
||||
$this->violations->add(new ConstraintViolation(
|
||||
$this->translator->trans($message, $parameters, $this->translationDomain),
|
||||
$message,
|
||||
|
@ -259,4 +267,49 @@ class ExecutionContext implements ExecutionContextInterface
|
|||
|
||||
return PropertyPath::append($propertyPath, $subPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addViolationAt($subPath, $message, array $parameters = array(), $invalidValue = null, $pluralization = null, $code = null)
|
||||
{
|
||||
throw new BadMethodCallException(
|
||||
'addViolationAt() is not supported anymore in the new API. '.
|
||||
'Please use buildViolation() or enable the legacy mode.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value, $subPath = '', $groups = null, $traverse = false, $deep = false)
|
||||
{
|
||||
throw new BadMethodCallException(
|
||||
'validate() is not supported anymore in the new API. '.
|
||||
'Please use getValidator() or enable the legacy mode.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateValue($value, $constraints, $subPath = '', $groups = null)
|
||||
{
|
||||
throw new BadMethodCallException(
|
||||
'validateValue() is not supported anymore in the new API. '.
|
||||
'Please use getValidator() or enable the legacy mode.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMetadataFactory()
|
||||
{
|
||||
throw new BadMethodCallException(
|
||||
'getMetadataFactory() is not supported anymore in the new API. '.
|
||||
'Please use getMetadataFor() or hasMetadataFor() or enable the '.
|
||||
'legacy mode.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -110,13 +110,14 @@ class ExecutionContextManager extends AbstractVisitor implements ExecutionContex
|
|||
);
|
||||
}
|
||||
|
||||
$this->currentContext = new LegacyExecutionContext(
|
||||
$this->currentContext = $this->createContext(
|
||||
$root,
|
||||
$this->validator,
|
||||
$this->groupManager,
|
||||
$this->translator,
|
||||
$this->translationDomain
|
||||
);
|
||||
|
||||
$this->contextStack->push($this->currentContext);
|
||||
|
||||
return $this->currentContext;
|
||||
|
@ -188,4 +189,27 @@ class ExecutionContextManager extends AbstractVisitor implements ExecutionContex
|
|||
|
||||
$this->currentContext->popNode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new context.
|
||||
*
|
||||
* Can be overridden by subclasses.
|
||||
*
|
||||
* @param mixed $root The root value of the
|
||||
* validated object graph
|
||||
* @param ValidatorInterface $validator The validator
|
||||
* @param GroupManagerInterface $groupManager The manager for accessing
|
||||
* the currently validated
|
||||
* group
|
||||
* @param TranslatorInterface $translator The translator
|
||||
* @param string|null $translationDomain The translation domain to
|
||||
* use for translating
|
||||
* violation messages
|
||||
*
|
||||
* @return ExecutionContextInterface The created context
|
||||
*/
|
||||
protected function createContext($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain)
|
||||
{
|
||||
return new ExecutionContext($root, $validator, $groupManager, $translator, $translationDomain);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Context;
|
||||
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Symfony\Component\Validator\Group\GroupManagerInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
* A context manager that creates contexts compatible to the API < Symfony 2.5.
|
||||
*
|
||||
* @since 2.5
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*
|
||||
* @see ExecutionContextManagerInterface
|
||||
* @see \Symfony\Component\Validator\NodeVisitor\NodeVisitorInterface
|
||||
*/
|
||||
class LegacyExecutionContextManager extends ExecutionContextManager
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createContext($root, ValidatorInterface $validator, GroupManagerInterface $groupManager, TranslatorInterface $translator, $translationDomain)
|
||||
{
|
||||
return new LegacyExecutionContext($root, $validator, $groupManager, $translator, $translationDomain);
|
||||
}
|
||||
}
|
|
@ -11,9 +11,10 @@
|
|||
|
||||
namespace Symfony\Component\Validator\Tests\Fixtures;
|
||||
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\Exception\NoSuchMetadataException;
|
||||
use Symfony\Component\Validator\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\MetadataInterface;
|
||||
|
||||
class FakeMetadataFactory implements MetadataFactoryInterface
|
||||
{
|
||||
|
@ -53,4 +54,9 @@ class FakeMetadataFactory implements MetadataFactoryInterface
|
|||
{
|
||||
$this->metadatas[$metadata->getClassName()] = $metadata;
|
||||
}
|
||||
|
||||
public function addMetadataForValue($value, MetadataInterface $metadata)
|
||||
{
|
||||
$this->metadatas[$value] = $metadata;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,407 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\Callback;
|
||||
use Symfony\Component\Validator\Constraints\GroupSequence;
|
||||
use Symfony\Component\Validator\Constraints\Traverse;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\ConstraintViolationInterface;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\Tests\Fixtures\Entity;
|
||||
use Symfony\Component\Validator\Tests\Fixtures\Reference;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
* Verifies that a validator satisfies the API of Symfony 2.5+.
|
||||
*
|
||||
* @since 2.5
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
||||
{
|
||||
/**
|
||||
* @var ValidatorInterface
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @param MetadataFactoryInterface $metadataFactory
|
||||
*
|
||||
* @return ValidatorInterface
|
||||
*/
|
||||
abstract protected function createValidator(MetadataFactoryInterface $metadataFactory);
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->validator = $this->createValidator($this->metadataFactory);
|
||||
}
|
||||
|
||||
protected function validate($value, $constraints, $groups = null)
|
||||
{
|
||||
return $this->validator->validate($value, $constraints, $groups);
|
||||
}
|
||||
|
||||
protected function validateObject($object, $groups = null)
|
||||
{
|
||||
return $this->validator->validateObject($object, $groups);
|
||||
}
|
||||
|
||||
protected function validateCollection($collection, $groups = null, $deep = false)
|
||||
{
|
||||
return $this->validator->validateCollection($collection, $groups, $deep);
|
||||
}
|
||||
|
||||
protected function validateProperty($object, $propertyName, $groups = null)
|
||||
{
|
||||
return $this->validator->validateProperty($object, $propertyName, $groups);
|
||||
}
|
||||
|
||||
protected function validatePropertyValue($object, $propertyName, $value, $groups = null)
|
||||
{
|
||||
return $this->validator->validatePropertyValue($object, $propertyName, $value, $groups);
|
||||
}
|
||||
|
||||
public function testNoDuplicateValidationIfConstraintInMultipleGroups()
|
||||
{
|
||||
$entity = new Entity();
|
||||
|
||||
$callback = function ($value, ExecutionContextInterface $context) {
|
||||
$context->addViolation('Message');
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback,
|
||||
'groups' => array('Group 1', 'Group 2'),
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, array('Group 1', 'Group 2'));
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
}
|
||||
|
||||
public function testGroupSequenceAbortsAfterFailedGroup()
|
||||
{
|
||||
$entity = new Entity();
|
||||
|
||||
$callback1 = function ($value, ExecutionContextInterface $context) {
|
||||
$context->addViolation('Message 1');
|
||||
};
|
||||
$callback2 = function ($value, ExecutionContextInterface $context) {
|
||||
$context->addViolation('Message 2');
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => function () {},
|
||||
'groups' => 'Group 1',
|
||||
)));
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback1,
|
||||
'groups' => 'Group 2',
|
||||
)));
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback2,
|
||||
'groups' => 'Group 3',
|
||||
)));
|
||||
|
||||
$sequence = new GroupSequence(array('Group 1', 'Group 2', 'Group 3'));
|
||||
$violations = $this->validateObject($entity, $sequence);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Message 1', $violations[0]->getMessage());
|
||||
}
|
||||
|
||||
public function testGroupSequenceIncludesReferences()
|
||||
{
|
||||
$entity = new Entity();
|
||||
$entity->reference = new Reference();
|
||||
|
||||
$callback1 = function ($value, ExecutionContextInterface $context) {
|
||||
$context->addViolation('Reference violation 1');
|
||||
};
|
||||
$callback2 = function ($value, ExecutionContextInterface $context) {
|
||||
$context->addViolation('Reference violation 2');
|
||||
};
|
||||
|
||||
$this->metadata->addPropertyConstraint('reference', new Valid());
|
||||
$this->referenceMetadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback1,
|
||||
'groups' => 'Group 1',
|
||||
)));
|
||||
$this->referenceMetadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback2,
|
||||
'groups' => 'Group 2',
|
||||
)));
|
||||
|
||||
$sequence = new GroupSequence(array('Group 1', 'Entity'));
|
||||
$violations = $this->validateObject($entity, $sequence);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Reference violation 1', $violations[0]->getMessage());
|
||||
}
|
||||
|
||||
public function testValidateInSeparateContext()
|
||||
{
|
||||
$test = $this;
|
||||
$entity = new Entity();
|
||||
$entity->reference = new Reference();
|
||||
|
||||
$callback1 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
|
||||
$violations = $context
|
||||
->getValidator()
|
||||
// Since the validator is not context aware, the group must
|
||||
// be passed explicitly
|
||||
->validateObject($value->reference, 'Group')
|
||||
;
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$test->assertCount(1, $violations);
|
||||
$test->assertSame('Message value', $violations[0]->getMessage());
|
||||
$test->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||
$test->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||
$test->assertSame('', $violations[0]->getPropertyPath());
|
||||
// The root is different as we're in a new context
|
||||
$test->assertSame($entity->reference, $violations[0]->getRoot());
|
||||
$test->assertSame($entity->reference, $violations[0]->getInvalidValue());
|
||||
$test->assertNull($violations[0]->getMessagePluralization());
|
||||
$test->assertNull($violations[0]->getCode());
|
||||
|
||||
// Verify that this method is called
|
||||
$context->addViolation('Separate violation');
|
||||
};
|
||||
|
||||
$callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
|
||||
$test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
|
||||
$test->assertNull($context->getPropertyName());
|
||||
$test->assertSame('', $context->getPropertyPath());
|
||||
$test->assertSame('Group', $context->getGroup());
|
||||
$test->assertSame($test->referenceMetadata, $context->getMetadata());
|
||||
$test->assertSame($entity->reference, $context->getRoot());
|
||||
$test->assertSame($entity->reference, $context->getValue());
|
||||
$test->assertSame($entity->reference, $value);
|
||||
|
||||
$context->addViolation('Message %param%', array('%param%' => 'value'));
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback1,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
$this->referenceMetadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback2,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$test->assertSame('Separate violation', $violations[0]->getMessage());
|
||||
}
|
||||
|
||||
public function testValidateInContext()
|
||||
{
|
||||
$test = $this;
|
||||
$entity = new Entity();
|
||||
$entity->reference = new Reference();
|
||||
|
||||
$callback1 = function ($value, ExecutionContextInterface $context) {
|
||||
$context
|
||||
->getValidator()
|
||||
->inContext($context)
|
||||
->atPath('subpath')
|
||||
->validateObject($value->reference)
|
||||
;
|
||||
};
|
||||
|
||||
$callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
|
||||
$test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
|
||||
$test->assertNull($context->getPropertyName());
|
||||
$test->assertSame('subpath', $context->getPropertyPath());
|
||||
$test->assertSame('Group', $context->getGroup());
|
||||
$test->assertSame($test->referenceMetadata, $context->getMetadata());
|
||||
$test->assertSame($entity, $context->getRoot());
|
||||
$test->assertSame($entity->reference, $context->getValue());
|
||||
$test->assertSame($entity->reference, $value);
|
||||
|
||||
$context->addViolation('Message %param%', array('%param%' => 'value'));
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback1,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
$this->referenceMetadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback2,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Message value', $violations[0]->getMessage());
|
||||
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||
$this->assertSame('subpath', $violations[0]->getPropertyPath());
|
||||
$this->assertSame($entity, $violations[0]->getRoot());
|
||||
$this->assertSame($entity->reference, $violations[0]->getInvalidValue());
|
||||
$this->assertNull($violations[0]->getMessagePluralization());
|
||||
$this->assertNull($violations[0]->getCode());
|
||||
}
|
||||
|
||||
public function testValidateArrayInContext()
|
||||
{
|
||||
$test = $this;
|
||||
$entity = new Entity();
|
||||
$entity->reference = new Reference();
|
||||
|
||||
$callback1 = function ($value, ExecutionContextInterface $context) {
|
||||
$context
|
||||
->getValidator()
|
||||
->inContext($context)
|
||||
->atPath('subpath')
|
||||
->validateCollection(array('key' => $value->reference))
|
||||
;
|
||||
};
|
||||
|
||||
$callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
|
||||
$test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
|
||||
$test->assertNull($context->getPropertyName());
|
||||
$test->assertSame('subpath[key]', $context->getPropertyPath());
|
||||
$test->assertSame('Group', $context->getGroup());
|
||||
$test->assertSame($test->referenceMetadata, $context->getMetadata());
|
||||
$test->assertSame($entity, $context->getRoot());
|
||||
$test->assertSame($entity->reference, $context->getValue());
|
||||
$test->assertSame($entity->reference, $value);
|
||||
|
||||
$context->addViolation('Message %param%', array('%param%' => 'value'));
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback1,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
$this->referenceMetadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback2,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validateObject($entity, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Message value', $violations[0]->getMessage());
|
||||
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||
$this->assertSame('subpath[key]', $violations[0]->getPropertyPath());
|
||||
$this->assertSame($entity, $violations[0]->getRoot());
|
||||
$this->assertSame($entity->reference, $violations[0]->getInvalidValue());
|
||||
$this->assertNull($violations[0]->getMessagePluralization());
|
||||
$this->assertNull($violations[0]->getCode());
|
||||
}
|
||||
|
||||
public function testValidateAcceptsValid()
|
||||
{
|
||||
$test = $this;
|
||||
$entity = new Entity();
|
||||
|
||||
$callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
|
||||
$test->assertSame($test::ENTITY_CLASS, $context->getClassName());
|
||||
$test->assertNull($context->getPropertyName());
|
||||
$test->assertSame('', $context->getPropertyPath());
|
||||
$test->assertSame('Group', $context->getGroup());
|
||||
$test->assertSame($test->metadata, $context->getMetadata());
|
||||
$test->assertSame($entity, $context->getRoot());
|
||||
$test->assertSame($entity, $context->getValue());
|
||||
$test->assertSame($entity, $value);
|
||||
|
||||
$context->addViolation('Message %param%', array('%param%' => 'value'));
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
// This is the same as when calling validateObject()
|
||||
$violations = $this->validator->validate($entity, new Valid(), 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Message value', $violations[0]->getMessage());
|
||||
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||
$this->assertSame('', $violations[0]->getPropertyPath());
|
||||
$this->assertSame($entity, $violations[0]->getRoot());
|
||||
$this->assertSame($entity, $violations[0]->getInvalidValue());
|
||||
$this->assertNull($violations[0]->getMessagePluralization());
|
||||
$this->assertNull($violations[0]->getCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
||||
*/
|
||||
public function testExpectTraversableIfTraverse()
|
||||
{
|
||||
$entity = new Entity();
|
||||
|
||||
$this->validate($entity, new Traverse());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
||||
*/
|
||||
public function testExpectTraversableIfTraverseOnClass()
|
||||
{
|
||||
$entity = new Entity();
|
||||
|
||||
$this->metadata->addConstraint(new Traverse());
|
||||
|
||||
$this->validateObject($entity);
|
||||
}
|
||||
|
||||
public function testAddCustomizedViolation()
|
||||
{
|
||||
$entity = new Entity();
|
||||
|
||||
$callback = function ($value, ExecutionContextInterface $context) {
|
||||
$context->buildViolation('Message %param%')
|
||||
->setParameter('%param%', 'value')
|
||||
->setInvalidValue('Invalid value')
|
||||
->setPluralization(2)
|
||||
->setCode('Code')
|
||||
->addViolation();
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback($callback));
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Message value', $violations[0]->getMessage());
|
||||
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||
$this->assertSame('', $violations[0]->getPropertyPath());
|
||||
$this->assertSame($entity, $violations[0]->getRoot());
|
||||
$this->assertSame('Invalid value', $violations[0]->getInvalidValue());
|
||||
$this->assertSame(2, $violations[0]->getMessagePluralization());
|
||||
$this->assertSame('Code', $violations[0]->getCode());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,224 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\Callback;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\ConstraintViolationInterface;
|
||||
use Symfony\Component\Validator\ExecutionContextInterface;
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\Tests\Fixtures\Entity;
|
||||
use Symfony\Component\Validator\Tests\Fixtures\Reference;
|
||||
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
|
||||
|
||||
/**
|
||||
* Verifies that a validator satisfies the API of Symfony < 2.5.
|
||||
*
|
||||
* @since 2.5
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
*/
|
||||
abstract class AbstractLegacyApiTest extends AbstractValidatorTest
|
||||
{
|
||||
/**
|
||||
* @var LegacyValidatorInterface
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @param MetadataFactoryInterface $metadataFactory
|
||||
*
|
||||
* @return LegacyValidatorInterface
|
||||
*/
|
||||
abstract protected function createValidator(MetadataFactoryInterface $metadataFactory);
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->validator = $this->createValidator($this->metadataFactory);
|
||||
}
|
||||
|
||||
protected function validate($value, $constraints, $groups = null)
|
||||
{
|
||||
return $this->validator->validateValue($value, $constraints, $groups);
|
||||
}
|
||||
|
||||
protected function validateObject($object, $groups = null)
|
||||
{
|
||||
return $this->validator->validate($object, $groups);
|
||||
}
|
||||
|
||||
protected function validateCollection($collection, $groups = null, $deep = false)
|
||||
{
|
||||
return $this->validator->validate($collection, $groups, true, $deep);
|
||||
}
|
||||
|
||||
protected function validateProperty($object, $propertyName, $groups = null)
|
||||
{
|
||||
return $this->validator->validateProperty($object, $propertyName, $groups);
|
||||
}
|
||||
|
||||
protected function validatePropertyValue($object, $propertyName, $value, $groups = null)
|
||||
{
|
||||
return $this->validator->validatePropertyValue($object, $propertyName, $value, $groups);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
|
||||
*/
|
||||
public function testTraversableTraverseDisabled()
|
||||
{
|
||||
$test = $this;
|
||||
$entity = new Entity();
|
||||
$traversable = new \ArrayIterator(array('key' => $entity));
|
||||
|
||||
$callback = function () use ($test) {
|
||||
$test->fail('Should not be called');
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$this->validator->validate($traversable, 'Group');
|
||||
}
|
||||
|
||||
public function testValidateInContext()
|
||||
{
|
||||
$test = $this;
|
||||
$entity = new Entity();
|
||||
$entity->reference = new Reference();
|
||||
|
||||
$callback1 = function ($value, ExecutionContextInterface $context) {
|
||||
$context->validate($value->reference, 'subpath');
|
||||
};
|
||||
|
||||
$callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
|
||||
$test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
|
||||
$test->assertNull($context->getPropertyName());
|
||||
$test->assertSame('subpath', $context->getPropertyPath());
|
||||
$test->assertSame('Group', $context->getGroup());
|
||||
$test->assertSame($test->referenceMetadata, $context->getMetadata());
|
||||
$test->assertSame($test->metadataFactory, $context->getMetadataFactory());
|
||||
$test->assertSame($entity, $context->getRoot());
|
||||
$test->assertSame($entity->reference, $context->getValue());
|
||||
$test->assertSame($entity->reference, $value);
|
||||
|
||||
$context->addViolation('Message %param%', array('%param%' => 'value'));
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback1,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
$this->referenceMetadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback2,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validator->validate($entity, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Message value', $violations[0]->getMessage());
|
||||
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||
$this->assertSame('subpath', $violations[0]->getPropertyPath());
|
||||
$this->assertSame($entity, $violations[0]->getRoot());
|
||||
$this->assertSame($entity->reference, $violations[0]->getInvalidValue());
|
||||
$this->assertNull($violations[0]->getMessagePluralization());
|
||||
$this->assertNull($violations[0]->getCode());
|
||||
}
|
||||
|
||||
public function testValidateArrayInContext()
|
||||
{
|
||||
$test = $this;
|
||||
$entity = new Entity();
|
||||
$entity->reference = new Reference();
|
||||
|
||||
$callback1 = function ($value, ExecutionContextInterface $context) {
|
||||
$context->validate(array('key' => $value->reference), 'subpath');
|
||||
};
|
||||
|
||||
$callback2 = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
|
||||
$test->assertSame($test::REFERENCE_CLASS, $context->getClassName());
|
||||
$test->assertNull($context->getPropertyName());
|
||||
$test->assertSame('subpath[key]', $context->getPropertyPath());
|
||||
$test->assertSame('Group', $context->getGroup());
|
||||
$test->assertSame($test->referenceMetadata, $context->getMetadata());
|
||||
$test->assertSame($test->metadataFactory, $context->getMetadataFactory());
|
||||
$test->assertSame($entity, $context->getRoot());
|
||||
$test->assertSame($entity->reference, $context->getValue());
|
||||
$test->assertSame($entity->reference, $value);
|
||||
|
||||
$context->addViolation('Message %param%', array('%param%' => 'value'));
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback1,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
$this->referenceMetadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback2,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
$violations = $this->validator->validate($entity, 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Message value', $violations[0]->getMessage());
|
||||
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||
$this->assertSame('subpath[key]', $violations[0]->getPropertyPath());
|
||||
$this->assertSame($entity, $violations[0]->getRoot());
|
||||
$this->assertSame($entity->reference, $violations[0]->getInvalidValue());
|
||||
$this->assertNull($violations[0]->getMessagePluralization());
|
||||
$this->assertNull($violations[0]->getCode());
|
||||
}
|
||||
|
||||
public function testAddCustomizedViolation()
|
||||
{
|
||||
$entity = new Entity();
|
||||
|
||||
$callback = function ($value, ExecutionContextInterface $context) {
|
||||
$context->addViolation(
|
||||
'Message %param%',
|
||||
array('%param%' => 'value'),
|
||||
'Invalid value',
|
||||
2,
|
||||
'Code'
|
||||
);
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback($callback));
|
||||
|
||||
$violations = $this->validateObject($entity);
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Message value', $violations[0]->getMessage());
|
||||
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||
$this->assertSame('', $violations[0]->getPropertyPath());
|
||||
$this->assertSame($entity, $violations[0]->getRoot());
|
||||
$this->assertSame('Invalid value', $violations[0]->getInvalidValue());
|
||||
$this->assertSame(2, $violations[0]->getMessagePluralization());
|
||||
$this->assertSame('Code', $violations[0]->getCode());
|
||||
}
|
||||
|
||||
public function testGetMetadataFactory()
|
||||
{
|
||||
$this->assertSame($this->metadataFactory, $this->validator->getMetadataFactory());
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Validator;
|
||||
|
||||
use Symfony\Component\Validator\DefaultTranslator;
|
||||
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
||||
use Symfony\Component\Validator\Context\LegacyExecutionContextManager;
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\NodeVisitor\GroupSequenceResolver;
|
||||
use Symfony\Component\Validator\NodeVisitor\NodeValidator;
|
||||
use Symfony\Component\Validator\NodeTraverser\NodeTraverser;
|
||||
use Symfony\Component\Validator\Validator\LegacyValidator;
|
||||
|
||||
class LegacyValidator2Dot5ApiTest extends Abstract2Dot5ApiTest
|
||||
{
|
||||
protected function createValidator(MetadataFactoryInterface $metadataFactory)
|
||||
{
|
||||
$nodeTraverser = new NodeTraverser($metadataFactory);
|
||||
$nodeValidator = new NodeValidator($nodeTraverser, new ConstraintValidatorFactory());
|
||||
$contextManager = new LegacyExecutionContextManager($nodeValidator, new DefaultTranslator());
|
||||
$validator = new LegacyValidator($nodeTraverser, $metadataFactory, $contextManager);
|
||||
$groupSequenceResolver = new GroupSequenceResolver();
|
||||
|
||||
// The context manager needs the validator for passing it to created
|
||||
// contexts
|
||||
$contextManager->initialize($validator);
|
||||
|
||||
// The node validator needs the context manager for passing the current
|
||||
// context to the constraint validators
|
||||
$nodeValidator->initialize($contextManager);
|
||||
|
||||
$nodeTraverser->addVisitor($groupSequenceResolver);
|
||||
$nodeTraverser->addVisitor($contextManager);
|
||||
$nodeTraverser->addVisitor($nodeValidator);
|
||||
|
||||
return $validator;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Validator;
|
||||
|
||||
use Symfony\Component\Validator\DefaultTranslator;
|
||||
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
||||
use Symfony\Component\Validator\Context\LegacyExecutionContextManager;
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\NodeVisitor\GroupSequenceResolver;
|
||||
use Symfony\Component\Validator\NodeVisitor\NodeValidator;
|
||||
use Symfony\Component\Validator\NodeTraverser\NodeTraverser;
|
||||
use Symfony\Component\Validator\Validator\LegacyValidator;
|
||||
|
||||
class LegacyValidatorLegacyApiTest extends AbstractLegacyApiTest
|
||||
{
|
||||
protected function createValidator(MetadataFactoryInterface $metadataFactory)
|
||||
{
|
||||
$nodeTraverser = new NodeTraverser($metadataFactory);
|
||||
$nodeValidator = new NodeValidator($nodeTraverser, new ConstraintValidatorFactory());
|
||||
$contextManager = new LegacyExecutionContextManager($nodeValidator, new DefaultTranslator());
|
||||
$validator = new LegacyValidator($nodeTraverser, $metadataFactory, $contextManager);
|
||||
$groupSequenceResolver = new GroupSequenceResolver();
|
||||
|
||||
// The context manager needs the validator for passing it to created
|
||||
// contexts
|
||||
$contextManager->initialize($validator);
|
||||
|
||||
// The node validator needs the context manager for passing the current
|
||||
// context to the constraint validators
|
||||
$nodeValidator->initialize($contextManager);
|
||||
|
||||
$nodeTraverser->addVisitor($groupSequenceResolver);
|
||||
$nodeTraverser->addVisitor($contextManager);
|
||||
$nodeTraverser->addVisitor($nodeValidator);
|
||||
|
||||
return $validator;
|
||||
}
|
||||
}
|
|
@ -1,100 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\Tests\Fixtures\Entity;
|
||||
use Symfony\Component\Validator\Validator as LegacyValidator;
|
||||
use Symfony\Component\Validator\DefaultTranslator;
|
||||
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
||||
|
||||
class LegacyValidatorTest extends AbstractValidatorTest
|
||||
{
|
||||
protected function createValidator(MetadataFactoryInterface $metadataFactory)
|
||||
{
|
||||
return new LegacyValidator($metadataFactory, new ConstraintValidatorFactory(), new DefaultTranslator());
|
||||
}
|
||||
|
||||
public function testNoDuplicateValidationIfConstraintInMultipleGroups()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testGroupSequenceAbortsAfterFailedGroup()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testGroupSequenceIncludesReferences()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testValidateInContext()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testValidateArrayInContext()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testValidateInSeparateContext()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testArray()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testRecursiveArray()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testTraversableTraverseEnabled()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testRecursiveTraversableRecursiveTraversalDisabled()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testRecursiveTraversableRecursiveTraversalEnabled()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testExpectTraversableIfTraverse()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
public function testExpectTraversableIfTraverseOnClass()
|
||||
{
|
||||
$this->markTestSkipped('Not supported in the legacy API');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Validator\Exception\ValidatorException
|
||||
*/
|
||||
public function testValidateValueRejectsValid()
|
||||
{
|
||||
$this->validator->validateValue(new Entity(), new Valid());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Validator;
|
||||
|
||||
use Symfony\Component\Validator\DefaultTranslator;
|
||||
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextManager;
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\NodeVisitor\GroupSequenceResolver;
|
||||
use Symfony\Component\Validator\NodeVisitor\NodeValidator;
|
||||
use Symfony\Component\Validator\NodeTraverser\NodeTraverser;
|
||||
use Symfony\Component\Validator\Validator\Validator;
|
||||
|
||||
class Validator2Dot5ApiTest extends Abstract2Dot5ApiTest
|
||||
{
|
||||
protected function createValidator(MetadataFactoryInterface $metadataFactory)
|
||||
{
|
||||
$nodeTraverser = new NodeTraverser($metadataFactory);
|
||||
$nodeValidator = new NodeValidator($nodeTraverser, new ConstraintValidatorFactory());
|
||||
$contextManager = new ExecutionContextManager($nodeValidator, new DefaultTranslator());
|
||||
$validator = new Validator($nodeTraverser, $metadataFactory, $contextManager);
|
||||
$groupSequenceResolver = new GroupSequenceResolver();
|
||||
|
||||
// The context manager needs the validator for passing it to created
|
||||
// contexts
|
||||
$contextManager->initialize($validator);
|
||||
|
||||
// The node validator needs the context manager for passing the current
|
||||
// context to the constraint validators
|
||||
$nodeValidator->initialize($contextManager);
|
||||
|
||||
$nodeTraverser->addVisitor($groupSequenceResolver);
|
||||
$nodeTraverser->addVisitor($contextManager);
|
||||
$nodeTraverser->addVisitor($nodeValidator);
|
||||
|
||||
return $validator;
|
||||
}
|
||||
}
|
|
@ -1,97 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests\Validator;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\Callback;
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\ConstraintViolationInterface;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Symfony\Component\Validator\DefaultTranslator;
|
||||
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextManager;
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\NodeVisitor\GroupSequenceResolver;
|
||||
use Symfony\Component\Validator\NodeVisitor\NodeValidator;
|
||||
use Symfony\Component\Validator\NodeTraverser\NodeTraverser;
|
||||
use Symfony\Component\Validator\Tests\Fixtures\Entity;
|
||||
use Symfony\Component\Validator\Validator\LegacyValidator;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
class ValidatorTest extends AbstractValidatorTest
|
||||
{
|
||||
/**
|
||||
* @var ValidatorInterface
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
protected function createValidator(MetadataFactoryInterface $metadataFactory)
|
||||
{
|
||||
$nodeTraverser = new NodeTraverser($metadataFactory);
|
||||
$nodeValidator = new NodeValidator($nodeTraverser, new ConstraintValidatorFactory());
|
||||
$contextManager = new ExecutionContextManager($nodeValidator, new DefaultTranslator());
|
||||
$validator = new LegacyValidator($nodeTraverser, $metadataFactory, $contextManager);
|
||||
$groupSequenceResolver = new GroupSequenceResolver();
|
||||
|
||||
// The context manager needs the validator for passing it to created
|
||||
// contexts
|
||||
$contextManager->initialize($validator);
|
||||
|
||||
// The node validator needs the context manager for passing the current
|
||||
// context to the constraint validators
|
||||
$nodeValidator->initialize($contextManager);
|
||||
|
||||
$nodeTraverser->addVisitor($groupSequenceResolver);
|
||||
$nodeTraverser->addVisitor($contextManager);
|
||||
$nodeTraverser->addVisitor($nodeValidator);
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
||||
public function testValidateAcceptsValid()
|
||||
{
|
||||
$test = $this;
|
||||
$entity = new Entity();
|
||||
|
||||
$callback = function ($value, ExecutionContextInterface $context) use ($test, $entity) {
|
||||
$test->assertSame($test::ENTITY_CLASS, $context->getClassName());
|
||||
$test->assertNull($context->getPropertyName());
|
||||
$test->assertSame('', $context->getPropertyPath());
|
||||
$test->assertSame('Group', $context->getGroup());
|
||||
$test->assertSame($test->metadata, $context->getMetadata());
|
||||
$test->assertSame($test->metadataFactory, $context->getMetadataFactory());
|
||||
$test->assertSame($entity, $context->getRoot());
|
||||
$test->assertSame($entity, $context->getValue());
|
||||
$test->assertSame($entity, $value);
|
||||
|
||||
$context->addViolation('Message %param%', array('%param%' => 'value'));
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback(array(
|
||||
'callback' => $callback,
|
||||
'groups' => 'Group',
|
||||
)));
|
||||
|
||||
// This is the same as when calling validateObject()
|
||||
$violations = $this->validator->validate($entity, new Valid(), 'Group');
|
||||
|
||||
/** @var ConstraintViolationInterface[] $violations */
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('Message value', $violations[0]->getMessage());
|
||||
$this->assertSame('Message %param%', $violations[0]->getMessageTemplate());
|
||||
$this->assertSame(array('%param%' => 'value'), $violations[0]->getMessageParameters());
|
||||
$this->assertSame('', $violations[0]->getPropertyPath());
|
||||
$this->assertSame($entity, $violations[0]->getRoot());
|
||||
$this->assertSame($entity, $violations[0]->getInvalidValue());
|
||||
$this->assertNull($violations[0]->getMessagePluralization());
|
||||
$this->assertNull($violations[0]->getCode());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Validator\Tests;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\Valid;
|
||||
use Symfony\Component\Validator\MetadataFactoryInterface;
|
||||
use Symfony\Component\Validator\Tests\Fixtures\Entity;
|
||||
use Symfony\Component\Validator\Tests\Validator\AbstractLegacyApiTest;
|
||||
use Symfony\Component\Validator\Validator as LegacyValidator;
|
||||
use Symfony\Component\Validator\DefaultTranslator;
|
||||
use Symfony\Component\Validator\ConstraintValidatorFactory;
|
||||
|
||||
class ValidatorTest extends AbstractLegacyApiTest
|
||||
{
|
||||
protected function createValidator(MetadataFactoryInterface $metadataFactory)
|
||||
{
|
||||
return new LegacyValidator($metadataFactory, new ConstraintValidatorFactory(), new DefaultTranslator());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Validator\Exception\ValidatorException
|
||||
*/
|
||||
public function testValidateValueRejectsValid()
|
||||
{
|
||||
$this->validator->validateValue(new Entity(), new Valid());
|
||||
}
|
||||
}
|
Reference in New Issue