Skip to content

Commit f56f28b

Browse files
committed
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 ------- 5ab5010 Remove Validator\TypeTestCase and add validator logic to base TypeTestCase
2 parents e5ddd14 + 5ab5010 commit f56f28b

File tree

7 files changed

+68
-44
lines changed

7 files changed

+68
-44
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Test\Traits;
13+
14+
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
15+
use Symfony\Component\Form\Test\TypeTestCase;
16+
use Symfony\Component\Validator\Mapping\ClassMetadata;
17+
use Symfony\Component\Validator\Validator\ValidatorInterface;
18+
19+
trait ValidatorExtensionTrait
20+
{
21+
protected $validator;
22+
23+
protected function getValidatorExtension()
24+
{
25+
if (!interface_exists(ValidatorInterface::class)) {
26+
throw new \Exception('In order to use the "ValidatorExtensionTrait", the symfony/validator component must be installed');
27+
}
28+
29+
if (!$this instanceof TypeTestCase) {
30+
throw new \Exception(sprintf('The trait "ValidatorExtensionTrait" can only be added to a class that extends %s', TypeTestCase::class));
31+
}
32+
33+
$this->validator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
34+
$metadata = $this->getMockBuilder(ClassMetadata::class)->disableOriginalConstructor()->getMock();
35+
$this->validator->expects($this->any())->method('getMetadataFor')->will($this->returnValue($metadata));
36+
$this->validator->expects($this->any())->method('validate')->will($this->returnValue(array()));
37+
38+
return new ValidatorExtension($this->validator);
39+
}
40+
}

src/Symfony/Component/Form/Test/TypeTestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\FormBuilder;
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
16+
use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;
1617

1718
abstract class TypeTestCase extends FormIntegrationTestCase
1819
{
@@ -34,6 +35,24 @@ protected function setUp()
3435
$this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory);
3536
}
3637

38+
protected function tearDown()
39+
{
40+
if (in_array(ValidatorExtensionTrait::class, class_uses($this))) {
41+
$this->validator = null;
42+
}
43+
}
44+
45+
protected function getExtensions()
46+
{
47+
$extensions = array();
48+
49+
if (in_array(ValidatorExtensionTrait::class, class_uses($this))) {
50+
$extensions[] = $this->getValidatorExtension();
51+
}
52+
53+
return $extensions;
54+
}
55+
3756
public static function assertDateTimeEquals(\DateTime $expected, \DateTime $actual)
3857
{
3958
self::assertEquals($expected->format('c'), $actual->format('c'));

src/Symfony/Component/Form/Tests/Extension/Validator/Type/BaseValidatorExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
1313

1414
use Symfony\Component\Form\Test\FormInterface;
15+
use Symfony\Component\Form\Test\TypeTestCase;
1516
use Symfony\Component\Validator\Constraints\GroupSequence;
1617

1718
/**

src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
1313

1414
use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension;
15+
use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;
1516
use Symfony\Component\Validator\Constraints\Valid;
1617
use Symfony\Component\Validator\ConstraintViolationList;
1718

1819
class FormTypeValidatorExtensionTest extends BaseValidatorExtensionTest
1920
{
21+
use ValidatorExtensionTrait;
22+
2023
public function testSubmitValidatesData()
2124
{
2225
$builder = $this->factory->createBuilder(

src/Symfony/Component/Form/Tests/Extension/Validator/Type/SubmitTypeValidatorExtensionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
1313

14+
use Symfony\Component\Form\Test\Traits\ValidatorExtensionTrait;
15+
1416
class SubmitTypeValidatorExtensionTest extends BaseValidatorExtensionTest
1517
{
18+
use ValidatorExtensionTrait;
19+
1620
protected function createForm(array $options = array())
1721
{
1822
return $this->factory->create('Symfony\Component\Form\Extension\Core\Type\SubmitType', null, $options);

src/Symfony/Component/Form/Tests/Extension/Validator/Type/TypeTestCase.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Symfony/Component/Form/Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Validator\Type;
1313

1414
use Symfony\Component\Form\Extension\Validator\Type\UploadValidatorExtension;
15+
use Symfony\Component\Form\Test\TypeTestCase;
1516
use Symfony\Component\OptionsResolver\OptionsResolver;
1617
use Symfony\Component\OptionsResolver\Options;
1718

0 commit comments

Comments
 (0)