Skip to content

Commit e924230

Browse files
pierredupfabpot
authored andcommitted
Remove Validator\TypeTestCase and add validator logic to base TypeTestCase
1 parent bd954a6 commit e924230

7 files changed

+68
-44
lines changed
+40
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 <[email protected]>
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+
}

Test/TypeTestCase.php

+19
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'));

Tests/Extension/Validator/Type/BaseValidatorExtensionTest.php

+1
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
/**

Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php

+3
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(

Tests/Extension/Validator/Type/SubmitTypeValidatorExtensionTest.php

+4
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);

Tests/Extension/Validator/Type/TypeTestCase.php

-44
This file was deleted.

Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php

+1
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)