10000 Added note about using the Validator TypeTestCase in form unit testing by pierredup · Pull Request #7587 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reverted some changes
  • Loading branch information
javiereguiluz authored Jan 10, 2018
commit ff1bd8ee7384436c815c8458d7cd27a189afda64
12 changes: 10 additions & 2 deletions form/unit_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ Adding Custom Extensions
------------------------

It often happens that you use some options that are added by
:doc:`form extensions </form/create_form_type_extension>`.
:doc:`form extensions </form/create_form_type_extension>`. 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.
Expand All @@ -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),
);
}

Expand Down
0