/** * {@inheritdoc} */ public function save(\Magento\Customer\Api\Data\GroupInterface $group) { $this->_validate($group); /** @var \Magento\Customer\Model\Group $groupModel */ $groupModel = null; if ($group->getId()) { $this->_verifyTaxClassModel($group->getTaxClassId(), $group); $groupModel = $this->groupRegistry->retrieve($group->getId()); $groupDataAttributes = $this->dataObjectProcessor->buildOutputDataArray($group, '\\Magento\\Customer\\Api\\Data\\GroupInterface'); foreach ($groupDataAttributes as $attributeCode => $attributeData) { $groupModel->setDataUsingMethod($attributeCode, $attributeData); } } else { $groupModel = $this->groupFactory->create(); $groupModel->setCode($group->getCode()); $taxClassId = $group->getTaxClassId() ?: self::DEFAULT_TAX_CLASS_ID; $this->_verifyTaxClassModel($taxClassId, $group); $groupModel->setTaxClassId($taxClassId); } try { $this->groupResourceModel->save($groupModel); } catch (\Magento\Framework\Exception\LocalizedException $e) { /** * Would like a better way to determine this error condition but * difficult to do without imposing more database calls */ if ($e->getMessage() == (string) __('Customer Group already exists.')) { throw new InvalidTransitionException(__('Customer Group already exists.')); } throw $e; } $this->groupRegistry->remove($groupModel->getId()); $groupDataObject = $this->groupDataFactory->create()->setId($groupModel->getId())->setCode($groupModel->getCode())->setTaxClassId($groupModel->getTaxClassId())->setTaxClassName($groupModel->getTaxClassName()); return $groupDataObject; }
public function testDelete() { $dbAdapter = $this->getMock('Magento\\Framework\\DB\\Adapter\\AdapterInterface'); $this->resource->expects($this->any())->method('getConnection')->will($this->returnValue($dbAdapter)); $customer = $this->getMock('Magento\\Customer\\Model\\Customer', ['__wakeup', 'load', 'getId', 'getStoreId', 'setGroupId', 'save'], [], '', false); $customerId = 1; $customer->expects($this->once())->method('getId')->will($this->returnValue($customerId)); $customer->expects($this->once())->method('load')->with($customerId)->will($this->returnSelf()); $defaultCustomerGroup = $this->getMock('Magento\\Customer\\Model\\Group', ['getId'], [], '', false); $this->groupManagement->expects($this->once())->method('getDefaultGroup')->will($this->returnValue($defaultCustomerGroup)); $defaultCustomerGroup->expects($this->once())->method('getId')->will($this->returnValue(1)); $customer->expects($this->once())->method('setGroupId')->with(1); $customerCollection = $this->getMock('Magento\\Customer\\Model\\ResourceModel\\Customer\\Collection', [], [], '', false); $customerCollection->expects($this->once())->method('addAttributeToFilter')->will($this->returnSelf()); $customerCollection->expects($this->once())->method('load')->will($this->returnValue([$customer])); $this->customersFactory->expects($this->once())->method('create')->will($this->returnValue($customerCollection)); $this->relationProcessorMock->expects($this->once())->method('delete'); $this->groupModel->expects($this->any())->method('getData')->willReturn(['data' => 'value']); $this->groupResourceModel->delete($this->groupModel); }
/** * @expectedException \Magento\Framework\Exception\State\InvalidTransitionException */ public function testSaveWithException() { $taxClass = $this->getMockForAbstractClass('Magento\\Tax\\Api\\Data\\TaxClassInterface', [], '', false); $this->groupFactory->expects($this->once())->method('create')->willReturn($this->groupModel); $this->group->expects($this->atLeastOnce())->method('getCode')->willReturn('Code'); $this->group->expects($this->atLeastOnce())->method('getId')->willReturn(false); $this->group->expects($this->atLeastOnce())->method('getTaxClassId')->willReturn(234); $this->group->expects($this->atLeastOnce())->method('getTaxClassId')->willReturn(17); $this->groupModel->expects($this->once())->method('setCode')->with('Code'); $this->groupModel->expects($this->once())->method('setTaxClassId')->with(234); $this->taxClassRepository->expects($this->once())->method('get')->with(234)->willReturn($taxClass); $taxClass->expects($this->once())->method('getClassType')->willReturn('CUSTOMER'); $this->groupResourceModel->expects($this->once())->method('save')->with($this->groupModel)->willThrowException(new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('Customer Group already exists.'))); $this->model->save($this->group); }