4
votes

I'm trying to test my form. I read that :

But I get a null exception

class MediaTypeTest extends TypeTestCase
{
    protected function setUp()
    {

    }

    protected function tearDown()
    {
    }

    // tests
    public function testMe()
    {

        $formData = array(
            'test' => 'test',
            'test2' => 'test2',
        );

        $form = $this->factory->create(MediaType::class);

        // submit the data to the form directly
        $form->submit($formData);

        $this->assertTrue($form->isSynchronized());
        $this->assertEquals(new Media(), $form->getData());

        $view = $form->createView();
        $children = $view->children;

        foreach (array_keys($formData) as $key) {
            $this->assertArrayHasKey($key, $children);
        }
    }
}

I understand that the line buggy is :

$form = $this->factory->create(MediaType::class);

But how can I resolve ?

I launch :

phpunit tests/unit/Form/MediaTypeTest.php

Or via codeception :

php vendor/bin/codecept run unit Form/MediaTypeTest.php

Any idea ?

1
check you are extending the right class: Symfony\Component\Form\Test\TypeTestCase instead of Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase. Let me knowMatteo

1 Answers

3
votes

The factory object is initializated in the setup method of the parent test class so You should call the parent in the setup method of your testCase (or remove the empty implementation of it).

So in general remember to call the parent class method when you override the inherit method:

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

protected function tearDown()
{
    parent::tearDown();
}

Hope this help