9

I am testing a Form Type I defined for an application. During testing the form type, using symfony's TypeTestCase class a message "Could not load type "entity"" appears. What can I do to solve the problem??

class MyType extends AbstractType {
  public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('otherType', 'entity', array('class' => 'Bundle:OtherType'));
  }
}

class MyTypeTest extends TypeTestCase {
  public function testSth() {
    $type = new MyType();
  }
}
7
  • Please, provide relevant code. Thank you Commented May 3, 2013 at 9:29
  • added code, sry for forgetting it! Commented May 3, 2013 at 9:36
  • To me it seems there isn't any error, but sure that we miss something ... Commented May 3, 2013 at 9:45
  • duplicate: stackoverflow.com/questions/16341149/… Commented May 3, 2013 at 9:52
  • how can I still test this formtype, probably using mocking?? Commented May 3, 2013 at 9:57

2 Answers 2

15

I already got the same problem when testing some of my customized Types.

Here's the way I figure it out (by mocking EntityType),

First, make sure your test class extends TypeTestCase,

class MyTypeTest extends TypeTestCase
{
    // ... 
}

Then, add a preloaded extension to your form factory in order to take into account the EntityType

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

    $this->factory = Forms::createFormFactoryBuilder()
      ->addExtensions($this->getExtensions())
      ->getFormFactory();
}
// Where this->getExtensions() returns the EntityType preloaded extension 
// (see the last step)    
}

And finally, add an Entity Type mock to your preloaded extension.

protected function getExtensions()
{
    $mockEntityType = $this->getMockBuilder('Symfony\Bridge\Doctrine\Form\Type\EntityType')
        ->disableOriginalConstructor()
        ->getMock();

    $mockEntityType->expects($this->any())->method('getName')
                   ->will($this->returnValue('entity'));

    return array(new PreloadedExtension(array(
            $mockEntityType->getName() => $mockEntityType,
    ), array()));
}

But, you may need to ...

Mock the registry that DoctrineType takes as parameter when calling its default constructor because it's used by setDefaultOptions() (Keep in mind that EntityType extends DoctrineType) to take into account class and property options of your Entity field.

Your may then need to mock the entityType as follow:

$mockEntityManager = $this->getMockBuilder('\Doctrine\ORM\EntityManager')->getMock();

$mockRegistry = $this->getMockBuilder('Doctrine\Bundle\DoctrineBundle\Registry')
    ->disableOriginalConstructor()
    ->setMethods(array('getManagerForClass'))
    ->getMock();

$mockRegistry->expects($this->any())->method('getManagerForClass')
             ->will($this->returnValue($mockEntityManager));

$mockEntityType = $this->getMockBuilder('Symfony\Bridge\Doctrine\Form\Type\EntityType')
    ->setMethods(array('getName'))
    ->setConstructorArgs(array($mockRegistry))
    ->getMock();

$mockEntityType->expects($this->any())->method('getName')
               ->will($this->returnValue('entity'));
Sign up to request clarification or add additional context in comments.

3 Comments

Hello, i followed what you have posted but, i got an error: Call to protected Doctrine\ORM\EntityManager::__construct() from context 'PHPUnit_Framework_MockObject_Generator' in /usr/share/php/PHPUnit/Framework/MockObject/Generator.php on line 237
@Ahmed Siouani Can you please answer the above comment
I get the same error as @skonsoft. Can you tell us possible solution for that, please?
0

The Ahmed Siouani's answer is well writted and allowed me to understand how to add an Extension in the TypeTestCase.

But if you want to make an Intergration test, who is much simpler than a Unit test in this case, you can do as follow:

protected function getExtensions()
{
    $childType = new TestChildType();
    return array(new PreloadedExtension(array(
        $childType->getName() => $childType,
    ), array()));
}

As described in this documentation: http://symfony.com/doc/current/cookbook/form/unit_testing.html#adding-a-type-your-form-depends-on

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.