Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@pierredup
Copy link
Contributor

@pierreduppierredup commentedFeb 27, 2017
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed ticketsN/A
LicenseMIT
Doc PRsymfony/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


$this->dispatcher =$this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
$this->builder =newFormBuilder(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 theTypeTestCase class, but do not have the Validator component installed.

Copy link
ContributorAuthor

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

$this->builder =newFormBuilder(null,null,$this->dispatcher,$this->factory);
}

protectedfunctiongetTypedExtensions()
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 parentFormIntegrationTestCase class, you will have to rename this method togetTypeExtensions().

Copy link
ContributorAuthor

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 begetTypeExtensions, I don't know how that d sneaked in there

@HeahDude
Copy link
Contributor

Hi@pierredup, thank you for proposing this.

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

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

@pierredup
Copy link
ContributorAuthor

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

This is because thevalidate method is not mocked, which means when it is called it just returnsnull 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 thePreloadedExtension 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 thegetExtensions 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.

returnarray();
}

protectedfunctiongetTypeExtensions()
Copy link
Contributor

@HeahDudeHeahDudeMar 4, 2017
edited
Loading

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:

useSymfony\Component\Form\PreloadedExtension;// ...protectedfunctiongetExtensions(){returnarray(newPreloadedExtension(array(newSomeType($this->getDependencyMock())),array(newSomeTypeExtension(newSomeService()),array(newSomeGuesser($this->getDependencyMock2()),newSomeOtherGuesser()),        ),    ));}

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

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

@pierreduppierredup changed the title[Form] Add the FormTypeValidatorExtension to the TypeTestCase factory builder[Form]Mar 6, 2017
@pierreduppierredup changed the title[Form][Form] Add validate method call to mocked validator in TypeTestCaseMar 6, 2017
@pierreduppierredup changed the title[Form] Add validate method call to mocked validator in TypeTestCase[Form] Add helper method to register form extensions during unit testingMar 6, 2017
@pierreduppierredup 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 testingMar 6, 2017
@pierredup
Copy link
ContributorAuthor

pierredup commentedMar 6, 2017
edited
Loading

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

👍

Status: Reviewed

@fabpot
Copy link
Member

Can you add a note in the CHANGELOG?

@fabpot
Copy link
Member

Thank you@pierredup.

@fabpot
Copy link
Member

see638bdc8

HeahDude and smithandre reacted with thumbs up emoji

returnarray();
}

protectedfunctiongetTypedExtensions()
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 namedgetTypeExtensions()

xabbuh added a commit to symfony/symfony-docs that referenced this pull requestApr 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 testingDocs forsymfony/symfony#21780 to register custom extensions and typesCommits-------45d6ed8 Add docs to register custom form extensions and types for unit testing
fabpot added a commit that referenced this pull requestApr 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()
@fabpotfabpot mentioned this pull requestMay 1, 2017
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@xabbuhxabbuhxabbuh left review comments

+2 more reviewers

@HeahDudeHeahDudeHeahDude requested changes

@smithandresmithandresmithandre approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

3.3

Development

Successfully merging this pull request may close these issues.

7 participants

@pierredup@HeahDude@xabbuh@fabpot@smithandre@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp