Skip to content

[DX] [Form] Add helper method to register form extensions during unit testing #21780

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

Closed
wants to merge 4 commits into from

Conversation

pierredup
Copy link
Contributor

@pierredup pierredup commented Feb 27, 2017

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR symfony/symfony-docs#7578

Add helper method to register form extensions during unit testing, so it's easier to register custom form type extensions, form types, or type guessers

protected function setUp()
{
parent::setUp();

$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
$this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory);
$this->validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock();
Copy link
Member

Choose a reason for hiding this comment

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

This will break existing applications that use the TypeTestCase class, but do not have the Validator component installed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wrapped the extension in a check to make sure ValidatorInterface exists

@pierredup pierredup force-pushed the form-type-extensions branch from 082c642 to 4986008 Compare February 27, 2017 19:12
@@ -34,6 +36,20 @@ protected function setUp()
$this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory);
}

protected function getTypedExtensions()
Copy link
Member

Choose a reason for hiding this comment

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

If you want this to be called automatically based on the parent FormIntegrationTestCase class, you will have to rename this method to getTypeExtensions().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, it was supposed to be getTypeExtensions, I don't know how that d sneaked in there

@pierredup pierredup force-pushed the form-type-extensions branch from 4986008 to 3481246 Compare February 27, 2017 20:06
@HeahDude
Copy link
Contributor

HeahDude commented Mar 3, 2017

Hi @pierredup, thank you for proposing this.

In fact there is already this TypeTestCase form the validator extension that can be extended, it does extend the base TypeTestCase with the change you suggest.

I've opened symfony/symfony-docs#7566 so we don't forget to document it, we may close here though.

@pierredup
Copy link
Contributor Author

@HeahDude I initially did try using that, but I got the error Invalid argument supplied for foreach() in Symfony/Component/Form/Extension/Validator/EventListener/ValidationListener.php:57.

This is because the validate method is not mocked, which means when it is called it just returns null which breaks the listener. So to solve this I can add the mock to the validator TypeTestCase class, but I opted to rather update the base TypeTestCase since most people would probably use that class in their tests and not know about the other class. But I'm happy to make that change instead.

This still leaves the issue that it's not easy to register your own type extensions. You can pass it as the second argument to the PreloadedExtension class, but that is not very intuitive (I'm not sure if most people even know about this).
Having a method to register a type extension the same as the getExtensions method is a lot clearer and easier to use


if (interface_exists(ValidatorInterface::class)) {
$validator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
$validator->expects($this->any())->method('validate')->will($this->returnValue(array()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Then only this line should be added in the proper validator extension base type, we don't need to add the extension overhead to all base tests.

->getFormFactory();
}

protected function getExtensions()
{
return array();
}

protected function getTypeExtensions()
Copy link
Contributor

@HeahDude HeahDude Mar 4, 2017

Choose a reason for hiding this comment

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

If this line were too be added in favor of:

use Symfony\Component\Form\PreloadedExtension;

// ...

protected function getExtensions()
{
    return array(new PreloadedExtension(
        array(new SomeType($this->getDependencyMock())),
        array(new SomeTypeExtension(new SomeService()),
        array(
            new SomeGuesser($this->getDependencyMock2()),
            new SomeOtherGuesser()),
        ),
    ));
}

We should also add two other protected functions for types and guessers, and add addTypeGuessers and addTypes call above as well to be consistent.

I would not vote against it, but I would vote for a better doc.

@pierredup pierredup changed the title [Form] Add the FormTypeValidatorExtension to the TypeTestCase factory builder [Form] Mar 6, 2017
@pierredup pierredup changed the title [Form] [Form] Add validate method call to mocked validator in TypeTestCase Mar 6, 2017
@pierredup pierredup changed the title [Form] Add validate method call to mocked validator in TypeTestCase [Form] Add helper method to register form extensions during unit testing Mar 6, 2017
@pierredup pierredup changed the title [Form] Add helper method to register form extensions during unit testing [DX] [Form] Add helper method to register form extensions during unit testing Mar 6, 2017
@pierredup
Copy link
Contributor Author

pierredup commented Mar 6, 2017

I created a separate PR (#21886) to address the validate method issue, which can go as a bug fix, and updated this PR as a new feature to include extra helper methods to register form extensions for unit testing for DX

@xabbuh
Copy link
Member

xabbuh commented Mar 6, 2017

👍

Status: Reviewed

@fabpot
Copy link
Member

fabpot commented Mar 6, 2017

Can you add a note in the CHANGELOG?

@fabpot
Copy link
Member

fabpot commented Mar 6, 2017

Thank you @pierredup.

@fabpot fabpot closed this in 7638f6e Mar 6, 2017
@fabpot
Copy link
Member

fabpot commented Mar 6, 2017

see 638bdc8

->getFormFactory();
}

protected function getExtensions()
{
return array();
}

protected function getTypedExtensions()
Copy link
Member

Choose a reason for hiding this comment

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

the method should IMO be named getTypeExtensions()

xabbuh added a commit to symfony/symfony-docs that referenced this pull request Apr 15, 2017
…or unit testing (pierredup)

This PR was squashed before being merged into the master branch (closes #7578).

Discussion
----------

Add docs to register custom form extensions and types for unit testing

Docs for symfony/symfony#21780 to register custom extensions and types

Commits
-------

45d6ed8 Add docs to register custom form extensions and types for unit testing
fabpot added a commit that referenced this pull request Apr 18, 2017
…) (xabbuh)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Form] rename getTypedExtensions() to getTypeExtensions()

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21780 (comment)
| License       | MIT
| Doc PR        |

Commits
-------

099f626 rename getTypedExtensions() to getTypeExtensions()
@fabpot fabpot mentioned this pull request May 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants