-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
279b0ef
to
082c642
Compare
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
082c642
to
4986008
Compare
@@ -34,6 +36,20 @@ protected function setUp() | |||
$this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory); | |||
} | |||
|
|||
protected function getTypedExtensions() |
There was a problem hiding this comment.
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()
.
There was a problem hiding this comment.
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
4986008
to
3481246
Compare
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. |
@HeahDude I initially did try using that, but I got the error This is because the 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 |
|
||
if (interface_exists(ValidatorInterface::class)) { | ||
$validator = $this->getMockBuilder(ValidatorInterface::class)->getMock(); | ||
$validator->expects($this->any())->method('validate')->will($this->returnValue(array())); |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
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 |
👍 Status: Reviewed |
Can you add a note in the CHANGELOG? |
Thank you @pierredup. |
see 638bdc8 |
->getFormFactory(); | ||
} | ||
|
||
protected function getExtensions() | ||
{ | ||
return array(); | ||
} | ||
|
||
protected function getTypedExtensions() |
There was a problem hiding this comment.
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()
…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
…) (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()
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