From 50905ff253e5e1164b377ad5f285d1c439e7bdb7 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Tue, 7 May 2013 22:32:26 +0200 Subject: [PATCH 1/6] Adding lazy services documentation as of symfony/symfony#7890 --- components/dependency_injection/index.rst | 1 + .../dependency_injection/lazy_services.rst | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 components/dependency_injection/lazy_services.rst diff --git a/components/dependency_injection/index.rst b/components/dependency_injection/index.rst index e1d6b0eab8d..b3960eb8e00 100644 --- a/components/dependency_injection/index.rst +++ b/components/dependency_injection/index.rst @@ -14,5 +14,6 @@ configurators parentservices advanced + lazy_services workflow diff --git a/components/dependency_injection/lazy_services.rst b/components/dependency_injection/lazy_services.rst new file mode 100644 index 00000000000..ca97b5cad24 --- /dev/null +++ b/components/dependency_injection/lazy_services.rst @@ -0,0 +1,68 @@ +.. index:: + single: Dependency Injection; Lazy Services + +Lazy Services +============= + +.. versionadded:: 2.3 + Lazy services were added in Symfony 2.3. + +Configuring lazy services +------------------------- + +In some particular cases where a very heavy service is always requested, +but not always used, you may want to mark it as ``lazy`` to delay its instantiation. + +In order to have services to lazily instantiate, you will first need to install +the `ProxyManager bridge`_:: + + php composer.phar require symfony/proxy-manager-bridge:2.3.* + +You can mark the service as ``lazy`` by manipulating its definitions: + + +.. configuration-block:: + + .. code-block:: yaml + + services: + foo: + class: Example\Foo + lazy: true + + .. code-block:: xml + + + + .. code-block:: php + + $definition = new Definition('Example\Foo'); + $definition->setLazy(true); + $container->setDefinition('foo', $definition); + +You can then require the service from the container:: + + $service = $container->get($serviceId); + +At this point the retrieved ``$service`` should be a virtual `proxy`_ with the same +signature of the class representing the service. + +.. note:: + + If you don't install the `ProxyManager bridge`_, the container will just skip + over the ``lazy`` flag and simply instantiate the service as it would normally do. + +The proxy gets initialized and the actual service is instantiated as soon as you interact +in any way with this object. + +Additional resources +-------------------- + + +You can read more about how proxies are instantiated, generated and initialized in +the `documentation of ProxyManager`_. + + +.. _`ProxyManager bridge`: https://github.com/symfony/symfony/tree/2.3/src/Symfony/Bridge/ProxyManager +.. _`proxy`: http://en.wikipedia.org/wiki/Proxy_pattern +.. _`documentation of ProxyManager`: https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md \ No newline at end of file From c3c3e98bab34427db516ab391bca73dbd0b04a3a Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Wed, 8 May 2013 18:35:49 +0200 Subject: [PATCH 2/6] [Components][Console] Fixed typos for table helper --- components/console/helpers/tablehelper.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/console/helpers/tablehelper.rst b/components/console/helpers/tablehelper.rst index 04301af5048..1145dd7b603 100644 --- a/components/console/helpers/tablehelper.rst +++ b/components/console/helpers/tablehelper.rst @@ -48,8 +48,8 @@ You can also control table rendering by setting custom rendering option values: * :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setPaddingChar` * :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setHorizontalBorderChar` * :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setVerticalBorderChar` -* :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setVrossingChar` -* :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setVellHeaderFormat` -* :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setVellRowFormat` +* :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setCrossingChar` +* :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setCellHeaderFormat` +* :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setCellRowFormat` * :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setBorderFormat` * :method:`Symfony\\Component\\Console\\Helper\\TableHelper::setPadType` From 931091d8e3aa0b9d4f1393d313388d5ee1a5d47d Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 9 May 2013 20:18:51 +0200 Subject: [PATCH 3/6] Applying changes suggested by @WouterJ, adding lazy_services to components map --- .../dependency_injection/lazy_services.rst | 17 ++++++++--------- components/map.rst.inc | 1 + 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/components/dependency_injection/lazy_services.rst b/components/dependency_injection/lazy_services.rst index ca97b5cad24..599d503f7e6 100644 --- a/components/dependency_injection/lazy_services.rst +++ b/components/dependency_injection/lazy_services.rst @@ -14,35 +14,35 @@ In some particular cases where a very heavy service is always requested, but not always used, you may want to mark it as ``lazy`` to delay its instantiation. In order to have services to lazily instantiate, you will first need to install -the `ProxyManager bridge`_:: +the `ProxyManager bridge`_: - php composer.phar require symfony/proxy-manager-bridge:2.3.* +.. code-block:: bash + $ php composer.phar require symfony/proxy-manager-bridge:2.3.* You can mark the service as ``lazy`` by manipulating its definitions: - .. configuration-block:: .. code-block:: yaml services: foo: - class: Example\Foo + class: Acme\Foo lazy: true .. code-block:: xml - + .. code-block:: php - $definition = new Definition('Example\Foo'); + $definition = new Definition('Acme\Foo'); $definition->setLazy(true); $container->setDefinition('foo', $definition); You can then require the service from the container:: - $service = $container->get($serviceId); + $service = $container->get('foo'); At this point the retrieved ``$service`` should be a virtual `proxy`_ with the same signature of the class representing the service. @@ -55,10 +55,9 @@ signature of the class representing the service. The proxy gets initialized and the actual service is instantiated as soon as you interact in any way with this object. -Additional resources +Additional Resources -------------------- - You can read more about how proxies are instantiated, generated and initialized in the `documentation of ProxyManager`_. diff --git a/components/map.rst.inc b/components/map.rst.inc index e29df506e55..d360c65a706 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -39,6 +39,7 @@ * :doc:`/components/dependency_injection/configurators` * :doc:`/components/dependency_injection/parentservices` * :doc:`/components/dependency_injection/advanced` + * :doc:`/components/dependency_injection/lazy_services` * :doc:`/components/dependency_injection/workflow` * **DOM Crawler** From 438c8244e04e8c9ad818d93f22699ab5c8aa0289 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Wed, 15 May 2013 13:01:26 +0200 Subject: [PATCH 4/6] Made the Icu component compatible with ICU 3.8 --- components/intl.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/intl.rst b/components/intl.rst index cf42b9778d5..e9c5028d4c2 100644 --- a/components/intl.rst +++ b/components/intl.rst @@ -72,7 +72,7 @@ expose them manually by adding the following lines to your autoload code:: but usually Composer does this for you automatically: * 1.0.*: when the intl extension is not available - * 1.1.*: when intl is compiled with ICU 4.0 or higher + * 1.1.*: when intl is compiled with ICU 3.8 or higher * 1.2.*: when intl is compiled with ICU 4.4 or higher These versions are important when you deploy your application to a **server with From d7ea3a540a5dc425bf1d16f4113746975404da38 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Wed, 15 May 2013 22:04:52 -0500 Subject: [PATCH 5/6] [#2619] Tweaks for new proxy/lazy services entry --- .../dependency_injection/lazy_services.rst | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/components/dependency_injection/lazy_services.rst b/components/dependency_injection/lazy_services.rst index 599d503f7e6..88ad0c51f59 100644 --- a/components/dependency_injection/lazy_services.rst +++ b/components/dependency_injection/lazy_services.rst @@ -7,19 +7,41 @@ Lazy Services .. versionadded:: 2.3 Lazy services were added in Symfony 2.3. -Configuring lazy services -------------------------- +Why Lazy Services? +------------------ -In some particular cases where a very heavy service is always requested, -but not always used, you may want to mark it as ``lazy`` to delay its instantiation. +In some cases, you may want to inject a service that is a bit heavy to instantiate, +but is not always used inside your object. For example, imagine you have +a ``NewsletterManager`` and you inject a ``mailer`` service into it. Only +a few methods on your ``NewsletterManager`` actually use the ``mailer``, +but even when you don't need it, a ``mailer`` service is always instantiated +in order to construct your ``NewsletterManager``. -In order to have services to lazily instantiate, you will first need to install +Configuring lazy services is one answer to this. With a lazy service, a "proxy" +of the ``mailer`` service is actually injected. It looks and acts just like +the ``mailer``, except that the ``mailer`` isn't actually instantiated until +you interact with the proxy in some way. + +Installation +------------ + +In order to use the lazy service instantiation, you will first need to install the `ProxyManager bridge`_: .. code-block:: bash + $ php composer.phar require symfony/proxy-manager-bridge:2.3.* -You can mark the service as ``lazy`` by manipulating its definitions: +.. note:: + + If you're using the full-stack framework, this package is not included + and needs to be added to ``composer.json`` and installed (which is what + the above command does). + +Configuration +------------- + +You can mark the service as ``lazy`` by manipulating its definition: .. configuration-block:: @@ -44,24 +66,27 @@ You can then require the service from the container:: $service = $container->get('foo'); -At this point the retrieved ``$service`` should be a virtual `proxy`_ with the same -signature of the class representing the service. +At this point the retrieved ``$service`` should be a virtual `proxy`_ with +the same signature of the class representing the service. You can also inject +the service just like normal into other services. The object that's actually +injected will be the proxy. .. note:: - If you don't install the `ProxyManager bridge`_, the container will just skip - over the ``lazy`` flag and simply instantiate the service as it would normally do. + If you don't install the `ProxyManager bridge`_, the container will just + skip over the ``lazy`` flag and simply instantiate the service as it would + normally do. -The proxy gets initialized and the actual service is instantiated as soon as you interact -in any way with this object. +The proxy gets initialized and the actual service is instantiated as soon +as you interact in any way with this object. Additional Resources -------------------- -You can read more about how proxies are instantiated, generated and initialized in -the `documentation of ProxyManager`_. +You can read more about how proxies are instantiated, generated and initialized +in the `documentation of ProxyManager`_. -.. _`ProxyManager bridge`: https://github.com/symfony/symfony/tree/2.3/src/Symfony/Bridge/ProxyManager +.. _`ProxyManager bridge`: https://github.com/symfony/symfony/tree/master/src/Symfony/Bridge/ProxyManager .. _`proxy`: http://en.wikipedia.org/wiki/Proxy_pattern .. _`documentation of ProxyManager`: https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md \ No newline at end of file From 314f5eed9839ba396d538c29a2a61a8d81777b2f Mon Sep 17 00:00:00 2001 From: Ulrich Date: Thu, 13 Jun 2013 16:04:34 +0200 Subject: [PATCH 6/6] Update how to add custom Type New in sf2.3, update the way we have to use to add custom Type. --- cookbook/form/unit_testing.rst | 499 +++++++++++++++++---------------- 1 file changed, 253 insertions(+), 246 deletions(-) diff --git a/cookbook/form/unit_testing.rst b/cookbook/form/unit_testing.rst index 8522f39efa9..9cdd6cd71f6 100644 --- a/cookbook/form/unit_testing.rst +++ b/cookbook/form/unit_testing.rst @@ -1,246 +1,253 @@ -.. index:: - single: Form; Form testing - -How to Unit Test your Forms -=========================== - -The Form Component consists of 3 core objects: a form type (implementing -:class:`Symfony\\Component\\Form\\FormTypeInterface`), the -:class:`Symfony\\Component\\Form\\Form` and the -:class:`Symfony\\Component\\Form\\FormView`. - -The only class that is usually manipulated by programmers is the form type class -which serves as a form blueprint. It is used to generate the ``Form`` and the -``FormView``. You could test it directly by mocking its interactions with the -factory but it would be complex. It is better to pass it to FormFactory like it -is done in a real application. It is simple to bootstrap and you can trust -the Symfony components enough to use them as a testing base. - -There is already a class that you can benefit from for simple FormTypes -testing: :class:`Symfony\\Component\\Form\\Test\\TypeTestCase`. It is used to -test the core types and you can use it to test your types too. - -.. versionadded:: 2.3 - The ``TypeTestCase`` has moved to the ``Symfony\Component\Form\Test`` - namespace in 2.3. Previously, the class was located in - ``Symfony\Component\Form\Tests\Core\Extension\Type``. - -The Basics ----------- - -The simplest ``TypeTestCase`` implementation looks like the following:: - - // src/Acme/TestBundle/Tests/Form/Type/TestedTypeTests.php - namespace Acme\TestBundle\Tests\Form\Type; - - use Acme\TestBundle\Form\Type\TestedType; - use Acme\TestBundle\Model\TestObject; - use Symfony\Component\Form\Test\TypeTestCase; - - class TestedTypeTest extends TypeTestCase - { - public function testSubmitValidData() - { - $formData = array( - 'test' => 'test', - 'test2' => 'test2', - ); - - $type = new TestedType(); - $form = $this->factory->create($type); - - $object = new TestObject(); - $object->fromArray($formData); - - // submit the data to the form directly - $form->submit($formData); - - $this->assertTrue($form->isSynchronized()); - $this->assertEquals($object, $form->getData()); - - $view = $form->createView(); - $children = $view->children; - - foreach (array_keys($formData) as $key) { - $this->assertArrayHasKey($key, $children); - } - } - } - -So, what does it test? Let's explain it line by line. - -First you verify if the ``FormType`` compiles. This includes basic class -inheritance, the ``buildForm`` function and options resolution. This should -be the first test you write:: - - $type = new TestedType(); - $form = $this->factory->create($type); - -This test checks that none of your data transformers used by the form -failed. The :method:`Symfony\\Component\\Form\\FormInterface::isSynchronized`` -method is only set to ``false`` if a data transformer throws an exception:: - - $form->submit($formData); - $this->assertTrue($form->isSynchronized()); - -.. note:: - - Don't test the validation: it is applied by a listener that is not - active in the test case and it relies on validation configuration. - Instead, unit test your custom constraints directly. - -Next, verify the submission and mapping of the form. The test below -checks if all the fields are correctly specified:: - - $this->assertEquals($object, $form->getData()); - -Finally, check the creation of the ``FormView``. You should check if all -widgets you want to display are available in the children property:: - - $view = $form->createView(); - $children = $view->children; - - foreach (array_keys($formData) as $key) { - $this->assertArrayHasKey($key, $children); - } - -Adding a Type your Form depends on ----------------------------------- - -Your form may depend on other types that are defined as services. It -might look like this:: - - // src/Acme/TestBundle/Form/Type/TestedType.php - - // ... the buildForm method - $builder->add('acme_test_child_type'); - -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:: - - // src/Acme/TestBundle/Tests/Form/Type/TestedTypeTests.php - namespace Acme\TestBundle\Tests\Form\Type; - - use Acme\TestBundle\Form\Type\TestedType; - use Acme\TestBundle\Model\TestObject; - use Symfony\Component\Form\Test\TypeTestCase; - - class TestedTypeTest extends TypeTestCase - { - public function testSubmitValidData() - { - $this->factory->addType(new TestChildType()); - - $type = new TestedType(); - $form = $this->factory->create($type); - - // ... your test - } - } - -.. caution:: - - Make sure the child type you add is well tested. Otherwise you may - be getting errors that are not related to the form you are currently - testing but to its children. - -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. -The ``TypeTestCase`` loads only the core form extension so an "Invalid option" -exception will be raised if you try to use it for testing a class that depends -on other extensions. You need add those extensions to the factory object:: - - // src/Acme/TestBundle/Tests/Form/Type/TestedTypeTests.php - namespace Acme\TestBundle\Tests\Form\Type; - - use Acme\TestBundle\Form\Type\TestedType; - use Acme\TestBundle\Model\TestObject; - use Symfony\Component\Form\Test\TypeTestCase; - - class TestedTypeTest extends TypeTestCase - { - protected function setUp() - { - parent::setUp(); - - $this->factory = Forms::createFormFactoryBuilder() - ->addTypeExtension( - new FormTypeValidatorExtension( - $this->getMock('Symfony\Component\Validator\ValidatorInterface') - ) - ) - ->addTypeGuesser( - $this->getMockBuilder( - 'Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser' - ) - ->disableOriginalConstructor() - ->getMock() - ) - ->getFormFactory(); - - $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); - $this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory); - } - - // ... your tests - } - -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/Acme/TestBundle/Tests/Form/Type/TestedTypeTests.php - namespace Acme\TestBundle\Tests\Form\Type; - - use Acme\TestBundle\Form\Type\TestedType; - use Acme\TestBundle\Model\TestObject; - use Symfony\Component\Form\Test\TypeTestCase; - - class TestedTypeTest extends TypeTestCase - { - - /** - * @dataProvider getValidTestData - */ - public function testForm($data) - { - // ... your test - } - - public function getValidTestData() - { - return array( - array( - 'data' => array( - 'test' => 'test', - 'test2' => 'test2', - ), - ), - array( - 'data' => array(), - ), - array( - 'data' => array( - 'test' => null, - 'test2' => null, - ), - ), - ); - } - } - -The code above will run your test three times with 3 different sets of -data. This allows for decoupling the test fixtures from the tests and -easily testing against multiple sets of data. - -You can also pass another argument, such as a boolean if the form has to -be synchronized with the given set of data or not etc. - -.. _`data providers`: http://www.phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers +.. index:: + single: Form; Form testing + +How to Unit Test your Forms +=========================== + +The Form Component consists of 3 core objects: a form type (implementing +:class:`Symfony\\Component\\Form\\FormTypeInterface`), the +:class:`Symfony\\Component\\Form\\Form` and the +:class:`Symfony\\Component\\Form\\FormView`. + +The only class that is usually manipulated by programmers is the form type class +which serves as a form blueprint. It is used to generate the ``Form`` and the +``FormView``. You could test it directly by mocking its interactions with the +factory but it would be complex. It is better to pass it to FormFactory like it +is done in a real application. It is simple to bootstrap and you can trust +the Symfony components enough to use them as a testing base. + +There is already a class that you can benefit from for simple FormTypes +testing: :class:`Symfony\\Component\\Form\\Test\\TypeTestCase`. It is used to +test the core types and you can use it to test your types too. + +.. versionadded:: 2.3 + The ``TypeTestCase`` has moved to the ``Symfony\Component\Form\Test`` + namespace in 2.3. Previously, the class was located in + ``Symfony\Component\Form\Tests\Core\Extension\Type``. + +The Basics +---------- + +The simplest ``TypeTestCase`` implementation looks like the following:: + + // src/Acme/TestBundle/Tests/Form/Type/TestedTypeTests.php + namespace Acme\TestBundle\Tests\Form\Type; + + use Acme\TestBundle\Form\Type\TestedType; + use Acme\TestBundle\Model\TestObject; + use Symfony\Component\Form\Test\TypeTestCase; + + class TestedTypeTest extends TypeTestCase + { + public function testSubmitValidData() + { + $formData = array( + 'test' => 'test', + 'test2' => 'test2', + ); + + $type = new TestedType(); + $form = $this->factory->create($type); + + $object = new TestObject(); + $object->fromArray($formData); + + // submit the data to the form directly + $form->submit($formData); + + $this->assertTrue($form->isSynchronized()); + $this->assertEquals($object, $form->getData()); + + $view = $form->createView(); + $children = $view->children; + + foreach (array_keys($formData) as $key) { + $this->assertArrayHasKey($key, $children); + } + } + } + +So, what does it test? Let's explain it line by line. + +First you verify if the ``FormType`` compiles. This includes basic class +inheritance, the ``buildForm`` function and options resolution. This should +be the first test you write:: + + $type = new TestedType(); + $form = $this->factory->create($type); + +This test checks that none of your data transformers used by the form +failed. The :method:`Symfony\\Component\\Form\\FormInterface::isSynchronized`` +method is only set to ``false`` if a data transformer throws an exception:: + + $form->submit($formData); + $this->assertTrue($form->isSynchronized()); + +.. note:: + + Don't test the validation: it is applied by a listener that is not + active in the test case and it relies on validation configuration. + Instead, unit test your custom constraints directly. + +Next, verify the submission and mapping of the form. The test below +checks if all the fields are correctly specified:: + + $this->assertEquals($object, $form->getData()); + +Finally, check the creation of the ``FormView``. You should check if all +widgets you want to display are available in the children property:: + + $view = $form->createView(); + $children = $view->children; + + foreach (array_keys($formData) as $key) { + $this->assertArrayHasKey($key, $children); + } + +Adding a Type your Form depends on +---------------------------------- + +Your form may depend on other types that are defined as services. It +might look like this:: + + // src/Acme/TestBundle/Form/Type/TestedType.php + + // ... the buildForm method + $builder->add('acme_test_child_type'); + +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 PreloadedExtension class:: + + // src/Acme/TestBundle/Tests/Form/Type/TestedTypeTests.php + namespace Acme\TestBundle\Tests\Form\Type; + + use Acme\TestBundle\Form\Type\TestedType; + use Acme\TestBundle\Model\TestObject; + use Symfony\Component\Form\Test\TypeTestCase; + use Symfony\Component\Form\PreloadedExtension; + + class TestedTypeTest extends TypeTestCase + { + protected function getExtensions() + { + $childType = new TestChildType(); + return array(new PreloadedExtension(array( + $childType->getName() => $childType + ), array())); + } + + public function testSubmitValidData() + { + $type = new TestedType(); + $form = $this->factory->create($type); + + // ... your test + } + } + +.. caution:: + + Make sure the child type you add is well tested. Otherwise you may + be getting errors that are not related to the form you are currently + testing but to its children. + +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. +The ``TypeTestCase`` loads only the core form extension so an "Invalid option" +exception will be raised if you try to use it for testing a class that depends +on other extensions. You need add those extensions to the factory object:: + + // src/Acme/TestBundle/Tests/Form/Type/TestedTypeTests.php + namespace Acme\TestBundle\Tests\Form\Type; + + use Acme\TestBundle\Form\Type\TestedType; + use Acme\TestBundle\Model\TestObject; + use Symfony\Component\Form\Test\TypeTestCase; + + class TestedTypeTest extends TypeTestCase + { + protected function setUp() + { + parent::setUp(); + + $this->factory = Forms::createFormFactoryBuilder() + ->addTypeExtension( + new FormTypeValidatorExtension( + $this->getMock('Symfony\Component\Validator\ValidatorInterface') + ) + ) + ->addTypeGuesser( + $this->getMockBuilder( + 'Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser' + ) + ->disableOriginalConstructor() + ->getMock() + ) + ->getFormFactory(); + + $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory); + } + + // ... your tests + } + +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/Acme/TestBundle/Tests/Form/Type/TestedTypeTests.php + namespace Acme\TestBundle\Tests\Form\Type; + + use Acme\TestBundle\Form\Type\TestedType; + use Acme\TestBundle\Model\TestObject; + use Symfony\Component\Form\Test\TypeTestCase; + + class TestedTypeTest extends TypeTestCase + { + + /** + * @dataProvider getValidTestData + */ + public function testForm($data) + { + // ... your test + } + + public function getValidTestData() + { + return array( + array( + 'data' => array( + 'test' => 'test', + 'test2' => 'test2', + ), + ), + array( + 'data' => array(), + ), + array( + 'data' => array( + 'test' => null, + 'test2' => null, + ), + ), + ); + } + } + +The code above will run your test three times with 3 different sets of +data. This allows for decoupling the test fixtures from the tests and +easily testing against multiple sets of data. + +You can also pass another argument, such as a boolean if the form has to +be synchronized with the given set of data or not etc. + +.. _`data providers`: http://www.phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy