diff --git a/src/Symfony/Component/Form/Test/Traits/ValidatorExtensionTrait.php b/src/Symfony/Component/Form/Test/Traits/ValidatorExtensionTrait.php new file mode 100644 index 0000000000000..0724f697ba77a --- /dev/null +++ b/src/Symfony/Component/Form/Test/Traits/ValidatorExtensionTrait.php @@ -0,0 +1,40 @@ + + * + * 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); + } +} diff --git a/src/Symfony/Component/Form/Test/TypeTestCase.php b/src/Symfony/Component/Form/Test/TypeTestCase.php index ff3f78292f5f0..346dc61648896 100644 --- a/src/Symfony/Component/Form/Test/TypeTestCase.php +++ b/src/Symfony/Component/Form/Test/TypeTestCase.php @@ -13,6 +13,7 @@ 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 @@ protected function setUp() $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')); diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/BaseValidatorExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/BaseValidatorExtensionTest.php index f12b915b9a378..e83aaaa3491dc 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/BaseValidatorExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/BaseValidatorExtensionTest.php @@ -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; /** diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php index 2bf10bb36bce2..0dca9c476b56d 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php @@ -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( diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/SubmitTypeValidatorExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/SubmitTypeValidatorExtensionTest.php index 48fc8de51d9c8..015b9582b5764 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/SubmitTypeValidatorExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/SubmitTypeValidatorExtensionTest.php @@ -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); diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/TypeTestCase.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/TypeTestCase.php deleted file mode 100644 index cb6000aa8fb81..0000000000000 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/TypeTestCase.php +++ /dev/null @@ -1,44 +0,0 @@ - - * - * 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), - )); - } -} diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php index 8c0984f7e4e13..17792b67454f2 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/UploadValidatorExtensionTest.php @@ -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;