123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Validator\Tests\Constraints;
- use Symfony\Component\Validator\Constraint;
- use Symfony\Component\Validator\Constraints\Callback;
- use Symfony\Component\Validator\Constraints\CallbackValidator;
- use Symfony\Component\Validator\ExecutionContextInterface;
- use Symfony\Component\Validator\Validation;
- class CallbackValidatorTest_Class
- {
- public static function validateCallback($object, ExecutionContextInterface $context)
- {
- $context->addViolation('Callback message', array('{{ value }}' => 'foobar'));
- return false;
- }
- }
- class CallbackValidatorTest_Object
- {
- public function validate(ExecutionContextInterface $context)
- {
- $context->addViolation('My message', array('{{ value }}' => 'foobar'));
- return false;
- }
- public static function validateStatic($object, ExecutionContextInterface $context)
- {
- $context->addViolation('Static message', array('{{ value }}' => 'baz'));
- return false;
- }
- }
- class CallbackValidatorTest extends AbstractConstraintValidatorTest
- {
- protected function getApiVersion()
- {
- return Validation::API_VERSION_2_5;
- }
- protected function createValidator()
- {
- return new CallbackValidator();
- }
- public function testNullIsValid()
- {
- $this->validator->validate(null, new Callback());
- $this->assertNoViolation();
- }
- public function testSingleMethod()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback('validate');
- $this->validator->validate($object, $constraint);
- $this->buildViolation('My message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- public function testSingleMethodExplicitName()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(array('callback' => 'validate'));
- $this->validator->validate($object, $constraint);
- $this->buildViolation('My message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- public function testSingleStaticMethod()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback('validateStatic');
- $this->validator->validate($object, $constraint);
- $this->buildViolation('Static message')
- ->setParameter('{{ value }}', 'baz')
- ->assertRaised();
- }
- public function testClosure()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(function ($object, ExecutionContextInterface $context) {
- $context->addViolation('My message', array('{{ value }}' => 'foobar'));
- return false;
- });
- $this->validator->validate($object, $constraint);
- $this->buildViolation('My message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- public function testClosureNullObject()
- {
- $constraint = new Callback(function ($object, ExecutionContextInterface $context) {
- $context->addViolation('My message', array('{{ value }}' => 'foobar'));
- return false;
- });
- $this->validator->validate(null, $constraint);
- $this->buildViolation('My message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- public function testClosureExplicitName()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(array(
- 'callback' => function ($object, ExecutionContextInterface $context) {
- $context->addViolation('My message', array('{{ value }}' => 'foobar'));
- return false;
- },
- ));
- $this->validator->validate($object, $constraint);
- $this->buildViolation('My message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- public function testArrayCallable()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(array(__CLASS__.'_Class', 'validateCallback'));
- $this->validator->validate($object, $constraint);
- $this->buildViolation('Callback message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- public function testArrayCallableNullObject()
- {
- $constraint = new Callback(array(__CLASS__.'_Class', 'validateCallback'));
- $this->validator->validate(null, $constraint);
- $this->buildViolation('Callback message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- public function testArrayCallableExplicitName()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(array(
- 'callback' => array(__CLASS__.'_Class', 'validateCallback'),
- ));
- $this->validator->validate($object, $constraint);
- $this->buildViolation('Callback message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- /**
- * @group legacy
- */
- public function testLegacySingleMethodBc()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(array('validate'));
- $this->validator->validate($object, $constraint);
- $this->buildViolation('My message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- /**
- * @group legacy
- */
- public function testLegacySingleMethodBcExplicitName()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(array('methods' => array('validate')));
- $this->validator->validate($object, $constraint);
- $this->buildViolation('My message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- /**
- * @group legacy
- */
- public function testLegacyMultipleMethodsBc()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(array('validate', 'validateStatic'));
- $this->validator->validate($object, $constraint);
- $this->buildViolation('My message')
- ->setParameter('{{ value }}', 'foobar')
- ->buildNextViolation('Static message')
- ->setParameter('{{ value }}', 'baz')
- ->assertRaised();
- }
- /**
- * @group legacy
- */
- public function testLegacyMultipleMethodsBcExplicitName()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(array(
- 'methods' => array('validate', 'validateStatic'),
- ));
- $this->validator->validate($object, $constraint);
- $this->buildViolation('My message')
- ->setParameter('{{ value }}', 'foobar')
- ->buildNextViolation('Static message')
- ->setParameter('{{ value }}', 'baz')
- ->assertRaised();
- }
- /**
- * @group legacy
- */
- public function testLegacySingleStaticMethodBc()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(array(
- array(__CLASS__.'_Class', 'validateCallback'),
- ));
- $this->validator->validate($object, $constraint);
- $this->buildViolation('Callback message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- /**
- * @group legacy
- */
- public function testLegacySingleStaticMethodBcExplicitName()
- {
- $object = new CallbackValidatorTest_Object();
- $constraint = new Callback(array(
- 'methods' => array(array(__CLASS__.'_Class', 'validateCallback')),
- ));
- $this->validator->validate($object, $constraint);
- $this->buildViolation('Callback message')
- ->setParameter('{{ value }}', 'foobar')
- ->assertRaised();
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- */
- public function testExpectValidMethods()
- {
- $object = new CallbackValidatorTest_Object();
- $this->validator->validate($object, new Callback(array('callback' => array('foobar'))));
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- */
- public function testExpectValidCallbacks()
- {
- $object = new CallbackValidatorTest_Object();
- $this->validator->validate($object, new Callback(array('callback' => array('foo', 'bar'))));
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- * @group legacy
- */
- public function testLegacyExpectEitherCallbackOrMethods()
- {
- $object = new CallbackValidatorTest_Object();
- $this->validator->validate($object, new Callback(array(
- 'callback' => 'validate',
- 'methods' => array('validateStatic'),
- )));
- }
- public function testConstraintGetTargets()
- {
- $constraint = new Callback(array());
- $targets = array(Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT);
- $this->assertEquals($targets, $constraint->getTargets());
- }
- // Should succeed. Needed when defining constraints as annotations.
- public function testNoConstructorArguments()
- {
- $constraint = new Callback();
- $this->assertSame(array(Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT), $constraint->getTargets());
- }
- public function testAnnotationInvocationSingleValued()
- {
- $constraint = new Callback(array('value' => 'validateStatic'));
- $this->assertEquals(new Callback('validateStatic'), $constraint);
- }
- public function testAnnotationInvocationMultiValued()
- {
- $constraint = new Callback(array('value' => array(__CLASS__.'_Class', 'validateCallback')));
- $this->assertEquals(new Callback(array(__CLASS__.'_Class', 'validateCallback')), $constraint);
- }
- }
|