/**
  * @param Account         $account
  * @param PaymentTerm|null $paymentTerm
  */
 public function setPaymentTermToAccount(Account $account, PaymentTerm $paymentTerm = null)
 {
     $oldPaymentTermByAccount = $this->getOnePaymentTermByAccount($account);
     if ($oldPaymentTermByAccount && $paymentTerm && $oldPaymentTermByAccount->getId() === $paymentTerm->getId()) {
         return;
     }
     if ($oldPaymentTermByAccount) {
         $oldPaymentTermByAccount->removeAccount($account);
     }
     if ($paymentTerm) {
         $paymentTerm->addAccount($account);
     }
 }
 protected function prepareServices()
 {
     $this->form->expects($this->once())->method('setData')->with($this->entity);
     $this->form->expects($this->once())->method('submit')->with($this->request);
     $this->request->setMethod('POST');
     $this->form->expects($this->once())->method('isValid')->willReturn(true);
     $this->manager->expects($this->at(0))->method('persist')->with($this->isType('object'));
     $repository = $this->getMockBuilder('OroB2B\\Bundle\\PaymentBundle\\Entity\\Repository\\PaymentTermRepository')->disableOriginalConstructor()->getMock();
     $repository->expects($this->any())->method('setPaymentTermToAccount')->will($this->returnCallback(function (Account $account, PaymentTerm $paymentTerm = null) {
         $this->entity->removeAccount($account);
         if ($paymentTerm) {
             $this->entity->addAccount($account);
         }
     }));
     $repository->expects($this->any())->method('setPaymentTermToAccountGroup')->will($this->returnCallback(function (AccountGroup $accountGroup, PaymentTerm $paymentTerm = null) {
         $this->entity->removeAccountGroup($accountGroup);
         if ($paymentTerm) {
             $this->entity->addAccountGroup($accountGroup);
         }
     }));
     $this->manager->expects($this->any())->method('getRepository')->with('OroB2BPaymentBundle:PaymentTerm')->willReturn($repository);
     $this->manager->expects($this->exactly(2))->method('flush');
 }
 /**
  * {@inheritdoc}
  */
 public function load(ObjectManager $manager)
 {
     foreach ($this->data as $paymentTermData) {
         $paymentTerm = new PaymentTerm();
         $paymentTerm->setLabel($paymentTermData['label']);
         foreach ($paymentTermData['groups'] as $groupName) {
             $paymentTerm->addAccountGroup($this->getReference($groupName));
         }
         foreach ($paymentTermData['accounts'] as $accountName) {
             $paymentTerm->addAccount($this->getReference($accountName));
         }
         $manager->persist($paymentTerm);
         $this->addReference(static::PAYMENT_TERM_REFERENCE_PREFIX . $paymentTermData['label'], $paymentTerm);
     }
     $manager->flush();
 }