Skip to content

Added note about using the Validator TypeTestCase in form unit testing #7587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 10, 2018
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions form/unit_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -161,43 +161,34 @@ Adding Custom Extensions
------------------------

It often happens that you use some options that are added by
:doc:`form extensions </form/create_form_type_extension>`. One of the
cases may be the ``ValidatorExtension`` with its ``invalid_message`` option.
:doc:`form extensions </form/create_form_type_extension>`.
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.
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;
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),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these changes also be reverted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so

new MyFormExtension(),
);
}

Expand All @@ -210,7 +201,7 @@ 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;
Expand Down