/**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $type = $options['addressType'];
     $order = $options['order'];
     $isManualEditGranted = $this->orderAddressSecurityProvider->isManualEditGranted($type);
     $addresses = $this->orderAddressManager->getGroupedAddresses($order, $type);
     $accountAddressOptions = ['label' => false, 'required' => false, 'mapped' => false, 'choices' => $this->getChoices($addresses), 'configs' => ['placeholder' => 'orob2b.order.form.address.choose'], 'attr' => ['data-addresses' => json_encode($this->getPlainData($addresses)), 'data-default' => $this->getDefaultAddressKey($order, $type, $addresses)]];
     if ($isManualEditGranted) {
         $accountAddressOptions['choices'] = array_merge($accountAddressOptions['choices'], ['orob2b.order.form.address.manual']);
         $accountAddressOptions['configs']['placeholder'] = 'orob2b.order.form.address.choose_or_create';
     }
     $builder->add('accountAddress', 'genemu_jqueryselect2_choice', $accountAddressOptions);
     $builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) use($isManualEditGranted) {
         if (!$isManualEditGranted) {
             $event->setData(null);
         }
         $form = $event->getForm();
         if (!$form->has('accountAddress')) {
             return;
         }
         $identifier = $form->get('accountAddress')->getData();
         if ($identifier === null) {
             return;
         }
         //Enter manually or Account/AccountUser address
         $orderAddress = $event->getData();
         $address = null;
         if ($identifier) {
             $address = $this->orderAddressManager->getEntityByIdentifier($identifier);
         }
         if ($orderAddress || $address) {
             $event->setData($this->orderAddressManager->updateFromAbstract($address, $orderAddress));
         }
     }, -10);
 }
 public function testGetEntityByIdentifier()
 {
     $entity = $this->getEntity('OroB2B\\Bundle\\AccountBundle\\Entity\\AccountUserAddress', 1);
     $em = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager');
     $em->expects($this->exactly(2))->method('find')->with($this->isType('string'), $this->isType('integer'))->will($this->onConsecutiveCalls($entity, null));
     $this->registry->expects($this->any())->method('getManagerForClass')->willReturn($em);
     $this->manager->addEntity('au', 'OroB2B\\Bundle\\AccountBundle\\Entity\\AccountUserAddress');
     $this->assertEquals($entity, $this->manager->getEntityByIdentifier('au_1'));
     $this->assertNull($this->manager->getEntityByIdentifier('au_2'));
 }
 public function testFinishView()
 {
     $view = new FormView();
     $view->children = ['country' => new FormView(), 'city' => new FormView(), 'accountAddress' => new FormView()];
     $this->orderAddressManager->expects($this->once())->method('getGroupedAddresses')->willReturn([]);
     $this->orderAddressSecurityProvider->expects($this->atLeastOnce())->method('isManualEditGranted')->willReturn(false);
     $form = $this->factory->create($this->formType, new OrderAddress(), ['addressType' => AddressTypeEntity::TYPE_SHIPPING, 'order' => new Order()]);
     $this->formType->finishView($view, $form, ['addressType' => AddressTypeEntity::TYPE_SHIPPING]);
     foreach (['country', 'city'] as $childName) {
         $this->assertTrue($view->offsetGet($childName)->vars['disabled']);
         $this->assertFalse($view->offsetGet($childName)->vars['required']);
         $this->assertArrayNotHasKey('data-validation', $view->offsetGet($childName)->vars['attr']);
         $this->assertArrayNotHasKey('data-required', $view->offsetGet($childName)->vars['attr']);
         $this->assertArrayNotHasKey('label_attr', $view->offsetGet($childName)->vars);
     }
     $this->assertFalse($view->offsetGet('accountAddress')->vars['disabled']);
     $this->assertFalse($view->offsetGet('accountAddress')->vars['required']);
 }