diff --git a/src/Symfony/Component/Validator/Context/ExecutionContext.php b/src/Symfony/Component/Validator/Context/ExecutionContext.php index e2b582843f..4379498805 100644 --- a/src/Symfony/Component/Validator/Context/ExecutionContext.php +++ b/src/Symfony/Component/Validator/Context/ExecutionContext.php @@ -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.' + ); + } } diff --git a/src/Symfony/Component/Validator/Context/ExecutionContextManager.php b/src/Symfony/Component/Validator/Context/ExecutionContextManager.php index decefdc8a8..6214eabf46 100644 --- a/src/Symfony/Component/Validator/Context/ExecutionContextManager.php +++ b/src/Symfony/Component/Validator/Context/ExecutionContextManager.php @@ -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); + } } diff --git a/src/Symfony/Component/Validator/Context/LegacyExecutionContextManager.php b/src/Symfony/Component/Validator/Context/LegacyExecutionContextManager.php new file mode 100644 index 0000000000..361445bbfc --- /dev/null +++ b/src/Symfony/Component/Validator/Context/LegacyExecutionContextManager.php @@ -0,0 +1,36 @@ + + * + * 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 + * + * @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); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Fixtures/FakeMetadataFactory.php b/src/Symfony/Component/Validator/Tests/Fixtures/FakeMetadataFactory.php index ba39823be6..b03eacf71e 100644 --- a/src/Symfony/Component/Validator/Tests/Fixtures/FakeMetadataFactory.php +++ b/src/Symfony/Component/Validator/Tests/Fixtures/FakeMetadataFactory.php @@ -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; + } } diff --git a/src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php b/src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php new file mode 100644 index 0000000000..48d8f07141 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Validator/Abstract2Dot5ApiTest.php @@ -0,0 +1,407 @@ + + * + * 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 + */ +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()); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Validator/AbstractLegacyApiTest.php b/src/Symfony/Component/Validator/Tests/Validator/AbstractLegacyApiTest.php new file mode 100644 index 0000000000..4d76fa38c1 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Validator/AbstractLegacyApiTest.php @@ -0,0 +1,224 @@ + + * + * 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 + */ +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()); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Validator/AbstractValidatorTest.php b/src/Symfony/Component/Validator/Tests/Validator/AbstractValidatorTest.php index 36b13cfd96..99d250fb5a 100644 --- a/src/Symfony/Component/Validator/Tests/Validator/AbstractValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Validator/AbstractValidatorTest.php @@ -11,25 +11,16 @@ namespace Symfony\Component\Validator\Tests\Validator; -use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\Callback; use Symfony\Component\Validator\Constraints\GroupSequence; -use Symfony\Component\Validator\Constraints\Traverse; use Symfony\Component\Validator\ConstraintViolationInterface; -use Symfony\Component\Validator\Context\ExecutionContext; use Symfony\Component\Validator\ExecutionContextInterface; -use Symfony\Component\Validator\MetadataFactoryInterface; use Symfony\Component\Validator\Tests\Fixtures\FakeMetadataFactory; use Symfony\Component\Validator\Constraints\Valid; use Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity; use Symfony\Component\Validator\Tests\Fixtures\Reference; -use Symfony\Component\Validator\ConstraintViolation; -use Symfony\Component\Validator\ConstraintViolationList; -use Symfony\Component\Validator\Tests\Fixtures\FailingConstraint; use Symfony\Component\Validator\Tests\Fixtures\Entity; -use Symfony\Component\Validator\Tests\Fixtures\ConstraintA; use Symfony\Component\Validator\Mapping\ClassMetadata; -use Symfony\Component\Validator\ValidatorInterface; /** * @since 2.5 @@ -41,11 +32,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase const REFERENCE_CLASS = 'Symfony\Component\Validator\Tests\Fixtures\Reference'; - /** - * @var ValidatorInterface - */ - protected $validator; - /** * @var FakeMetadataFactory */ @@ -64,7 +50,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->metadataFactory = new FakeMetadataFactory(); - $this->validator = $this->createValidator($this->metadataFactory); $this->metadata = new ClassMetadata(self::ENTITY_CLASS); $this->referenceMetadata = new ClassMetadata(self::REFERENCE_CLASS); $this->metadataFactory->addMetadata($this->metadata); @@ -74,12 +59,54 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase protected function tearDown() { $this->metadataFactory = null; - $this->validator = null; $this->metadata = null; $this->referenceMetadata = null; } - abstract protected function createValidator(MetadataFactoryInterface $metadataFactory); + abstract protected function validate($value, $constraints, $groups = null); + + abstract protected function validateObject($object, $groups = null); + + abstract protected function validateCollection($collection, $groups = null, $deep = false); + + abstract protected function validateProperty($object, $propertyName, $groups = null); + + abstract protected function validatePropertyValue($object, $propertyName, $value, $groups = null); + + public function testValidate() + { + $test = $this; + + $callback = function ($value, ExecutionContextInterface $context) use ($test) { + $test->assertNull($context->getClassName()); + $test->assertNull($context->getPropertyName()); + $test->assertSame('', $context->getPropertyPath()); + $test->assertSame('Group', $context->getGroup()); + $test->assertSame('Bernhard', $context->getRoot()); + $test->assertSame('Bernhard', $context->getValue()); + $test->assertSame('Bernhard', $value); + + $context->addViolation('Message %param%', array('%param%' => 'value')); + }; + + $constraint = new Callback(array( + 'callback' => $callback, + 'groups' => 'Group', + )); + + $violations = $this->validate('Bernhard', $constraint, '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('Bernhard', $violations[0]->getRoot()); + $this->assertSame('Bernhard', $violations[0]->getInvalidValue()); + $this->assertNull($violations[0]->getMessagePluralization()); + $this->assertNull($violations[0]->getCode()); + } public function testClassConstraint() { @@ -92,7 +119,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $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); @@ -105,7 +131,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validate($entity, 'Group'); + $violations = $this->validateObject($entity, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -133,7 +159,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('firstName', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($propertyMetadatas[0], $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); $test->assertSame($entity, $context->getRoot()); $test->assertSame('Bernhard', $context->getValue()); $test->assertSame('Bernhard', $value); @@ -146,7 +171,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validate($entity, 'Group'); + $violations = $this->validateObject($entity, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -174,7 +199,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('lastName', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($propertyMetadatas[0], $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); $test->assertSame($entity, $context->getRoot()); $test->assertSame('Schussek', $context->getValue()); $test->assertSame('Schussek', $value); @@ -187,7 +211,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validate($entity, 'Group'); + $violations = $this->validateObject($entity, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -213,7 +237,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('[key]', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($test->metadata, $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); $test->assertSame($array, $context->getRoot()); $test->assertSame($entity, $context->getValue()); $test->assertSame($entity, $value); @@ -226,46 +249,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validateCollection($array, '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('[key]', $violations[0]->getPropertyPath()); - $this->assertSame($array, $violations[0]->getRoot()); - $this->assertSame($entity, $violations[0]->getInvalidValue()); - $this->assertNull($violations[0]->getMessagePluralization()); - $this->assertNull($violations[0]->getCode()); - } - - public function testArrayLegacyApi() - { - $test = $this; - $entity = new Entity(); - $array = array('key' => $entity); - - $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, $array) { - $test->assertSame($test::ENTITY_CLASS, $context->getClassName()); - $test->assertNull($context->getPropertyName()); - $test->assertSame('[key]', $context->getPropertyPath()); - $test->assertSame('Group', $context->getGroup()); - $test->assertSame($test->metadata, $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); - $test->assertSame($array, $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', - ))); - - $violations = $this->validator->validate($array, 'Group'); + $violations = $this->validateCollection($array, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -291,7 +275,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('[2][key]', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($test->metadata, $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); $test->assertSame($array, $context->getRoot()); $test->assertSame($entity, $context->getValue()); $test->assertSame($entity, $value); @@ -304,7 +287,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validateCollection($array, 'Group'); + $violations = $this->validateCollection($array, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -318,46 +301,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->assertNull($violations[0]->getCode()); } - public function testRecursiveArrayLegacyApi() - { - $test = $this; - $entity = new Entity(); - $array = array(2 => array('key' => $entity)); - - $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, $array) { - $test->assertSame($test::ENTITY_CLASS, $context->getClassName()); - $test->assertNull($context->getPropertyName()); - $test->assertSame('[2][key]', $context->getPropertyPath()); - $test->assertSame('Group', $context->getGroup()); - $test->assertSame($test->metadata, $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); - $test->assertSame($array, $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', - ))); - - $violations = $this->validator->validate($array, '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('[2][key]', $violations[0]->getPropertyPath()); - $this->assertSame($array, $violations[0]->getRoot()); - $this->assertSame($entity, $violations[0]->getInvalidValue()); - $this->assertNull($violations[0]->getMessagePluralization()); - $this->assertNull($violations[0]->getCode()); - } - - public function testTraversableTraverseEnabled() + public function testTraversable() { $test = $this; $entity = new Entity(); @@ -369,7 +313,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('[key]', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($test->metadata, $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); $test->assertSame($traversable, $context->getRoot()); $test->assertSame($entity, $context->getValue()); $test->assertSame($entity, $value); @@ -382,7 +325,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validateCollection($traversable, 'Group', true); + $violations = $this->validateCollection($traversable, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -396,66 +339,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->assertNull($violations[0]->getCode()); } - public function testTraversableTraverseEnabledLegacyApi() - { - $test = $this; - $entity = new Entity(); - $traversable = new \ArrayIterator(array('key' => $entity)); - - $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, $traversable) { - $test->assertSame($test::ENTITY_CLASS, $context->getClassName()); - $test->assertNull($context->getPropertyName()); - $test->assertSame('[key]', $context->getPropertyPath()); - $test->assertSame('Group', $context->getGroup()); - $test->assertSame($test->metadata, $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); - $test->assertSame($traversable, $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', - ))); - - $violations = $this->validator->validate($traversable, 'Group', true); - - /** @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('[key]', $violations[0]->getPropertyPath()); - $this->assertSame($traversable, $violations[0]->getRoot()); - $this->assertSame($entity, $violations[0]->getInvalidValue()); - $this->assertNull($violations[0]->getMessagePluralization()); - $this->assertNull($violations[0]->getCode()); - } - - /** - * @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException - */ - public function testTraversableTraverseDisabledLegacyApi() - { - $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'); - } - /** * @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException */ @@ -476,30 +359,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $this->validator->validateCollection($traversable, 'Group'); - } - - /** - * @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException - */ - public function testRecursiveTraversableRecursiveTraversalDisabledLegacyApi() - { - $test = $this; - $entity = new Entity(); - $traversable = new \ArrayIterator(array( - 2 => 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', true); + $this->validateCollection($traversable, 'Group'); } public function testRecursiveTraversableRecursiveTraversalEnabled() @@ -516,7 +376,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('[2][key]', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($test->metadata, $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); $test->assertSame($traversable, $context->getRoot()); $test->assertSame($entity, $context->getValue()); $test->assertSame($entity, $value); @@ -529,48 +388,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validateCollection($traversable, 'Group', true); - - /** @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('[2][key]', $violations[0]->getPropertyPath()); - $this->assertSame($traversable, $violations[0]->getRoot()); - $this->assertSame($entity, $violations[0]->getInvalidValue()); - $this->assertNull($violations[0]->getMessagePluralization()); - $this->assertNull($violations[0]->getCode()); - } - - public function testRecursiveTraversableRecursiveTraversalEnabledLegacyApi() - { - $test = $this; - $entity = new Entity(); - $traversable = new \ArrayIterator(array( - 2 => new \ArrayIterator(array('key' => $entity)), - )); - - $callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, $traversable) { - $test->assertSame($test::ENTITY_CLASS, $context->getClassName()); - $test->assertNull($context->getPropertyName()); - $test->assertSame('[2][key]', $context->getPropertyPath()); - $test->assertSame('Group', $context->getGroup()); - $test->assertSame($test->metadata, $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); - $test->assertSame($traversable, $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', - ))); - - $violations = $this->validator->validate($traversable, 'Group', true, true); + $violations = $this->validateCollection($traversable, 'Group', true); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -596,7 +414,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('reference', $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); @@ -610,7 +427,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validate($entity, 'Group'); + $violations = $this->validateObject($entity, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -639,7 +456,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('reference.value', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($propertyMetadatas[0], $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); $test->assertSame($entity, $context->getRoot()); $test->assertSame('Foobar', $context->getValue()); $test->assertSame('Foobar', $value); @@ -653,7 +469,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validate($entity, 'Group'); + $violations = $this->validateObject($entity, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -682,7 +498,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('reference.privateValue', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($propertyMetadatas[0], $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); $test->assertSame($entity, $context->getRoot()); $test->assertSame('Bamboo', $context->getValue()); $test->assertSame('Bamboo', $value); @@ -696,7 +511,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validate($entity, 'Group'); + $violations = $this->validateObject($entity, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -717,7 +532,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->metadata->addPropertyConstraint('reference', new Valid()); - $violations = $this->validator->validate($entity); + $violations = $this->validateObject($entity); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(0, $violations); @@ -733,7 +548,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->metadata->addPropertyConstraint('reference', new Valid()); - $this->validator->validate($entity); + $this->validateObject($entity); } public function testArrayReference() @@ -748,7 +563,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('reference[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['key'], $context->getValue()); $test->assertSame($entity->reference['key'], $value); @@ -762,7 +576,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validate($entity, 'Group'); + $violations = $this->validateObject($entity, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -789,7 +603,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('reference[2][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[2]['key'], $context->getValue()); $test->assertSame($entity->reference[2]['key'], $value); @@ -803,7 +616,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validate($entity, 'Group'); + $violations = $this->validateObject($entity, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -831,7 +644,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase ))); $this->referenceMetadata->addConstraint(new Callback($callback)); - $violations = $this->validator->validate($entity); + $violations = $this->validateObject($entity); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -851,7 +664,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase ))); $this->referenceMetadata->addConstraint(new Callback($callback)); - $violations = $this->validator->validate($entity); + $violations = $this->validateObject($entity); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -864,7 +677,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->metadata->addPropertyConstraint('reference', new Valid()); - $violations = $this->validator->validate($entity); + $violations = $this->validateObject($entity); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(0, $violations); @@ -877,7 +690,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->metadata->addPropertyConstraint('reference', new Valid()); - $violations = $this->validator->validate($entity); + $violations = $this->validateObject($entity); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(0, $violations); @@ -895,7 +708,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('reference[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['key'], $context->getValue()); $test->assertSame($entity->reference['key'], $value); @@ -909,7 +721,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validate($entity, 'Group'); + $violations = $this->validateObject($entity, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -938,7 +750,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase ))); $this->referenceMetadata->addConstraint(new Callback($callback)); - $violations = $this->validator->validate($entity); + $violations = $this->validateObject($entity); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(0, $violations); @@ -956,7 +768,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'traverse' => false, ))); - $this->validator->validate($entity, 'Default', ''); + $this->validateObject($entity, 'Default', ''); } public function testNoRecursiveTraversableTraversal() @@ -974,7 +786,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->metadata->addPropertyConstraint('reference', new Valid()); $this->referenceMetadata->addConstraint(new Callback($callback)); - $violations = $this->validator->validate($entity); + $violations = $this->validateObject($entity); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(0, $violations); @@ -994,7 +806,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('reference[2][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[2]['key'], $context->getValue()); $test->assertSame($entity->reference[2]['key'], $value); @@ -1010,7 +821,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validate($entity, 'Group'); + $violations = $this->validateObject($entity, 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -1039,7 +850,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('firstName', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($propertyMetadatas[0], $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); $test->assertSame($entity, $context->getRoot()); $test->assertSame('Bernhard', $context->getValue()); $test->assertSame('Bernhard', $value); @@ -1060,7 +870,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validateProperty($entity, 'firstName', 'Group'); + $violations = $this->validateProperty($entity, 'firstName', 'Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -1082,14 +892,9 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase // $metadata does not implement PropertyMetadataContainerInterface $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface'); - $metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface'); - $metadataFactory->expects($this->any()) - ->method('getMetadataFor') - ->with('VALUE') - ->will($this->returnValue($metadata)); - $validator = $this->createValidator($metadataFactory); + $this->metadataFactory->addMetadataForValue('VALUE', $metadata); - $validator->validateProperty('VALUE', 'someProperty'); + $this->validateProperty('VALUE', 'someProperty'); } public function testValidatePropertyValue() @@ -1106,7 +911,6 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $test->assertSame('firstName', $context->getPropertyPath()); $test->assertSame('Group', $context->getGroup()); $test->assertSame($propertyMetadatas[0], $context->getMetadata()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); $test->assertSame($entity, $context->getRoot()); $test->assertSame('Bernhard', $context->getValue()); $test->assertSame('Bernhard', $value); @@ -1127,7 +931,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group', ))); - $violations = $this->validator->validatePropertyValue( + $violations = $this->validatePropertyValue( $entity, 'firstName', 'Bernhard', @@ -1154,80 +958,9 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase // $metadata does not implement PropertyMetadataContainerInterface $metadata = $this->getMock('Symfony\Component\Validator\MetadataInterface'); - $metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface'); - $metadataFactory->expects($this->any()) - ->method('getMetadataFor') - ->with('VALUE') - ->will($this->returnValue($metadata)); - $validator = $this->createValidator($metadataFactory); + $this->metadataFactory->addMetadataForValue('VALUE', $metadata); - $validator->validatePropertyValue('VALUE', 'someProperty', 'someValue'); - } - - public function testValidateValue() - { - $test = $this; - - $callback = function ($value, ExecutionContextInterface $context) use ($test) { - $test->assertNull($context->getClassName()); - $test->assertNull($context->getPropertyName()); - $test->assertSame('', $context->getPropertyPath()); - $test->assertSame('Group', $context->getGroup()); - $test->assertSame($test->metadataFactory, $context->getMetadataFactory()); - $test->assertSame('Bernhard', $context->getRoot()); - $test->assertSame('Bernhard', $context->getValue()); - $test->assertSame('Bernhard', $value); - - $context->addViolation('Message %param%', array('%param%' => 'value')); - }; - - $constraint = new Callback(array( - 'callback' => $callback, - 'groups' => 'Group', - )); - - $violations = $this->validator->validateValue('Bernhard', $constraint, '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('Bernhard', $violations[0]->getRoot()); - $this->assertSame('Bernhard', $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->validator->validate($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()); + $this->validatePropertyValue('VALUE', 'someProperty', 'someValue'); } public function testValidateObjectOnlyOncePerGroup() @@ -1244,7 +977,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->metadata->addPropertyConstraint('reference2', new Valid()); $this->referenceMetadata->addConstraint(new Callback($callback)); - $violations = $this->validator->validate($entity); + $violations = $this->validateObject($entity); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -1264,7 +997,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->metadata->addPropertyConstraint('reference2', new Valid()); $this->referenceMetadata->addConstraint(new Callback($callback)); - $violations = $this->validator->validate($entity); + $violations = $this->validateObject($entity); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(2, $violations); @@ -1287,7 +1020,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group 2', ))); - $violations = $this->validator->validate($entity, 'Group 2'); + $violations = $this->validateObject($entity, 'Group 2'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -1310,93 +1043,12 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase 'groups' => 'Group 2', ))); - $violations = $this->validator->validate($entity, array('Group 1', 'Group 2')); + $violations = $this->validateObject($entity, array('Group 1', 'Group 2')); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(2, $violations); } - 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->validator->validate($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->validator->validate($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->validator->validate($entity, $sequence); - - /** @var ConstraintViolationInterface[] $violations */ - $this->assertCount(1, $violations); - $this->assertSame('Reference violation 1', $violations[0]->getMessage()); - } - public function testReplaceDefaultGroupByGroupSequenceObject() { $entity = new Entity(); @@ -1424,7 +1076,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $sequence = new GroupSequence(array('Group 1', 'Group 2', 'Group 3', 'Entity')); $this->metadata->setGroupSequence($sequence); - $violations = $this->validator->validate($entity, 'Default'); + $violations = $this->validateObject($entity, 'Default'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -1458,7 +1110,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $sequence = array('Group 1', 'Group 2', 'Group 3', 'Entity'); $this->metadata->setGroupSequence($sequence); - $violations = $this->validator->validate($entity, 'Default'); + $violations = $this->validateObject($entity, 'Default'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -1490,7 +1142,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $sequence = new GroupSequence(array('Group 1', 'Entity')); $this->metadata->setGroupSequence($sequence); - $violations = $this->validator->validate($entity, 'Default'); + $violations = $this->validateObject($entity, 'Default'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -1520,7 +1172,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $sequence = new GroupSequence(array('Group 1', 'Entity')); $this->metadata->setGroupSequence($sequence); - $violations = $this->validator->validate($entity, 'Other Group'); + $violations = $this->validateObject($entity, 'Other Group'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -1556,7 +1208,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->metadataFactory->addMetadata($metadata); - $violations = $this->validator->validate($entity, 'Default'); + $violations = $this->validateObject($entity, 'Default'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); @@ -1592,295 +1244,10 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase $this->metadataFactory->addMetadata($metadata); - $violations = $this->validator->validate($entity, 'Default'); + $violations = $this->validateObject($entity, 'Default'); /** @var ConstraintViolationInterface[] $violations */ $this->assertCount(1, $violations); $this->assertSame('Violation in Group 2', $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($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 testValidateInContextLegacyApi() - { - $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 - ->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($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 testValidateArrayInContextLegacyApi() - { - $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 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($test->metadataFactory, $context->getMetadataFactory()); - $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->validator->validate($entity, 'Group'); - - /** @var ConstraintViolationInterface[] $violations */ - $this->assertCount(1, $violations); - $test->assertSame('Separate violation', $violations[0]->getMessage()); - } - - /** - * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException - */ - public function testExpectTraversableIfTraverse() - { - $entity = new Entity(); - - $this->validator->validateValue($entity, new Traverse()); - } - - /** - * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException - */ - public function testExpectTraversableIfTraverseOnClass() - { - $entity = new Entity(); - - $this->metadata->addConstraint(new Traverse()); - - $this->validator->validate($entity); - } - - public function testGetMetadataFactory() - { - $this->assertSame($this->metadataFactory, $this->validator->getMetadataFactory()); - } } diff --git a/src/Symfony/Component/Validator/Tests/Validator/LegacyValidator2Dot5ApiTest.php b/src/Symfony/Component/Validator/Tests/Validator/LegacyValidator2Dot5ApiTest.php new file mode 100644 index 0000000000..e3715731ce --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Validator/LegacyValidator2Dot5ApiTest.php @@ -0,0 +1,47 @@ + + * + * 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; + } +} diff --git a/src/Symfony/Component/Validator/Tests/Validator/LegacyValidatorLegacyApiTest.php b/src/Symfony/Component/Validator/Tests/Validator/LegacyValidatorLegacyApiTest.php new file mode 100644 index 0000000000..c106d765c1 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Validator/LegacyValidatorLegacyApiTest.php @@ -0,0 +1,47 @@ + + * + * 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; + } +} diff --git a/src/Symfony/Component/Validator/Tests/Validator/LegacyValidatorTest.php b/src/Symfony/Component/Validator/Tests/Validator/LegacyValidatorTest.php deleted file mode 100644 index 0d8ade455c..0000000000 --- a/src/Symfony/Component/Validator/Tests/Validator/LegacyValidatorTest.php +++ /dev/null @@ -1,100 +0,0 @@ - - * - * 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()); - } -} diff --git a/src/Symfony/Component/Validator/Tests/Validator/Validator2Dot5ApiTest.php b/src/Symfony/Component/Validator/Tests/Validator/Validator2Dot5ApiTest.php new file mode 100644 index 0000000000..2bf4693dde --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Validator/Validator2Dot5ApiTest.php @@ -0,0 +1,47 @@ + + * + * 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; + } +} diff --git a/src/Symfony/Component/Validator/Tests/Validator/ValidatorTest.php b/src/Symfony/Component/Validator/Tests/Validator/ValidatorTest.php deleted file mode 100644 index 2b0d00205e..0000000000 --- a/src/Symfony/Component/Validator/Tests/Validator/ValidatorTest.php +++ /dev/null @@ -1,97 +0,0 @@ - - * - * 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()); - } -} diff --git a/src/Symfony/Component/Validator/Tests/ValidatorTest.php b/src/Symfony/Component/Validator/Tests/ValidatorTest.php new file mode 100644 index 0000000000..a983a78a70 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/ValidatorTest.php @@ -0,0 +1,36 @@ + + * + * 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()); + } +}