phpunit - Symfony Form Unit Test -
how unit test formtype has parent of entity? or case class should ignored phpunit? code included below.
1) audit\receiptbundle\tests\form\type\contacttypetest::testsubmitvaliddata symfony\component\optionsresolver\exception\missingoptionsexception: required option "class" missing.
class contacttype extends abstracttype { public function __construct() { } public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'empty_value' => 'choose contact', )); } public function getparent() { return 'entity'; } public function getname() { return 'contact'; } }
the test code:
protected function getextensions() { return array(new preloadedextension(array( 'entity' => $this->mockfieldentitytype(), ), array())); } public function testsubmitvaliddata() { $formdata = [ ]; $type = new contacttype(); $resolve = new \symfony\component\optionsresolver\optionsresolver(); $resolve->setdefaults([ 'class' => 'foo:bar' ]); $type->setdefaultoptions($resolve); $form = $this->factory->create($type); // submit data form directly $form->submit($formdata); $this->asserttrue($form->issynchronized()); $this->assertequals($formdata, $form->getdata()); $view = $form->createview(); $children = $view->children; foreach (array_keys($formdata) $key) { $this->assertarrayhaskey($key, $children); } }
mockfieldentitytype() take answer provided in how load form types in tests
protected function mockfieldentitytype() { $mockentitymanager = $this->getentitymanager(); $mockregistry = $this->getmockbuilder('doctrine\bundle\doctrinebundle\registry') ->disableoriginalconstructor() ->getmock(); $mockregistry->expects($this->any())->method('getmanagerforclass') ->will($this->returnvalue($mockentitymanager)); $mockentitymanager->expects($this->any())->method('getclassmetadata') ->withanyparameters() ->will($this->returnvalue(new classmetadata('entity'))); $entitytype = new entitytype($mockregistry); return $entitytype; }
Comments
Post a Comment