8000 [Form] Fix "Adding custom extensions" section · Issue #5225 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fix "Adding custom extensions" section #5225

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
webmozart opened this issue May 4, 2015 · 3 comments
Closed

[Form] Fix "Adding custom extensions" section #5225

webmozart opened this issue May 4, 2015 · 3 comments
Labels
actionable Clear and specific issues ready for anyone to take them. Form good first issue Ideal for your first contribution! (some Symfony experience may be required) hasPR A Pull Request has already been submitted for this issue.

Comments

@webmozart
Copy link
Contributor

Currently, the section "Adding custom extensions" in http://symfony.com/doc/current/cookbook/form/unit_testing.html#adding-custom-extensions is overly complicated. The same result as in the code on that page can be achieved with:

use Symfony\Component\Form\PreloadedExtension;

class TestedTypeTest extends TypeTestCase
{
    protected function getExtensions()
    {
        $validator = $this->getMock('\Symfony\Component\Validator\Validator\ValidatorInterface');
        $validator->method('validate')->will($this->returnValue(new ConstraintViolationList()));

        $typeGuesser = $this->getMockBuilder('Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser')
            ->disableOriginalConstructor()
            ->getMock();

        return array(
            new PreloadedExtension(
                // Form types
                array(),
                // Form type extensions
                array(new FormTypeValidatorExtension($validator)),
                // Type guesser
                $typeGuesser
            ),
        );
    }

    // ... your tests
}
@wouterj wouterj added good first issue Ideal for your first contribution! (some Symfony experience may be required) actionable Clear and specific issues ready for anyone to take them. Form labels May 4, 2015
@stof
Copy link
Member
stof commented May 4, 2015

And for the case of the validator, it can even be simpler with the ValidatorExtension instead of using the PreloadedExtensio 8000 n and registering the different things manually (in latest versions, the ButtonTypeValidatorExtension might be needed too for instance)

@soullivaneuh
Copy link
Contributor

@stof an code example could be appreciated. :-) 👍

@stof
Copy link
Member
stof commented May 9, 2015
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;

class TestedTypeTest extends TypeTestCase
{
    protected function getExtensions()
    {
        $validator = $this->getMock('\Symfony\Component\Validator\Validator\ValidatorInterface');
        $validator->method('validate')->will($this->returnValue(new ConstraintViolationList()));

        return array(
            new ValidatorExtension($validator),
        );
    }

    // ... your tests
}

@wouterj wouterj added the hasPR A Pull Request has already been submitted for this issue. label Dec 5, 2015
wouterj added a commit that referenced this issue Dec 17, 2015
…tion (snoek09)

This PR was squashed before being merged into the 2.3 branch (closes #5962).

Discussion
----------

Simplify code example in "Adding custom extensions" section

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | all
| Fixed tickets | #5225

 As suggested by @webmozart and @stof.

Commits
-------

d203ef0 Simplify code example in "Adding custom extensions" section
@wouterj wouterj closed this as completed Dec 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable Clear and specific issues ready for anyone to take them. Form good first issue Ideal for your first contribution! (some Symfony experience may be required) hasPR A Pull Request has already been submitted for this issue.
Projects
None yet
Development

No branches or pull requests

4 participants
0