public function testRemoveAccount() { $account = new Account(); $account->setId(1); $contact = new Contact(); $contact->setId(2); $contact->addAccount($account); $this->assertCount(1, $contact->getAccounts()->toArray()); $contact->removeAccount($account); $this->assertEmpty($contact->getAccounts()->toArray()); }
/** * @dataProvider processValidDataProvider * * @param bool $isDataChanged */ public function testProcessValidData($isDataChanged) { $appendedAccount = new Account(); $appendedAccount->setId(1); $removedAccount = new Account(); $removedAccount->setId(2); $this->entity->addAccount($removedAccount); $this->request->setMethod('POST'); $this->form->expects($this->once())->method('setData')->with($this->entity); $this->form->expects($this->once())->method('submit')->with($this->request); $this->form->expects($this->once())->method('isValid')->will($this->returnValue(true)); $appendForm = $this->getMockBuilder('Symfony\\Component\\Form\\Form')->disableOriginalConstructor()->getMock(); $appendForm->expects($this->once())->method('getData')->will($this->returnValue(array($appendedAccount))); $this->form->expects($this->at(3))->method('get')->with('appendAccounts')->will($this->returnValue($appendForm)); $removeForm = $this->getMockBuilder('Symfony\\Component\\Form\\Form')->disableOriginalConstructor()->getMock(); $removeForm->expects($this->once())->method('getData')->will($this->returnValue(array($removedAccount))); $this->form->expects($this->at(4))->method('get')->with('removeAccounts')->will($this->returnValue($removeForm)); if ($isDataChanged) { $this->manager->expects($this->once())->method('persist')->with($this->entity); } else { $this->manager->expects($this->exactly(2))->method('persist')->with($this->entity); } $this->manager->expects($this->once())->method('flush'); $this->configureUnitOfWork($isDataChanged); $this->assertTrue($this->handler->process($this->entity)); $actualAccounts = $this->entity->getAccounts()->toArray(); $this->assertCount(1, $actualAccounts); $this->assertEquals($appendedAccount, current($actualAccounts)); }
/** * @param Contact $contact * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function fixRequest($contact) { $formAlias = $this->getFormAlias(); $contactData = $this->getRequest()->request->get($formAlias); if (array_key_exists('accounts', $contactData)) { $accounts = $contactData['accounts']; $appendAccounts = array_key_exists('appendAccounts', $contactData) ? $contactData['appendAccounts'] : array(); $removeAccounts = array_key_exists('removeAccounts', $contactData) ? $contactData['removeAccounts'] : array(); if ($contact->getId()) { foreach ($contact->getAccounts() as $account) { if (!in_array($account->getId(), $accounts)) { $removeAccounts[] = $account->getId(); } } } $contactData['appendAccounts'] = array_merge($appendAccounts, $accounts); $contactData['removeAccounts'] = $removeAccounts; unset($contactData['accounts']); $this->getRequest()->request->set($formAlias, $contactData); } // @todo: just a temporary workaround until new API is implemented // - convert country name to country code (as result we accept both the code and the name) // also it will be good to accept ISO3 code in future, need to be discussed with product owners // - convert region name to region code (as result we accept the combined code, code and name) // - move region name to region_text field for unknown region if (array_key_exists('addresses', $contactData)) { foreach ($contactData['addresses'] as &$address) { if (!empty($address['country'])) { $countryCode = $this->getCountryCodeByName($address['country']); if (!empty($countryCode)) { $address['country'] = $countryCode; } } if (!empty($address['region']) && !$this->isRegionCombinedCodeByCode($address['region'])) { if (!empty($address['country'])) { $regionId = $this->getRegionCombinedCodeByCode($address['country'], $address['region']); if (!empty($regionId)) { $address['region'] = $regionId; } else { $regionId = $this->getRegionCombinedCodeByName($address['country'], $address['region']); if (!empty($regionId)) { $address['region'] = $regionId; } else { $address['region_text'] = $address['region']; unset($address['region']); } } } else { $address['region_text'] = $address['region']; unset($address['region']); } } } $this->getRequest()->request->set($formAlias, $contactData); } }
/** * @param Contact $entity */ protected function processMultipleRelations(Contact $entity) { // update groups foreach ($entity->getGroups() as $group) { $entity->removeGroup($group); if ($group = $this->findExistingEntity($group)) { $entity->addGroup($group); } } // clear accounts foreach ($entity->getAccounts() as $account) { $entity->removeAccount($account); } // update addresses /** @var ContactAddress $contactAddress */ foreach ($entity->getAddresses() as $contactAddress) { // update address types foreach ($contactAddress->getTypes() as $addressType) { $contactAddress->removeType($addressType); $existingAddressType = $this->findExistingEntity($addressType); if ($existingAddressType) { $contactAddress->addType($existingAddressType); } } } }
/** * @param Contact $entity * * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function fixRequestAttributes($entity) { $formAlias = $this->getFormAlias(); $contactData = $this->getRequest()->request->get($formAlias); if (array_key_exists('accounts', $contactData)) { $accounts = $contactData['accounts']; $appendAccounts = array_key_exists('appendAccounts', $contactData) ? $contactData['appendAccounts'] : array(); $removeAccounts = array_key_exists('removeAccounts', $contactData) ? $contactData['removeAccounts'] : array(); if ($entity->getId()) { foreach ($entity->getAccounts() as $account) { if (!in_array($account->getId(), $accounts)) { $removeAccounts[] = $account->getId(); } } } $contactData['appendAccounts'] = array_merge($appendAccounts, $accounts); $contactData['removeAccounts'] = $removeAccounts; unset($contactData['accounts']); $this->getRequest()->request->set($formAlias, $contactData); } // @todo: just a temporary workaround until new API is implemented // - convert country name to country code (as result we accept both the code and the name) // also it will be good to accept ISO3 code in future, need to be discussed with product owners // - convert region name to region code (as result we accept the combined code, code and name) // - move region name to region_text field for unknown region if (array_key_exists('addresses', $contactData)) { foreach ($contactData['addresses'] as &$address) { AddressApiUtils::fixAddress($address, $this->get('doctrine.orm.entity_manager')); } $this->getRequest()->request->set($formAlias, $contactData); } parent::fixRequestAttributes($entity); }