Testing Symfony2 Forms causes Could not load type "entity" -
i testing form type defined application. during testing form type, using symfony's typetestcase class message "could not load type "entity"" appears. can solve 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(); } }
i got same problem when testing of customized types.
here's way figure out (by mocking entitytype),
first, make sure test class extends typetestcase,
class mytypetest extends typetestcase { // ... }
then, add preloaded extension form factory in order take account entitytype
protected function setup() { parent::setup(); $this->factory = forms::createformfactorybuilder() ->addextensions($this->getextensions()) ->getformfactory(); } // this->getextensions() returns entitytype preloaded extension // (see last step) }
and finally, add entity type mock 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, may need ...
mock registry doctrinetype takes parameter when calling default constructor because it's used setdefaultoptions()
(keep in mind entitytype extends doctrinetype) take account class , property options of entity field.
your may need mock entitytype 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'));
Comments
Post a Comment