public function testaddNewInstance()
 {
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $pool = new Pool($container, 'title', 'logo.png');
     $helper = new AdminHelper($pool);
     $admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $admin->expects($this->once())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
     $fieldDescription = $this->getMock('Sonata\\AdminBundle\\Admin\\FieldDescriptionInterface');
     $fieldDescription->expects($this->once())->method('getAssociationAdmin')->will($this->returnValue($admin));
     $fieldDescription->expects($this->once())->method('getAssociationMapping')->will($this->returnValue(array('fieldName' => 'fooBar')));
     $object = $this->getMock('sdtClass', array('addFooBar'));
     $object->expects($this->once())->method('addFooBar');
     $helper->addNewInstance($object, $fieldDescription);
 }
 /**
  * @throws NotFoundHttpException
  *
  * @param Request $request
  *
  * @return Response
  */
 public function retrieveFormFieldElementAction(Request $request)
 {
     $code = $request->get('code');
     $elementId = $request->get('elementId');
     $objectId = $request->get('objectId');
     $uniqid = $request->get('uniqid');
     $admin = $this->pool->getInstance($code);
     $admin->setRequest($request);
     if ($uniqid) {
         $admin->setUniqid($uniqid);
     }
     if ($objectId) {
         $subject = $admin->getModelManager()->find($admin->getClass(), $objectId);
         if (!$subject) {
             throw new NotFoundHttpException(sprintf('Unable to find the object id: %s, class: %s', $objectId, $admin->getClass()));
         }
     } else {
         $subject = $admin->getNewInstance();
     }
     $admin->setSubject($subject);
     $formBuilder = $admin->getFormBuilder($subject);
     $form = $formBuilder->getForm();
     $form->handleRequest($request);
     $view = $this->helper->getChildFormView($form->createView(), $elementId);
     // render the widget
     // todo : fix this, the twig environment variable is not set inside the extension ...
     $extension = $this->twig->getExtension('form');
     $extension->initRuntime($this->twig);
     $extension->renderer->setTheme($view, $admin->getFormTheme());
     return new Response($extension->renderer->searchAndRenderBlock($view, 'widget'));
 }
 public function testAppendFormFieldElementNested()
 {
     $admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $object = $this->getMock('stdClass', array('getSubObject'));
     $simpleObject = $this->getMock('stdClass', array('getSubObject'));
     $subObject = $this->getMock('stdClass', array('getAnd'));
     $sub2Object = $this->getMock('stdClass', array('getMore'));
     $sub3Object = $this->getMock('stdClass', array('getFinalData'));
     $dataMapper = $this->getMock('Symfony\\Component\\Form\\DataMapperInterface');
     $formFactory = $this->getMock('Symfony\\Component\\Form\\FormFactoryInterface');
     $eventDispatcher = $this->getMock('Symfony\\Component\\EventDispatcher\\EventDispatcherInterface');
     $formBuilder = new FormBuilder('test', get_class($simpleObject), $eventDispatcher, $formFactory);
     $childFormBuilder = new FormBuilder('subObject', get_class($subObject), $eventDispatcher, $formFactory);
     $object->expects($this->atLeastOnce())->method('getSubObject')->will($this->returnValue(array($subObject)));
     $subObject->expects($this->atLeastOnce())->method('getAnd')->will($this->returnValue($sub2Object));
     $sub2Object->expects($this->atLeastOnce())->method('getMore')->will($this->returnValue(array($sub3Object)));
     $sub3Object->expects($this->atLeastOnce())->method('getFinalData')->will($this->returnValue('value'));
     $formBuilder->setCompound(true);
     $formBuilder->setDataMapper($dataMapper);
     $formBuilder->add($childFormBuilder);
     $admin->expects($this->once())->method('getFormBuilder')->will($this->returnValue($formBuilder));
     $admin->expects($this->once())->method('getSubject')->will($this->returnValue($object));
     $this->setExpectedException('Exception', 'unknown collection class');
     $this->helper->appendFormFieldElement($admin, $simpleObject, 'uniquePartOfId_sub_object_0_and_more_0_final_data');
 }
 public function testAddNewInstance()
 {
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $pool = new Pool($container, 'title', 'logo.png');
     $helper = new AdminHelper($pool);
     $mockFormView = $this->getMockBuilder('Symfony\\Component\\Form\\FormView')->disableOriginalConstructor()->getMock();
     $mockForm = $this->getMockBuilder('Symfony\\Component\\Form\\Form')->disableOriginalConstructor()->getMock();
     $mockForm->expects($this->any())->method('createView')->will($this->returnValue($mockFormView));
     $mockFormBuilder = $this->getMockBuilder('Symfony\\Component\\Form\\FormBuilder')->disableOriginalConstructor()->getMock();
     $mockFormBuilder->expects($this->any())->method('getForm')->will($this->returnValue($mockForm));
     $collection = $this->getMock('Doctrine\\ORM\\ArrayCollection');
     $collection->expects($this->any())->method('add');
     $entity = new \stdClass();
     $entity->foo = $collection;
     $admin = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $admin->expects($this->any())->method('getNewInstance')->will($this->returnValue(new \stdClass()));
     $admin->expects($this->any())->method('getFormBuilder')->will($this->returnValue($mockFormBuilder));
     $admin->expects($this->any())->method('getSubject')->will($this->returnValue($entity));
     $elementId = 'foo';
     $helper->addNewInstance($admin, $elementId);
 }