From 1645b069bef1ebc2788aebcb3bc7c2fabcf1763c Mon Sep 17 00:00:00 2001 From: Henry Snoek Date: Sat, 5 Dec 2015 13:30:20 +0100 Subject: [PATCH 1/5] simplify code example as suggested by @webmozart and @stof --- cookbook/form/unit_testing.rst | 35 +++++++++++----------------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/cookbook/form/unit_testing.rst b/cookbook/form/unit_testing.rst index 5c6a0c8011b..7345a460f05 100644 --- a/cookbook/form/unit_testing.rst +++ b/cookbook/form/unit_testing.rst @@ -39,37 +39,24 @@ The simplest ``TypeTestCase`` implementation looks like the following:: // src/AppBundle/Tests/Form/Type/TestedTypeTest.php namespace AppBundle\Tests\Form\Type; - use AppBundle\Form\Type\TestedType; - use AppBundle\Model\TestObject; + use Symfony\Component\Form\Extension\Validator\ValidatorExtension; use Symfony\Component\Form\Test\TypeTestCase; + use Symfony\Component\Validator\ConstraintViolationList; + use Symfony\Component\Validator\Validator\ValidatorInterface; class TestedTypeTest extends TypeTestCase { - public function testSubmitValidData() + protected function getExtensions() { - $formData = array( - 'test' => 'test', - 'test2' => 'test2', - ); - - $type = new TestedType(); - $form = $this->factory->create($type); - - $object = TestObject::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; + $validator = $this->getMock('ValidatorInterface::class'); + $validator->method('validate')->will($this->returnValue(new ConstraintViolationList())); - foreach (array_keys($formData) as $key) { - $this->assertArrayHasKey($key, $children); - } + return array( + new ValidatorExtension($validator), + ); } + + // ... your tests } So, what does it test? Here comes a detailed explanation. From 12757b96fe456ce93d6da33be5812faa9b53f4eb Mon Sep 17 00:00:00 2001 From: Henry Snoek Date: Sat, 5 Dec 2015 13:40:44 +0100 Subject: [PATCH 2/5] Revert "simplify code example as suggested by @webmozart and @stof" This reverts commit 1645b069bef1ebc2788aebcb3bc7c2fabcf1763c. --- cookbook/form/unit_testing.rst | 35 +++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/cookbook/form/unit_testing.rst b/cookbook/form/unit_testing.rst index 7345a460f05..5c6a0c8011b 100644 --- a/cookbook/form/unit_testing.rst +++ b/cookbook/form/unit_testing.rst @@ -39,24 +39,37 @@ The simplest ``TypeTestCase`` implementation looks like the following:: // src/AppBundle/Tests/Form/Type/TestedTypeTest.php namespace AppBundle\Tests\Form\Type; - use Symfony\Component\Form\Extension\Validator\ValidatorExtension; + use AppBundle\Form\Type\TestedType; + use AppBundle\Model\TestObject; use Symfony\Component\Form\Test\TypeTestCase; - use Symfony\Component\Validator\ConstraintViolationList; - use Symfony\Component\Validator\Validator\ValidatorInterface; class TestedTypeTest extends TypeTestCase { - protected function getExtensions() + public function testSubmitValidData() { - $validator = $this->getMock('ValidatorInterface::class'); - $validator->method('validate')->will($this->returnValue(new ConstraintViolationList())); - - return array( - new ValidatorExtension($validator), + $formData = array( + 'test' => 'test', + 'test2' => 'test2', ); - } - // ... your tests + $type = new TestedType(); + $form = $this->factory->create($type); + + $object = TestObject::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? Here comes a detailed explanation. From cd085da36761347686d123fdeeb31d5cce5afc83 Mon Sep 17 00:00:00 2001 From: Henry Snoek Date: Sat, 5 Dec 2015 13:46:14 +0100 Subject: [PATCH 3/5] simplify code example as suggested by @webmozart and @stof --- cookbook/form/unit_testing.rst | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/cookbook/form/unit_testing.rst b/cookbook/form/unit_testing.rst index 5c6a0c8011b..89551c837fb 100644 --- a/cookbook/form/unit_testing.rst +++ b/cookbook/form/unit_testing.rst @@ -173,39 +173,23 @@ on other extensions. You need to add those extensions to the factory object:: use AppBundle\Form\Type\TestedType; use AppBundle\Model\TestObject; - use Symfony\Component\Form\Test\TypeTestCase; + use Symfony\Component\Form\Extension\Validator\ValidatorExtension; use Symfony\Component\Form\Forms; use Symfony\Component\Form\FormBuilder; - use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension; + use Symfony\Component\Form\Test\TypeTestCase; use Symfony\Component\Validator\ConstraintViolationList; + use Symfony\Component\Validator\Validator\ValidatorInterface class TestedTypeTest extends TypeTestCase { - protected function setUp() + protected function getExtensions() { - parent::setUp(); - - $validator = $this->getMock('\Symfony\Component\Validator\ValidatorInterface'); + $validator = $this->getMock(ValidatorInterface::class); $validator->method('validate')->will($this->returnValue(new ConstraintViolationList())); - $this->factory = Forms::createFormFactoryBuilder() - ->addExtensions($this->getExtensions()) - ->addTypeExtension( - new FormTypeValidatorExtension( - $validator - ) - ) - ->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); + return array( + new ValidatorExtension($validator), + ); } // ... your tests From 949c70d544b5a881df8842249b0f7881c69a3757 Mon Sep 17 00:00:00 2001 From: Henry Snoek Date: Sat, 5 Dec 2015 13:48:14 +0100 Subject: [PATCH 4/5] fix missing semicolon --- cookbook/form/unit_testing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/form/unit_testing.rst b/cookbook/form/unit_testing.rst index 89551c837fb..7f95cd83fbe 100644 --- a/cookbook/form/unit_testing.rst +++ b/cookbook/form/unit_testing.rst @@ -178,7 +178,7 @@ on other extensions. You need to add those extensions to the factory object:: use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\Test\TypeTestCase; use Symfony\Component\Validator\ConstraintViolationList; - use Symfony\Component\Validator\Validator\ValidatorInterface + use Symfony\Component\Validator\Validator\ValidatorInterface; class TestedTypeTest extends TypeTestCase { From 60fcedc3a7d8ee7b6aed32d95660cd7b854b8816 Mon Sep 17 00:00:00 2001 From: Henry Snoek Date: Sun, 6 Dec 2015 10:06:24 +0100 Subject: [PATCH 5/5] example should work with php version < 5.5 --- cookbook/form/unit_testing.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cookbook/form/unit_testing.rst b/cookbook/form/unit_testing.rst index 7f95cd83fbe..c2d4490079c 100644 --- a/cookbook/form/unit_testing.rst +++ b/cookbook/form/unit_testing.rst @@ -178,13 +178,12 @@ on other extensions. You need to add those extensions to the factory object:: use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\Test\TypeTestCase; use Symfony\Component\Validator\ConstraintViolationList; - use Symfony\Component\Validator\Validator\ValidatorInterface; class TestedTypeTest extends TypeTestCase { protected function getExtensions() { - $validator = $this->getMock(ValidatorInterface::class); + $validator = $this->getMock('\Symfony\Component\Validator\Validator\ValidatorInterface'); $validator->method('validate')->will($this->returnValue(new ConstraintViolationList())); return array(