feature #21960 Remove Validator\TypeTestCase and add validator logic to base TypeTestCase (pierredup)

This PR was squashed before being merged into the 3.4 branch (closes #21960).

Discussion
----------

Remove Validator\TypeTestCase and add validator logic to base TypeTestCase

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no/possibly
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | symfony/symfony-docs#7587

Based on a discussion in the docs, users should not really extend classes in the `Tests` namespace. This means that if you want to unit test forms, you need to extend the `Test\TypeTestCase` class which doesn't have the validation logic (Not adding the validator extension gives an error if you use the `constraints` option in your forms).
So I propose to remove the `Validator\TypeTestCase` class (or it can possibly be deprecated if it is wrongly used by someone), and add the validator extension logic to the base `TypTestCase` class.

The benefit is that there is only one class to extend (both for internal or userland tests), and it makes it easy to add more core extensions if necessary.

The one part that I don't like too much at the moment, is keeping the extension in the `getExtensions` method. This means that you extend the class and need to register custom extensions, that you would need to do `return array_merge(parent::getExtensions(), ... ` (only in the case when you want core extensions enabled). So I don't know if we rather want to add a private method like `getCoreExtensions`?

Commits
-------

5ab50103ae Remove Validator\TypeTestCase and add validator logic to base TypeTestCase
This commit is contained in:
Fabien Potencier 2017-09-27 09:12:49 -07:00
commit f56f28b0f3
7 changed files with 68 additions and 44 deletions

View File

@ -0,0 +1,40 @@
<?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\Form\Test\Traits;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Validator\ValidatorInterface;
trait ValidatorExtensionTrait
{
protected $validator;
protected function getValidatorExtension()
{
if (!interface_exists(ValidatorInterface::class)) {
throw new \Exception('In order to use the "ValidatorExtensionTrait", the symfony/validator component must be installed');
}
if (!$this instanceof TypeTestCase) {
throw new \Exception(sprintf('The trait "ValidatorExtensionTrait" can only be added to a class that extends %s', TypeTestCase::class));
}
$this->validator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
$metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
$this->validator->expects($this->any())->method('getMetadataFor')->will($this->returnValue($metadata));
$this->validator->expects($this->any())->method('validate')->will($this->returnValue(array()));
return new ValidatorExtension($this->validator);
}
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Form\Test;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;
abstract class TypeTestCase extends FormIntegrationTestCase
{
@ -34,6 +35,24 @@ abstract class TypeTestCase extends FormIntegrationTestCase
$this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory);
}
protected function tearDown()
{
if (in_array(ValidatorExtensionTrait::class, class_uses($this))) {
$this->validator = null;
}
}
protected function getExtensions()
{
$extensions = array();
if (in_array(ValidatorExtensionTrait::class, class_uses($this))) {
$extensions[] = $this->getValidatorExtension();
}
return $extensions;
}
public static function assertDateTimeEquals(\DateTime $expected, \DateTime $actual)
{
self::assertEquals($expected->format('c'), $actual->format('c'));

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
use Symfony\Component\Form\Test\FormInterface;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Validator\Constraints\GroupSequence;
/**

View File

@ -12,11 +12,14 @@
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension;
use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintViolationList;
class FormTypeValidatorExtensionTest extends BaseValidatorExtensionTest
{
use ValidatorExtensionTrait;
public function testSubmitValidatesData()
{
$builder = $this->factory->createBuilder(

View File

@ -11,8 +11,12 @@
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;
class SubmitTypeValidatorExtensionTest extends BaseValidatorExtensionTest
{
use ValidatorExtensionTrait;
protected function createForm(array $options = array())
{
return $this->factory->create('Symfony\Component\Form\Extension\Core\Type\SubmitType', null, $options);

View File

@ -1,44 +0,0 @@
<?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\Form\Tests\Extension\Validator\Type;
use Symfony\Component\Form\Test\TypeTestCase as BaseTypeTestCase;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
abstract class TypeTestCase extends BaseTypeTestCase
{
protected $validator;
protected function setUp()
{
$this->validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock();
$metadata = $this->getMockBuilder('Symfony\Component\Validator\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$this->validator->expects($this->once())->method('getMetadataFor')->will($this->returnValue($metadata));
$this->validator->expects($this->any())->method('validate')->will($this->returnValue(array()));
parent::setUp();
}
protected function tearDown()
{
$this->validator = null;
parent::tearDown();
}
protected function getExtensions()
{
return array_merge(parent::getExtensions(), array(
new ValidatorExtension($this->validator),
));
}
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
use Symfony\Component\Form\Extension\Validator\Type\UploadValidatorExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\Options;