This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php
Fabien Potencier b91866f6c1 Merge branch '2.4' into 2.5
* 2.4:
  fixed CS
  [Process] fixed some volatile tests
  [HttpKernel] fixed a volatile test
  [HttpFoundation] fixed some volatile tests
  [Tests] PHPUnit Optimizations
  Use getPathname() instead of string casting to get BinaryFileReponse file path

Conflicts:
	src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php
	src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/SessionController.php
	src/Symfony/Component/ClassLoader/Tests/ApcUniversalClassLoaderTest.php
	src/Symfony/Component/Console/Formatter/OutputFormatterStyle.php
	src/Symfony/Component/HttpKernel/DataCollector/LoggerDataCollector.php
	src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php
	src/Symfony/Component/HttpKernel/Tests/DataCollector/LoggerDataCollectorTest.php
	src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php
	src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php
	src/Symfony/Component/Process/Tests/AbstractProcessTest.php
	src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
	src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php
	src/Symfony/Component/Security/Acl/Dbal/MutableAclProvider.php
	src/Symfony/Component/Security/Core/Authentication/Provider/PreAuthenticatedAuthenticationProvider.php
	src/Symfony/Component/Security/Http/Tests/Firewall/SwitchUserListenerTest.php
	src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php
	src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php
	src/Symfony/Component/Validator/Constraints/ChoiceValidator.php
	src/Symfony/Component/Validator/Constraints/CollectionValidator.php
	src/Symfony/Component/Validator/Tests/Constraints/AbstractConstraintValidatorTest.php
	src/Symfony/Component/Validator/Tests/Constraints/IsbnValidatorTest.php
	src/Symfony/Component/Validator/Tests/ValidationVisitorTest.php
	src/Symfony/Component/Yaml/Parser.php
2014-09-22 11:14:18 +02:00

343 lines
9.7 KiB
PHP

<?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(array('foo')));
$this->assertNoViolation();
}
public function testSingleMethod()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback('validate');
$this->validator->validate($object, $constraint);
$this->assertViolation('My message', array(
'{{ value }}' => 'foobar',
));
}
public function testSingleMethodExplicitName()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback(array('callback' => 'validate'));
$this->validator->validate($object, $constraint);
$this->assertViolation('My message', array(
'{{ value }}' => 'foobar',
));
}
public function testSingleStaticMethod()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback('validateStatic');
$this->validator->validate($object, $constraint);
$this->assertViolation('Static message', array(
'{{ value }}' => 'baz',
));
}
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->assertViolation('My message', array(
'{{ value }}' => 'foobar',
));
}
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->assertViolation('My message', array(
'{{ value }}' => 'foobar',
));
}
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->assertViolation('My message', array(
'{{ value }}' => 'foobar',
));
}
public function testArrayCallable()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback(array(__CLASS__.'_Class', 'validateCallback'));
$this->validator->validate($object, $constraint);
$this->assertViolation('Callback message', array(
'{{ value }}' => 'foobar',
));
}
public function testArrayCallableNullObject()
{
$constraint = new Callback(array(__CLASS__.'_Class', 'validateCallback'));
$this->validator->validate(null, $constraint);
$this->assertViolation('Callback message', array(
'{{ value }}' => 'foobar',
));
}
public function testArrayCallableExplicitName()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback(array(
'callback' => array(__CLASS__.'_Class', 'validateCallback'),
));
$this->validator->validate($object, $constraint);
$this->assertViolation('Callback message', array(
'{{ value }}' => 'foobar',
));
}
// BC with Symfony < 2.4
public function testSingleMethodBc()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback(array('validate'));
$this->validator->validate($object, $constraint);
$this->assertViolation('My message', array(
'{{ value }}' => 'foobar',
));
}
// BC with Symfony < 2.4
public function testSingleMethodBcExplicitName()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback(array('methods' => array('validate')));
$this->validator->validate($object, $constraint);
$this->assertViolation('My message', array(
'{{ value }}' => 'foobar',
));
}
// BC with Symfony < 2.4
public function testMultipleMethodsBc()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback(array('validate', 'validateStatic'));
$this->validator->validate($object, $constraint);
$this->assertViolations(array(
$this->createViolation('My message', array(
'{{ value }}' => 'foobar',
)),
$this->createViolation('Static message', array(
'{{ value }}' => 'baz',
)),
));
}
// BC with Symfony < 2.4
public function testMultipleMethodsBcExplicitName()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback(array(
'methods' => array('validate', 'validateStatic'),
));
$this->validator->validate($object, $constraint);
$this->assertViolations(array(
$this->createViolation('My message', array(
'{{ value }}' => 'foobar',
)),
$this->createViolation('Static message', array(
'{{ value }}' => 'baz',
)),
));
}
// BC with Symfony < 2.4
public function testSingleStaticMethodBc()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback(array(
array(__CLASS__.'_Class', 'validateCallback'),
));
$this->validator->validate($object, $constraint);
$this->assertViolation('Callback message', array(
'{{ value }}' => 'foobar',
));
}
// BC with Symfony < 2.4
public function testSingleStaticMethodBcExplicitName()
{
$object = new CallbackValidatorTest_Object();
$constraint = new Callback(array(
'methods' => array(array(__CLASS__.'_Class', 'validateCallback')),
));
$this->validator->validate($object, $constraint);
$this->assertViolation('Callback message', array(
'{{ value }}' => 'foobar',
));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testExpectValidMethods()
{
$object = new CallbackValidatorTest_Object();
$this->validator->validate($object, new Callback(array('foobar')));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testExpectValidCallbacks()
{
$object = new CallbackValidatorTest_Object();
$this->validator->validate($object, new Callback(array(array('foo', 'bar'))));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testExpectEitherCallbackOrMethods()
{
$object = new CallbackValidatorTest_Object();
$this->validator->validate($object, new Callback(array(
'callback' => 'validate',
'methods' => array('validateStatic'),
)));
}
public function testConstraintGetTargets()
{
$constraint = new Callback(array('foo'));
$targets = array(Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT);
$this->assertEquals($targets, $constraint->getTargets());
}
// Should succeed. Needed when defining constraints as annotations.
public function testNoConstructorArguments()
{
new Callback();
}
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);
}
}