[Validator] fixed usage of deprecate Validator features

This commit is contained in:
Fabien Potencier 2015-01-09 07:49:26 +01:00
parent 91606b55b5
commit fb3f9d2d1c
9 changed files with 42 additions and 84 deletions

View File

@ -44,7 +44,11 @@ class DeprecationErrorHandler
$class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class']; $class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
$method = $trace[$i]['function']; $method = $trace[$i]['function'];
$type = 0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') || strpos($class, '\Legacy') ? 'legacy' : 'remaining'; $type =
0 === strpos($method, 'testLegacy')
|| 0 === strpos($method, 'provideLegacy')
|| strpos($class, '\Legacy')
? 'legacy' : 'remaining';
if ('legacy' === $type && 0 === (error_reporting() & E_USER_DEPRECATED)) { if ('legacy' === $type && 0 === (error_reporting() & E_USER_DEPRECATED)) {
@++$deprecations[$type]['Silenced']['count']; @++$deprecations[$type]['Silenced']['count'];

View File

@ -47,12 +47,15 @@ class Callback extends Constraint
$options = $options['value']; $options = $options['value'];
} }
if (is_array($options) && isset($options['methods'])) {
trigger_error('The "methods" option of the '.__CLASS__.' class is deprecated since version 2.4 and will be removed in 3.0. Use the "callback" option instead.', E_USER_DEPRECATED);
}
if (is_array($options) && !isset($options['callback']) && !isset($options['methods']) && !isset($options['groups'])) { if (is_array($options) && !isset($options['callback']) && !isset($options['methods']) && !isset($options['groups'])) {
if (is_callable($options)) { if (is_callable($options) || !$options) {
$options = array('callback' => $options); $options = array('callback' => $options);
} else { } else {
// BC with Symfony < 2.4 // @deprecated, to be removed in 3.0
trigger_error('The "methods" option of the '.__CLASS__.' class is deprecated since version 2.4 and will be removed in 3.0. Use the "callback" option instead.', E_USER_DEPRECATED);
$options = array('methods' => $options); $options = array('methods' => $options);
} }
} }

View File

@ -138,7 +138,7 @@ class ClassMetadata extends ElementMetadata implements ClassMetadataInterface
if (null === $propagatedGroup && Constraint::DEFAULT_GROUP === $group if (null === $propagatedGroup && Constraint::DEFAULT_GROUP === $group
&& ($this->hasGroupSequence() || $this->isGroupSequenceProvider())) { && ($this->hasGroupSequence() || $this->isGroupSequenceProvider())) {
if ($this->hasGroupSequence()) { if ($this->hasGroupSequence()) {
$groups = $this->getGroupSequence(); $groups = $this->getGroupSequence()->groups;
} else { } else {
$groups = $value->getGroupSequence(); $groups = $value->getGroupSequence();
} }
@ -479,7 +479,7 @@ class ClassMetadata extends ElementMetadata implements ClassMetadataInterface
*/ */
public function hasGroupSequence() public function hasGroupSequence()
{ {
return count($this->groupSequence) > 0; return $this->groupSequence && count($this->groupSequence->groups) > 0;
} }
/** /**

View File

@ -58,7 +58,7 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest
public function testNullIsValid() public function testNullIsValid()
{ {
$this->validator->validate(null, new Callback(array('foo'))); $this->validator->validate(null, new Callback());
$this->assertNoViolation(); $this->assertNoViolation();
} }
@ -186,7 +186,7 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest
} }
// BC with Symfony < 2.4 // BC with Symfony < 2.4
public function testSingleMethodBc() public function testLegacySingleMethodBc()
{ {
$object = new CallbackValidatorTest_Object(); $object = new CallbackValidatorTest_Object();
$constraint = new Callback(array('validate')); $constraint = new Callback(array('validate'));
@ -199,7 +199,7 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest
} }
// BC with Symfony < 2.4 // BC with Symfony < 2.4
public function testSingleMethodBcExplicitName() public function testLegacySingleMethodBcExplicitName()
{ {
$object = new CallbackValidatorTest_Object(); $object = new CallbackValidatorTest_Object();
$constraint = new Callback(array('methods' => array('validate'))); $constraint = new Callback(array('methods' => array('validate')));
@ -212,7 +212,7 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest
} }
// BC with Symfony < 2.4 // BC with Symfony < 2.4
public function testMultipleMethodsBc() public function testLegacyMultipleMethodsBc()
{ {
$object = new CallbackValidatorTest_Object(); $object = new CallbackValidatorTest_Object();
$constraint = new Callback(array('validate', 'validateStatic')); $constraint = new Callback(array('validate', 'validateStatic'));
@ -227,7 +227,7 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest
} }
// BC with Symfony < 2.4 // BC with Symfony < 2.4
public function testMultipleMethodsBcExplicitName() public function testLegacyMultipleMethodsBcExplicitName()
{ {
$object = new CallbackValidatorTest_Object(); $object = new CallbackValidatorTest_Object();
$constraint = new Callback(array( $constraint = new Callback(array(
@ -244,7 +244,7 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest
} }
// BC with Symfony < 2.4 // BC with Symfony < 2.4
public function testSingleStaticMethodBc() public function testLegacySingleStaticMethodBc()
{ {
$object = new CallbackValidatorTest_Object(); $object = new CallbackValidatorTest_Object();
$constraint = new Callback(array( $constraint = new Callback(array(
@ -259,7 +259,7 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest
} }
// BC with Symfony < 2.4 // BC with Symfony < 2.4
public function testSingleStaticMethodBcExplicitName() public function testLegacySingleStaticMethodBcExplicitName()
{ {
$object = new CallbackValidatorTest_Object(); $object = new CallbackValidatorTest_Object();
$constraint = new Callback(array( $constraint = new Callback(array(
@ -296,7 +296,7 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest
/** /**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/ */
public function testExpectEitherCallbackOrMethods() public function testLegacyExpectEitherCallbackOrMethods()
{ {
$object = new CallbackValidatorTest_Object(); $object = new CallbackValidatorTest_Object();
@ -308,7 +308,7 @@ class CallbackValidatorTest extends AbstractConstraintValidatorTest
public function testConstraintGetTargets() public function testConstraintGetTargets()
{ {
$constraint = new Callback(array('foo')); $constraint = new Callback(array());
$targets = array(Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT); $targets = array(Constraint::CLASS_CONSTRAINT, Constraint::PROPERTY_CONSTRAINT);
$this->assertEquals($targets, $constraint->getTargets()); $this->assertEquals($targets, $constraint->getTargets());

View File

@ -32,22 +32,28 @@ class GroupSequenceTest extends \PHPUnit_Framework_TestCase
$this->assertSame(array('Group 1', 'Group 2'), $sequence->groups); $this->assertSame(array('Group 1', 'Group 2'), $sequence->groups);
} }
public function testIterate() public function testLegacyIterate()
{ {
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
$sequence = new GroupSequence(array('Group 1', 'Group 2')); $sequence = new GroupSequence(array('Group 1', 'Group 2'));
$this->assertSame(array('Group 1', 'Group 2'), iterator_to_array($sequence)); $this->assertSame(array('Group 1', 'Group 2'), iterator_to_array($sequence));
} }
public function testCount() public function testLegacyCount()
{ {
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
$sequence = new GroupSequence(array('Group 1', 'Group 2')); $sequence = new GroupSequence(array('Group 1', 'Group 2'));
$this->assertCount(2, $sequence); $this->assertCount(2, $sequence);
} }
public function testArrayAccess() public function testLegacyArrayAccess()
{ {
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
$sequence = new GroupSequence(array('Group 1', 'Group 2')); $sequence = new GroupSequence(array('Group 1', 'Group 2'));
$this->assertSame('Group 1', $sequence[0]); $this->assertSame('Group 1', $sequence[0]);
@ -67,15 +73,19 @@ class GroupSequenceTest extends \PHPUnit_Framework_TestCase
/** /**
* @expectedException \Symfony\Component\Validator\Exception\OutOfBoundsException * @expectedException \Symfony\Component\Validator\Exception\OutOfBoundsException
*/ */
public function testGetExpectsExistingKey() public function testLegacyGetExpectsExistingKey()
{ {
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
$sequence = new GroupSequence(array('Group 1', 'Group 2')); $sequence = new GroupSequence(array('Group 1', 'Group 2'));
$sequence[2]; $sequence[2];
} }
public function testUnsetIgnoresNonExistingKeys() public function testLegacyUnsetIgnoresNonExistingKeys()
{ {
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
$sequence = new GroupSequence(array('Group 1', 'Group 2')); $sequence = new GroupSequence(array('Group 1', 'Group 2'));
// should not fail // should not fail

View File

@ -19,7 +19,7 @@ use Symfony\Component\Validator\Tests\Fixtures\Entity;
use Symfony\Component\Validator\Tests\Validator\AbstractLegacyApiTest; use Symfony\Component\Validator\Tests\Validator\AbstractLegacyApiTest;
use Symfony\Component\Validator\Validator as LegacyValidator; use Symfony\Component\Validator\Validator as LegacyValidator;
class ValidatorTest extends AbstractLegacyApiTest class LegacyValidatorTest extends AbstractLegacyApiTest
{ {
protected function createValidator(MetadataFactoryInterface $metadataFactory, array $objectInitializers = array()) protected function createValidator(MetadataFactoryInterface $metadataFactory, array $objectInitializers = array())
{ {

View File

@ -72,16 +72,16 @@ class MemberMetadataTest extends \PHPUnit_Framework_TestCase
public function testSerializeCollectionCascaded() public function testSerializeCollectionCascaded()
{ {
$this->metadata->addConstraint(new Valid(array('traverse' => true, 'deep' => false))); $this->metadata->addConstraint(new Valid(array('traverse' => true)));
$metadata = unserialize(serialize($this->metadata)); $metadata = unserialize(serialize($this->metadata));
$this->assertEquals($this->metadata, $metadata); $this->assertEquals($this->metadata, $metadata);
} }
public function testSerializeCollectionCascadedDeeply() public function testLegacySerializeCollectionCascadedDeeply()
{ {
$this->metadata->addConstraint(new Valid(array('traverse' => true, 'deep' => true))); $this->metadata->addConstraint(new Valid(array('traverse' => true)));
$metadata = unserialize(serialize($this->metadata)); $metadata = unserialize(serialize($this->metadata));

View File

@ -506,65 +506,6 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
$this->assertCount(0, $violations); $this->assertCount(0, $violations);
} }
public function testReferenceTraversalRecursionEnabledOnReferenceTraversalEnabledOnClass()
{
$entity = new Entity();
$entity->reference = new \ArrayIterator(array(
2 => new \ArrayIterator(array('key' => new Reference())),
));
$callback = function ($value, ExecutionContextInterface $context) {
$context->addViolation('Message');
};
$traversableMetadata = new ClassMetadata('ArrayIterator');
$traversableMetadata->addConstraint(new Traverse(true));
$this->metadataFactory->addMetadata($traversableMetadata);
$this->referenceMetadata->addConstraint(new Callback(array(
'callback' => $callback,
'groups' => 'Group',
)));
$this->metadata->addPropertyConstraint('reference', new Valid(array(
'deep' => true,
)));
$violations = $this->validate($entity, new Valid(), 'Group');
/** @var ConstraintViolationInterface[] $violations */
$this->assertCount(1, $violations);
}
public function testReferenceTraversalRecursionDisabledOnReferenceTraversalEnabledOnClass()
{
$test = $this;
$entity = new Entity();
$entity->reference = new \ArrayIterator(array(
2 => new \ArrayIterator(array('key' => new Reference())),
));
$callback = function ($value, ExecutionContextInterface $context) use ($test) {
$test->fail('Should not be called');
};
$traversableMetadata = new ClassMetadata('ArrayIterator');
$traversableMetadata->addConstraint(new Traverse(true));
$this->metadataFactory->addMetadata($traversableMetadata);
$this->referenceMetadata->addConstraint(new Callback(array(
'callback' => $callback,
'groups' => 'Group',
)));
$this->metadata->addPropertyConstraint('reference', new Valid(array(
'deep' => false,
)));
$violations = $this->validate($entity, new Valid(), 'Group');
/** @var ConstraintViolationInterface[] $violations */
$this->assertCount(0, $violations);
}
public function testAddCustomizedViolation() public function testAddCustomizedViolation()
{ {
$entity = new Entity(); $entity = new Entity();

View File

@ -766,7 +766,7 @@ abstract class AbstractValidatorTest extends \PHPUnit_Framework_TestCase
}; };
$this->metadata->addPropertyConstraint('reference', new Valid(array( $this->metadata->addPropertyConstraint('reference', new Valid(array(
'deep' => true, 'traverse' => true,
))); )));
$this->referenceMetadata->addConstraint(new Callback(array( $this->referenceMetadata->addConstraint(new Callback(array(
'callback' => $callback, 'callback' => $callback,