Skip to content
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

Unit testing FormType with constructor #6053

Closed
hackzilla opened this issue Dec 20, 2015 · 3 comments
Closed

Unit testing FormType with constructor #6053

hackzilla opened this issue Dec 20, 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

@hackzilla
Copy link
Contributor

This code used to work in Symfony <= 2.7

        $userManager = $this->getMock('Hackzilla\Bundle\TicketBundle\User\UserInterface');
        $form = $this->factory->create(new \Hackzilla\Bundle\TicketBundle\Form\Type\TicketMessageType($userManager));

However in Symfony 3.0, the following error is thrown.

Symfony\Component\Form\Exception\UnexpectedTypeException: Expected argument of type "string", "Hackzilla\Bundle\TicketBundle\Form\Type\TicketMessageType" given

/ticketapp/vendor/hackzilla/ticket-bundle/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php:64
/ticketapp/vendor/hackzilla/ticket-bundle/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php:39
/ticketapp/vendor/hackzilla/ticket-bundle/Tests/Form/Type/TicketMessageTypeTest.php:33

The functionality was changed in this commit. There doesn't seem to be any mention of the above, in any change logs, only that getName was deprecated.

[Form] removed deprecated FormType::getName()

I've been on the hunt for documentation that explains how to do this.

In How to Unit Test your Forms (3.0), the following code appears, but I can't see how it'd work.

        $type = new TestedType();
        $form = $this->factory->create($type);

Whereas it should be passed in as a string.

$form = $this->factory->create('\AppBundle\Form\Type\TestedType');

Though I'm still confused as to how you're supposed to do the above when your type has a constructor.

Closest I have found seems to be:

class TicketMessageTypeTest extends TypeTestCase
{
    protected function getExtensions()
    {
        $userManager = $this->getMock('Hackzilla\Bundle\TicketBundle\User\UserInterface');
        $ticketMessageType = new \Hackzilla\Bundle\TicketBundle\Form\Type\TicketMessageType($userManager);

        return [new PreloadedExtension([
            $ticketMessageType->getBlockPrefix() => $ticketMessageType,
        ], [])];
    }
...
}
@wouterj
Copy link
Member

wouterj commented Dec 20, 2015

The article you reference indeed needs an update. In case the form type doesn't have dependencies, you would pass the fully-qualified class name instead of an instance.

If your form type has dependencies, the solution you proposed seems to be correct. I don't like it that much either... @webmozart do you have an alternative or an idea how we can improve testing form types with dependencies?

@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 Dec 20, 2015
@hackzilla
Copy link
Contributor Author

There is something I'm missing with the above example, as it still errors, and I the form type isn't in FormRegistry.

@webmozart
Copy link
Contributor

You were on a good path already. Something like this should work:

protected function setUp()
{
    $this->user = $this->getMock(UserInterface::class);

    parent::setUp();
}

protected function getExtensions()
{
    return array(
        new PreloadedExtension(array(
            new TicketMessageType($this->user)
        )),
    );
}

public function testFoobar()
{
    $form = $this->factory->create(TicketMessageType::class);

    // ...
}

@wouterj wouterj added the hasPR A Pull Request has already been submitted for this issue. label Dec 21, 2015
wouterj added a commit that referenced this issue Feb 6, 2016
…(WouterJ)

This PR was merged into the 2.8 branch.

Discussion
----------

Update Testing Form Types article for 2.8 refactorings

| Q | A
| --- | ---
| Doc fix? | yes
| New docs? | yes
| Applies to | 2.8+
| Fixed tickets | #6053

Commits
-------

906d55a Update Testing Form Types article for 2.8 refactorings
@wouterj wouterj closed this as completed Feb 6, 2016
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

3 participants