From 3e6dc191465c2703720a675fb72d59fe50847592 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Mon, 6 Mar 2017 13:30:35 +0200 Subject: [PATCH 1/9] Add docs about the validator TypeTestCase class --- form/unit_testing.rst | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/form/unit_testing.rst b/form/unit_testing.rst index 53d82d2171b..b89b2605626 100644 --- a/form/unit_testing.rst +++ b/form/unit_testing.rst @@ -157,12 +157,31 @@ before creating the parent form using the ``PreloadedExtension`` class:: be getting errors that are not related to the form you are currently testing but to its children. +Forms Using Validation +------------------------------------ + +If your forms uses the ``invalid_message`` or ``constraints`` option for validation, you need to +register the validation extension which provides this options. +Luckily Symfony provides a custom test class which does this for you. +In order to have this option registered, your test needs to extend from the +:class:`Symfony\\Component\\Form\\Tests\\Extension\\Validator\\Type\\TypeTestCase` +class:: + + // tests/AppBundle/Form/Type/TestedTypeTests.php + namespace Tests\AppBundle\Form\Type; + + use Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase; + + class TestedTypeTest extends TypeTestCase + { + // ... + } + Adding Custom Extensions ------------------------ It often happens that you use some options that are added by -:doc:`form extensions `. One of the -cases may be the ``ValidatorExtension`` with its ``invalid_message`` option. +:doc:`form extensions `. The ``TypeTestCase`` only loads the core form extension, which means an :class:`Symfony\\Component\\OptionsResolver\\Exception\\InvalidOptionsException` will be raised if you try to test a class that depends on other extensions. @@ -173,37 +192,32 @@ allows you to return a list of extensions to register:: namespace AppBundle\Tests\Form\Type; use AppBundle\Form\Type\TestedType; - use Symfony\Component\Form\Extension\Validator\ValidatorExtension; use Symfony\Component\Form\Form; use Symfony\Component\Form\Forms; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\Test\TypeTestCase; - use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Mapping\ClassMetadata; - use Symfony\Component\Validator\Validator\ValidatorInterface; class TestedTypeTest extends TypeTestCase { protected function getExtensions() { - $validator = $this->createMock(ValidatorInterface::class); // use getMock() on PHPUnit 5.3 or below // $validator = $this->getMock(ValidatorInterface::class); - $validator - ->method('validate') - ->will($this->returnValue(new ConstraintViolationList())); $validator ->method('getMetadataFor') ->will($this->returnValue(new ClassMetadata(Form::class))); - return array( - new ValidatorExtension($validator), + new MyFormExtension(), ); } // ... your tests } +It is also possible to load custom form types, form type extensions or type guessers using the +``getTypedExtensions``, ``getTypes`` and ``getTypeGuessers`` methods. + Testing against Different Sets of Data -------------------------------------- From d4979eb9f71b541eea4cdb6c47c17b68fb3660d7 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Mon, 6 Mar 2017 13:53:05 +0200 Subject: [PATCH 2/9] Fix typos --- form/unit_testing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/form/unit_testing.rst b/form/unit_testing.rst index b89b2605626..79b96fedcf2 100644 --- a/form/unit_testing.rst +++ b/form/unit_testing.rst @@ -163,7 +163,7 @@ Forms Using Validation If your forms uses the ``invalid_message`` or ``constraints`` option for validation, you need to register the validation extension which provides this options. Luckily Symfony provides a custom test class which does this for you. -In order to have this option registered, your test needs to extend from the +In order to have these options registered, your test needs to extend the :class:`Symfony\\Component\\Form\\Tests\\Extension\\Validator\\Type\\TypeTestCase` class:: From d17821add4a02812053274a1da2ed15d3491f49d Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Mon, 6 Mar 2017 16:31:34 +0200 Subject: [PATCH 3/9] Fix heading underlining --- form/unit_testing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/form/unit_testing.rst b/form/unit_testing.rst index 79b96fedcf2..152cd1a31ef 100644 --- a/form/unit_testing.rst +++ b/form/unit_testing.rst @@ -158,7 +158,7 @@ before creating the parent form using the ``PreloadedExtension`` class:: testing but to its children. Forms Using Validation ------------------------------------- +---------------------- If your forms uses the ``invalid_message`` or ``constraints`` option for validation, you need to register the validation extension which provides this options. From 070b2c07f082a812557589565f9177bb4b4cc61f Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Tue, 7 Mar 2017 12:00:38 +0200 Subject: [PATCH 4/9] Renamed TestedTypeTests to TestedTypeTest --- form/unit_testing.rst | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/form/unit_testing.rst b/form/unit_testing.rst index 152cd1a31ef..76909be8093 100644 --- a/form/unit_testing.rst +++ b/form/unit_testing.rst @@ -124,7 +124,7 @@ To create your form correctly, you need to make the type available to the form factory in your test. The easiest way is to register it manually before creating the parent form using the ``PreloadedExtension`` class:: - // src/AppBundle/Tests/Form/Type/TestedTypeTests.php + // src/AppBundle/Tests/Form/Type/TestedTypeTest.php namespace AppBundle\Tests\Form\Type; use AppBundle\Form\Type\TestedType; @@ -167,7 +167,7 @@ In order to have these options registered, your test needs to extend the :class:`Symfony\\Component\\Form\\Tests\\Extension\\Validator\\Type\\TypeTestCase` class:: - // tests/AppBundle/Form/Type/TestedTypeTests.php + // tests/AppBundle/Form/Type/TestedTypeTest.php namespace Tests\AppBundle\Form\Type; use Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase; @@ -188,7 +188,7 @@ will be raised if you try to test a class that depends on other extensions. The :method:`Symfony\\Component\\Form\\Test\\TypeTestCase::getExtensions` method allows you to return a list of extensions to register:: - // src/AppBundle/Tests/Form/Type/TestedTypeTests.php + // src/AppBundle/Tests/Form/Type/TestedTypeTest.php namespace AppBundle\Tests\Form\Type; use AppBundle\Form\Type\TestedType; @@ -215,16 +215,13 @@ allows you to return a list of extensions to register:: // ... your tests } -It is also possible to load custom form types, form type extensions or type guessers using the -``getTypedExtensions``, ``getTypes`` and ``getTypeGuessers`` methods. - Testing against Different Sets of Data -------------------------------------- If you are not familiar yet with PHPUnit's `data providers`_, this might be a good opportunity to use them:: - // src/AppBundle/Tests/Form/Type/TestedTypeTests.php + // src/AppBundle/Tests/Form/Type/TestedTypeTest.php namespace AppBundle\Tests\Form\Type; use AppBundle\Form\Type\TestedType; From cf0f9e6968f097081b284f86911352e63b81fce6 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Tue, 7 Mar 2017 12:02:29 +0200 Subject: [PATCH 5/9] Update fomr testing sentence --- form/unit_testing.rst | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/form/unit_testing.rst b/form/unit_testing.rst index 76909be8093..52764a3b145 100644 --- a/form/unit_testing.rst +++ b/form/unit_testing.rst @@ -160,11 +160,10 @@ before creating the parent form using the ``PreloadedExtension`` class:: Forms Using Validation ---------------------- -If your forms uses the ``invalid_message`` or ``constraints`` option for validation, you need to -register the validation extension which provides this options. -Luckily Symfony provides a custom test class which does this for you. -In order to have these options registered, your test needs to extend the -:class:`Symfony\\Component\\Form\\Tests\\Extension\\Validator\\Type\\TypeTestCase` +If your form uses the ``invalid_message`` or ``constraints`` option for validation, you need to +register the validation extension which provides these options. +Luckily Symfony provides a base test class which takes care of it, just extend +``Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase`` class:: // tests/AppBundle/Form/Type/TestedTypeTest.php From 6fda2df30c6597e998cc5f682fb327fe770f0cc5 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Tue, 7 Mar 2017 12:08:26 +0200 Subject: [PATCH 6/9] Removed extra class --- form/unit_testing.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/form/unit_testing.rst b/form/unit_testing.rst index 52764a3b145..64883922a35 100644 --- a/form/unit_testing.rst +++ b/form/unit_testing.rst @@ -163,8 +163,7 @@ Forms Using Validation If your form uses the ``invalid_message`` or ``constraints`` option for validation, you need to register the validation extension which provides these options. Luckily Symfony provides a base test class which takes care of it, just extend -``Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase`` -class:: +``Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase``:: // tests/AppBundle/Form/Type/TestedTypeTest.php namespace Tests\AppBundle\Form\Type; From f0887ae0c6b9ed6cedbbf8c2ce16aba5ecfb4785 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Sat, 24 Jun 2017 15:53:04 +0200 Subject: [PATCH 7/9] Remove section about forms using validation --- form/unit_testing.rst | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/form/unit_testing.rst b/form/unit_testing.rst index 64883922a35..38c718f3502 100644 --- a/form/unit_testing.rst +++ b/form/unit_testing.rst @@ -157,24 +157,6 @@ before creating the parent form using the ``PreloadedExtension`` class:: be getting errors that are not related to the form you are currently testing but to its children. -Forms Using Validation ----------------------- - -If your form uses the ``invalid_message`` or ``constraints`` option for validation, you need to -register the validation extension which provides these options. -Luckily Symfony provides a base test class which takes care of it, just extend -``Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase``:: - - // tests/AppBundle/Form/Type/TestedTypeTest.php - namespace Tests\AppBundle\Form\Type; - - use Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase; - - class TestedTypeTest extends TypeTestCase - { - // ... - } - Adding Custom Extensions ------------------------ From ff1bd8ee7384436c815c8458d7cd27a189afda64 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 10 Jan 2018 10:02:54 +0100 Subject: [PATCH 8/9] Reverted some changes --- form/unit_testing.rst | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/form/unit_testing.rst b/form/unit_testing.rst index 38c718f3502..171ea407eb9 100644 --- a/form/unit_testing.rst +++ b/form/unit_testing.rst @@ -161,7 +161,8 @@ Adding Custom Extensions ------------------------ It often happens that you use some options that are added by -:doc:`form extensions `. +:doc:`form extensions `. One of the +cases may be the ``ValidatorExtension`` with its ``invalid_message`` option. The ``TypeTestCase`` only loads the core form extension, which means an :class:`Symfony\\Component\\OptionsResolver\\Exception\\InvalidOptionsException` will be raised if you try to test a class that depends on other extensions. @@ -172,23 +173,30 @@ allows you to return a list of extensions to register:: namespace AppBundle\Tests\Form\Type; use AppBundle\Form\Type\TestedType; + use Symfony\Component\Form\Extension\Validator\ValidatorExtension; use Symfony\Component\Form\Form; use Symfony\Component\Form\Forms; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\Test\TypeTestCase; + use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Mapping\ClassMetadata; + use Symfony\Component\Validator\Validator\ValidatorInterface; class TestedTypeTest extends TypeTestCase { protected function getExtensions() { + $validator = $this->createMock(ValidatorInterface::class); // use getMock() on PHPUnit 5.3 or below // $validator = $this->getMock(ValidatorInterface::class); + $validator + ->method('validate') + ->will($this->returnValue(new ConstraintViolationList())); $validator ->method('getMetadataFor') ->will($this->returnValue(new ClassMetadata(Form::class))); return array( - new MyFormExtension(), + new ValidatorExtension($validator), ); } From c495b92e084b776944598310525848a1125bae93 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 10 Jan 2018 10:03:39 +0100 Subject: [PATCH 9/9] Readded a blank line --- form/unit_testing.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/form/unit_testing.rst b/form/unit_testing.rst index 171ea407eb9..4bd16d805c4 100644 --- a/form/unit_testing.rst +++ b/form/unit_testing.rst @@ -195,6 +195,7 @@ allows you to return a list of extensions to register:: $validator ->method('getMetadataFor') ->will($this->returnValue(new ClassMetadata(Form::class))); + return array( new ValidatorExtension($validator), );