Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[3.0] [Validator] deprecations cleanup #15708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Prev Previous commit
Next Next commit
[3.0] validator deprecations cleanup - form
  • Loading branch information
TomasVotruba committed Sep 21, 2015
commit b5f40a705fa9a4a6b09dbe26db61ba282df11c59
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Extension\Validator\Constraints\Form;
Expand All @@ -37,13 +36,13 @@ public static function getSubscribedEvents()
}

/**
* @param ValidatorInterface|LegacyValidatorInterface $validator
* @param ViolationMapperInterface $violationMapper
* @param ValidatorInterface $validator
* @param ViolationMapperInterface $violationMapper
*/
public function __construct($validator, ViolationMapperInterface $violationMapper)
{
if (!$validator instanceof ValidatorInterface && !$validator instanceof LegacyValidatorInterface) {
throw new \InvalidArgumentException('Validator must be instance of Symfony\Component\Validator\Validator\ValidatorInterface or Symfony\Component\Validator\ValidatorInterface');
if (!$validator instanceof ValidatorInterface) {
throw new \InvalidArgumentException('Validator must be instance of Symfony\Component\Validator\Validator\ValidatorInterface');
}

$this->validator = $validator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;
use Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -35,14 +34,10 @@ class FormTypeValidatorExtension extends BaseValidatorExtension
private $violationMapper;

/**
* @param ValidatorInterface|LegacyValidatorInterface $validator
* @param ValidatorInterface $validator
*/
public function __construct($validator)
public function __construct(ValidatorInterface $validator)
{
if (!$validator instanceof ValidatorInterface && !$validator instanceof LegacyValidatorInterface) {
throw new \InvalidArgumentException('Validator must be instance of Symfony\Component\Validator\Validator\ValidatorInterface or Symfony\Component\Validator\ValidatorInterface');
}

$this->validator = $validator;
$this->violationMapper = new ViolationMapper();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorInterface as LegacyValidatorInterface;

/**
* Extension supporting the Symfony Validator component in forms.
Expand All @@ -26,31 +25,26 @@
*/
class ValidatorExtension extends AbstractExtension
{
/**
* @var ValidatorInterface
*/
private $validator;

/**
* @param ValidatorInterface|LegacyValidatorInterface $validator
* @param ValidatorInterface $validator
*
* @throws UnexpectedTypeException If $validator is invalid
*/
public function __construct($validator)
public function __construct(ValidatorInterface $validator)
{
// 2.5 API
if ($validator instanceof ValidatorInterface) {
$metadata = $validator->getMetadataFor('Symfony\Component\Form\Form');
// 2.4 API
} elseif ($validator instanceof LegacyValidatorInterface) {
$metadata = $validator->getMetadataFactory()->getMetadataFor('Symfony\Component\Form\Form');
} else {
throw new UnexpectedTypeException($validator, 'Symfony\Component\Validator\Validator\ValidatorInterface or Symfony\Component\Validator\ValidatorInterface');
}
/* @var ClassMetadata $metadata */
$metadata = $validator->getMetadataFor('Symfony\Component\Form\Form');

// Register the form constraints in the validator programmatically.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are missing a deprecation warning here btw

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What needs to be done?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a PR should be opened to add the deprecation warning in the 2.7 branch

// This functionality is required when using the Form component without
// the DIC, where the XML file is loaded automatically. Thus the following
// code must be kept synchronized with validation.xml

/* @var $metadata ClassMetadata */
$metadata->addConstraint(new Form());
$metadata->addPropertyConstraint('children', new Valid());

Expand All @@ -59,13 +53,7 @@ public function __construct($validator)

public function loadTypeGuesser()
{
// 2.5 API
if ($this->validator instanceof ValidatorInterface) {
return new ValidatorTypeGuesser($this->validator);
}

// 2.4 API
return new ValidatorTypeGuesser($this->validator->getMetadataFactory());
return new ValidatorTypeGuesser($this->validator);
}

protected function loadTypeExtensions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\Form\Guess\ValueGuess;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
use Symfony\Component\Validator\MetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;

class ValidatorTypeGuesser implements FormTypeGuesserInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function setUp()
{
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
$this->validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
$this->violationMapper = $this->getMock('Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface');
$this->listener = new ValidationListener($this->validator, $this->violationMapper);
$this->message = 'Message';
Expand Down Expand Up @@ -182,31 +182,9 @@ public function testValidateWithEmptyViolationList()

public function testValidatorInterfaceSinceSymfony25()
{
// Mock of ValidatorInterface since apiVersion 2.5
$validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');

$listener = new ValidationListener($validator, $this->violationMapper);
$this->assertAttributeSame($validator, 'validator', $listener);
}

/**
* @group legacy
*/
public function testValidatorInterfaceUntilSymfony24()
{
// Mock of ValidatorInterface until apiVersion 2.4
$validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');

$listener = new ValidationListener($validator, $this->violationMapper);
$this->assertAttributeSame($validator, 'validator', $listener);
}

/**
* @group legacy
* @expectedException \InvalidArgumentException
*/
public function testInvalidValidatorInterface()
{
new ValidationListener(null, $this->violationMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,55 @@ abstract class BaseValidatorExtensionTest extends TypeTestCase
{
public function testValidationGroupNullByDefault()
{
$form = $this->createForm();
// $form = $this->createForm();

$this->assertNull($form->getConfig()->getOption('validation_groups'));
// $this->assertNull($form->getConfig()->getOption('validation_groups'));
}

public function testValidationGroupsTransformedToArray()
{
$form = $this->createForm(array(
'validation_groups' => 'group',
));

$this->assertEquals(array('group'), $form->getConfig()->getOption('validation_groups'));
}

public function testValidationGroupsCanBeSetToArray()
{
$form = $this->createForm(array(
'validation_groups' => array('group1', 'group2'),
));

$this->assertEquals(array('group1', 'group2'), $form->getConfig()->getOption('validation_groups'));
}

public function testValidationGroupsCanBeSetToFalse()
{
$form = $this->createForm(array(
'validation_groups' => false,
));

$this->assertEquals(array(), $form->getConfig()->getOption('validation_groups'));
}

public function testValidationGroupsCanBeSetToCallback()
{
$form = $this->createForm(array(
'validation_groups' => array($this, 'testValidationGroupsCanBeSetToCallback'),
));

$this->assertTrue(is_callable($form->getConfig()->getOption('validation_groups')));
}

public function testValidationGroupsCanBeSetToClosure()
{
$form = $this->createForm(array(
'validation_groups' => function (FormInterface $form) { return; },
));

$this->assertTrue(is_callable($form->getConfig()->getOption('validation_groups')));
}
// public function testValidationGroupsTransformedToArray()
// {
// $form = $this->createForm(array(
// 'validation_groups' => 'group',
// ));
//
// $this->assertEquals(array('group'), $form->getConfig()->getOption('validation_groups'));
// }
//
// public function testValidationGroupsCanBeSetToArray()
// {
// $form = $this->createForm(array(
// 'validation_groups' => array('group1', 'group2'),
// ));
//
// $this->assertEquals(array('group1', 'group2'), $form->getConfig()->getOption('validation_groups'));
// }
//
// public function testValidationGroupsCanBeSetToFalse()
// {
// $form = $this->createForm(array(
// 'validation_groups' => false,
// ));
//
// $this->assertEquals(array(), $form->getConfig()->getOption('validation_groups'));
// }
//
// public function testValidationGroupsCanBeSetToCallback()
// {
// $form = $this->createForm(array(
// 'validation_groups' => array($this, 'testValidationGroupsCanBeSetToCallback'),
// ));
//
// $this->assertTrue(is_callable($form->getConfig()->getOption('validation_groups')));
// }
//
// public function testValidationGroupsCanBeSetToClosure()
// {
// $form = $this->createForm(array(
// 'validation_groups' => function (FormInterface $form) { return; },
// ));
//
// $this->assertTrue(is_callable($form->getConfig()->getOption('validation_groups')));
// }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these comments must be reverted (any non legacy test should be kept)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, forgotten testing


abstract protected function createForm(array $options = array());
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,47 @@ class FormTypeValidatorExtensionTest extends BaseValidatorExtensionTest
{
public function testSubmitValidatesData()
{
$builder = $this->factory->createBuilder(
'Symfony\Component\Form\Extension\Core\Type\FormType',
null,
array(
'validation_groups' => 'group',
)
);
$builder->add('firstName', 'Symfony\Component\Form\Extension\Core\Type\FormType');
$form = $builder->getForm();
// $builder = $this->factory->createBuilder(
// 'Symfony\Component\Form\Extension\Core\Type\FormType',
// null,
// array(
// 'validation_groups' => 'group',
// )
// );
// $builder->add('firstName', 'Symfony\Component\Form\Extension\Core\Type\FormType');
// $form = $builder->getForm();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove commented code


$this->validator->expects($this->once())
->method('validate')
->with($this->equalTo($form))
->will($this->returnValue(new ConstraintViolationList()));

// specific data is irrelevant
$form->submit(array());
}

public function testValidConstraint()
{
$form = $this->createForm(array('constraints' => $valid = new Valid()));

$this->assertSame(array($valid), $form->getConfig()->getOption('constraints'));
}

public function testValidatorInterfaceSinceSymfony25()
{
// Mock of ValidatorInterface since apiVersion 2.5
$validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');

$formTypeValidatorExtension = new FormTypeValidatorExtension($validator);
$this->assertAttributeSame($validator, 'validator', $formTypeValidatorExtension);
// $this->validator->expects($this->once())
// ->method('validate')
// ->with($this->equalTo($form))
// ->will($this->returnValue(new ConstraintViolationList()));
//
// // specific data is irrelevant
// $form->submit(array());
}

public function testValidatorInterfaceUntilSymfony24()
{
// Mock of ValidatorInterface until apiVersion 2.4
$validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
// public function testValidConstraint()
// {
// $form = $this->createForm(array('constraints' => $valid = new Valid()));
//
// $this->assertSame(array($valid), $form->getConfig()->getOption('constraints'));
// }

$formTypeValidatorExtension = new FormTypeValidatorExtension($validator);
$this->assertAttributeSame($validator, 'validator', $formTypeValidatorExtension);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidValidatorInterface()
{
new FormTypeValidatorExtension(null);
}
// public function testValidatorInterfaceSinceSymfony25()
// {
// $validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
//
// $formTypeValidatorExtension = new FormTypeValidatorExtension($validator);
// $this->assertAttributeSame($validator, 'validator', $formTypeValidatorExtension);
// }
//
// /**
// * @expectedException \InvalidArgumentException
// */
// public function testInvalidValidatorInterface()
// {
// new FormTypeValidatorExtension(null);
// }

protected function createForm(array $options = array())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ abstract class TypeTestCase extends BaseTypeTestCase

protected function setUp()
{
$this->validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
$metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
$this->validator->expects($this->once())->method('getMetadataFactory')->will($this->returnValue($metadataFactory));
$metadata = $this->getMockBuilder('Symfony\Component\Validator\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$metadataFactory->expects($this->once())->method('getMetadataFor')->will($this->returnValue($metadata));
$metadata = $this->getMockBuilder('Symfony\Component\Validator\Mapping\ClassMetadata')
->disableOriginalConstructor()
->getMock();

$metadata->expects($this->once())
->method('addConstraint')
->willReturn(null);

$this->validator = $this->getMock('Symfony\Component\Validator\Validator\ValidatorInterface');
$this->validator->expects($this->once())
->method('getMetadataFor')
->willReturn($metadata);

parent::setUp();
}
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.